Skip to content
All library documents

Floating-Point Comparison and Rounding Utilities for Trading Software

Article MQL5 code base

Summary

The document describes utility functions for handling floating-point values in MetaTrader 4. Its central lesson is that decimal values produced through different calculations may differ at tiny binary precision levels, making direct equality and boundary comparisons unreliable. It illustrates the issue with a decimal addition example and trading cases such as stop levels, hidden profit targets, and grid price boundaries. The suggested approach is to use tolerance-based comparisons for equality and ordering, alongside suitable rounding or price and volume normalization. The listed utilities also cover range remapping, clamping, safe division, significant digits, binary representation, units in the last place, and number formatting. These are general software tools rather than a trading strategy, and the document does not define tolerances for particular instruments or broker rules. Developers still need to choose precision appropriate to the symbol and application, especially where price ticks or volume steps impose discrete constraints.

Key ideas

  • Binary floating-point representation can make mathematically equivalent decimal calculations differ slightly.
  • Exact equality and ordering checks can mis-handle price thresholds and other trading boundaries.
  • Tolerance-based comparison functions provide alternatives to direct floating-point comparisons.
  • Rounding and normalization utilities can align prices and volumes to chosen precision or increments.
  • The appropriate tolerance depends on instrument and application constraints and is not specified here.

Tags

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