如果数字以 .0 结尾,我正在尝试运行测试
我正在运行一个程序,其中的数字相差几个数量级,所以我无法估计到一定数量的数字。使用 % 也不起作用,因为某些数字被排除在外。这个程序中的所有数字都是 float ,所以我需要一种方法来检查它是否以 .0 结尾,而不是以 .00000000000001232 或它必须完全以 .0 结尾的东西
round 函数的问题是我要处理几个数量级的数字。我需要一些东西来检查它在 . 之后是否只有一位小数。或者检查小数点是否为 0 的东西。
代码:
from myro import *
from math import *
def main():
z = 3
a = 2
b = 2
x = 3
y = 2 #starts at y = 3
lim = 25
c = (a**x + b**y)**(1.0/z)
resultnum = 0
while z <= lim:
while a <= lim:
while b <= lim:
while x <= lim:
while y <= lim:
y = y + 1
c = (a**x + b**y)**(1.0/z)
if float(int(c) + 1) != round(c, 6):
pass
else:
print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(int(c)+1) + "^" + str(z)
resultnum = resultnum + 1
print c
y = 3
x = x + 1
x = 3
b = b + 1
b = 3
a = a + 1
a = 3
z = z + 1
print z
print "code cycle complete"
print str(resultnum) + " results"
main()
最佳答案
>>> n1 = 2.0
>>> int(n1) == n1 and isinstance(n1, float)
True
>>> n1 = 2
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 2.01
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 1E1 #works for scientific notation as well
>>> int(n1) == n1 and isinstance(n1, float)
True
关于python - 如何查看数字是否以 .0 结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995249/