Skip to content
AITroveRead. Build. Understand.
Make this comfortable

NumPy floating-point checks: finite values and declared tolerances

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

Floating-point arithmetic represents many real values approximately, so equality and tolerance checks express different contracts.

Download Python source kit

Operation contract

The sensor fixture rejects nonfinite values before computing a comparison. Its tolerance check sets both relative and absolute tolerances explicitly. A near-zero measurement needs an absolute scale, while a larger reference can use a relative budget. The fixture does not reuse these tolerances for money or every measurement unit.

Failure and ownership boundary

NumPy isclose uses its second operand as the reference scale and is not generally symmetric. Default tolerances can accept differences that are too large for tiny values. NaN and infinity need a separate acceptance policy, not an accidental comparison result. Python Decimal money: parse decimal text and choose rounding explicitly and NumPy broadcasting: declare axes and bound integer arithmetic solve other representation constraints.

Tested environment

Dependency check: this program was executed on CPython 3.14.6 with numpy==2.5.3. Install these versions in a separate virtual environment. The download includes the recorded environment snapshot; no third-party package is part of the website runtime.

Working program

python
import numpy as np

measurements = np.array([0.1 + 0.2, 0.0], dtype=np.float64)
references = np.array([0.3, 1e-12], dtype=np.float64)
if not np.isfinite(measurements).all() or not np.isfinite(references).all():
    raise ValueError("finite measurements required")
print((measurements == references).tolist())
print(np.isclose(measurements, references, rtol=1e-10, atol=1e-11).tolist())
print(bool(np.isfinite(np.array([1.0, np.nan])).all()))

Output

Output
[False, False]
[True, True]
False

Costs and limits

Elementwise checks scan n values and allocate boolean results unless an API-specific output strategy avoids them. Array broadcasting can enlarge intermediate shapes. Tolerances describe an application error budget; they do not improve the underlying stored precision.

Common Mistakes

  • Choose tolerances in the measurement’s units.
  • Reject or handle nonfinite data before claiming a numerical result is usable.

Connected lessons

NumPy broadcasting: declare axes and bound integer arithmetic, Python Decimal money: parse decimal text and choose rounding explicitly, Pandas nullable integers: missing amounts are not zero.

Follow the related contract

NumPy least squares: inspect rank before trusting fitted coefficients.

Check the next state boundary

NumPy bootstrap means: seeded resampling does not repair biased input.

Trace the related workflow

NumPy finite gate: reject NaN and infinity before computing totals.

python
numpy-precision
Storage details