[英]Why is double 0.1 more accurate than its long double version?
How can I understand this another floating-point paradox --- 0.1 represented as double is more accurate than 0.1 represented as long double?我如何理解另一个浮点悖论 --- 0.1 表示为 double 比 0.1 表示为 long double 更准确?
In [134]: np.double(0.1)
Out[134]: 0.1
In [135]: np.longdouble(0.1)
Out[135]: 0.10000000000000000555
It's not more accurate.这不是更准确。 The longdouble repr is just showing you more of the inaccuracy that was already present. longdouble repr 只是向您展示了更多已经存在的不准确性。
0.1
is a Python float, which has the same precision as numpy.double
. 0.1
是一个 Python 浮点数,与numpy.double
具有相同的精度。 It does not represent the exact decimal value 0.1, because binary cannot represent that value in a finite number of bits.它不代表精确的十进制值 0.1,因为二进制不能以有限位数表示该值。 0.1
represents this value: 0.1
代表这个值:
>>> import decimal
>>> decimal.Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
which is the closest value to 0.1 that can be represented within the limits of the type's precision.这是可以在类型精度限制内表示的最接近 0.1 的值。
When you construct a numpy.double
or numpy.longdouble
from 0.1
, this is the value you get.当您从0.1
构造numpy.double
或numpy.longdouble
时,这就是您得到的值。 For numpy.longdouble
, this is not the best approximation of 0.1 the type could store.对于numpy.longdouble
,这不是类型可以存储的 0.1 的最佳近似值。
The repr
of both numpy.double
and numpy.longdouble
show the minimum number of decimal digits needed to produce an output that will reproduce the original value if converted back to the original type. numpy.double
和numpy.longdouble
的repr
显示了生成 output 所需的最小十进制位数,如果转换回原始类型,它将重现原始值。 For numpy.double
, that's just "0.1"
, because 0.1
was already the closest double-precision floating point value to 0.1.对于numpy.double
,这只是"0.1"
,因为0.1
已经是最接近 0.1 的双精度浮点值。 For numpy.longdouble
, it requires more digits, because numpy.longdouble
has more precision, so it can represent values closer to 0.1 than 0.1
.对于numpy.longdouble
,它需要更多的数字,因为numpy.longdouble
具有更高的精度,因此它可以表示更接近 0.1 的值而不是0.1
。
If you want the best long double approximation of 0.1, you should pass a string instead of a Python float:如果您想要 0.1 的最佳长双精度近似值,您应该传递一个字符串而不是 Python 浮点数:
numpy.longdouble('0.1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.