Skip to content
All library documents

Comparing Floating-Point Values with a Precision Tolerance

Article MQL5 code base

Summary

This programming note explains why direct equality checks can give unexpected results when comparing floating-point values. A decimal such as 1.15 may be represented internally by a nearby value, so two numbers that appear equal when printed may differ at greater precision. The proposed comparator examines the difference between the values against a configurable tolerance and returns an ordering result: equal within tolerance, greater, or less.

The example varies the precision setting while comparing 1.15 with a nearby value, illustrating how a difference treated as meaningful at finer precision can count as equal at coarser precision. The class sets a default tolerance based on the numeric type and allows that setting to be changed. This is a compact implementation example rather than a trading method or a complete guide to numerical computing. Its main practical lesson is that tolerance should reflect the scale and purpose of the comparison; a fixed decimal threshold may not suit values with different magnitudes or error requirements.

Key ideas

  • Binary floating-point representation can make visually identical decimal values differ internally.
  • A tolerance-based comparison classifies numbers as equal when their difference is below a chosen threshold.
  • The example shows that comparison outcomes change as the precision setting changes.
  • A fixed absolute tolerance may need adjustment for the scale and use of the values being compared.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.