← Back to news
v3.6.0Jul 19, 2026

DeFiMath v3.6.0: sqrt and cbrt handle full uint256

Two changes:

  1. sqrt and cbrt accept the full uint256 domain. Both errors and their upper-bound constants (SqrtUpperBoundError, CbrtUpperBoundError, SQRT_UPPER_BOUND, CBRT_UPPER_BOUND) are removed. Any input in [0, uint256.max] returns a valid FP18 result.
  2. pow overflow bugfix. The internal a · ln(x) multiplication inside unchecked could silently overflow int256 for adversarial exponents (|a| ≳ 3.25 × 10⁵⁶), letting pow return plausible-looking garbage. New MAX_POW_EXPONENT = 1e54 bound with a typed PowExponentOutOfBoundsError revert — well below the overflow point and ~18 orders of magnitude above any realistic financial exponent (real value 10³⁶).

For a deep dive into the new sqrt — the CLZ seed, the five Newton iterations, and how it lands at 197 gas — see the companion post: How I wrote a fixed-point Solidity sqrt that runs in 197 gas.

Gas changes

Beyond sqrt and cbrt, sqrtTime also picked up the new CLZ-derived seed algorithm — its savings cascade through every Black-Scholes function that computes an annualized time term. expm1 and log1p shed a few gas each, which propagates into continuousToDiscrete and discreteToContinuous in the Rates module. The sqrt savings compound into every statistics function that calls it — geometricMean, stdDev, historicalVolatility, sharpeRatio. Options and Binary options both benefit — every function that depends on sqrtTime (all Greeks, IV, both pricing functions) drops ~15 gas. pow is the only row that went up: the new MAX_POW_EXPONENT check costs ~40 gas — the price of catching the silent overflow bug.

Function v3.5.0 gas v3.6.0 gas Saved
sqrt245197−48 (−20%)
cbrt368340−28 (−8%)
sqrtTime184163−21 (−11%)
callOptionPrice2,7292,708−21 (−0.8%)
binaryCallPrice2,0181,997−21 (−1%)
pow763803+40 (safety)
expm1415407−8
log1p487476−11
continuousToDiscrete507491−16
discreteToContinuous589574−15
geometricMean330284−46
stdDev (N=100)49,81149,765−46
historicalVolatility (N=100)86,59586,500−95
sharpeRatio (N=100)86,73386,638−95

API removed

  • SqrtUpperBoundError() and CbrtUpperBoundError() errors deleted.
  • SQRT_UPPER_BOUND and CBRT_UPPER_BOUND constants deleted.
  • New: PowExponentOutOfBoundsError() and MAX_POW_EXPONENT for the pow bugfix.

Callers who caught SqrtUpperBoundError or CbrtUpperBoundError need to remove those handlers — those code paths no longer exist. Callers who pass finite exponents to pow see no behavior change.

Get it

npm install defimath-lib