为什么 %e 在格式字符串中的行为与 %g 不同?
Why does %e behave different than %g in format strings?
我已经在 Python2、Python3 和 C:
中尝试过这个
为什么这些格式字符串 return 数字的精度不同?
>>> "%.3e" % 123456789
'1.235e+08'
>>> "%.3g" % 123456789
'1.23e+08'
来自 python 文档:
'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.
'g'
General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.
The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.
A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.
备选值:
>>> "%.3e" % 123
'1.230e+02'
>>> "%.3g" % 123
'123'
>>> "%.3e" % 1234
'1.234e+03'
>>> "%.3g" % 1234
'1.23e+03'
区别显然在于如何指定精度。 g
似乎使用精度作为精度的正常定义,而 e
使用小数点后 的位数。
e, E
The double argument is rounded and converted in the style [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00.
...
g, G
The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.
因此,即使精度相同,它们也会做不同的事情。
我将尝试先描述 %g
的规则。
我发现@Dale Myers 引用的文档已经很详细了,我建议看一下,但我会在这里给出更清楚的解释。
指数和精度
在我们开始讨论规则之前,让我们先弄清楚exp
(科学记数法中使用的指数)和precision
(将被简称为p
,格式化表达式中使用的精度,%.6e
或%.6g
。精度默认为6)。
通用格式规则
好的,下面是 %g
的规则(通用格式):
-4 <= exp < p
:使用十进制格式
exp < -4
:使用指数格式
exp >= p
:使用指数格式
- 计算精度位数时会四舍五入。
- 精度用于限制所有数字,而不仅仅是十进制数字。
让我们看看下面的所有边缘情况:
>>> gx = '%.6g'
>>> ex = '%.6e'
# -4 <= exp < p
>>> gx % 12345
12345
>>> gx % 0.012345
0.012345
# exp == p
>>> ex % 1234567
1.23456e+06
>>> gx % 1234567
1.23457e+06 # Notic the decimal digits are rounded
# exp == -4
>>> ex % 0.000123456
1.234560e-4
>>> gx % 0.000123456
0.000123456
# exp < -4
>>> ex % 0.0000123456
1.234560e-5
>>> gx % 0.0000123456
1.23456e-5
为什么我们需要 %g
如果你现在理解了 %g
的规则,很明显它试图在 %f
和 e
之间找到一个中间地带,所以在格式化数字时,它将使用最适合您的格式,而不是您自己决定 %f
或 %e
。
我已经在 Python2、Python3 和 C:
中尝试过这个为什么这些格式字符串 return 数字的精度不同?
>>> "%.3e" % 123456789
'1.235e+08'
>>> "%.3g" % 123456789
'1.23e+08'
来自 python 文档:
'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.
'g'
General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.
A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.
备选值:
>>> "%.3e" % 123
'1.230e+02'
>>> "%.3g" % 123
'123'
>>> "%.3e" % 1234
'1.234e+03'
>>> "%.3g" % 1234
'1.23e+03'
区别显然在于如何指定精度。 g
似乎使用精度作为精度的正常定义,而 e
使用小数点后 的位数。
e, E
The double argument is rounded and converted in the style [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00.
...
g, G
The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.
因此,即使精度相同,它们也会做不同的事情。
我将尝试先描述 %g
的规则。
我发现@Dale Myers 引用的文档已经很详细了,我建议看一下,但我会在这里给出更清楚的解释。
指数和精度
在我们开始讨论规则之前,让我们先弄清楚exp
(科学记数法中使用的指数)和precision
(将被简称为p
,格式化表达式中使用的精度,%.6e
或%.6g
。精度默认为6)。
通用格式规则
好的,下面是 %g
的规则(通用格式):
-4 <= exp < p
:使用十进制格式exp < -4
:使用指数格式exp >= p
:使用指数格式- 计算精度位数时会四舍五入。
- 精度用于限制所有数字,而不仅仅是十进制数字。
让我们看看下面的所有边缘情况:
>>> gx = '%.6g'
>>> ex = '%.6e'
# -4 <= exp < p
>>> gx % 12345
12345
>>> gx % 0.012345
0.012345
# exp == p
>>> ex % 1234567
1.23456e+06
>>> gx % 1234567
1.23457e+06 # Notic the decimal digits are rounded
# exp == -4
>>> ex % 0.000123456
1.234560e-4
>>> gx % 0.000123456
0.000123456
# exp < -4
>>> ex % 0.0000123456
1.234560e-5
>>> gx % 0.0000123456
1.23456e-5
为什么我们需要 %g
如果你现在理解了 %g
的规则,很明显它试图在 %f
和 e
之间找到一个中间地带,所以在格式化数字时,它将使用最适合您的格式,而不是您自己决定 %f
或 %e
。