簡體   English   中英

Python 2 is_integer無法正確返回

[英]Python 2 is_integer not returning properly

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()))

此代碼返回false,這將導致if語句根本不運行。 此外,立方根64為4,因此,結果應為整數。 但是,在1到100(含1和100)之間的范圍內,在這種情況下,1、8和27返回true。 對於為什么64的立方根未返回true的任何幫助,我們將不勝感激。

您已將float用於多維數據集根值。 以下代碼為您提供了float(i**Fraction(1,3)) = 3.9999999

嘗試先舍入該值,然后再將其轉換為浮點型。

if float(round(i**Fraction(1,3),2)).is_integer() == True:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM