推荐学习书目
› 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 · Sep 3, 2015 · 3450 views
    This topic created in 4045 days ago, the information mentioned may be changed or developed.
    1.45-1 不等于 0.45 ,具体看图,该怎么办?
    3 replies  •  2015-09-07 10:24:21 +08:00
    codercai
        2
    codercai  
       Sep 5, 2015   ❤️ 1
    然而这并不奇怪,机器存储浮点数本来就是个近似值,而非精确值
    Sinic
        3
    Sinic  
       Sep 7, 2015   ❤️ 1
    遇到这种情况,使用
    >>> from decimal import Decimal
    >>> Decimal ('1.45') - 1
    Decimal ('0.45')
    >>> (Decimal ('1.45') - 1 ) * 2
    Decimal ('0.90')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2575 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:25 · PVG 22:25 · LAX 07:25 · JFK 10:25
    ♥ Do have faith in what you're doing.