rust_number 0.1.0

Rust port of number-precision for safer floating-point arithmetic
Documentation
  • Coverage
  • 0%
    0 out of 18 items documented0 out of 14 items with examples
  • Size
  • Source code size: 22.67 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 473.93 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 58s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Super1Windcloud

rust_number

rust_number is a Rust port of number-precision for safer decimal arithmetic with f64.

It helps avoid common floating-point surprises such as:

0.1 + 0.2 != 0.3
1.0 - 0.9 != 0.1
3.0 * 0.3 != 0.9

Features

  • Corrects common precision issues for addition, subtraction, multiplication, division, and rounding
  • Supports decimal strings and scientific notation
  • Exposes helper utilities like strip, digit_length, and float2fixed
  • Includes iterator-based helpers for multi-value operations

Installation

Add this crate to your Cargo.toml:

[dependencies]
rust_number = "0.1.0"

Quick Start

use rust_number::{divide, minus, plus, round, strip, times};

fn main() {
    assert_eq!(strip(0.09999999999999998_f64, 15), 0.1);
    assert_eq!(plus(0.1, 0.2), 0.3);
    assert_eq!(minus(1.0, 0.9), 0.1);
    assert_eq!(times(3, 0.3), 0.9);
    assert_eq!(divide(1.21, 1.1), 1.1);
    assert_eq!(round(0.105, 2), 0.11);
}

Run the example locally:

cargo run --example basic_usage

API

  • strip(num, precision) -> f64
  • digit_length(num) -> u32
  • float2fixed(num) -> i128
  • plus(a, b) -> f64
  • minus(a, b) -> f64
  • times(a, b) -> f64
  • divide(a, b) -> f64
  • round(num, decimal) -> f64
  • plus_all(values) -> f64
  • minus_all(values) -> f64
  • times_all(values) -> f64
  • divide_all(values) -> f64
  • enable_boundary_checking(enabled)

All numeric APIs accept:

  • integer types
  • f32 / f64
  • &str
  • String

Multi-Value Operations

use rust_number::{divide_all, plus_all, times_all};

fn main() {
    assert_eq!(plus_all([0.1, 0.2, 0.3]), 0.6);
    assert_eq!(times_all([0.1, 0.2, 10.0]), 0.2);
    assert_eq!(divide_all([1.2, 0.3, 2.0]), 2.0);
}

Scientific Notation

use rust_number::{digit_length, divide, float2fixed, times};

fn main() {
    assert_eq!(digit_length(1.23e-5), 7);
    assert_eq!(float2fixed(1.23e-5), 123);
    assert_eq!(times("2.5e-3", "4e2"), 1.0);
    assert_eq!(divide("3e-4", "1.5e-2"), 0.02);
}

Testing

cargo test

License

MIT