Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 49 ยท Array Math

Comparing Arrays and Floating Point

Compare NumPy arrays safely. Learn array_equal, allclose and isclose with tolerances, how NaN and inf behave, logical operators and np.errstate.

Comparing two numbers sounds like the easiest thing in programming, but with arrays of floating-point values it hides several traps. Is 0.1 + 0.2 equal to 0.3? Is NaN equal to itself? What does it mean for two arrays to be equal: every element, or just the shapes? And how do you combine several conditions without hitting the famous "truth value is ambiguous" error?

This lesson gives you the vocabulary to answer all of those questions. You will compare arrays exactly and with tolerances, handle NaN and infinity on purpose, combine boolean arrays with logical functions, and control the warnings NumPy raises when a calculation goes wrong. It builds on vectorized operations and boolean masks.

Comparison operators return arrays

Writing a == b for two arrays does not return one answer. It compares element by element and returns a boolean array of the same shape (after broadcasting). To get a single yes or no, you have to reduce that array yourself, or call a helper that does it for you.

The helpers you will use most:

Function Question it answers
np.array_equal(a, b) Same shape and every element exactly equal?
np.allclose(a, b) Same shape (after broadcasting) and every element equal within a tolerance?
np.isclose(a, b) Elementwise: which pairs are equal within a tolerance?
np.isnan(a), np.isinf(a), np.isfinite(a) Elementwise tests for special values

Floating-point numbers are approximations

Computers store most decimal fractions in binary, and 0.1 has no exact binary form. It is stored as the closest value that fits in 64 bits. Arithmetic then rounds again, so results that are equal on paper often differ in the last bit.

flowchart LR
    A["0.1 + 0.2"] --> B["stored as<br/>0.30000000000000004"]
    C["0.3"] --> D["stored as<br/>0.299999999999999989"]
    B --> E{"bit-for-bit<br/>equal?"}
    D --> E
    E -- "no" --> F["== is False"]
    B --> G{"difference<br/>within tolerance?"}
    D --> G
    G -- "yes" --> H["isclose is True"]

Two values that agree on paper can differ in the last bit, so test for closeness instead of equality.

Try it with arrays. The array b below is computed in a different way from a, but both are meant to hold 0.1, 0.2 and 0.3:

Python Exact equality versus closeness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first two elements differ by about 1e-17. That is far below anything that matters in a real measurement, yet == reports False. np.allclose says the arrays match, and np.isclose shows which positions matched, so you can also see where a mismatch is when one exists.

Tip: Use == and np.array_equal for integers, booleans and strings, where equality is exact. For floating-point results of any calculation, use np.isclose or np.allclose.

How tolerances work

np.isclose(a, b) returns True when

TEXT
abs(a - b) <= atol + rtol * abs(b)

where rtol is the relative tolerance (default 1e-05) and atol is the absolute tolerance (default 1e-08). The relative part scales with the size of the numbers, which is what you want for large values. The absolute part matters near zero, where a relative test alone would reject almost everything, because any number is "infinitely far" from 0 in relative terms.

Python Tuning rtol and atol

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the defaults accept 1e-10 and 2e-10 as close even though one is double the other. That is the absolute tolerance at work: both are below 1e-08. If your data lives at that scale, set atol explicitly (often to 0, with a deliberate rtol). The defaults are tuned for values near 1.

Also remember that isclose is not symmetric in the strict sense, because the tolerance is scaled by b. Treat b as the reference value.

NaN and infinity

A floating-point array can hold two kinds of special values: infinity (np.inf, -np.inf) and NaN, "not a number", which marks an undefined result such as inf - inf or a missing measurement. NaN has one famous property: it is not equal to anything, including itself.

Python Special values in comparisons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three things to take from the output:

  • x == x is False at the NaN position, so even an array compared with itself is not "equal" by np.array_equal. Pass equal_nan=True when NaNs in the same places should count as matching. np.allclose and np.isclose take the same option.
  • To find NaN values, never write x == np.nan. Use np.isnan(x). np.isfinite(x) is the opposite of "NaN or infinite", which makes it a handy filter before you compute a mean.
  • Any calculation that touches a NaN returns NaN, so x.max() is nan. The nan-aware functions, such as np.nanmax, skip those entries. They are covered in aggregations and missing data.

Infinity, by contrast, compares sensibly: it is larger than every finite number, and -inf is smaller than all of them. Only operations that have no sensible answer, such as inf - inf or 0 * inf, produce NaN.

Logical operations on boolean arrays

Real conditions combine several tests: "warm and raining", "weekend or holiday". For boolean arrays NumPy offers two equivalent styles:

  • operators: & (and), | (or), ^ (xor), ~ (not)
  • functions: np.logical_and, np.logical_or, np.logical_xor, np.logical_not

The functions also accept non-boolean input and treat zero as false and anything else as true, so they work on numbers directly. The operators work on booleans (and bitwise on integers), which is why they need care.

Python Combining conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There are two traps here, and both end in the same message:

  1. Operator precedence. In Python, & binds tighter than > and <. Without parentheses, temps > 10 & temps < 30 is read as a chain that cannot be evaluated on arrays. Always wrap each comparison in parentheses.
  2. Python's and, or, not and if. They need a single truth value, but an array with several elements has no single answer. NumPy refuses to guess and raises the "ambiguous" error. Reduce the array with .any() or .all() first, or use the elementwise operators above.

np.logical_and has one more trick: with .reduce it can collapse a whole list of masks, as in np.logical_and.reduce([m1, m2, m3]). You will meet that pattern in advanced techniques.

Controlling warnings with np.errstate

Some calculations cannot produce a clean answer: dividing by zero, taking the square root of a negative real number, overflowing the largest float. NumPy does not stop your program. It returns inf, nan or a wrapped value and, by default, prints a RuntimeWarning. Four categories exist:

Category Triggered by Default
divide division by zero warn
over result too large for the dtype warn
under result too small (rounds to 0) ignore
invalid undefined result, such as 0/0 or sqrt(-1) warn

np.errstate is a context manager that changes this policy only inside its with block. The settings are "ignore", "warn", "raise", "call" and "print".

Python Warn, ignore or raise

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Use "raise" while debugging to find the exact line that creates the first inf or nan, which is often much earlier than the place where you notice the problem. Use "ignore" only when you expect the special values and handle them afterwards, for example with np.isfinite. Do not silence warnings globally "to clean up the output": they are often the only sign that the data is wrong.

The complex-number line shows another way out of the invalid warning: if negative inputs are legitimate, work in complex numbers and sqrt returns the imaginary root.

Asserting closeness in tests

When you write tests, np.testing.assert_allclose(actual, desired, rtol=...) is better than assert np.allclose(...), because on failure it prints which elements differ and by how much. You will meet more of np.testing in Common Pitfalls and Debugging Arrays.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does np.array_equal(x, x) return False for an array x that contains np.nan?

    By the floating-point rules NaN == NaN is False. array_equal follows them by default, and equal_nan=True makes NaNs in the same positions count as equal.
  2. 2. Which test is the right way to check whether two float arrays computed in different ways hold the same values?

    Rounding makes exact equality unreliable for floats. np.allclose accepts differences within a relative and absolute tolerance. array_equal is still exact, and is only tests identity.
  3. 3. What does np.isclose(1e-10, 2e-10, atol=0) return, given the default rtol of 1e-05?

    With atol=0 only the relative term rtol * abs(b) is left, which is about 2e-15 here, far below the difference of 1e-10. With the default atol of 1e-08 the same call would return True.
  4. 4. Why does (temps > 10) & (temps < 30) need parentheses?

    Python gives & a higher precedence than the comparison operators. Without parentheses it first computes 10 & temps, then chains comparisons, which fails with the ambiguous truth value error.
  5. 5. What does with np.errstate(divide="raise"): change?

    errstate is a context manager. The new policy applies only inside the with block, and the previous settings return afterwards.

Summary

  • Comparisons on arrays are elementwise and return boolean arrays; reduce them with .all(), .any() or a helper.
  • Use np.array_equal for exact equality and np.allclose or np.isclose for floats. isclose tests abs(a - b) <= atol + rtol * abs(b), so set atol yourself near zero.
  • NaN is never equal to anything. Find it with np.isnan, and use np.isfinite to exclude both NaN and infinity. equal_nan=True lets matching NaNs compare equal.
  • Combine masks with &, |, ^, ~ (each comparison in parentheses) or the np.logical_* functions. Never use and/or/if on multi-element arrays.
  • np.errstate controls what happens on divide, overflow, underflow and invalid operations, only inside its block.

Next, learn to change an array's shape without touching its data in Reshaping Arrays.