Always false float comparisons

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Mon May 16 02:00:49 PDT 2016


Am Mon, 16 May 2016 00:52:41 -0700
schrieb Walter Bright <newshound2 at digitalmars.com>:

> On 5/15/2016 11:14 PM, Ola Fosheim Grøstad wrote:
> > 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.

To be fair, several image editors have various dithering
algorithms that seek to reduce the apparent banding when
converting an image from a higher to a lower bit depth.
3dfx also performed dithering in hardware when going from
internal 24-bit RGB calculations to 16-bit output.
Ola's example could be some X-ray imaging format. Although
it is still a vague example, this "cast doesn't do anything
to floating point constants" semantic surprised me too, I
have to say. I don't want to dive too deep into the matter
though.

-- 
Marco



More information about the Digitalmars-d mailing list