How to check if a float value is a whole number(如何检查浮点值是否为整数)
问题描述
我试图找到最大的立方根,它是一个整数,小于 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
我不知道如何检查它是否是一个整数!我可以将其转换为字符串,然后使用索引来检查最终值并查看它们是否为零,但这似乎相当麻烦.有没有更简单的方法?
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?
推荐答案
要检查浮点值是否为整数,请使用 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/3
是 0
(整数操作数的地板除法!),并且浮点运算可能不精确(a float
是使用二进制分数的近似值,不是精确的实数).但是稍微调整一下你的循环会给出:
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
您必须检查数字 close 来代替整数,或者不使用 float()
来查找您的数字.就像向下取整 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()
函数 查看浮点值是否在可配置的边距内:
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
对于旧版本,该函数的简单实现(跳过错误检查并忽略无穷大和 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)
这篇关于如何检查浮点值是否为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查浮点值是否为整数
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01