Float Comparison Trap Demonstrator
Binary floating point cannot represent most decimal fractions exactly, so a price or lot value built by arithmetic - adding a point repeatedly, dividing then multiplying back, summing several small amounts - usually lands a few ulps away from the "same" number reached a different way, even though both display identically once rounded for the chart. Code that compares two such values with == will occasionally, silently, treat two prices that are the same tick as different, or two lot volumes that are the same size as unequal. This script runs four real comparison cases and prints each value at 16 significant digits so the actual difference is visible instead of hidden by normal rounding: the textbook 0.1-added-ten-times-versus-1.0 case; a price built by adding Point repeatedly versus the same distance reached by one multiplication, against the live symbol's own current price; the same pair after both sides are passed through NormalizeDouble(), to show normalising first is the other valid fix alongside an epsilon compare; and a value round-tripped through division then multiplication by the same divisor, the exact shape the trap usually takes when it reaches real trading code (dividing by a lot step or a pip value, then multiplying back). Every case that fails a naive == still passes a tolerant compare - within half a point for prices, or the equivalent half-step for lot volumes - which is the specific, permanent fix this script exists to make visible: not "round more carefully", but "never test binary floats for exact equality when either side reached its value through arithmetic". Notes and limitations - This is a read-only demonstration script - it sends no orders and requires no connected trading account, only live market data. - Case 4b (the live lot step round trip) is included for real-world context but is explicitly NOT guaranteed to reproduce the trap on every broker - whether it diverges depends on the specific lot step value in use. Case 4 (the fixed 1.0/49.0*49.0 round trip) is the guaranteed, broker-independent reproduction and is the one to rely on for a reliable failing example. - The tolerant-compare helper here (PricesEqual) is a small, self- contained reimplementation for this demo - it is not shared with or included from any other project in this catalogue, per house style.
Script · MT5 · v1.0 · Free
About Float Comparison Trap Demonstrator
Float Comparison Trap Demonstrator is a script for MetaTrader 5 based on Divergence. It can alert you with a pop-up, sound, email or push message. It has 1 setting, such as Addition steps. MQL source code included.
Files in the download: FloatComparisonTrap.ex5, FloatComparisonTrap.mq5, FloatComparisonTrap.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Addition steps | 10 | How many times Point is added to build the "same" price the long way |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Binary Options · Alerts · Divergence · MT5 Script · MQL5 Source Code