Comments (4)
知道了
from wtfpython-cn.
虽然 @Cuick 已经找到了答案,写一下自己的理解:
[ins] In [14]: from decimal import *
[ins] In [15]: getcontext().prec
Out[15]: 28
[ins] In [16]: Decimal.from_float(0.3)
Out[16]: Decimal('0.299999999999999988897769753748434595763683319091796875')
[ins] In [17]: Decimal.from_float(0.1 + 0.2)
Out[17]: Decimal('0.3000000000000000444089209850062616169452667236328125')
[ins] In [18]: 0.3
Out[18]: 0.3
[ins] In [19]: 0.1 + 0.2
Out[19]: 0.30000000000000004
上面代码直接展示了0.1+0.2
和0.3
不是同一个东西,这不只是Python
中的情况,可以说绝大多数编程语言的浮点数在这个问题上都是一致的。
[16]
的输出是直接把0.3
转换成二进制表示对应的十进制的值[17]
的输出是分别把0.1
和0.2
转换成二进制表示相加之后对应的十进制的值[18]
的输出对应[16]
[19]
的输出对应[17]
,这个很直接二者都有0.30000000000000004
也就是说在Python
解释器中直接输入0.3
,其实际的二进制对应的值是0.3000000000000000444089209850062616169452667236328125
,然后解释器print
的时候肯定不是全部输出而是要round
一下,那输出几位呢?原则就是**在没有引起混乱的情况下,输出的位数就尽量短`。
为什么这么说呢?
因为不是所有的浮点数都有二进制的精确表示,并且浮点数的二进制位数有限制,所以会存在多个浮点数对应一个二进制的情况(实际就是一个浮点数二进制对应了实数域的一个连续区间,该区间内的浮点数都会映射到这个二进制表示),比如:
[ins] In [23]: Decimal.from_float(0.30000000000000003) # 和0.1+0.2对应的一样,也就是输入和输出不一样
Out[23]: Decimal('0.3000000000000000444089209850062616169452667236328125')
上面那个不引起混乱的规则就是将输出的值反过来代入Decimal.from_float
之后还必须是0.3000000000000000444089209850062616169452667236328125
而不能是其他的值(还能映射回那个二进制)。而0.3000000000000000444089209850062616169452667236328125
,不引起混乱的最短打印值(输出位数尽量短规则)就是round到0.30000000000000004
位数再少就变成了0.3000000000000000
也就是0.3
,这个就换成二进制就是[16]
的输出了。
ref
一个浮点数到底是怎么被转换为字符串输出?一个浮点数不精确,那么其输出的值是怎么被确定的呢? - 大宽宽的回答 - 知乎
from wtfpython-cn.
二进制表示浮点数会有截断误差 就是精度的问题 是正常的
问题是 如果我们想比较 0.1+0.2 之后的数值是否和0.3相等
也就是说从程序员的角度看 他们应该是相等的 输出是true
但是如果从计算机的角度看他们可能不等 那怎么解决呢?
from wtfpython-cn.
这个里面有个方法 只能比较很接近的情况下 认为相等https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python
from wtfpython-cn.
Related Issues (20)
- 『Strings can be tricky sometimes/微妙的字符串 *』中 问题 HOT 4
- Class attributes and instance attributes/类属性和实例属性 HOT 2
- Subclass relationships/子类关系 * 的翻译 HOT 1
- is is not what it is!/出人意料的is! HOT 1
- is is not what it is!/出人意料的is! 单行赋值,交互式环境执行的版本问题
- 一个未配对的括号 HOT 1
- windows 10 运行 wtfpython 报错 HOT 1
- 333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 HOT 2
- 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 HOT 2
- MacOS pip3.6 安装wtfpython, run wtfpython后报错FileNotFoundError
- A tic-tac-toe where X wins in the first attempt!/一蹴即至! HOT 1
- Section: Strings can be tricky sometimes/微妙的字符串 * 中常量折叠在3.7中已经迁移至新的AST 优化器 HOT 1
- 字符串缓存中注释存在错误? HOT 5
- Sync with English version HOT 2
- About Evaluation time discrepancy/执行时机差异 HOT 3
- 对/=运算符的疑惑 HOT 4
- 一个错别字... HOT 3
- Beware of default mutable arguments!/当心默认的可变参数!
- 中文版本的更新问题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wtfpython-cn.