Compare doubles: 50000.3 > 50000.3 = true!?

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!?

why might that be?

Do you need to check if one is bigger or is it enough to know if they are equal / unequal?

https://docs.xojo.com/Equals

1 Like

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”!

check the debugger you’ll see a double is not to be expected as-is.

50000.3 could as well be stored as 50000.2999999999999999999999999999999999 in your computer.
It’s not a xojo thing, it’s how doubles work.

System.DebugLog "testdouble = "+testdouble.toString+", testcalc = "+testcalc.ToString

gives out:
testdouble = 50000.300000, testcalc = 50000.300000

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.

FWIW,
50000.3 is stored as 50000.300000000003
50000.1 is 50000.099999999999
50000.1 + 0.2 is 50000.299999999999

So, yes, testdouble is greater than testcalc.

50000.300000000003 > 50000.299999999999

1 Like

Crazy. Thank you for the detailed explanation.

No. Try to express 0.1 in binary and you’ll understand …

Not crazy. It’s how floating point works.

1 Like

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…

1 Like

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.

Awesome, I didn’t know about Double.Equals, and I happen to have a need right now, thanks! This forum rocks :slight_smile:

5 Likes
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.