python判断整数_在python中检查float是否等价于一个整数值

1586010002-jmsa.png

In Python 3, I am checking whether a given value is triangular, that is, it can be represented as n(n+1)/2 for some positive integer n

Can I just write:

import math

def is_triangular1(x):

num=(1/2) * (math.sqrt(8*x+1)-1 )

return int(num)==num

Or do I need to do it like this? :

epsilon = 0.000000000001

def is_triangular2(x):

num=(1/2) * (math.sqrt(8*x+1)-1 )

return abs(int(num) - num)

I checked that both of the functions return same results for x up to 1,000,000. But I am not sure if generally speaking int(x) == x will always correctly determine whether a number is integer, because of the cases when for example 5 is represented as 4.99999999999997 etc.

As far as I know, the second way is the correct one if I do it in C, but I am not sure about Python 3.

解决方案

You'll want to do the latter. In Programming in Python 3 the following example is given as the most accurate way to compare

def equal_float(a, b):

#return abs(a - b) <= sys.float_info.epsilon

return abs(a - b) <= chosen_value #see edit below for more info

Also, since epsilon is the "smallest difference the machine can distinguish between two floating-point numbers", you'll want to use <= in your function.

Edit: After reading the comments below I have looked back at the book and it specifically says "Here is a simple function for comparing floats for equality to the limit of the machines accuracy". I believe this was just an example for comparing floats to extreme precision but the fact that error is introduced with many float calculations this should rarely if ever be used. I characterized it as the "most accurate" way to compare in my answer, which in some sense is true, but rarely what is intended when comparing floats or integers to floats. Choosing a value (ex: 0.00000000001) based on the "problem domain" of the function instead of using sys.float_info.epsilon is the correct approach.

Thanks to S.Lott and Sven Marnach for their corrections, and I apologize if I led anyone down the wrong path.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。那么关于 Python 的异常处理 try/except 语句和判断整数的问题,我可以为您解答。 在 Python ,异常处理是一种非常重要的编程技巧,可以帮助我们在程序出现错误时做出相应的处理,避免程序崩溃或出现不可预料的结果。其,try/except 语句是 Python 用来捕获异常的一种关键字,其基本语法如下: ``` try: # 可能会出现异常的代码块 except 异常类型1: # 处理异常类型1的代码块 except 异常类型2: # 处理异常类型2的代码块 ... else: # 没有异常发生时执行的代码块 finally: # 无论是否有异常都会执行的代码块 ``` 在这里,我们可以使用 try/except 语句来判断一个字符串是否能够转换为整数。例如: ``` try: num = int(input("请输入一个整数:")) print("输入的整数是:", num) except ValueError: print("输入的不是整数!") ``` 在这个例子,我们使用 int() 函数将用户输入的字符串转换为整数。如果用户输入的是一个非数字字符串,那么就会抛出 ValueError 异常,我们就可以在 except 块进行相应的处理。 至于判断一个字符串是否能够转换为整数,我们可以使用 Python 的 isdigit() 方法,将其与 try/except 语句结合使用。例如: ``` num_str = input("请输入一个数字字符串:") if num_str.isdigit(): num = int(num_str) print("转换后的整数是:", num) else: print("输入的不是数字字符串!") ``` 在这个例子,我们首先使用 isdigit() 方法判断用户输入的字符串是否都是数字字符,如果是,就可以使用 int() 函数将其转换为整数;否则,就输出相应的提示信息。 希望这些解答能够对您有所帮助,如有疑问请随时追问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值