I encounter some strange behaviour of a greater than comparison:
var teststring as string = "50000.3"
var testdouble as double = teststring.toDouble
var testcalc as double = 50000.1+0.2
var test as Boolean = testdouble > testcalc
System.DebugLog "test = "+test.ToString // outputs true!?
I would like to hear from Xojo, that the calculated 50000.3 is not smaller than the “string-originated” 50000.3
50000.3 > 50000.3 should be false, hence:
testdouble > testcalc should be false
But Xojo gives out a “true”!
You’re not seeing the actual 15-digit fractional portion. ToString is rounding it. You cannot reliably compare double values. That’s what Equals is for: Equal to a given number of decimals. Or round both values yourself and compare. As @DerkJ said, it’s just the nature of double precision in computer programming. There’s a reason they’re not used for monetary applications.
If the CPU could split the number (the integer and decimal parts) and use both as integers, this problem wouldn’t exist. But probably this would be incompatible with how they could or couldn’t work…
Most pocket calculators work that way. But the deduction is incorrect. Try division or taking roots or sinus or numbers like pi etc and you are back to square one - you do Maths with limited precision.
But when dealing with money, wanting precision to the cents, currency is what you want to avoid accumulating fractional differences up and down. But this conversation leads to more factors, like how you store and recover and mix values without losing precision during possible conversions.
Yes, Xojo having some decimal type with arbitrary precision compatible with current SQL standards, where 12.34 and 12.34000000 are the exact same thing for a Decimal(20,8) would be great.