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
Comparing numbers
Re: Comparing numbers
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.
Best regards,
Tom
"Did I offend you?"
"No."
"Okay, give me a second chance."
Tom
"Did I offend you?"
"No."
"Okay, give me a second chance."
Re: Comparing numbers
Starting with the dinosaur days of dBase II, we always used val(str()) to deal with this..
Brian Wolfsohn
Retired and traveling around the country to music festivals in my RV.
OOPS.. Corona Virus, so NOT traveling right now...
http://www.breadmanrises.com
FB travel group: The Breadman Rises
Retired and traveling around the country to music festivals in my RV.
OOPS.. Corona Virus, so NOT traveling right now...
http://www.breadmanrises.com
FB travel group: The Breadman Rises
Re: Comparing numbers
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.
Best regards,
Tom
"Did I offend you?"
"No."
"Okay, give me a second chance."
Tom
"Did I offend you?"
"No."
"Okay, give me a second chance."
Re: Comparing numbers
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?
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'.
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
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
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
Richard
- Eugene Lutsenko
- Posts: 1649
- Joined: Sat Feb 04, 2012 2:23 am
- Location: Russia, Southern federal district, city of Krasnodar
- Contact:
Re: Comparing numbers
I'm shocked. But I saw it and took it into account. Purely empirically, when debugging