JSONata bug? how come 0.1 > 0.1 returns true?

Am I missing something? This is from try.jsonata.org but it certainly is the same in Node-red. This should be 'false', right?

image

You can see the same thing in JavaScript:

$ node
Welcome to Node.js v16.13.1.
Type ".help" for more information.
> 1.1-1>0.1
true

When you evaluate 1.1-1 you can see what is happening:

> 1.1-1
0.10000000000000009

Welcome to the way JavaScript represents floating point numbers.

2 Likes

Thank you! I thought I was going crazy :slight_smile:

Not javascript alone...

pi@nrpi:~ $ python3
Python 3.6.3 (default, Apr 26 2019, 12:57:09)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.1-1
0.10000000000000009
>>>

2 Likes

Interesting. PowerShell gets it right.

> 1.1-1
0.1

And then screws it up!

> 1.1-1 -gt 0.1
True
> 1.1 -gt 1.1
False

R also messes up. As does Lua and PHP. Got bored after that!

1 Like

haha yeah, its an float thing. Unfortunately you have to know this to um, know this :man_shrugging:

So where it "looks" like powershell gets it right, whats likely happening is it rounds (for display) after x amount of decimal places (e.g. 0.10000000000000009 is rounded to 0.10000000 which is then shown as 0.1) but the actual number is still 0.10000000000000009 therefore 1.1 - 1 == 0.10000000000000009 and then 0.10000000000000009 -gt 0.1 == true. I'd rather be told 1.1 - 1 == 0.10000000000000009 then at least i can understand why 1.1-1 -gt 0.1 == true

1 Like

It's still wonky 1.1-1 = 0.1, where does that 9 come from lol

maybe from your user name @9toejack :smiley:

1 Like

walked into that one! maybe i should change my profile pic to my foot!

It is just how floating point math works. I'm sure all of these programs are using the same C compiler.

$ perl -e 'printf "%0.30f\n", $_ for 1.1-1.0, 0.1, 13.1-13'
0.100000000000000088817841970013
0.100000000000000005551115123126
0.099999999999999644728632119950

Its not a compiler issue, it is an IEEE 754 design issue. You only have 64 bits to fit an almost infinite amount of numbers into. It simply has limitations.

1 Like

It's a design issue. There's BCD and there's fixed-point arithmetic. But for some reason, they chose to go with floating-point.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.