Arbitrary-Precision Floating-Point Library
ARPFloat is an implementation of arbitrary precision floating point data structures and utilities. The library can be used to emulate existing floating point types, such as FP16, and create new floating point types. Floating point types can scale to hundreds of digits, and perform very accurate calculations. In ARPFloat the rounding mode is a part of the type-system, and this defines away a number of problem that show up when using fenv.h.
no_std
environments are supported by disabling the std
feature.
python
bindings are supported by enabling the python
feature.
Example
use Float;
use FP128;
// Create the number '5' in FP128 format.
let n = from_f64.cast;
// Use Newton-Raphson to find the square root of 5.
let mut x = n.clone;
for _ in 0..20
println!;
println!;
The program above will print this output:
fp128: 2.2360679774997896964091736687312763
fp64: 2.23606797749979
The library also provides API that exposes rounding modes, and low-level operations.
use FP128;
use NearestTiesToEven;
use Float;
let x = from_u64;
let y = from_f64.cast;
let val = mul_with_rm;
View the internal representation of numbers:
use Float;
use FP16;
let fp = from_i64;
fp.dump; // Prints FP[+ E=+3 M=11110000000]
let m = fp.get_mantissa;
m.dump; // Prints 11110000000
Control the rounding mode for type conversion:
use ;
let x = from_u64;
let b = x.cast_with_rm;
println!; // Prints 2648!
Define new float formats and use high-precision transcendental functions:
use ;
// Define a new float format with 120 bits of accuracy, and
// dynamic range of 2^10.
let sem = new;
let pi = pi;
let x = exp;
println!; // Prints 23.1406926327792....
Floating point numbers can be converted to Continued Fractions that approximate the value.
use ;
let ln = ln2;
println!;
for i in 1..20
The program above will print this output:
ln(2) = .6931471805599453094172321214581765680755001343602552.....
0/1
1/1
2/3
7/10
9/13
61/88
192/277
253/365
445/642
1143/1649
1588/2291
2731/3940
....
The examples directory contains a few programs that demonstrate the use of this library.
Python Bindings
The has python bindings that can be installed with 'pip install -e .'
>>>
>>> =
>>> =
>>> +
4.
>>> =
>>>
>>>
4.789062
>>>
3.1415927
>>>
3.140625
>>>
3.140625
Arpfloat allows you to experiment with new floating point formats. For example, Nvidia's new FP8 format can be defined as:
# Create two random numpy arrays in the range [0,1)
=
=
# Calculate the numpy dot product of the two arrays
# Create the fp8 format (4 exponent bits, 3 mantissa bits + 1 implicit bit)
=
# Convert the arrays to fp8
=
=
=
Resources
There are excellent resources out there, some of which are referenced in the code:
- Books:
- Handbook of Floating-Point Arithmetic 2010th by Jean-Michel Muller et al.
- Elementary Functions: Algorithms and Implementation by Jean-Michel Muller.
- Modern Computer Arithmetic by Brent and Zimmermann.
- Papers:
- An Accurate Elementary Mathematical Library for the IEEE Floating Point Standard, by Gal and Bachels.
- How to print floating-point numbers accurately by Steele, White.
- What Every Computer Scientist Should Know About Floating-Point Arithmetic by David Goldberg.
- Fast Multiple-Precision Evaluation of Elementary Functions by Richard Brent.
- Fast Trigonometric functions for Arbitrary Precision number by Henrik Vestermark.
- Other excellent software implementations: APFloat, RYU, libBF, newlib, musl, etc.
License
Licensed under Apache-2.0