Simple important glitch: 2015.01-2015 == 0.01 returns FALSE

Its simple. Why does the following code behave this way:

> (2015.01-2015)==0.01
[1] FALSE

Yet

> 2015.01-2015
[1] 0.01

It's not a glitch.
Most programming languages have difficulties in comparing whether floats are equal.

> format(2015.01-2015,digits=20)
 "0.0099999999999909051"

You can use all.equal instead:

> all.equal((2015.01-2015),0.01)
[1] TRUE

For details, you can reference:

2 Likes

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