Comparing numbers

This forum is for eXpress++ general support.
Post Reply
Message
Author
richardc
Posts: 21
Joined: Sat May 11, 2013 3:46 pm

Comparing numbers

#1 Post 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

User avatar
Tom
Posts: 1171
Joined: Thu Jan 28, 2010 12:59 am
Location: Berlin, Germany

Re: Comparing numbers

#2 Post 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.
Best regards,
Tom

"Did I offend you?"
"No."
"Okay, give me a second chance."

richardc
Posts: 21
Joined: Sat May 11, 2013 3:46 pm

Re: Comparing numbers

#3 Post by richardc »

Thanks Tom.

bwolfsohn
Posts: 648
Joined: Thu Jan 28, 2010 7:07 am
Location: Alachua, Florida USA
Contact:

Re: Comparing numbers

#4 Post by bwolfsohn »

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

User avatar
Tom
Posts: 1171
Joined: Thu Jan 28, 2010 12:59 am
Location: Berlin, Germany

Re: Comparing numbers

#5 Post 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.
Best regards,
Tom

"Did I offend you?"
"No."
"Okay, give me a second chance."

skiman
Posts: 1185
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: Comparing numbers

#6 Post 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'.
Best regards,

Chris.
www.aboservice.be

richardc
Posts: 21
Joined: Sat May 11, 2013 3:46 pm

Re: Comparing numbers

#7 Post 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

User avatar
Eugene Lutsenko
Posts: 1649
Joined: Sat Feb 04, 2012 2:23 am
Location: Russia, Southern federal district, city of Krasnodar
Contact:

Re: Comparing numbers

#8 Post by Eugene Lutsenko »

I'm shocked. But I saw it and took it into account. Purely empirically, when debugging

Post Reply