0

I am trying to create a program that checks if an equation creates a whole number answer but the equation creates floating point numbers that won't compare to integers. When it gets to the first integer, which is supposed to be 390625 it prints it as 390625.0 and it doesn't leave the while loop when it gets to that number.

I'm new to programming so please keep it simple.

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

5 回答 5

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?这是您的方程式的真实结果,还是由于计算机硬件中表示浮点数的方式而导致的一些小偏差?

您应该阅读以下内容:浮点算术:问题和限制

也许考虑使用这个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
于 2013-06-07T08:49:28.843 回答
1

令我惊讶的是,每个人都跳出结论认为问题出在浮点比较上。在得出结论并急于回答之前,你们应该看看完整的问题/代码。

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

Felis Vulpes,当“c”为整数时,您希望循环中断。但是您的条件“int(c)!= c”并未像您想象的那样经常检查。1.进入循环时会检查这个。那时“c”的值将是 2.51984209979 2。只有在内部的所有循环都完成后才会进行下一次检查。届时,c 的值为 25.7028456664

您需要做的是每次重新计算时检查“c”的值。您的代码可能如下所示

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()
于 2013-06-07T08:57:48.380 回答
0

你需要的东西checks if an equation creates a whole number answer

也许你可以这样做:

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

让 x 是一个数字。则x = [x] + {x} ... (1),其中 [] 为最大整数函数(地板函数),{} 为小数函数

例如 x = 4.5 = 4 + 0.5 意味着,[4.5] = 4,{4.5} = 0.5

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

于 2013-06-07T07:46:08.603 回答
0
abs(c - int(c)) < 0.0000001
于 2013-06-07T06:48:21.513 回答
-1

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

ab = 1.00
ab = int(ab)
于 2013-06-07T06:39:48.410 回答