tolomea - 1 year ago 67

Python Question

It's well known that comparing floats for equality is a little fiddly due to rounding and precision issues.

For example:

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

What is the recommended way to deal with this in Python?

Surely there is a standard library function for this somewhere?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Python 3.5 adds the `math.isclose`

and `cmath.isclose`

functions as described in PEP 485.

If you're using an earlier version of Python, the equivalent function is given in the documentation.

```
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**