micromath

    Embedded math arithmetic, optimized for small code size at the cost of precision

    math
    embedded
    Download zip
    Author
    Version
    0.4.0
    License
    MIT
    Last updated
    1 hour ago
    Downloads
    1K

    No dependencies

    #Micromath for MoonBit

    Port of the Rust micromath library to MoonBit, giving fast Float math operations.

    Optimizes for performance and small code size at the cost of precision.

    #Benchmarks

    $ moon bench --target wasm [applejag/micromath] bench tan.mbt:88 (#3) ok name time (mean ± σ) range (min … max) @math.tanf 0.04 µs ± 0.00 µs 0.04 µs … 0.04 µs in 10 × 100000 runs @micromath.tan 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs [applejag/micromath] bench sqrt.mbt:65 (#2) ok name time (mean ± σ) range (min … max) Float::sqrt 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs @micromath.sqrt 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs [applejag/micromath] bench sin.mbt:78 (#1) ok name time (mean ± σ) range (min … max) @math.sinf 0.04 µs ± 0.00 µs 0.04 µs … 0.04 µs in 10 × 100000 runs @micromath.sin 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs [applejag/micromath] bench cos.mbt:83 (#1) ok name time (mean ± σ) range (min … max) @math.cosf 0.04 µs ± 0.00 µs 0.04 µs … 0.04 µs in 10 × 100000 runs @micromath.cos 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs [applejag/micromath] bench atan2.mbt:61 (#1) ok name time (mean ± σ) range (min … max) @math.atan2 0.04 µs ± 0.00 µs 0.04 µs … 0.04 µs in 10 × 100000 runs @micromath.atan2 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs [applejag/micromath] bench atan.mbt:70 (#3) ok name time (mean ± σ) range (min … max) @math.atanf 0.07 µs ± 0.00 µs 0.07 µs … 0.07 µs in 10 × 100000 runs @micromath.atan 0.02 µs ± 0.00 µs 0.02 µs … 0.02 µs in 10 × 100000 runs Total tests: 6, passed: 6, failed: 0.

    #License

    Copyright © 2026 Kalle Fagerberg

    Most code in this repository is based on the micromath library.

    This repository complies with the REUSE recommendations. Different licenses are used for different files. In general:

    Please see each file's header or accompanied .license file for specifics.

    DEG_TO_RAD

    let DEG_TO_RAD : Float

    Degrees-to-radians conversion constant.

    This is equal to (PI * 2) / 360

    FRAC_1_PI

    let FRAC_1_PI : Float

    1 divided by mathematical constant pi (π).

    FRAC_2_PI

    let FRAC_2_PI : Float

    2 divided by mathematical constant pi (π).

    FRAC_PI_2

    let FRAC_PI_2 : Float

    Mathematical constant pi (π) divided by 2.

    FRAC_PI_3

    let FRAC_PI_3 : Float

    Mathematical constant pi (π) divided by 3.

    FRAC_PI_4

    let FRAC_PI_4 : Float

    Mathematical constant pi (π) divided by 4.

    FRAC_PI_6

    let FRAC_PI_6 : Float

    Mathematical constant pi (π) divided by 6.

    FRAC_PI_8

    let FRAC_PI_8 : Float

    Mathematical constant pi (π) divided by 8.
    let PI : Float

    Mathematical constant pi (π).

    PI also exists as @math.PI, but as a Double. This PI is a Float.

    RAD_TO_DEG

    let RAD_TO_DEG : Float

    Radians-to-degrees conversion constant.

    This is equal to 360 / (PI * 2)

    TAU

    let TAU : Float

    Mathematical constant tau (τ), i.e 2*PI

    atan

    fn atan(x : Float) -> Float

    Approximates atan(x) approximation in radians with a maximum error of 0.002.

    Returns [@float.not_a_number] if the number is [@float.not_a_number].

    atan2

    fn atan2(y : Float, x : Float) -> Float

    Approximates the four quadrant arctangent of y and x in radians with a maximum error of 0.002.

    copysign

    fn copysign(num : Float, sign : Float) -> Float

    Returns a number composed of the magnitude of num and the sign of sign.

    cos

    fn cos(x : Float) -> Float

    Approximates cos(x) in radians with a maximum error of 0.002.

    signum

    fn signum(sign : Float) -> Float

    Returns a number that represents the sign of a number.

    • 1.0 if the number is positive, +0.0 or @float.infinity
    • -1.0 if the number is negative, -0.0 or @float.neg_infinity
    • @float.not_a_number if the number is @float.not_a_number

    sin

    fn sin(x : Float) -> Float

    Approximates sin(x) in radians with a maximum error of 0.002.

    sqrt

    fn sqrt(n : Float) -> Float

    Approximates the square root of a number with an average deviation of ~5%.

    Returns [@float.not_a_number] if n is a negative number.

    tan

    fn tan(x : Float) -> Float

    Approximates tan(x) in radians with a maximum error of 0.6.