V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 3097 views
    This topic created in 3890 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   ·   Solana   ·   3233 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 11:57 · PVG 19:57 · LAX 04:57 · JFK 07:57
    ♥ Do have faith in what you're doing.