有没有办法强制numpy.set_printoptions显示精确的浮点值?

2024-06-16 10:29:48 发布

您现在位置:Python中文网/ 问答频道 /正文

question 59674518之后,numpy.set_printoptions是否有办法确保显示准确的浮点值,而不显示尾随的零,也不事先知道该值?我已经尝试了所有4个浮动模式选项,但都没有效果。只是试着去理解;没有最终目标。(我认为这是不可能的,如果是的话,理性是什么?只是一个不存在的特征而已?)

Mac_3.2.57$python3
Python 3.7.1 (default, Nov  6 2018, 18:45:35) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.set_printoptions(precision=15)
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 11)
array([0.0107421875])
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.278255462646484e-07])
>>> print("%.45f" % (numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)).item(0))
0.000000327825546264648437500000000000000000000
>>> numpy.set_printoptions(floatmode="fixed")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.278255462646484e-07])
>>> numpy.set_printoptions(floatmode="unique")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.2782554626464844e-07])
>>> numpy.set_printoptions(floatmode="maxprec")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.278255462646484e-07])
>>> numpy.set_printoptions(floatmode="maxprec_equal")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.278255462646484e-07])
>>> numpy.set_printoptions(precision=45,floatmode="fixed")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.278255462646484375000000000000000000000000000e-07])
>>> numpy.set_printoptions(precision=45,floatmode="unique")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.2782554626464844e-07])
>>> numpy.set_printoptions(precision=45,floatmode="maxprec")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.2782554626464844e-07])
>>> numpy.set_printoptions(precision=45,floatmode="maxprec_equal")
>>> numpy.array([22.0], dtype=numpy.float64) / (2 ** 26)
array([3.2782554626464844e-07])
>>> 

Tags: numpyequalarrayprecision浮点fixeduniquequestion