Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General way of comparing numerics in Python [duplicate]

I have been looking around to find a general way of comparing two numerics in Python. In particular, I want to figure out whether they are the same or not.

The numeric types in Python are:

int, long, float & complex

For example, I can compare 2 integers (a type of numeric) by simply saying:

a == b

For floats, we have to be more careful due to rounding precision, but I can compare them within some tolerance.

Question

We get 2 general numerics a and b: How do we compare them? I was thinking of casting both to complex (which would then have a 0 imaginary part if the type is, say, int) and compare in that domain?

This question is more general than simply comparing floats directly. Certainly, it is related to this problem, but it is not the same.

like image 990
jesperk.eth Avatar asked Oct 13 '16 14:10

jesperk.eth


People also ask

How do you check if a number is repeated in a list Python?

Using Count() The python list method count() returns count of how many times an element occurs in list. So if we have the same element repeated in the list then the length of the list using len() will be same as the number of times the element is present in the list using the count().

How do you check if two numbers are the same in Python?

If equals test in Python: if with == The equals ( == ) operator tests for equality. It returns True when both tested values are the same. When their values differ, the operator returns False .

How do you compare numbers in a list Python?

sort() and == operator. The list. sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.


1 Answers

In Python 3.5 (and in Numpy) you can use isclose

Read the PEP 485 that describes it, Python 3.5 math library listing and numpy.isclose for more. The numpy version works in all versions of Python that numpy is supported.

Examples:

>>> from math import isclose
>>> isclose(1,1.00000000001)
True
>>> isclose(1,1.00001)
False

The relative and absolute tolerance can be changed.

Relative tolerance can be thought of as +- a percentage between the two values:

>>> isclose(100,98.9, rel_tol=0.02)
True
>>> isclose(100,97.1, rel_tol=0.02)
False

The absolute tolerance is a absolute value between the two values. It is the same as the test of abs(a-b)<=tolerance

All numeric types of Python are support with the Python 3.5 version. (Use the cmath version for complex)

I think longer term, this is your better bet for numerics. For older Python, just import the source. There is a version on Github.

Or, (forgoing error checking and inf and NaN support) you can just use:

def myisclose(a, b, *, rel_tol=1e-09, abs_tol=0.0):
   return abs(a-b) <= max( rel_tol * max(abs(a), abs(b)), abs_tol )
like image 106
dawg Avatar answered Sep 21 '22 23:09

dawg