如何比较浮点数和整数

时间:2013-06-07 06:38:10

标签: python while-loop integer point floating

我正在尝试创建一个程序来检查方程是否创建了一个整数答案,但该方程创建的浮点数不会与整数相比较。当它到达第一个整数时,它应该是390625,它将它打印为390625.0,当它到达那个数字时它不会离开while循环。

我是编程新手,所以请保持简单。

from myro import *
from math import *

def main():
    z = 3
    a = 2
    b = 2
    x = 3
    y = 3

    lim = 25

    c = (a**x + b**y)**(1.0/z)

    while int(c) != c:
        while z <= lim:
            while a <= lim:
                while b <= lim:
                    while x <= lim:
                        while y <= lim:
                            c = (a**x + b**y)**(1.0/z)
                            print a, b, c, x, y, z
                            y = y + 1

                        y = 3
                        print a, b, c, x, y, z
                        x = x + 1

                    x = 3
                    print a, b, c, x, y, z
                    b = b + 1

                b = 3
                print a, b, c, x, y, z
                a = a + 1

            a = 3
            print a, b, c, x, y, z
            z = z + 1

        print "code cycle complete. no numbers meet criteria"

    print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(c) + "^" + str(z)

main()

5 个答案:

答案 0 :(得分:1)

您必须了解浮动内部由硬件表示的方式。例如:

>>> x = 9999999.99
>>> y = 9999999.9900000002
>>> x == y
True
>>> x
9999999.9900000002
>>> y
9999999.9900000002

(这是Python 2.6,Intel CentOS-64bit;结果可能会因您的架构而异,但您明白了)

那就是说,如果你的结果恰好是100.0,那么你肯定会说这是一个整数。那么100.000000000000000000001呢?这是你的等式的真实结果,还是由于浮动的方式在你的计算机硬件中出现的一些小的偏差?

您应该阅读:Floating Point Arithmetic: Issues and Limitations

也许可以考虑使用decimal包(有一些性能权衡)

<强>更新

如果您使用decimal包,则可以使用余数运算符%is_zero()方法。例如:

>>> from decimal import Decimal
>>> x = Decimal('100.00000000000001')
>>> y = Decimal('100.00000000000000')
>>> (x % 1).is_zero()
False
>>> (y % 1).is_zero()
True

答案 1 :(得分:1)

我很惊讶这一事实,每个人都跳进去得出这个问题是浮点比较的问题。在得出结论并急于回答之前,你们应该看看完整的问题/代码。

让我们回到原点。我并没有试图解释浮点比较的问题。我没有看到嵌套的while循环。考虑到当计算结果为整数时,作者需要打破循环这一事实我会回答。

Felis Vulpes,    你希望循环在“c&#39;是一个整数。但是你的情况&#34; int(c)!= c&#34;没有像你想象的那样经常检查。 1.进入循环时将检查此项。那时候&#34; c&#34;的价值将是2.51984209979 2.只有在内部所有循环完成后才会进行下一次检查。那时,c的值将是25.7028456664

你需要做的是检查&#34; c&#34;的价值。每次你重新计算它。 您的代码可能如下所示

from myro import *
from math import *

def main():
    z = 3
    a = 2
    b = 2
    x = 3
    y = 3

    lim = 25

    c = (a**x + b**y)**(1.0/z)

    #while int(c) != c:
    while z <= lim:
        while a <= lim:
            while b <= lim:
                while x <= lim:
                    while y <= lim:
                        c = (a**x + b**y)**(1.0/z)
                        print a, b, c, x, y, z
                        if int(c) == c:
                            print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(c) + "^" + str(z)
                            return
                        y = y + 1

                    y = 3
                    print a, b, c, x, y, z
                    x = x + 1

                x = 3
                print a, b, c, x, y, z
                b = b + 1

            b = 3
            print a, b, c, x, y, z
            a = a + 1

        a = 3
        print a, b, c, x, y, z
        z = z + 1

    print "code cycle complete. no numbers meet criteria"

main()

答案 2 :(得分:0)

abs(c - int(c)) < 0.0000001

答案 3 :(得分:0)

您需要checks if an equation creates a whole number answer

的内容

可能你可以这样做:

if(c-int(c))==0:
    #code

设x为数字。然后x = [x] + {x} ... (1),其中[]是最大整数函数(floor函数),{}是小数函数

例如,x = 4.5 = 4 + 0.5表示,[4.5] = 4,{4.5} = 0.5

从(1),{x} = x - [x],如果x是整数,则该值应为零。

答案 4 :(得分:-1)

你可以将float转换为int,就像这样......

ab = 1.00
ab = int(ab)
相关问题