Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing a float and an int in Python

Tags:

python

I am trying to see if the calculated distance between two points is smaller than a given radius like this:

if distance(lat1, long1, lat2, long2) < radius:
      print "Distance: %s Radius: %s" % (distance(lat1, long1, lat2, long2), radius)

Here distance would effectively return a float and radius is an int.

I do know that I should not compare floats directly and that I should compare with a threshold. Given that, is there a better way to check if a float is less than an int (or another float).

Update This comparison seems to be ok from all of the replies. But I did observe this:

>>> 1.2000000000000001 > 1.2
True
>>> 1.20000000000000001 > 1.2
False

Isn't this a problem? I am using Python 2.6.7 on Mac

like image 481
Abhishek Chanda Avatar asked Apr 05 '12 22:04

Abhishek Chanda


People also ask

Can you compare float to int in Python?

Python also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 .

Can you compare a float and an int?

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.

What happens if we compare int and float?

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

Can you compare float in Python?

How To Compare Floats in Python. If abs(a - b) is smaller than some percentage of the larger of a or b , then a is considered sufficiently close to b to be "equal" to b . This percentage is called the relative tolerance. You can specify the relative tolerance with the rel_tol keyword argument of math.


1 Answers

Just compare them directly, there is no harm in that at all.

Python handles comparing numbers of different types perfectly well:

>>> type(1.1)
<class 'float'>
>>> type(1)
<class 'int'>
>>> 1.1 > 1
True
>>> 1.1 < 1
False
>>> 1 < 2
True
>>> 2.2 == 2.2
True
>>> 2 == 2.2
False
>>> 1.6 < 2
True
>>> 1.6 > 2
False
>>> 1.6 == 2
False

Python is duck typed, so in general you shouldn't worry about types directly, just if they can work in the way you need.

There could be some issues with comparing floats for equality with other floats due to precision errors:

>>> 0.3+0.3+0.3 == 0.9
False
>>> 0.3+0.3+0.3
0.8999999999999999

But in comparing to ints and/or < or > operations, you shouldn't worry.

In your update, we can use the decimal module to show the cause:

>>> Decimal(1.2000000000000001)
Decimal('1.20000000000000017763568394002504646778106689453125')
>>> Decimal(1.20000000000000001)
Decimal('1.1999999999999999555910790149937383830547332763671875')

But does this really matter? It's an inherent problem with floating point numbers, but only matters where you need really high precision.

like image 98
Gareth Latty Avatar answered Sep 17 '22 20:09

Gareth Latty