为什么将0.29999999999999998转换为0.3? [英] Why is 0.29999999999999998 converted to 0.3?

查看:57
本文介绍了为什么将0.29999999999999998转换为0.3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在内部如何工作?

即使0.3无法用二进制表示,它如何决定将0.29999999999999998转换为0.3?

还有更多示例:

scala> 0.29999999999999998
res1: Double = 0.3

scala> 0.29999999999999997
res2: Double = 0.3

scala> 0.29999999999999996
res3: Double = 0.29999999999999993

scala> 0.29999999999999995
res4: Double = 0.29999999999999993

解决方案

涉及两个转换.

第一个0.29999999999999998转换为最接近的可表示数字0.2999999999999999999988897769753748434595763683319091796875.

接下来,将0.299999999999999988897769753748434595763683319091796875转换为十进制以进行打印. 0.3也是转换为0.299999999999999988888897769753748434595763683319091796875的数字之一,因为它太短而被打印.

每个有限双精度数都可以精确表示为小数.通常,默认输出不会尝试打印确切的值,因为它可能很长-远远长于上面的示例.通常的选择是打印最短的十进制分数,该分数将转换为输入的double.两种转换都是使用非平​​凡的算法完成的.有关一些讨论和对输出算法的引用,请参见将IEEE 754双精度转换为字符串的算法?.

================================================ ===============

评论中对值0.30000000000000004进行了一些讨论.我同意里克·里根(Rick Regan)和杰斯珀(Jesper)的评论,但认为添加到此答案可能会有用.

最接近于0.30000000000000004的双精度数的精确值为0.3000000000000000444089209850062616169452667236328125. [0.3000000000000000166533453693773481063544750213623046875、0.3000000000000000721644966006351751275360584259033203125]范围内的所有十进制数字都将转换为该值,即使是稍微超出该范围的数字也不会转换为该值. 0.3000000000000000超出范围,所以它没有足够的数字. 0.30000000000000004在范围内,因此不需要更多的数字即可正确识别双精度数.

How does it work internally?

How does it decide to convert 0.29999999999999998 to 0.3, even though 0.3 cannot be represented in binary?

Here are some more example:

scala> 0.29999999999999998
res1: Double = 0.3

scala> 0.29999999999999997
res2: Double = 0.3

scala> 0.29999999999999996
res3: Double = 0.29999999999999993

scala> 0.29999999999999995
res4: Double = 0.29999999999999993

解决方案

There are two conversions involved.

First 0.29999999999999998 is converted to 0.299999999999999988897769753748434595763683319091796875, the nearest representable number.

Next, 0.299999999999999988897769753748434595763683319091796875 is converted to decimal for printing. 0.3 is also one of the numbers that converts to 0.299999999999999988897769753748434595763683319091796875, and it is the one that gets printed because it is so short.

Every finite double number is exactly representable as a decimal fraction. Generally, default output does not attempt to print the exact value, because it can be very long - far longer than the example above. A common choice is to print the shortest decimal fraction that would convert to the double on input. Both conversions are done using non-trivial algorithms. See Algorithm to convert an IEEE 754 double to a string? for some discussion and references to output algorithms.

==============================================================

There has been some discussion in comments on the value 0.30000000000000004. I agree with the comments by Rick Regan and Jesper, but thought it might be useful to add to this answer.

The exact value of the closest double to 0.30000000000000004 is 0.3000000000000000444089209850062616169452667236328125. All decimal numbers in the range [0.3000000000000000166533453693773481063544750213623046875, 0.3000000000000000721644966006351751275360584259033203125] convert to that value, and no numbers even slightly outside that range do so. 0.3000000000000000 is outside the range, so it does not have enough digits. 0.30000000000000004 is inside the range, so there is no need for more digits to correctly identify the double.

这篇关于为什么将0.29999999999999998转换为0.3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆