Skip to main content

Crate solmath

Crate solmath 

Source
Expand description

§solmath

Fixed-point financial math for Solana. Black-Scholes, Greeks, implied volatility, fat-tail pricing, and weighted pool math — all in pure integer arithmetic.

All values use u128 or i128 scaled by 1e12 (12 decimal places). HP variants use 1e15 internally but accept and return 1e12 values.

use solmath::*;

// 1.5 in fixed-point
let x: u128 = 1_500_000_000_000;
let ln_x = ln_fixed_i(x)?; // ≈ 0.405 * 1e12

// Black-Scholes: S=100, K=100, r=5%, σ=20%, T=1yr
let s = 100 * SCALE;
let k = 100 * SCALE;
let r = 50_000_000_000u128;  // 0.05
let sigma = 200_000_000_000u128; // 0.20
let t = SCALE; // 1.0
let greeks = bs_full_hp(s, k, r, sigma, t)?;
// greeks.call, greeks.put, greeks.gamma, greeks.vega, ...

Agrees with QuantLib’s AnalyticEuropeanEngine to 10-14 significant figures.

Re-exports§

pub use double_word::DoubleWord;
pub use error::SolMathError;
pub use arithmetic::fp_mul;
pub use arithmetic::fp_mul_i;
pub use arithmetic::fp_mul_round;
pub use arithmetic::fp_mul_i_round;
pub use arithmetic::fp_mul_i_round_dw;
pub use arithmetic::fp_div;
pub use arithmetic::fp_div_i;
pub use arithmetic::fp_div_round;
pub use arithmetic::fp_div_floor;
pub use arithmetic::fp_div_ceil;
pub use arithmetic::fp_sqrt;
pub use overflow::checked_mul_div_i;
pub use overflow::checked_mul_div_floor_i;
pub use overflow::checked_mul_div_ceil_i;
pub use mul_div::mul_div_floor;
pub use mul_div::mul_div_ceil;
pub use mul_div::mul_div_floor_u128;
pub use mul_div::mul_div_ceil_u128;
pub use transcendental::ln_fixed_i;
pub use transcendental::exp_fixed_i;
pub use transcendental::pow_fixed;
pub use transcendental::pow_int;
pub use transcendental::pow_fixed_i;
pub use transcendental::expm1_fixed;
pub use trig::sin_fixed;
pub use trig::cos_fixed;
pub use trig::sincos_fixed;
pub use normal::norm_cdf_poly;
pub use normal::norm_pdf;
pub use normal::norm_cdf_and_pdf;
pub use normal::inverse_norm_cdf;
pub use hp::ln_fixed_hp;
pub use hp::exp_fixed_hp;
pub use hp::pow_fixed_hp;
pub use hp::pow_product_hp;
pub use hp::norm_cdf_poly_hp;
pub use hp::black_scholes_price_hp;
pub use hp::bs_full_hp;
pub use hp::fp_mul_hp_i;
pub use hp::fp_mul_hp_u;
pub use hp::fp_div_hp_safe;
pub use i64_math::nig_call_64;
pub use i64_math::nig_put_64;
pub use complex::Complex;
pub use complex::complex_mul;
pub use complex::complex_div;
pub use complex::complex_exp;
pub use complex::complex_sqrt;

Modules§

arithmetic
complex
double_word
error
hp
i64_math
mul_div
normal
overflow
transcendental
trig

Structs§

BsFull
Complete Black-Scholes output: prices and all Greeks at SCALE.

Constants§

LN2_HP_LO
Sub-ULP residual for split LN2 at HP scale.
LN2_LO
Sub-ULP residual for split LN2 at standard scale. true_ln2 × SCALE ≈ LN2_I + LN2_LO / SCALE to sub-ULP precision. Replaces the crude -(k*55+500)/1000 correction in ln and exp.
LN_REMEZ_COEFFS
Remez degree-7 ln polynomial as array for compensated evaluation. Same coefficients as LN_REMEZ_W0..W7, ascending order.
LN_REMEZ_HP_COEFFS
HP Remez degree-9 ln polynomial as array for compensated evaluation. Same coefficients as LN_REMEZ_HP0..HP9, ascending order.
SCALE
Standard fixed-point scale factor: 1e12.
SCALE_I
Signed standard scale factor: 1e12.