-1

The situation can be reproduced by a simple line as below:

Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).

scala> 0.2+0.7
res0: Double = 0.8999999999999999

So what's the reason behind this? And how can I test the answer to be right if an intermediate process produce an imprecise result?

Thanks.

Ambling
  • 446
  • 5
  • 12

1 Answers1

2

This problem has to do more with how math works with our processors and less with Scala.

Scala,

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.

scala> 0.2 + 0.7
res0: Double = 0.8999999999999999

Python,

$ python
Python 2.7.12 (default, Jun 29 2016, 14:05:02) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> 0.2 + 0.7
0.8999999999999999

Node.js

$ node
> 0.2 + 0.7
0.8999999999999999
sarveshseri
  • 13,738
  • 28
  • 47