npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cairo-fp

v1.0.0

Published

Fixed-point math library for Cairo and Starknet

Readme

cairo-fp

Tests License: MIT

A fixed-point math library in 64.64 and 32.32 representation built for Cairo & Starknet.

Credits

This project is a fork and enhancement of Cubit by Unstoppable Games, Inc. The original library laid the foundation for fixed-point arithmetic in Cairo and we are grateful for their pioneering work.

Original Authors: Unstoppable Games, Inc. / Influence

Cubit itself was the successor to cairo-math-64x61.

What's New in cairo-fp

  • DeFi Primitives: Interest calculations, AMM pool math, ratio utilities
  • Cube Root (cbrt): Essential for 3-asset pool calculations
  • Enhanced Precision Tests: DeFi-typical value ranges validated
  • Gas Benchmarks: Documented gas costs for all operations
  • JavaScript Utilities: Off-chain computation helpers for frontend integration
  • Modern Toolchain: Updated for Cairo 2.15+ and Scarb

Installation

Add to your Scarb.toml:

[dependencies]
cairo_fp = { git = "https://github.com/ametel01/cairo-fp" }

Fixed-Point Number Formats

| Format | Type | Range | Precision | |--------|------|-------|-----------| | 64.64 | f128::Fixed | -2^64 to 2^64 | ~1e-20 | | 32.32 | f64::Fixed | -2^32 to 2^32 | ~1e-9 |

A signed 64.64-bit fixed-point number is a fraction where the numerator is a signed 128-bit integer and the denominator is 2^64. The denominator is implicit, eliminating storage overhead.

Usage

use cairo_fp::f128::types::fixed::{Fixed, FixedTrait};

// Create fixed-point numbers
let a = FixedTrait::new_unscaled(5, false);  // 5.0
let b = FixedTrait::new(9223372036854775808, false);  // 0.5 (0.5 * 2^64)

// Arithmetic
let sum = a + b;      // 5.5
let product = a * b;  // 2.5
let quotient = a / b; // 10.0

// Math functions
let exp_val = a.exp();    // e^5
let ln_val = a.ln();      // ln(5)
let sqrt_val = a.sqrt();  // sqrt(5)
let pow_val = a.pow(b);   // 5^0.5

Core Library

All functions are available under cairo_fp::f64 or cairo_fp::f128.

Operators

| Operator | Trait | |----------|-------| | +, += | Add, AddEq | | -, -= | Sub, SubEq | | *, *= | Mul, MulEq | | /, /= | Div, DivEq | | ==, != | PartialEq | | >, >=, <, <= | PartialOrd |

Math Functions (math::ops)

| Function | Description | Gas (f128) | |----------|-------------|------------| | ceil | Round up to nearest integer | ~34k | | floor | Round down to nearest integer | ~34k | | round | Round to nearest integer | ~34k | | sqrt | Square root | ~33k | | cbrt | Cube root | ~217-363k | | exp | Natural exponential (e^x) | ~176k | | exp2 | Base-2 exponential (2^x) | ~17k | | ln | Natural logarithm | ~40k | | log2 | Base-2 logarithm | ~190k | | log10 | Base-10 logarithm | ~201k | | pow | Power function | ~75-362k |

Trigonometry (math::trig)

| Function | Fast Version | Description | |----------|--------------|-------------| | sin | sin_fast | Sine | | cos | cos_fast | Cosine | | tan | tan_fast | Tangent | | asin | asin_fast | Inverse sine | | acos | acos_fast | Inverse cosine | | atan | atan_fast | Inverse tangent |

Hyperbolic (math::hyp)

  • sinh, cosh, tanh
  • asinh, acosh, atanh

DeFi Primitives

Interest Calculations (math::interest)

use cairo_fp::f128::math::interest::{compound_interest, continuous_interest, linear_interpolate};

let principal = FixedTrait::new_unscaled(1000, false);
let rate = FixedTrait::new(922337203685477580, false);  // 5% = 0.05
let periods = FixedTrait::new_unscaled(12, false);

let amount = compound_interest(principal, rate, periods);

AMM Pool Math (math::pool)

use cairo_fp::f128::math::pool::{constant_product_price, liquidity_share, geometric_mean};

let reserve_a = FixedTrait::new_unscaled(1000000, false);
let reserve_b = FixedTrait::new_unscaled(2000000, false);

let price = constant_product_price(reserve_a, reserve_b);
let k = geometric_mean(reserve_a, reserve_b);

Ratio Utilities (math::ratio)

use cairo_fp::f128::math::ratio::{proportion, percentage_change, weighted_average};

let change = percentage_change(old_price, new_price);

JavaScript Utilities

For frontend integration, use the JS utilities to convert between JavaScript numbers and Cairo fixed-point representation:

import f128 from 'cairo-fp/src/f128/utils.js';

// Convert JS number to Fixed
const fixed = f128.toFixed(3.14159);
// { mag: 57952155664616982739n, sign: false }

// Convert Fixed back to JS number
const num = f128.fromFixed(fixed);
// 3.14159

// DeFi math helpers
const result = f128.exp(0.05);      // e^0.05
const sqrt = f128.sqrt(2);          // sqrt(2)
const cbrt = f128.cbrt(27);         // cbrt(27) = 3

Run JS tests:

bun test

Development

# Build
scarb build

# Run Cairo tests
snforge test

# Run JS tests
bun test

Gas Benchmarks

See inline documentation in src/f128/math/defi_precision.cairo and src/f64/math/defi_precision.cairo for detailed gas costs.

License

MIT

Original Cubit License: MIT © Unstoppable Games, Inc.