About scala Double datatype precision

I'm fairly a newbie to scala and in my baby steps, i'm using Centso 7.2 and Scala 2.12 with sbt console: here is my question is:

scala> val Y=5.12345938988398773983748934789473289472384723984723849
Y: Double = 5.123459389883988

This is with 15 precision and giving the value as excpected

and

scala> val u=4.91839028309283892389012
u: Double = 4.9183902830928385

This is supposed to be 4.9183902830928 390 ---> may be I'm missing something if not then it is loosing the precision value.

Can you please help me understand how Scala is treating this causing this precision inconsistency.

Upvotes: 1

Views: 473

Answers (1)

Nikita Gousak
Nikita Gousak

Reputation: 128

As you probably know, computers store numbers not in decimal but in binary format. And when it comes to floating point numbers they use IEEE 754 standard.

Here you can find a great explanation and examples of how computer converts floating point numbers to and from IEEE 754 format.

Upvotes: 2

Related Questions