Skip to main content

Crate g_math

Crate g_math 

Source
Expand description

§g_math: multi-domain fixed-point arithmetic

Zero-float, pure-Rust, deterministic fixed-point arithmetic. The format is a type, Fixed<S, F> (storage word S, F fraction bits), and any number of formats coexist in one build. The 17 transcendental functions, canonical expression trees and closed-form geometry maps are correctly rounded: computed one tier wider than storage and, where that result lies too close to a rounding boundary, decided at about 1000 bits.

Upgrading from 0.6: see docs/UPGRADING_0.7.md in the repository (the profiles became types; FixedPoint became a format such as Q64_64 or Fixed<i32, 10>). Every change is in CHANGELOG.md.

§Formats

  • Fixed<i32, F> for 2 <= F <= 30 (Q16_16 is F = 16; Fixed<i32, 10> is Q22.10)
  • Q32_32 = Fixed<i64, 32>: about 9 decimals
  • Q64_64 = Fixed<i128, 64>: about 19 decimals
  • Q128_128 = Fixed<I256, 128>: about 38 decimals
  • Q256_256 = Fixed<I512, 256>: about 77 decimals

§Canonical layer (routing, one rounding per tree)

use g_math::canonical::{gmath, evaluate};
use g_math::fixed_point::Q64_64;

let sum = evaluate::<Q64_64>(&(gmath("1.5") + gmath("2.5"))).unwrap();
assert_eq!(sum.to_string(), "4.0");
let e = evaluate::<Q64_64>(&gmath("1").exp()).unwrap();
assert!(e.to_string().starts_with("2.718281828459045235"));

§Imperative layer (direct, Copy)

use g_math::fixed_point::{Fixed, FixedVector, Q64_64};

type Q = Fixed<i32, 10>; // Q22.10
let x = Q::from_str("2").sqrt();
assert_eq!(x.raw(), 1448); // 1.4140625, the nearest of sqrt(2) at 10 fraction bits

let v: FixedVector<Q64_64> = FixedVector::from_slice(&[Q64_64::from_int(3), Q64_64::from_int(4)]);
assert_eq!(v.length(), Q64_64::from_int(5));

Re-exports§

pub use fixed_point::canonical;
pub use fixed_point::compute_tier;
pub use fixed_point::wide;

Modules§

fixed_point
The formats and every layer built on them.

Constants§

VERSION
Version information