GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - script3/soroban-fixed-point-math: Fixed point math library for Soroban smart contracts.

BranchesTags
Open more actions menu

Latest commit

 

History

19 Commits
19 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
.gitignore
.gitignore
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

Repository files navigation

soroban-fixed-point-math

A fixed-point math library for Soroban smart contacts.

Safety

This is experimental software and is provided on an "as is" and "as available" basis.

We do not give any warranties and will not be liable for any loss incurred through any use of this codebase.

Usage

Add this to your Cargo.toml:

[dependencies]
soroban_fixed_point_math = "<desired version>"

FixedPoint Trait

The FixedPoint trait is implemented for the Rust generic integers i64, u64, i128, and u128. Any overflow or divide by zero that occurs during the fixed point math calculation will return a value of None, in line with Rust's existing checked math functions.

Phantom overflows are errors that result from an overflow during an intermediate calculation, but the expected result would be within bounds. This trait manages this differently for each implemented integer type:

  • i64 and u64
    • In the event of a phantom overflow, the intermediary computation gets scaled to i128 / u128 and is retried.
  • i128 and u128
    • In the event of a phantom overflow, the function will terminate and return None. If you need larger numbers to be handled, please use the SorobanFixedPoint trait.

SorobanFixedPoint Trait

The SorobanFixedPoint trait is implemented for the Soroban host types I256 and U256. The trait will panic if any I256 or U256 arithmetic error occurs, as host function calls panic by default.

Additional implementations of the SorobanFixedPoint trait are included for i128 and u128 integer types to support larger intermediary computations. This removes the majority of phantom overflow events for 128 bit fixed point math.

Examples

For any supported type implementing FixedPoint, you can perform fixed-point operations like this:

use soroban_fixed_point_math::FixedPoint;

let x: u64 = 1_5000000;
let y: u64 = 2_0000000;
assert_eq!(x.fixed_mul_floor(y, 1_0000000).unwrap(), 3_0000000);

For any support type implementing SorobanFixedPoint, you can perform fixed-point operations like this:

use soroban_fixed_point_math::SorobanFixedPoint;
use soroban_sdk::{Env, I256};

let env: Env //... from your contract, or Env::default() in a test
let x: I256 = I256::from_i32(&env, 1_5000000);
let y: I256 = I256::from_i32(&env, 2_0000000);
assert_eq!(x.fixed_mul_ceil(&x, &y, I256::from_i32(&env, 1_0000000)), 3_0000000);

Acknowledgements

This library was inspired by or directly modified from many sources, primary:

  • Solmate
  • OpenZeppelin

WASM

The WASM target wasm32-unknown-unknown is supported.

Contributions

Contributions are welcome. Please check out the contribution guide (TODO)!

License

This library is released under the MIT License.

About

Fixed point math library for Soroban smart contracts.

Resources

Readme
MIT license
Activity
Custom properties

Stars

16 stars

Watchers

1 watching

Forks

11 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.