久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何檢查浮點(diǎn)值是否為整數(shù)

How to check if a float value is a whole number(如何檢查浮點(diǎn)值是否為整數(shù))
本文介紹了如何檢查浮點(diǎn)值是否為整數(shù)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我試圖找到最大的立方根,它是一個(gè)整數(shù),小于 12,000.

I am trying to find the largest cube root that is a whole number, that is less than 12,000.

processing = True
n = 12000
while processing:
    n -= 1
    if n ** (1/3) == #checks to see if this has decimals or not

我不知道如何檢查它是否是一個(gè)整數(shù)!我可以將其轉(zhuǎn)換為字符串,然后使用索引來檢查最終值并查看它們是否為零,但這似乎相當(dāng)麻煩.有沒有更簡(jiǎn)單的方法?

I am not sure how to check if it is a whole number or not though! I could convert it to a string then use indexing to check the end values and see whether they are zero or not, that seems rather cumbersome though. Is there a simpler way?

推薦答案

要檢查浮點(diǎn)值是否為整數(shù),請(qǐng)使用 float.is_integer() 方法:

To check if a float value is a whole number, use the float.is_integer() method:

>>> (1.0).is_integer()
True
>>> (1.555).is_integer()
False

該方法在 Python 2.6 中被添加到 float 類型中.

The method was added to the float type in Python 2.6.

考慮到在 Python 2 中,1/30(整數(shù)操作數(shù)的地板除法!),并且浮點(diǎn)運(yùn)算可能不精確(a float 是使用二進(jìn)制分?jǐn)?shù)的近似值,不是精確的實(shí)數(shù)).但是稍微調(diào)整一下你的循環(huán)會(huì)給出:

Take into account that in Python 2, 1/3 is 0 (floor division for integer operands!), and that floating point arithmetic can be imprecise (a float is an approximation using binary fractions, not a precise real number). But adjusting your loop a little this gives:

>>> for n in range(12000, -1, -1):
...     if (n ** (1.0/3)).is_integer():
...         print n
... 
27
8
1
0

這意味著任何超過 3 的立方(包括 10648)由于上述不精確性而被遺漏:

which means that anything over 3 cubed, (including 10648) was missed out due to the aforementioned imprecision:

>>> (4**3) ** (1.0/3)
3.9999999999999996
>>> 10648 ** (1.0/3)
21.999999999999996

您必須檢查數(shù)字 close 來代替整數(shù),或者不使用 float() 來查找您的數(shù)字.就像向下取整 12000 的立方根:

You'd have to check for numbers close to the whole number instead, or not use float() to find your number. Like rounding down the cube root of 12000:

>>> int(12000 ** (1.0/3))
22
>>> 22 ** 3
10648

如果您使用的是 Python 3.5 或更新版本,則可以使用 math.isclose() 函數(shù) 查看浮點(diǎn)值是否在可配置的邊距內(nèi):

If you are using Python 3.5 or newer, you can use the math.isclose() function to see if a floating point value is within a configurable margin:

>>> from math import isclose
>>> isclose((4**3) ** (1.0/3), 4)
True
>>> isclose(10648 ** (1.0/3), 22)
True

對(duì)于舊版本,該函數(shù)的簡(jiǎn)單實(shí)現(xiàn)(跳過錯(cuò)誤檢查并忽略無窮大和 NaN)為 :

For older versions, the naive implementation of that function (skipping error checking and ignoring infinity and NaN) as mentioned in PEP485:

def isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
    return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

這篇關(guān)于如何檢查浮點(diǎn)值是否為整數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 久久久亚洲一区 | 亚洲精品天堂 | 日韩中文字幕一区二区三区 | 中文字幕视频在线观看 | 久久久91 | 亚洲成人福利 | 亚洲免费在线 | 91精品国产99| www一级片| 欧美性受xxx | 亚洲欧美在线一区 | 欧美国产日韩在线观看成人 | 国产成人精品免费视频大全最热 | 91国内在线观看 | www.9191| 91久久精品日日躁夜夜躁国产 | 日本久久精品视频 | 91精品国产91久久综合桃花 | 九九热久久免费视频 | 久久的色| 欧美视频1区| 日韩一区和二区 | 成人在线网 | 国产高清在线精品一区二区三区 | 亚洲欧美视频一区二区 | www.国产精| 日韩精品一区二区不卡 | 国产精品3区 | 日韩1区 | www久久国产 | 久久精品国产99国产精品亚洲 | 精品1区 | 免费高潮视频95在线观看网站 | 亚洲国产成人精品在线 | 久久亚洲一区二区三区四区 | 久久精品中文字幕 | 99伊人网| 国产精品久久久久久久一区探花 | 国产一区二区三区四区区 | 日本精品一区二区在线观看 | 精品在线观看一区二区 |