Skip to content
All library documents

Floating-Point Comparisons and Numeric Utilities for Trading Software

Article MQL5 code base

Summary

This document describes a utility library for working with floating-point numbers in trading software. Its functions cover approximate comparisons, rounding to decimal places or increments, normalization and range mapping, clamping, safe division, binary representation, precision measurement, and numeric formatting. The trading examples include comparing stop levels, hidden profit targets, and price levels used by grid logic.

The central lesson is that decimal values stored as binary floating-point numbers can differ slightly even when calculations are intended to produce the same value. An exact equality or threshold comparison can therefore behave unexpectedly; the document illustrates this with a familiar decimal addition example and recommends tolerance-aware comparison functions. It mentions a validation script but does not show its results, implementation details, or how tolerances should be chosen for different instruments and price scales. The listed function signatures are a catalog rather than a complete usage guide, so users would need to consult implementation details before relying on them in production.

Key ideas

  • Binary floating-point arithmetic can make mathematically equivalent decimal calculations compare as unequal.
  • Tolerance-aware comparisons can help avoid unexpected threshold behavior in price and order logic.
  • The library includes rounding, scaling, clamping, safe division, and formatting helpers.
  • It also exposes utilities for inspecting floating-point bit patterns and precision.
  • The document lists functions but does not provide tolerance guidance or validation results.

Tags

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