Low-level fixed-point primitives in 18-decimal format (1e18 = 1.0). All pure, gas-efficient, and validated to sub-1e-12 absolute error against reference libraries.
Contract: Math.sol
| Function | Gas | Description |
|---|---|---|
| exp | 333 | Exponential function e^x |
| expm1 | 439 | e^x − 1 (precision-preserving for small x) |
| ln | 375 | Natural logarithm |
| log1p | 500 | ln(1 + x) (precision-preserving for small x) |
| log2 | 391 | Base-2 logarithm |
| log10 | 391 | Base-10 logarithm |
| pow | 750 | Power function x^a |
| sqrt | 245 | Square root |
| cbrt | 368 | Cube root |
| stdNormCDF | 731 | Standard normal CDF Φ(x) |
| erf | 685 | Error function |
npm install defimath-lib
1e18 = 1.0).exp takes int256, returns uint256; ln takes uint256, returns int256.internal pure — no storage access, no external calls.import "defimath-lib/contracts/math/Math.sol";
uint256 x = 2e18; // x = 2.0
uint256 root = DeFiMath.sqrt(x); // root ≈ 1.41421e18
int256 lnX = DeFiMath.ln(x); // lnX ≈ 0.69315e18
uint256 ePow = DeFiMath.exp(int256(x)); // ePow ≈ 7.389e18expm1 for small x. Computing ex − 1 via exp(x) - 1e18 catastrophically cancels when |x| < 0.01. expm1 uses a Taylor series in that range and preserves full 18-digit precision.log1p for small x. Same reason — log1p(x) preserves precision near zero where ln(1 + x) would lose ~15 digits to subtraction.exp underflows silently to 0. For x < −41.45e18, exp(x) returns 0 (not a revert) since the true value is below 1e-18 representational precision.ln, sqrt, cbrt, and sqrtTime emit the new CLZ opcode introduced in Osaka.| Error | Trigger |
|---|---|
| ExpUpperBoundError | exp(x) when x ≥ 135.305999e18 |
| LnLowerBoundError | ln(0) |
| Log1pLowerBoundError | log1p(x) when x ≤ −1e18 |
| SqrtUpperBoundError | sqrt(x) when x ≥ 280 (~1.2e24) |
| CbrtUpperBoundError | cbrt(x) when x ≥ 276 (~7.6e22) |