Big Integer Arithmetic
MQL4's widest built-in integer is `long`, a signed 64-bit type good for roughly 19 significant decimal digits before it silently wraps. A `double` looks like it might cover more ground, but it only carries 15-17 significant digits before it starts silently losing precision - it is not a safe substitute for exact integer arithmetic either. Anything that needs more - a concatenated identifier built from several account and order numbers, a running checksum built by repeated multiply-and-add over many inputs, a factorial or combinatorial count beyond what fits in a long - has nowhere built-in to go. This header stores a big integer as a fixed-size array of base- 1,000,000,000 "limbs" (least-significant first) inside a plain struct, and implements addition, subtraction and multiplication as real digit-array arithmetic: schoolbook long multiplication with per-row carry propagation, the same algorithm taught for multiplying numbers by hand, just in base one billion instead of base ten so each limb packs nine decimal digits. The struct is deliberately a FIXED-size array, not a dynamically sized one: a struct holding a dynamic array has no safe default assignment operator in MQL4, so every copy of it (a=b, a function return, an array element) would need a hand-written deep-copy call instead of behaving like an ordinary value. A fixed 40-limb (360 decimal digit) capacity is not truly arbitrary precision, but it comfortably covers any real identifier, checksum or factorial-scale use case while keeping a BigInt copyable everywhere exactly like an int or a double. Notes and limitations - Fixed capacity of 40 limbs (360 decimal digits), not unbounded. Add, subtract and multiply all return false (leaving the output struct unchanged) rather than silently truncating or wrapping if a result would need more limbs than that - always check the return value on a computation whose size is not already known to be safely inside capacity. - BigIntFromString validates that every character after an optional leading '-' is a decimal digit and returns false on anything else (empty string, a bare "-", any non-digit character) rather than parsing partially. - No division or modulo operator is provided - only add, subtract, multiply, compare and string conversion. Big-integer division is a materially larger algorithm (long division with a digit-estimation step) and was left out to keep this a genuinely "Lite" arithmetic kit rather than a full big-number library. - Comparison and arithmetic are sign-magnitude throughout; BigIntCompare handles the case of two negative values correctly (larger magnitude sorts as the smaller value), but this is worth testing directly if a caller's logic depends on subtle ordering near zero.
Script · MT4 · v1.0 · Free
About Big Integer Arithmetic
Big Integer Arithmetic is a script for MetaTrader 4. MQL source code included.
Files in the download: BigIntegerArithmetic.mqh, BigIntegerArithmetic.txt, BigIntegerArithmeticDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code