-3

I am posting two small codes. Both give different answers and I am not able to understand why this is happening and what is the work around of this problem.

i=3
a=i*0.1
a==0.3
FALSE

i=2
a=i*0.1
a==0.2
TRUE
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

1 Answers1

0

You can round a, to get more consistent result:

i <- 3L
a <- i*0.1
round(a, 1) == 0.3


i=2L
a=i*0.1
round(a, 1) == 0.2

This may be occurring due to data type mis-match.

Kumar Manglam
  • 2,780
  • 1
  • 19
  • 28