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
tanny
V2EX  ›  Python

新手请教问题:请问这个小数点后是什么原理?

  •  
  •   tanny · 2014-06-24 13:00:26 +08:00 · 3268 次点击
    这是一个创建于 3596 天前的主题,其中的信息可能已经有所发展或是发生改变。


    今天拿来当计算器时,发现其中一个结果后面出现了000000000001

    以后写程序时需要避免这种问题吗?
    oott123
        1
    oott123  
       2014-06-24 13:15:27 +08:00 via Android   ❤️ 1
    不用,无视之就可以了。
    这恰恰是精度高的表现。

    当然,也许你需要 "%.2f"
    phyng
        2
    phyng  
       2014-06-24 13:17:21 +08:00   ❤️ 1
    1、浮点数的精度问题。
    2、这样可以好一点:
    >>> 60+58.21
    118.21000000000001
    >>> print 60+58.21
    118.21
    >>>
    jokester
        3
    jokester  
       2014-06-24 13:47:51 +08:00   ❤️ 1
    浮点数就那么多bit, 不能保证使用到的所有浮点数都是二进制下的有限小数

    需要判断float A == float B时记得用 abs(A-B)<delta 来做
    kkzxak47
        4
    kkzxak47  
       2014-06-25 11:28:48 +08:00   ❤️ 1
    其实是一个值得深入的问题
    看看这个: http://justjavac.iteye.com/blog/1725977
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2549 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 15:47 · PVG 23:47 · LAX 08:47 · JFK 11:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.