如何判断数字是否以.0结尾

3

我正在尝试运行一个测试,以检查数字是否以 .0 结尾。

我正在运行一个数字差异很大的程序,因此无法估计到一定数量的小数位数。使用 % 也不起作用,因为某些数字会被排除。该程序中的所有数字都是浮点数,因此我需要一种方法来检查它是否以 .0 结尾,而不是以 .00000000000001232 或其他内容结尾,必须恰好以 .0 结尾。

round 函数的问题在于我正在处理几个数量级的数字。我需要一些东西来检查它是否仅有 1 个小数点后的数字,或者是否将该小数点转换为 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()

2
当使用浮点数时,通常不存在“完全为零”,就像 2.2 - 1.2 不完全等于 1 - Blender
浮点数就是这个问题所在。我需要它精确无误。 - Felis Vulpes
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

这个可以适用于“3e28”,而@MikeMüller的答案不行。 - jamylak

4

Python已经做到了这一点。使用Python提供的字符串可能是您想要的:

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

编辑

正如@jamylak所指出的那样,这种方法对于大数值并不适用,因为str使用科学计数法。为了保留将数字转换成字符串的基本思路,同时也考虑到大数值的情况,我们需要采用更加冗长而且有点丑陋的解决方案:

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')

这是@AshwiniChaudhary的答案中的解决方案。
def dot_zero_inst(number):
    return int(number) == number and isinstance(number, float)

比较不同的案例得出相同的结果:

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)

1
尝试使用“number = 3e28”进行测试(这不起作用,因为它打印为“3e+28”)。 - jamylak
我得到了一个“AssertionError”,并且预期使用“1.5555555555555555555555e12”。 - jamylak
我使用Python 3.3。在Python 2中,我也会遇到“AssertioError”。 - Mike Müller

0
x = 26.5
b % math.floor(b) == 0
>>> False

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

应该也这样做。

0

只是为了展示另一种方法,您总是可以通过“.”进行分割:

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

请注意,这个方法之所以有效,是因为您指定了所有都是浮点数。如果变量num是整型会导致错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接