2 unstable releases

0.2.0 Mar 8, 2024
0.1.0 Oct 9, 2023

#826 in Math

44 downloads per month

MIT and LGPL-3.0+

150KB
3K SLoC

mpmfnum

Rust library for simulating number systems.

Supports IEEE-754 floating-point numbers, fixed-point numbers, and more. Inspired by FPCore and the Titanic library.

This library is hosted as a crate here. The documentation can be found here.

Using mpmfnum

Run the following Cargo command in your project directory:

cargo add mpmfnum

Or add mpmfnum to your Cargo.toml like this:

[dependencies]
mpmfnum = "0.2.0"

lib.rs:

Number formats, precisions, and rounding modes.

mpmfnum is a numbers library for emulation various number formats, at multiple precisions with many rounding modes. Hence its name "Multi-Precision, Multi-Format" (MPMF). Unlike other number libraries like BigInt or Rug (MPFR), this library emphasizes a clean abstraction of various computer number systems rather than high-performance computation.

This library embraces design principles found in the FPCore standard. Most importantly, numerical programs need only be specified by (i) real-number mathematical operations and (ii) rounding. Number formats, e.g., double or float, are not first-class, as in most programming languages. Rather, formats are side effects of rounding and should be de-emphasized or eliminated entirely. Furthermore, all values within a numerical program should be viewed as (extended) real numbers.

These design principles are reflected in the two primary traits of this library:

For implementation purposes, we restrict any Real value to be of the form (-1)^s * c * 2^exp where s is 0 or 1, c is a non-negative integer, and exp is an integer. Implementations of RoundingContext may support any of the numerous operations (as traits) found under crate::ops. Operations provided by this library are correctly rounded.

mpmfnum supports various number systems through implementations of RoundingContext:

  • RealContext performs no rounding, that is, the operation is exact,
  • RFloatContext rounds a Real value to an arbitrary-precision, floating-point numbers with unbounded exponent,
  • FloatContext rounds a Real value to a fixed-precision, floating-point numbers with unbounded exponent,
  • IEEE754Context rounds a Real value to a floating-point number as described by the IEEE 754 standard,
  • FixedContext rounds a Real value to a fixed-point numbers,
  • PositContext rounds a Real value to a posit number as described by the Posit standard.

Dependencies

~20MB
~486K SLoC