分页: 1 / 1

[Python]从教程复制的代码,我这里显示的结果和教程中不同?

发表于 : 2016-08-17 0:22
科学之子
[Python]从教程复制的代码,我这里显示的结果和教程中不同?
教程地址:
https://docs.python.org/3.4/tutorial/in ... formatting
教程的执行结果:

代码: 全选

>>> import math
>>> print('The value of PI is approximately {}.'.format(math.pi))
The value of PI is approximately 3.14159265359.
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.
我的执行结果:

代码: 全选

Python 3.4.2 (default, Oct  8 2014, 13:14:40) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> print('The value of PI is approximately {}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.
>>> 
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.
>>> 

Re: [Python]从教程复制的代码,我这里显示的结果和教程中不同?

发表于 : 2016-08-17 6:10
poloshiao
1. 猜測
format(math.pi)
Floating Point Arithmetic
precision
不同

2. 參閱
2-1. https://docs.python.org/3.4/library/math.html
9.2.7. Constants
math.pi
The mathematical constant π = 3.141592..., to available precision.
2-2. https://docs.python.org/3.4/tutorial/fl ... -fp-issues
Floating Point Arithmetic: Issues and Limitations
使用 precision 關鍵字 搜尋

Re: [Python]从教程复制的代码,我这里显示的结果和教程中不同?

发表于 : 2016-08-17 9:34
astolia
python 3.2之前,str()转换数值默认的精度是12。教程偷懒没更新,直接拿原来版本的例子来用了。