[英]How to check if number is integer number, with good precision?
There is similar question: Checking whether a variable is an integer or not , but I see no answer to my question.有类似的问题: Checking if a variable is an integer or not ,但我看不到我的问题的答案。
I mean, I was fighting with big numbers recently, so friend of mine suggested me to install Python.我的意思是,我最近在与大数字作斗争,所以我的朋友建议我安装 Python。 I opened it today, so that I can compute big numbers and have good precision, but... how to use this precision?
我今天打开它,以便我可以计算大数字并具有良好的精度,但是......如何使用这种精度? I mean, if I do something like
pow(31,123)
it works fine, but if I want to check if number is integer, I get:我的意思是,如果我做类似
pow(31,123)
事情,它工作正常,但如果我想检查 number 是否为整数,我会得到:
>>> (4.00000000000001).is_integer()
False
>>> (4.000000000000001).is_integer()
False
>>> (4.0000000000000001).is_integer()
True
>>> (4.00000000000000001).is_integer()
True
I wanted to write simple loop to find some solutions of diophantine-equation , where I need to take square root from the very big number and check if it is integer number, but now I am in pinch.我想写一个简单的循环来找到diophantine-equation 的一些解,我需要从非常大的数字中取平方根并检查它是否是整数,但现在我很紧张。 Can someone help me or give me an advice how to achieve better precision?
有人可以帮助我或给我建议如何获得更好的精度吗?
Example:例子:
For example: $ 2x^2 = 1 + y^31 $, where x,y are integer numbers.例如:$ 2x^2 = 1 + y^31 $,其中 x,y 是整数。 My idea is to make loop, where I increment y (starting from 1), add 1, divide by 2, take square root, and then it must be integer to satisfy the equation.
我的想法是做循环,其中我增加y(从1开始),加1,除以2,取平方根,然后它必须是整数才能满足方程。 This is why I need it.
这就是我需要它的原因。
4.0000000000000001
cannot be accurately represented as float: 4.0000000000000001
不能准确地表示为浮点数:
>>> format(4.0000000000000001, '.53f')
'4.00000000000000000000000000000000000000000000000000000'
so that number is indeed an integer.所以这个数字确实是一个整数。 You cannot hope to go beyond 15 decimal digits on most systems:
在大多数系统上,您不能希望超过 15 位十进制数字:
>>> sys.float_info.dig
15
See thesys.float_info
structure, the dig
attribute represents:参见
sys.float_info
结构体, dig
属性表示:
maximum number of decimal digits that can be faithfully represented in a float
可以在浮点数中忠实表示的最大十进制位数
Use the decimal
module instead if you need more precision.如果您需要更高的精度,请改用
decimal
模块。
You can check if a given number is a square of an integer using the code below:您可以使用以下代码检查给定数字是否为整数的平方:
def is_square(x):
s = int(sqrt(x) + 0.5)
return s * s == x
Similar approach can be used for a diophantine equation.类似的方法可用于丢番图方程。 Just convert y found for a given x to int (y = int(y + 0.5)) and then check if diophantine equation is true for found given x and y
只需将给定 x 找到的 y 转换为 int (y = int(y + 0.5)),然后检查丢番图方程对于给定的 x 和 y 是否成立
Can you not use built in function 'type'?你不能使用内置函数“类型”吗? I ran the following:
我运行了以下内容:
print type(4.0000000000000000000000000000000000000000000000001)
print type(4)
and got the result:并得到了结果:
<type 'float'>
<type 'int'>
You can then check occurrence of 'int' and 'float' in the result using find() method然后,您可以使用 find() 方法检查结果中 'int' 和 'float' 的出现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.