在python中检查float是否等价于一个整数值 [英] Checking if float is equivalent to an integer value in python

查看:1412
本文介绍了在python中检查float是否等价于一个整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中,我正在检查一个给定的值是否是三角形的,也就是说,它可以表示为n(n + 1)/ 2对于某个正整数n



$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (1/2)*(math.sqrt(8 * x + 1)-1)
return int(num)== num

或者我需要这样做吗? :

  epsilon = 0.000000000001 
def is_triangular2(x):
num =(1/2)* (math.sqrt(8 * x + 1)-1)
return abs(int(num) - num)

我检查了两个函数都返回了相同的结果,x高达1,000,000。但我不知道,如果一般来说int(x)== x将总是正确地确定一个数字是否是整数,因为例如5表示为4.99999999999997等。



据我所知,第二种方法是正确的,如果我在C中执行它,但是我不确定Python 3是什么。

解决方案

你会想要做后者。在Python 3中进行编程 中给出了以下示例作为比较的最准确的方法

  def equal_float (a,b):
#return abs(a - b)< = sys.float_info.epsilon
返回abs(a - b)< = selected_value #see编辑更多信息$ b另外,由于epsilon是机器可以区分两个浮点数的最小差异,因此,我想在你的函数中使用< =。

编辑:阅读下面的评论后,我回头看了书,说:这是一个简单的功能比较花车的平等与机器的精度限制。我相信这只是一个比较浮点数的极端精度的例子,但是浮点运算引入了错误,所以很少使用。我把它定性为在我的答案中比较最准确的方法,从某种意义上来说这是真实的,但很少将浮点数或整数与浮点数进行比较。根据函数的问题域而不是使用sys.float_info.epsilon来选择一个值(例如:0.00000000001)是正确的方法。



感谢S. Lott和Sven Marnach纠正,如果我把任何人带到错误的路上,我表示歉意。

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

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.

这篇关于在python中检查float是否等价于一个整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆