使用 int() 转换 16.0 到 15
Casting using int() turns 16.0 to 15
所以我在尝试将浮点数转换为整数时遇到了一个奇怪的问题。我的代码目前看起来像这样:
from math import gcd
def dostuff(n,m):
L = np.sqrt(n**2+m**2+n*m)
dR = gcd(2*m+n,2*n+m)
atoms=4*L**2/dR
print(atoms)
atoms = int(atoms)
print(atoms)
当我运行这段代码n=4和m=4时,第一次打印returns 16.0,第二次打印returns 15,请问这是什么原因为此,我能做些什么来让第二个 return 变成 16 吗?
你试过圆形函数吗?
round(atoms) 它将四舍五入到最接近的整数 5.6 将是 6 而 5.4 将是 5
如果你想向上使用 ceil(atoms),否则使用 floor(atoms)。希望对你有帮助!
所以我在尝试将浮点数转换为整数时遇到了一个奇怪的问题。我的代码目前看起来像这样:
from math import gcd
def dostuff(n,m):
L = np.sqrt(n**2+m**2+n*m)
dR = gcd(2*m+n,2*n+m)
atoms=4*L**2/dR
print(atoms)
atoms = int(atoms)
print(atoms)
当我运行这段代码n=4和m=4时,第一次打印returns 16.0,第二次打印returns 15,请问这是什么原因为此,我能做些什么来让第二个 return 变成 16 吗?
你试过圆形函数吗? round(atoms) 它将四舍五入到最接近的整数 5.6 将是 6 而 5.4 将是 5 如果你想向上使用 ceil(atoms),否则使用 floor(atoms)。希望对你有帮助!