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

Python 计算皮尔逊 负数求根的运算问题

  •  
  •   luoerxiansheng · 2018-05-03 15:51:15 +08:00 · 1846 次点击
    这是一个创建于 2184 天前的主题,其中的信息可能已经有所发展或是发生改变。
    print (738.0-pow(340.0,2)/len(v1)),(3782.6436028-pow(1634.18064594,2)/len(v1))

    print sum1sq, sum1, sum2sq, sum2

    print '='*30

    print sum2sq

    print pow(sum2, 2) / 706

    print sum2sq - pow(sum2, 2) / 706

    print '=' * 30

    下面是执行的结果


    574.260623229 1.68902261066e-08

    738.0 340.0 3782.6436028 1634.18064594

    ==============================

    3782.6436028

    3782.6436028

    -7.32143234927e-11

    ==============================


    上面的代码一一对应 我不明白

    为什么会变成负数 而我写固定的数能计算正确 有没有大佬解惑 谢谢啦
    6 条回复    2018-05-04 22:56:00 +08:00
    msg7086
        1
    msg7086  
       2018-05-04 05:09:11 +08:00
    不太懂你说的东西,不过我看到

    3782.6436028
    3782.6436028

    这两个是一样的?什么东西变成了负数?
    mulog
        2
    mulog  
       2018-05-04 07:44:36 +08:00
    目测浮点数精度问题 有精度要求改用 Decimal
    luoerxiansheng
        3
    luoerxiansheng  
    OP
       2018-05-04 14:43:33 +08:00
    应该是浮点数精度问题
    luoerxiansheng
        4
    luoerxiansheng  
    OP
       2018-05-04 14:45:16 +08:00
    @msg7086 就是两个相同的数最后进行减法计算 得出负数
    msg7086
        5
    msg7086  
       2018-05-04 22:55:36 +08:00
    @luoerxiansheng 数值运算误差。
    msg7086
        6
    msg7086  
       2018-05-04 22:56:00 +08:00
    首先这两个数并不相同。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1429 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 65ms · UTC 23:52 · PVG 07:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.