Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if float is equivalent to an integer value in python

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 check within a tolerance instead?

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.

like image 885
Sunny88 Avatar asked Jun 01 '11 23:06

Sunny88


People also ask

How do you check if a value is an integer or a float in Python?

Use the isinstance() function to check if a number is an int or float, e.g. if isinstance(my_num, int): . The isinstance function will return True if the passed in object is an instance of the provided class ( int or float ).

Can you compare a float to an int?

Casting the int to a float explicitly will do absolutely nothing. The int will be promoted to a float for purposes of comparison anyway.

How do you test if a value is an integer in Python?

Using int() function The function int(x) converts the argument x to an integer. If x is already an integer or a float with integral value, then the expression int(x) == x will hold true. That's all about determining whether a variable is an integer or not in Python.


2 Answers

There is is_integer function in python float type:

>>> float(1.0).is_integer() True >>> float(1.001).is_integer() False >>>  
like image 155
ndpu Avatar answered Sep 19 '22 21:09

ndpu


Both your implementations have problems. It actually can happen that you end up with something like 4.999999999999997, so using int() is not an option.

I'd go for a completely different approach: First assume that your number is triangular, and compute what n would be in that case. In that first step, you can round generously, since it's only necessary to get the result right if the number actually is triangular. Next, compute n * (n + 1) / 2 for this n, and compare the result to x. Now, you are comparing two integers, so there are no inaccuracies left.

The computation of n can be simplified by expanding

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

and utilizing that

round(y - 0.5) = int(y) 

for positive y.

def is_triangular(x):     n = int(math.sqrt(2 * x))     return x == n * (n + 1) / 2 
like image 39
Sven Marnach Avatar answered Sep 17 '22 21:09

Sven Marnach