skg
skg

Reputation: 347

how does compare two different type of objects in scala?

When i am checking values inside scala interpreter like:

scala> 1==1.0000000000000001

res1: Boolean = true

scala> 1==1.000000000000001

res2: Boolean = false

Here I am not getting clear view related with "how does scala compiler interpret these as integer or floating points or doubles(and comparing)" .

Upvotes: 3

Views: 201

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340723

It is not really Scala related, it is more of a floating-point arithmetic issue. First of all when comparing Int with Double it will cast Int to Double (always safe). The second case is obvious - values are different.

What happens with the first case is that Double type is not capable of storing that many significant digits (17 in your case, 64-bit floating point can store up-to 16 decimal digits) so it rounds the value to 1. And 1 == 1.

Upvotes: 9

Related Questions