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.