Fun with floating point

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 7 14:46:56 PST 2015


To answer your other question, there is no Edit because this is a 
newsgroup (see NNTP). The forum interface is supposed to be a 
convenience but it hides that fact.

On 02/07/2015 01:33 PM, Kenny wrote:

 > The above code snippet works correctly

There is no right or wrong when you compare floating point values for 
equality (and inequality) unless those values can be represented exactly 
in the machine. 1.0 is famously not representable exactly. (It is 
similar to how 1/3 cannot be represented in the decimal system.)

 > when I use LDC compiler (it finds
 > expected 'f' value and prints it to console). I'm wondering is it a bug
 > in DMD?

Not a bug.

Ali

 >
 > p.s. the final code used by both compilers:
 >
 > import std.stdio;
 > import std.conv;
 >
 > int main(string[] argv)
 > {
 >      const float eps = 1.0f;
 >      float f = 0.0f;
 >      while (f + eps != f)
 >          f += 1.0f;
 >
 >      writeln("eps = ", eps, ", max_f = ", f);
 >      return 0;
 > }
 >
 >



More information about the Digitalmars-d-learn mailing list