Always false float comparisons

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Mon May 16 00:52:41 PDT 2016


On 5/15/2016 11:14 PM, Ola Fosheim Grøstad wrote:
> On Sunday, 15 May 2016 at 22:34:24 UTC, Walter Bright wrote:
>> So far, nobody has posted a legitimate one (i.e. not contrived).
> Oh, so comparing the exact same calculation, using the exact same binary
> executable function, is  not a legitimate algorithm.

What algorithm relies on such? And as numerous links posted here show, it 
doesn't work reliably on just about any C/C++ compiler, either. If you're 
writing C++ code that relies on such, you're in for some tragic late night 
debugging sessions as Manu relates.


> It is the most trivial
> thing to do and rather common. It should be _very_ convincing.

Post a link to some algorithm that does that.


> But hey, here is another one:
>
> const real x = f();
> assert(0<=x && x<1);
> x += 1;
>
> const float f32 = cast(float)(x);
> const real residue = x - cast(real)f32; // ZERO!!!!
>
> output(dither(f32, residue)); // DITHERING IS FUBAR!!!

Why would anyone dither based on the difference in precision of floats and 
doubles? Dithering is usually done based on different pixel densities.

I'm sure you can (and did) write code contrived to show a difference. I asked 
for something different, though.


More information about the Digitalmars-d mailing list