Python 2不是正确返回的整数 从分数导入分数 计数器=0; a=int(原始输入() b=int(原始输入() 如果1

Python 2不是正确返回的整数 从分数导入分数 计数器=0; a=int(原始输入() b=int(原始输入() 如果1,python,python-2.7,math,isinteger,Python,Python 2.7,Math,Isinteger,您已将float用于立方根值。下面的代码给出了float(i**Fraction(1,3))=3.9999999的值 在将值转换为浮点值之前,请先尝试将其舍入 如果float(舍入(i**Fraction(1,3,2)).is_integer()==True:您遇到了浮点数不太准确的问题-请参阅可能的重复,我不建议先舍入-这将为任何值返回True谢谢Peter,我错过了round函数中的浮点。float(round(0.5)).is_integer()返回True,尽管float(round(0

您已将float用于立方根值。下面的代码给出了
float(i**Fraction(1,3))=3.9999999的值

在将值转换为浮点值之前,请先尝试将其舍入


如果float(舍入(i**Fraction(1,3,2)).is_integer()==True:

您遇到了浮点数不太准确的问题-请参阅可能的重复,我不建议先舍入-这将为任何值返回True谢谢Peter,我错过了round函数中的浮点。
float(round(0.5)).is_integer()
返回True,尽管
float(round(0.5,2)).is_integer()
返回FALSE。这更好,但仍然值得一提的是精度折衷
from fractions import Fraction

counter = 0;
a = int(raw_input())
b = int(raw_input())

if 1 <= a <= 10 ** 8:
    if a <= b <= 10 ** 8:
        for i in range(a, b+1):
            if float(i**Fraction(1,3)).is_integer() == True:
                counter += 1
                print(i)

print(str(counter))
print (str(float(64**Fraction(1,3)).is_integer()))