---
title: "Compare decimal fractions with a tolerance · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/data-shapes/float-comparison
updated: 2026-10-21
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Compare decimal fractions with a tolerance

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Data shapes](https://agentwranglers.se/playbook/architecture/data-shapes.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers). Updated 2026-10-21.

- ID: ARC-73
- Level: Recommended
- Chapter: [Data shapes](https://agentwranglers.se/playbook/architecture/data-shapes)
- Rule: Never compare two computed floating-point numbers with `==`. Compare them within a stated tolerance, or use a decimal or integer type when the value must be exact.
- How: Binary floating point cannot store most decimal fractions exactly, so `0.1 + 0.1 + 0.1 == 0.3` is false. Use a helper such as Python's `math.isclose` or a tolerance named as a constant. Measurements such as weight or distance suit a tolerance. Money never does, so it follows [hold money as integer minor units everywhere](https://agentwranglers.se/playbook/payments/amounts-and-orders/money-in-minor-units).
- Check: A linter fails equality on floats. Examples are Clippy `float_cmp` for Rust, SpotBugs `FE_FLOATING_POINT_EQUALITY` for Java and the `-Wfloat-equal` warning in GCC and Clang. A test asserts that the sum of ten weights of 0.1 kg passes the 1.0 kg limit check.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

```python
WEIGHT_TOLERANCE_KG = 1e-9

# Bad: ten items of 0.1 kg do not add up to exactly 1.0
if parcel_weight == 1.0: ...

# Good: equal within a named tolerance
if math.isclose(parcel_weight, 1.0, abs_tol=WEIGHT_TOLERANCE_KG): ...
```

### TypeScript example

```ts
const WEIGHT_TOLERANCE_KG = 1e-9;

// Good: equal within a named tolerance
const isSameWeight = (a: number, b: number) => Math.abs(a - b) <= WEIGHT_TOLERANCE_KG;
```

## Sources

- [Python tutorial: floating-point arithmetic](https://docs.python.org/3/tutorial/floatingpoint.html)

## Related rules

- [Hold money as integer minor units everywhere](https://agentwranglers.se/playbook/payments/amounts-and-orders/money-in-minor-units) in Payments

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/architecture/data-shapes/float-comparison">Agent Wranglers architecture playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Keep the time zone on every time](https://agentwranglers.se/playbook/architecture/data-shapes/zone-aware-times)
- [Data shapes](https://agentwranglers.se/playbook/architecture/data-shapes)
