Compare commits

...
Author SHA1 Message Date
Mayukha Vadari e96a293213 docs: Warn against storing Number in static variables
A Number is normalized to the MantissaRange that is active when it is built. Comparisons between Numbers built under different ranges give wrong results. kAMMInvariantRelativeTolerance is one example: under the Small range it compares as about 1e-13, not 1e-11.
2026-10-16 12:38:57 -04:00
2 changed files with 9 additions and 0 deletions

No files matched your search

+7
View File
@@ -280,6 +280,13 @@ concept Integral64 = std::is_same_v<T, std::int64_t> || std::is_same_v<T, std::u
* 2. The max of the "large" range, 10^19-1, is the largest 10^X-1 value that
* fits in an unsigned 64-bit number. (10^19-1 < 2^64-1 and
* 10^20-1 > 2^64-1). This avoids under- and overflows.
* 3. Do not store a Number in a static, inline, or global variable. A Number
* is normalized to the MantissaRange that is active when it is built, and
* operator< and operator== give wrong results when the two operands were
* built under different ranges. Store the mantissa and exponent instead,
* and build the Number where you use it. Changing a runtime Number into a
* stored constant changes transaction results, so gate that change behind
* an amendment.
*
* ---- External Interface ----
*
+2
View File
@@ -49,6 +49,8 @@ reduceOffer(auto const& amount)
enum class IsDeposit : bool { No = false, Yes = true };
// This constant is built at startup under the Large MantissaRange. Under the
// Small range it compares as about 1e-13, not 1e-11. See note 3 on Number.
inline Number const kAMMInvariantRelativeTolerance{1, -11};
/**