Join our Newsletter — 33% off our NHI Course

Floating-point Tolerance

Floating-point tolerance is the permitted difference between two calculated numbers when exact equality is too strict. It is commonly used in tests for scores, distances, or thresholds where representation limits can create tiny differences without indicating a real error.

Why floating-point tolerance exists

Floating-point tolerance is a practical way to compare computed values when exact binary equality would be misleading. Many decimal numbers cannot be represented exactly in floating-point form, so two results that are effectively the same for business or scientific purposes may still differ by a tiny amount.

This is not a shortcut for ignoring errors. It is a test-design choice that reflects how numerical systems behave, especially when calculations involve rounding, accumulated operations, or platform-specific math libraries. Used correctly, tolerance separates meaningful deviation from representational noise.

Where tolerance matters in software

Tolerance matters most in tests and validation checks that evaluate outputs such as scores, distances, probabilities, timing values, or thresholds. In those cases, the real question is usually whether the result falls within an acceptable range, not whether every bit matches a previously stored value.

It is especially important when a result is produced through multiple arithmetic steps, because small representation differences can accumulate. That means two correct implementations can still produce slightly different final values, even if they are both numerically sound within the intended precision.

How tolerance is typically expressed

Teams usually express floating-point tolerance as an absolute difference, a relative difference, or both. Absolute tolerance works well when values stay in a narrow range, while relative tolerance is often better when results can vary across magnitudes. The right choice depends on the domain and the scale of acceptable error.

The key idea is to compare against a domain-specific threshold, not a universal rule. A tolerance that is appropriate for one metric may be far too loose or too strict for another, so it should be chosen with the meaning of the value in mind.

Common mistakes when using tolerance

The most common mistake is treating tolerance as a blanket permission for imprecision. If the threshold is set too wide, tests may pass even when the underlying calculation is wrong. If it is set too tight, tests may fail on harmless representation differences and create unstable results.

Another frequent issue is using one tolerance for every value in a system. A fixed threshold can hide problems in small numbers and overreact to normal variance in large numbers. Good use of tolerance is specific, documented, and tied to the acceptable error for the metric being checked.

Risk and Threat Considerations

Floating-point tolerance can hide real defects if it is set too loosely, but it can also create brittle tests if it is too strict. The practical risk is not adversarial by default, it is false confidence, noisy validation, and missed regressions in systems that depend on precise numerical behavior.

Failure mechanism: A poorly chosen threshold lets materially incorrect results pass as acceptable, or causes correct results to fail because of harmless representation drift.

Impact: Numerical errors may reach production unnoticed, while unstable tests can slow delivery, obscure genuine issues, and make it harder to trust validation results.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS, NIST SP 800-53 Rev 5 and CIS Controls v8 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP ASVS V15 — Secure Coding and Architecture Floating-point comparisons affect application logic and test reliability.
Recommendation — Define numeric comparison rules that preserve intended application behavior under precision limits.
NIST SP 800-53 Rev 5 SI-2 — Flaw Remediation Incorrect tolerance can mask calculation defects that need remediation and validation.
SA-11 — Developer Testing and Evaluation Tolerance is a test-design concern when verifying software behavior under finite precision.
Recommendation — Validate numerical controls after fixes to confirm the defect is no longer masked by comparison logic. Specify test conditions that account for floating-point representation limits.
CIS Controls v8 CIS-16 — Application Software Security Numerical comparison logic is part of secure application behavior and test quality.
Recommendation — Review application checks that rely on numeric equality so they fail only on meaningful deviation.

Practitioner Guidance

What to watch for: Use the smallest tolerance that still reflects the real precision of the use case. If the output is safety-critical, financially sensitive, or used as a decision boundary, define the acceptable error explicitly rather than inheriting a generic test constant.

Practitioner takeaway: Tolerance should match the meaning of the number, not just the convenience of the test.