Float compare broke!

Era Scarecrow rtcvb32 at yahoo.com
Mon Jun 11 13:37:00 PDT 2012


On Monday, 11 June 2012 at 20:28:06 UTC, Era Scarecrow wrote:
> On Monday, 11 June 2012 at 12:54:37 UTC, Adam D. Ruppe wrote:
>> a == b is probably done by the bits at runtime which match
>> because it is the same assignment.
>>
>> But a == i_f might be propagated down there as 80 bit compared
>> to 32 bit and thus be just slightly different.
>
>  Unfortunately I used FloatRep and printed out the exponent and 
> fractions, and they were identical (false, 128, 4788187).
>
>  Besides, shouldn't the same types do a direct bit-wise copy?

Although re-reading your post it may make a little more sense... 
So I added a couple more tests.. Strangely the exact same issue 
is there while the rest aren't. The question then, is why the 
32bit may be upgraded to an 80bit (if that indeed is happening)? 
But that doesn't make sense since the 80bit can hold identically 
anything the 32bit can hold and should still compare the same.

--
const float i_f = 3.14159265;
float a = i_f;
float b = i_f;
union fi {
	float f;
	ubyte[4] b;
}

fi c;

c.b = [219, 15, 73, 64]; //bit for bit of the same value.
float d = a;     //float to float copy

assert(a==b);    //passes (float/float copied from const)
assert(a==c.f);  //passes (float/float union)
assert(b==d);	 //passes (float/float)
assert(a==i_f);  //fails  (float/const float)


More information about the Digitalmars-d-learn mailing list