简体   繁体   English

为什么 double 0.1 比它的 long double 版本更准确?

[英]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.doublenumpy.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.doublenumpy.longdoublerepr显示了生成 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.

相关问题 如何获得 Python 长双精度文字? - How to get a Python long double literal? 为什么用numpy生成的0.0s列表会抛出 <RuntimeWarning: invalid value encountered in double_scalars> ,而不是除以0错误? - Why is a numpy-generated list of 0.0s throwing <RuntimeWarning: invalid value encountered in double_scalars>, rather than a divide by 0 error? Numpy longdouble算术似乎没有转换为long double - Numpy longdouble arithmetic does not seem to be in long double with conversion Python 错误 RuntimeError:预期标量类型 Long 但发现 Double - Python Error RuntimeError: expected scalar type Long but found Double 这种双列表理解是否可以直接进入数组(是否有其他Pythonic方式?) - Can this double list comprehension go straight into an array (Is there a more Pythonic way?) 为什么Numpy操作0.1M矩阵1000次比操作1M矩阵100次快很多? - Why does Numpy operate 0.1M matrix 1000 times is much faster than operate 1M matrix 100 times? 为什么有时p值小于numpy可以表示的最准确的数字 - Why sometimes p-value is smaller than the most accurate number that numpy can represent 为什么我们在Numpy中获得双布尔索引的结果? - Why do we get such a result for double boolean indexing in Numpy? 为什么numpy.where不适用于“双”切片数组? - Why numpy.where doesn't work with 'double' sliced arrays? 为什么双切片numpy数组的赋值不起作用? - Why does an assignment for double-sliced numpy arrays not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM