Be extremely careful using any of the other suggestions. It all depends on context.

I have spent a long time tracing a bugs in a system that presumed `a==b`

if `|a-b|<epsilon`

. The underlying problems were:

The implicit presumption in an algorithm that if `a==b`

and `b==c`

then `a==c`

.

Using the same epsilon for lines measured in inches and lines measured in mils (.001 inch). That is `a==b`

but `1000a!=1000b`

. (This is why AlmostEqual2sComplement asks for the epsilon or max ULPS).

The use of the same epsilon for both the cosine of angles and the length of lines!

Using such a compare function to sort items in a collection. (In this case using the builtin C++ operator == for doubles produced correct results.)

Like I said: it all depends on context and the expected size of `a`

and `b`

.

BTW, `std::numeric_limits<double>::epsilon()`

is the "machine epsilon". It is the difference between 1.0 and the next value representable by a double. I guess that it could be used in the compare function but only if the expected values are less than 1. (This is in response to @cdv's answer...)

Also, if you basically have `int`

arithmetic in `doubles`

(here we use doubles to hold int values in certain cases) your arithmetic will be correct. For example 4.0/2.0 will be the same as 1.0+1.0. This is as long as you do not do things that result in fractions (4.0/3.0) or do not go outside of the size of an int.

To check if `o`

is an instance of `str`

or any subclass of `str`

, use isinstance (this would be the "canonical" way):

```
if isinstance(o, str):
```

To check if the type of `o`

is exactly `str`

(exclude subclasses):

```
if type(o) is str:
```

The following also works, and can be useful in some cases:

```
if issubclass(type(o), str):
```

See Built-in Functions in the Python Library Reference for relevant information.

One more note: in this case, if you're using Python 2, you may actually want to use:

```
if isinstance(o, basestring):
```

because this will also catch Unicode strings (`unicode`

is not a subclass of `str`

; both `str`

and `unicode`

are subclasses of `basestring`

). Note that `basestring`

no longer exists in Python 3, where there's a strict separation of strings (`str`

) and binary data (`bytes`

).

Alternatively, `isinstance`

accepts a tuple of classes. This will return `True`

if `o`

is an instance of any subclass of any of `(str, unicode)`

:

```
if isinstance(o, (str, unicode)):
```

## Best Solution

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.

`rel_tol`

is a relative tolerance, it is multiplied by the greater of the magnitudes of the two arguments; as the values get larger, so does the allowed difference between them while still considering them equal.`abs_tol`

is an absolute tolerance that is applied as-is in all cases. If the difference is less than either of those tolerances, the values are considered equal.