V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
thought
V2EX  ›  Python

python 关于计算的一个怪异问题

  •  
  •   thought · 2015-09-03 20:43:49 +08:00 · 2317 次点击
    这是一个创建于 3158 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1.45-1 不等于 0.45 ,具体看图,该怎么办?
    3 条回复    2015-09-07 10:24:21 +08:00
    chengzhoukun
        1
    chengzhoukun  
       2015-09-03 21:06:23 +08:00   ❤️ 1
    codercai
        2
    codercai  
       2015-09-05 16:21:24 +08:00   ❤️ 1
    然而这并不奇怪,机器存储浮点数本来就是个近似值,而非精确值
    Sinic
        3
    Sinic  
       2015-09-07 10:24:21 +08:00   ❤️ 1
    遇到这种情况,使用
    >>> from decimal import Decimal
    >>> Decimal ('1.45') - 1
    Decimal ('0.45')
    >>> (Decimal ('1.45') - 1 ) * 2
    Decimal ('0.90')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2740 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:54 · PVG 19:54 · LAX 04:54 · JFK 07:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.