🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.allclose()

AI & DATA SCIENCE // np-allclose

np.allclose() returns a single True/False indicating whether every corresponding pair of elements in two arrays is equal within a given tolerance, correctly accounting for floating-point rounding error.

Syntax

np.allclose(a, b, rtol=1e-05, atol=1e-08)

Deep Dive Course

Because floating-point arithmetic accumulates tiny rounding errors, comparing computed float arrays with == can incorrectly report 'not equal' even when two arrays represent the same mathematical result. allclose() instead checks whether each pair of elements differs by less than a combined tolerance of atol plus rtol times the absolute value of the second element, returning a single overall True only if every element pair passes that check. It's the standard way to compare floating-point array results in tests and numerical code, rather than exact equality.

1Understanding np.allclose()

Because floating-point arithmetic accumulates tiny rounding errors, comparing computed float arrays with == can incorrectly report 'not equal' even when two arrays represent the same mathematical result. allclose() instead checks whether each pair of elements differs by less than a combined tolerance of atol plus rtol times the absolute value of the second element, returning a single overall True only if every element pair passes that check. It's the standard way to compare floating-point array results in tests and numerical code, rather than exact equality.

💡

Never use == to compare two arrays of computed floating-point results — always use np.allclose(), or np.isclose() for element-wise detail, with an appropriate tolerance instead, since floating-point rounding error makes exact equality unreliable.

editor.html
import numpy as np

a = np.array([0.1 + 0.2, 1.0])
b = np.array([0.3, 1.0])
print(a == b)
print(np.allclose(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.allclose() showing how it is used in production NumPy code.

editor.html
import numpy as np

result = np.array([1.0000001, 2.0])
expected = np.array([1.0, 2.0])
print(np.allclose(result, expected, atol=1e-5))
localhost:3000

3Best Practices

Follow these guidelines when working with np.allclose():

1. Use np.allclose() instead of == whenever comparing arrays of computed floating-point values, in tests or elsewhere

2. Adjust rtol/atol thoughtfully based on the expected magnitude and precision of your specific calculation, rather than always relying on the defaults

3. Use np.isclose() instead when you need the per-element boolean detail rather than a single overall True/False

⚠️

Tip: Never use == to compare two arrays of computed floating-point results — always use np.allclose(), or np.isclose() for element-wise detail, with an appropriate tolerance instead, since floating-point rounding error makes exact equality unreliable.

editor.html
import numpy as np

a = np.array([0.1 + 0.2, 1.0])
b = np.array([0.3, 1.0])
print(a == b)
print(np.allclose(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([0.1 + 0.2, 1.0])
b = np.array([0.3, 1.0])
print(a == b)
print(np.allclose(a, b))
Example 02Advanced Example
import numpy as np

result = np.array([1.0000001, 2.0])
expected = np.array([1.0, 2.0])
print(np.allclose(result, expected, atol=1e-5))

Best Practices

  • Use np.allclose() instead of == whenever comparing arrays of computed floating-point values, in tests or elsewhere
  • Adjust rtol/atol thoughtfully based on the expected magnitude and precision of your specific calculation, rather than always relying on the defaults
  • Use np.isclose() instead when you need the per-element boolean detail rather than a single overall True/False

Interview Question

Why does comparing 0.1 + 0.2 with == to 0.3 return False in NumPy, and how does np.allclose() correctly report them as equal?

Hint: Think about binary floating-point representation error.

0.1 and 0.2 can't be represented exactly in binary floating-point, so their sum accumulates a tiny rounding error, landing at a value that's extremely close to, but not bit-for-bit identical to, the floating-point representation of 0.3 — making a strict == comparison return False. np.allclose() doesn't check for bit-for-bit identity; it checks whether the difference between the two values falls within a small, configurable tolerance, which the tiny floating-point rounding error easily satisfies, correctly treating the two values as equal for practical numerical purposes.

Exercises

MediumPractice using np.allclose() in a real scenario.
View Solution
import numpy as np

a = np.array([0.1 + 0.2, 1.0])
b = np.array([0.3, 1.0])
print(a == b)
print(np.allclose(a, b))

Frequently Asked Questions

Why does comparing 0.1 + 0.2 with == to 0.3 return False in NumPy, and how does np.allclose() correctly report them as equal?

0.1 and 0.2 can't be represented exactly in binary floating-point, so their sum accumulates a tiny rounding error, landing at a value that's extremely close to, but not bit-for-bit identical to, the floating-point representation of 0.3 — making a strict == comparison return False. np.allclose() doesn't check for bit-for-bit identity; it checks whether the difference between the two values falls within a small, configurable tolerance, which the tiny floating-point rounding error easily satisfies, correctly treating the two values as equal for practical numerical purposes.

Related Functions

np-isclosenp-arrayfloats