[fpc-devel] samevalue function issue

Jonas Maebe jonas.maebe at elis.ugent.be
Wed Sep 30 13:18:10 CEST 2015

Andrea Mauri wrote on Wed, 30 Sep 2015:

> var
> a, b: Double;
> begin
>   a:= 5.1000000000000005;
>   b:= 5.1;
>   if samevalue(a, b) then              // I get TRUE - right!
>     label1.Caption:= 'same value (variable): True'
>   else
>     label1.Caption:= 'same value (variable): False';
>   if samevalue(a, 5.1) then             // I get FALSE -- I supposed  
> to get TRUE- bad!

That is because 5.1 cannot be represented exactly using floating  
point. As a result, the compiler will parse it using the highest  
precision available, which is probably the 80 bit extended type on  
your target platform. Therefore, the second samevalue calls the  
extended precision version of that routine, while the first one calls  
the double precision version (since both arguments are double).

Depending on the value, the extended and double precision versions of  
samevalue can indeed have different results. That's also the reason  
for the outcome of your second test.


PS: please don't cross-post to multiple lists.

More information about the fpc-devel mailing list