Python float\uuuu setformat\uuuuuu?

Python float\uuuu setformat\uuuuuu?,python,python-2.7,floating-point,Python,Python 2.7,Floating Point,我有个问题,我需要得到一个小数点后18位的浮点值。 当我使用默认floatnumber时,它只给我12位小数 然后我就去了 ['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__',

我有个问题,我需要得到一个小数点后18位的浮点值。 当我使用默认floatnumber时,它只给我12位小数

然后我就去了

    ['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__',
 '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__',
 '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__m
od__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '
__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '_
_rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__s
tr__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', '
fromhex', 'hex', 'imag', 'is_integer', 'real']
在那个块中有一个叫做uuu setformat_uuu的东西。它有什么用?如何使用它进行浮点精度设置


我使用的是Python2.7.5x64。

它只对python测试套件有用;helpfloat.\uuuuu设置格式\uuuuuu打印:

浮动.\uuuuu设置格式\uuuuu类型str,fmt->None

您可能不想使用此函数。它的存在主要是为了 在Python的测试套件中使用

typestr必须为“double”或“float”。fmt必须是“未知”之一, “IEEE,big-endian”或“IEEE,little-endian”,此外,只能是 后两者中的一个,如果它看起来与潜在的C现实相匹配

重写C级浮点类型的自动确定。 这会影响浮动与二进制字符串之间的转换方式

有一个float.\uuu getformat\uuuu以及用于相同信息、相同目的的getter方法

有关其使用的详细信息,请参阅


使用可获得更精确的十进制计算,但请查看平台上浮点数精度的详细信息。例如,我的64位Mac OS X系统只能管理15位数字。

这让我感到惊讶,在这个论坛上,人们如何千方百计地避免看到真正的问题,不可否认,对于一个显然对python非常陌生的人来说,这个问题的措辞非常糟糕。这是一个很好的新手问题,所以我会把事情说清楚

显然,setformat对于falloutx的预期用途是无用的。打字:

>>> help(float.__setformat__)
将给任何需要解释的人一个答案,这是任何新手都不可能有理由感兴趣的

然而,隐式查询是有效的

如果我这样做:

>>> from math import pi
>>> print pi
3.14159265359
我只看到小数点后十二位。Python使用双精度,因此我可以这样做:

>>> print "{:.16}".format(pi)
3.1415926535897931
>>> print "{:.16f}".format(1.0).rstrip('0')
1.
>>> class newfloat(float):
...   precision=4
...   def __str__(self):
...     s="{{:.{0}f}}".format(self.precision)
...     s=s.format(self)
...     if s.endswith('0'): s=s.rstrip('0')+'0'
...     return s
... 
>>> f=newfloat(pi)
>>> g=newfloat(1.0)
>>> print f,g
3.1416 1.0
>>> newfloat.precision=2
>>> print f,g
3.14 1.0
这是正确的,但尾随的“1”应该是“2”。就是 因为双精度只能表示大约16个有效数字,而整数“3”算作一个有效数字

因此,要回答真正的问题: 在内部,您有16个有效数字。这意味着对于-1.0 要查看完整精度,请使用如上所述的字符串格式

但是,可能会出现外观问题:

>>> print "{:.16f}".format(1.0)
1.0000000000000000
您可以这样做:

>>> print "{:.16}".format(pi)
3.1415926535897931
>>> print "{:.16f}".format(1.0).rstrip('0')
1.
>>> class newfloat(float):
...   precision=4
...   def __str__(self):
...     s="{{:.{0}f}}".format(self.precision)
...     s=s.format(self)
...     if s.endswith('0'): s=s.rstrip('0')+'0'
...     return s
... 
>>> f=newfloat(pi)
>>> g=newfloat(1.0)
>>> print f,g
3.1416 1.0
>>> newfloat.precision=2
>>> print f,g
3.14 1.0
另一个问题: 有一些有趣的方法来解决这个问题。因此,例如,如果print语句的冗长让您感到困扰,并且您确实希望看到至少一位十进制精度,您可以这样做:

>>> print "{:.16}".format(pi)
3.1415926535897931
>>> print "{:.16f}".format(1.0).rstrip('0')
1.
>>> class newfloat(float):
...   precision=4
...   def __str__(self):
...     s="{{:.{0}f}}".format(self.precision)
...     s=s.format(self)
...     if s.endswith('0'): s=s.rstrip('0')+'0'
...     return s
... 
>>> f=newfloat(pi)
>>> g=newfloat(1.0)
>>> print f,g
3.1416 1.0
>>> newfloat.precision=2
>>> print f,g
3.14 1.0
如果使用.format构造进行大量字符串格式化,则可以通过定义一个类(如上所述)来实现相同的结果,但命名它,例如, '_'. 然后你要这样做:

>>> "A {0} string with {1} custom float format {2}".format(*map(_,(1,2,pi)))
'A 1.0 string with 2.0 custom float format 3.14'

你试过:helpfloat.\uuuuuu setformat\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。您可能需要使用decimal.decimal。您可能需要了解IEEE 754,以便了解浮点没有固定的小数数,因为它存储为符号、尾数和指数,并具有NaN的特殊配置,等等。你能告诉我一种获得18位或更多精度的方法吗?@falloutx也许你在找这个模块