【问题标题】:How to see if a number ends in .0如何查看数字是否以 .0 结尾
【发布时间】:2013-06-08 01:55:08
【问题描述】:

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

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

round 函数的问题是我正在处理几个数量级的数字。我需要一些东西来检查它是否在 .或检查该小数是否为 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 - 1.2 不完全是 1
  • 这就是浮点数的问题。我需要准确。

标签: python floating-point integer decimal rounding


【解决方案1】:
>>> 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

【讨论】:

  • +1 这适用于3e28,而@MikeMüller 的回答则无效
【解决方案2】:

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)

【讨论】:

  • number = 3e28 上试试(因为它打印为3e+28,所以它不起作用
  • 我收到了AssertionError,正如预期的那样,1.5555555555555555555555e12
  • 我使用 Python 3.3。我还在 Python 2 中获得了 AssertioError
【解决方案3】:

只是为了展示另一种方法,你总是可以用'.'分割:

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

请注意,这是有效的,因为您说 all 都是浮点数。这将提供错误num is an int

【讨论】:

    【解决方案4】:
    x = 26.5
    b % math.floor(b) == 0
    >>> False
    
    x = 26.0
    b % math.floor(b) == 0
    >>> True
    

    也应该这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-06
      • 2023-02-09
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2019-06-18
      • 1970-01-01
      相关资源
      最近更新 更多