How do I compare two different types of objects in scala?
When I check for values inside scala interpreter like:
scala> 1==1.0000000000000001
res1: Boolean = true
scala> 1==1.000000000000001
res2: Boolean = false
Here I am not getting a clear idea of how the scala compiler interprets them as integers or floats or doubles (and compares). "
source to share
It is not really Scala related, it is rather ieee-754 with floating point arithmetic. First of all, when compared Int
with, Double
it will drop Int
before Double
(always safe). The second case is obvious - the meanings are different.
What happens with the first case is that the type is Double
not capable of storing many significant digits (17 in your case, a 64-bit floating point can store up to 16 decimal digits), so it rounds the value to 1
. And 1 == 1
.
source to share