Page 1 of 1

Comparing numbers

Posted: Thu Feb 24, 2022 9:59 am
by richardc
I am having a problem comparing numbers.

Example:
x := 22.74
y := 3.64
z := 19.10

? (x-y) returns 19.10
? (x-y) <> z returns .T. (incorrect)

If I use round() to 2 decimals, it will return .F. (correct)
? round(x-y,2) <> round(z,2) returns .F. (correct)

I do not understand why I have to use round(). I shouldn't have to.

Anyone have an idea why this is not working as expected?
Thanks,
Richard

Re: Comparing numbers

Posted: Thu Feb 24, 2022 10:07 am
by Tom
This is in Xbase++ from the beginning on (here's an open PDR for version 1.7: https://www.alaska-software.com/scripts ... PDRID=4707). It's a problem with the representation of numerics.

Re: Comparing numbers

Posted: Thu Feb 24, 2022 10:37 am
by richardc
Thanks Tom.

Re: Comparing numbers

Posted: Thu Feb 24, 2022 4:14 pm
by bwolfsohn
Starting with the dinosaur days of dBase II, we always used val(str()) to deal with this..

Re: Comparing numbers

Posted: Thu Feb 24, 2022 11:08 pm
by Tom
The reason is, Xbase++ is representing floating point numbers in DWORD storages. And calculation by computers is not done the way we do it, but bitwise. This leads to wrong results even though the calculation itself doesn't need rounding. Extending the value storage would solve this problem, but lead to incompatibility. I don't know, who still needs Clipper compatibility, but there seem to be some guys out there.

Re: Comparing numbers

Posted: Fri Feb 25, 2022 12:35 am
by skiman
Hi,

It's a pitty that this isn't solved because of clipper compatibility. I suppose this is less than 1% of the Xbase++ users?

Code: Select all

nBegin := 72895.21
nSaldo := -3185.99
nEnd := 69709.22 
// if the above values are the result of a calculation, you get the below results. When you define them as above, I suppose it will give other results.
if nBegin+nSaldo = nEinde  -> is FALSE
if int(nBegin*100)+int(nSaldo*100) = int(nEinde*100) -> is FALSE
if str(nBegin+nSaldo,12,2)= str(nEinde,12,2) -> is TRUE 
As Brian mentioned, when you use STR() it is always working.

I'm using this also since more than 20 years, but I didn't knew that 'Clipper compability' is the cause of this 'solution'.

Re: Comparing numbers

Posted: Fri Feb 25, 2022 8:42 am
by richardc
Thanks to everyone for the information. I will make the necessary changes to my code. I have been doing this for a long time and did not know about it.

Richard

Re: Comparing numbers

Posted: Sun Feb 27, 2022 6:04 am
by Eugene Lutsenko
I'm shocked. But I saw it and took it into account. Purely empirically, when debugging