Square root Python 2.7.12

后端 未结 4 1417
遥遥无期
遥遥无期 2021-01-22 01:29

Why does the math module return the wrong result?

First test

A = 12345678917
print \'A =\',A
B = sqrt(A**2)
print \'B =\',int(B)
         


        
4条回答
  •  日久生厌
    2021-01-22 02:09

    If you want to calculate sqrt of really large numbers and you need exact results, you can use sympy:

    import sympy
    
    num = sympy.Integer(123456758365483459347856)
    
    print(int(num) == int(sympy.sqrt(num**2)))
    

提交回复
热议问题