推荐学习书目
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 · May 3, 2018 · 2692 views
    This topic created in 2943 days ago, the information mentioned may be changed or developed.
    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 replies    2018-05-04 22:56:00 +08:00
    msg7086
        1
    msg7086  
       May 4, 2018
    不太懂你说的东西,不过我看到

    3782.6436028
    3782.6436028

    这两个是一样的?什么东西变成了负数?
    mulog
        2
    mulog  
       May 4, 2018
    目测浮点数精度问题 有精度要求改用 Decimal
    luoerxiansheng
        3
    luoerxiansheng  
    OP
       May 4, 2018
    应该是浮点数精度问题
    luoerxiansheng
        4
    luoerxiansheng  
    OP
       May 4, 2018
    @msg7086 就是两个相同的数最后进行减法计算 得出负数
    msg7086
        5
    msg7086  
       May 4, 2018
    @luoerxiansheng 数值运算误差。
    msg7086
        6
    msg7086  
       May 4, 2018
    首先这两个数并不相同。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3180 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 466ea39e · 48ms · UTC 00:36 · PVG 08:36 · LAX 17:36 · JFK 20:36
    ♥ Do have faith in what you're doing.