简体   繁体   English

关于Scala Double数据类型精度

[英]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的新手,在我的第一步中,我将Centso 7.2和Scala 2.12与sbt控制台配合使用:这是我的问题是:

scala> val Y=5.12345938988398773983748934789473289472384723984723849
Y: Double = 5.123459389883988

This is with 15 precision and giving the value as excpected 这是15精度,并给出了预期的值

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. 这应该是4.9183902830928 390 --->可能是我遗漏了一些东西(如果不是),那么它失去了精度值。

Can you please help me understand how Scala is treating this causing this precision inconsistency. 您能帮我了解Scala如何处理这种导致精度不一致的问题。

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 . 当涉及浮点数时,它们使用IEEE 754标准

Here you can find a great explanation and examples of how computer converts floating point numbers to and from IEEE 754 format. 在这里,您可以找到有关计算机如何将浮点数与IEEE 754格式相互转换的很好的解释和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM