Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

cairo_fp

A comprehensive fixed-point math library for Cairo and Starknet.

This library provides two precision levels:

  • f128 - 64.64 fixed-point format (128-bit, high precision)
  • f64 - 32.32 fixed-point format (64-bit, lower gas costs)

Features

- Basic Arithmetic: add, sub, mul, div, neg, abs - Power & Exponential: exp, exp2, pow, sqrt, cbrt - Logarithms: ln, log2, log10 - Trigonometry: sin, cos, tan, asin, acos, atan (with fast variants) - Hyperbolic: sinh, cosh, tanh, asinh, acosh, atanh - DeFi Primitives: compound interest, AMM pool math, ratio utilities - Procedural Generation: Simplex noise, random number generation - Vector Types: Vec2, Vec3, Vec4 for graphics/games

Quick Start

use cairo_fp::f128::types::fixed::FixedTrait;
use cairo_fp::f128::math::ops;

// Create fixed-point numbers
let a = FixedTrait::new_unscaled(10, false);  // 10.0
let b = FixedTrait::new_unscaled(3, false);   // 3.0

// Basic arithmetic
let sum = a + b;      // 13.0
let product = a * b;  // 30.0
let quotient = a / b; // 3.333...

// Advanced math
let sqrt_a = ops::sqrt(a);  // ~3.162
let exp_b = ops::exp(b);    // ~20.086

Choosing Between f128 and f64

Aspectf128f64
Precision~5×10⁻²⁰~2×10⁻¹⁰
Range±2⁶³±2³¹
Gas CostHigher40-60% lower

Use f64 when gas optimization is critical and precision requirements allow.

Fully qualified path: cairo_fp

Modules

f128
f64
utils