Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2015-09

samevalue function issue

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrea Mauri

Dear all,

I am using samevalue function provided with fpc (math unit). I am using samevalue instead of = in order to avoid problems related to precision.

If I compare two values:

   a:= 5.1000000000000005;
   b:= 5.1;

if I use = operator I get that the two numbers are different. Using samevalue I should get that a and b are equal.

Anyway I am encountering an issue that I cannot understand, see the two code samples below:

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!
     label2.Caption:= 'same value (const): True'
   else
     label2.Caption:= 'same value (const): False';
end;

why two different behaviours if I use b as a variable or as a constant?

Inversely:

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

It seems that samevalue works if I use samevalue(a, b) but not if I directly pass a number to samevalue as samevalue(a, 44). But I am not sure since I did only few tests.

Could you please provide some explanations? Why the two different behaviours? How should I use samevalue?

Let me know and best regards,
Andrea Mauri

Reply by email

Jonas Maebe
to Andrea Mauri

Andrea Mauri wrote on Wed, 30 Sep 2015:

Show quoted text 11 lines
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.

Jonas

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

Reply by email

waldo kitty
to Jonas Maebe: Andrea Mauri wrote on Wed, 30 Sep 2015: That is because 5.1 cannot be represented exactly…
Jonas Maebe wrote 19 lines
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.

Reply by email