[fpc-devel] samevalue function issue

waldo kitty wkitty42 at windstream.net
Wed Sep 30 17:14:59 CEST 2015


On 09/30/2015 07:18 AM, Jonas Maebe wrote:
> 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).

so the OP should get the desired results if they use

   if samevalue(a, double(5.1)) then

correct?

-- 
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list* unless
        private contact is specifically requested and granted.



More information about the fpc-devel mailing list