众所周知,由于舍入和精度问题,比较浮点数是否相等.
例如:https: //randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
在Python中处理这个问题的推荐方法是什么?
当然,这个地方有一个标准的库函数吗?
我想在Python中计算一个大于10 ^ 2000的数字的平方根.如果我将此数字视为普通整数,我将始终得到此结果:
Traceback (most recent call last):
File "...", line 3, in <module>
print( q*(0.5) )
OverflowError: int too large to convert to float
Run Code Online (Sandbox Code Playgroud)
我该如何解决?或者除了使用Python之外是否存在计算此平方根的可能性?
我试图在python 2.7中编写一个程序,它首先会看到一个数字是否均匀地划分另一个,如果它确实得到了除法的结果.
但是,当我使用大数字时,我得到了一些有趣的结果.
目前我正在使用:
from __future__ import division
import math
a=82348972389472433334783
b=2
if a/b==math.trunc(a/b):
answer=a/b
print 'True' #to quickly see if the if loop was invoked
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到:
True
Run Code Online (Sandbox Code Playgroud)
但82348972389472433334783显然不是.
任何帮助,将不胜感激.