Always false float comparisons
Joseph Rushton Wakeling via Digitalmars-d
digitalmars-d at puremagic.com
Mon May 16 03:19:49 PDT 2016
On Monday, 16 May 2016 at 09:54:51 UTC, Iain Buclaw wrote:
> On 16 May 2016 at 10:52, Ola Fosheim Grøstad via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>> On Monday, 16 May 2016 at 08:47:03 UTC, Iain Buclaw wrote:
>>>
>>> But you *didn't* request coercion to 32 bit floats.
>>> Otherwise you would have used 1.30f.
>>
>>
>> const float f = 1.3f;
>> float c = f;
>> assert(c*1.0 == f*1.0); // Fails! SHUTDOWN!
>>
>>
>
> Your still using doubles. Are you intentionally missing the
> point?
I think you're missing what Ola means when he's talking about
32-bit floats. Of course when you multiply a float by a double
(here, 1.0) you promote it to a double; but you'd expect the
result to reflect the data available in the 32-bit float.
Whereas in Ola's example, the fact that `const float f` is known
at compile-time means that the apparent 32-bit precision is in
fact entirely ignored when doing the * 1.0 calculation.
In other words, Ola's expecting
float * double == float * double
but is getting something more like
float * double == double * double
or maybe even
float * double == real * real
... because of the way FP constants are treated at compile time.
More information about the Digitalmars-d
mailing list