scalars 0.3.4

Minimal numeric traits: Zero, One, Inv, Sqrt, Exp, Logarithm, Trigonometry, Real, Integer
Documentation

scalars

Minimal numeric traits for Rust. Single-purpose traits instead of monolithic kitchen-sink traits.

#![no_std] compatible. Uses libm for float math functions.

Traits

Identities & Operations

  • Zerofn zero(), fn is_zero()
  • Onefn one(), fn is_one()
  • Invfn inv() with associated Output type
  • Sqrtfn sqrt()

Exponential

  • Expfn exp(), fn exp2() with associated Output type (supports Bivector → Rotor)

Trigonometry

  • Trigonometryfn sin_cos() -> [Self; 2], fn sin(), fn cos(), fn tan()
  • InverseTrigonometryfn asin(), fn acos(), fn atan(), fn atan2()

Ordering

  • Clampfn min(), fn max(), fn clamp()

Parsing

  • FromStrRadixfn from_str_radix() with associated Error type

Supertraits

  • NumericCopy + PartialEq + Add + Sub + Mul + Zero + One
  • IntegerNumeric + Eq + Ord + Div + Rem
  • RealNumeric + PartialOrd + Div + Neg + Inv + Sqrt + Exp + Trigonometry + InverseTrigonometry + Clamp

All supertraits have blanket implementations.

Implementations

  • f32, f64 — all traits including Real
  • i8..i128, u8..u128, isize, usizeZero, One, Clamp, FromStrRadix, Integer

Example

use scalars::{Real, Zero};

fn normalize<T: Real>(values: &mut [T]) {
    let sum: T = values.iter().copied().fold(T::zero(), |accumulator, value| accumulator + value);
    if !sum.is_zero() {
        for value in values {
            *value = *value / sum;
        }
    }
}

AI-assisted development

This crate is designed for use with AI coding assistants. Each trait covers exactly one concept, all supertraits use blanket implementations, and there is no implicit behavior. The Exp trait's associated Output type enables geometric algebra types (Bivector → Rotor) alongside scalar floats.