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

软件和网站开发以及相关技术探讨
回复
科学之子
帖子: 2284
注册时间: 2013-05-26 6:58
系统: Debian 9

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

#1

帖子 科学之子 » 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.
>>> 
poloshiao
论坛版主
帖子: 18279
注册时间: 2009-08-04 16:33

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

#2

帖子 poloshiao » 2016-08-17 6:10

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 關鍵字 搜尋
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

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

#3

帖子 astolia » 2016-08-17 9:34

python 3.2之前,str()转换数值默认的精度是12。教程偷懒没更新,直接拿原来版本的例子来用了。
回复