Always false float comparisons

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Sun May 15 14:06:22 PDT 2016


On Sunday, 15 May 2016 at 20:34:19 UTC, Era Scarecrow wrote:
>  I noticed my code example fails to specify float for the 
> immutable, fixing that only the first line with f == 1.30 fail 
> while all others succeed (for some reason?), which is good news.

Well, it isn't actually good news. Both "const float" and 
"immutable float" should fail as you have requested coercion to 
32 bit floats which ought to change the value of "1.30", but 
unfortunately D does not heed the coercion.

The net result is that adding const/immutable to a type can 
change the semantics of the program entirely at the whim of the 
compiler implementor.

In comparison C++:

   float f = 1.30;
   const float c = 1.30;
   constexpr float i = 1.30;

   std::cout << (f==1.30) << std::endl;  // false
   std::cout << (c==1.30) << std::endl;  // false
   std::cout << (i==1.30) << std::endl;  // false
   std::cout << (1.30==(float)1.30) << std::endl;  // false




More information about the Digitalmars-d mailing list