3

如果数字以 .0 结尾,我正在尝试运行测试

我正在运行一个数字数量级相隔的程序,所以我无法估计到一定数量的数字。使用 % 也不起作用,因为某些数字被排除在外。该程序中的所有数字都是浮点数,所以我需要一种方法来检查它是否以 .0 结尾,而不是以 .00000000000001232 或必须完全以 .0 结尾的东西

圆形函数的问题是我正在处理几个数量级的数字。我需要一些东西来检查它是否在 . 或检查该小数是否为0的东西。

代码:

from myro import *
from math import *


def main():

    z = 3
    a = 2
    b = 2
    x = 3
    y = 2 #starts at y = 3

    lim = 25

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

    resultnum = 0    

    while z <= lim:

        while a <= lim:

            while b <= lim:

                while x <= lim:

                    while y <= lim:

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

                        if float(int(c) + 1) != round(c, 6):
                            pass
                        else:
                            print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(int(c)+1) + "^" + str(z)
                            resultnum = resultnum + 1
                            print c

                    y = 3
                    x = x + 1

                x = 3
                b = b + 1

            b = 3
            a = a + 1

        a = 3
        z = z + 1
        print z

    print "code cycle complete"
    print str(resultnum) + " results"


main()
4

4 回答 4

8
>>> n1 = 2.0
>>> int(n1) == n1 and isinstance(n1, float)
True
>>> n1 = 2
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 2.01
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 1E1         #works for scientific notation as well
>>> int(n1) == n1 and isinstance(n1, float)
True
于 2013-06-08T01:57:04.727 回答
2

Python does this already. Going with what Python gives as a string might be what you want:

In [577]: def dot_zero(number):
   .....:     return str(number).endswith('.0')
   .....: 

In [578]: dot_zero(2.0)
Out[578]: True

In [579]: dot_zero(2)
Out[579]: False

In [580]: dot_zero(2.01)
Out[580]: False

EDIT

As pointed out by @jamylak this does not work for large numbers since the scientific notation used by str. Keeping the basic idea of conversion into a string, but also catering for large numbers, we end up with more verbose and admittedly rather ugly solution:

def dot_zero_string(number):
    # tested and works with Python 3.3
    split = str(number).split('e')
    return len(split) == 2 or split[0].endswith('.0')

This is the solution in the answer from @AshwiniChaudhary

def dot_zero_inst(number):
    return int(number) == number and isinstance(number, float)

Comparing different cases gives the same result:

numbers = [1, 1.0, 1000, 1000.0, 3e38, 1.5555555555555555555555e12, 
           1.5555555555555555555555e17, 0, 0.0]
numbers = numbers + [-number for number in numbers]
for number in numbers:
    assert dot_zero_inst(number) == dot_zero_string(number)
于 2013-06-08T02:13:14.173 回答
0

Just to show another method, you can always split by the '.':

>>> num = 12.023
>>> str(num).split('.')[1] == '0'
False
>>> num = 12.0
>>> str(num).split('.')[1] == '0'
True

Note that this works because you said that all were floating points. This will provide an error num is an int

于 2013-06-08T02:15:58.783 回答
0
x = 26.5
b % math.floor(b) == 0
>>> False

x = 26.0
b % math.floor(b) == 0
>>> True

也应该这样做。

于 2021-01-31T21:18:11.783 回答