Loading…

Numerical Reference Tests

MQL4 is missing a handful of standard-library math functions this catalogue relies on repeatedly: there is no MathErf/MathErfc (needed for a normal CDF/tail-probability calculation), and no MathArctan2 (needed for a quadrant-correct angle from a two-dimensional vector). Both are usually reimplemented by hand project by project - an Abramowitz & Stegun polynomial approximation for the error function, manual sign/quadrant logic built on the single-argument MathArctan for atan2. A hand-rolled approximation is only trustworthy once it has actually been checked against known-correct values, not just assumed correct because the formula looks right. This header provides that check in two layers. NrtAssertClose and NrtAssertCloseRelative are general-purpose: give them a label, a computed value, an expected value and a tolerance, and they report pass/fail with the exact error, tracking running counters the same way other assertion helpers in this catalogue do - usable for any project's own numerical routine, not just the ones bundled here. NrtRunBuiltinReferenceSuite then exercises this header's own NrtErf/NrtNormalCdf/NrtAtan2 against a table of literal high-precision reference constants (erf(1), the standard normal CDF at +/-1, the four axis-aligned atan2 cases including the x==0 boundary, plus a couple of MathSqrt/MathPow sanity checks) - not against any other implementation, since a reference test that only agrees with a second copy of the same formula proves nothing about correctness, only consistency. The erf/atan2 implementations here are this project's own, independent of any other project's copy elsewhere in the catalogue - every submission stands alone and does not #include another project's header, even when the same well-known approximation is the right tool in more than one place. Notes and limitations NrtErf/NrtErfc use the Abramowitz & Stegun 7.1.26 polynomial approximation, with a documented maximum absolute error of about 1.5e-7 - the built-in suite's tolerance (2e-6) is set with margin above that figure, not tightened to the theoretical limit, so it will not catch a regression smaller than the approximation's own inherent error. NrtAtan2 returns 0.0 for the (0,0) case, matching the common atan2 convention, though this is technically undefined territory (a zero vector has no angle) rather than a mathematically derived answer. NrtAssertCloseRelative floors its scale at max(|expected|, 1.0) specifically so a relative check near zero does not demand near-impossible precision - a caller wanting a true relative tolerance uniformly close to zero should use NrtAssertClose with an absolute tolerance instead. This header tests numerical accuracy only; it has no relationship to LogAssertionHelpers or any other test-helper header in this catalogue - each is a separate, independent submission.

Script · MT5 · v1.0 · Free

About Numerical Reference Tests

Numerical Reference Tests is a script for MetaTrader 5. MQL source code included.

Files in the download: NumericalReferenceTests.mq5.mqh, NumericalReferenceTests.txt, NumericalReferenceTestsDemo.ex5, NumericalReferenceTestsDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria