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

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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值