Very strange problem with comparing floating point numbers

jerro a at a.com
Sat Sep 29 23:21:20 PDT 2012


> The second one uses fstp twice, then fld twice. I don't know, 
> maybe
> this could be a bug.

You're right the lack of one fst/fld in the first case is a bug. 
x87 floating point registers are 80 bit. This:

fstp    dword [ebp-0CH]

Converts the value in ST0 to single precision float and stores it 
to memory (and pops ST0). When it is later loaded with fld, it is 
not the same as before storing since some precision is lost 
(because the D code compares floats and not reals, this is the 
correct behavior). In the first example, this storing and loading 
only happens for the first function call. For the second call the 
value is returned in ST0 and stays in x87 registers until it is 
compared with fucompp so it is not truncated as the result of the 
first function call was. That's why the compared values are not 
equal.



More information about the Digitalmars-d-learn mailing list