Rust bindings for GMP and MPFR
The gmp_mpfr
crate uses the
GNU Multiple Precision Arithmetic Library
(GMP), for integer and rational numbers. The
GNU MPFR Library, a library for
multiple-precision floating-point computations, is used for
floating-point numbers.
Documentation
Documentation is available at docs.rs.
It can also be helpful to reference the online documentation available at the GMP and MPFR pages.
Basic use
There are three main types defined in the crate:
Integer
which holds an arbitrary precision integer,Rational
which holds an arbitrary precision rational number, andFloat
which holds a floating-point number with an exact precision.
You can construct these types from primitive data types. The standard arithmetic operators work on these types. Operators can also operate on these types and primitive types; in this case, the result is returned as an arbitrary precision type.
Examples
You can construct arbitrary precision types from primitive types:
use ;
use ;
// Create an integer initialized as zero.
let int = new;
// Create a rational number, -22 / 4.
let rat = from;
// Create floating-point number with 16 bits of precision.
let flo = from_prec;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
Arithmetic operations with mixed arbitrary and primitive types are allowed.
use Integer;
let mut a = from;
a = + 0xffee;
assert!;
// ^ ^ ^ ^ ^
// 80 64 48 32 16
Note that in the above example, there is only one construction.
The Integer
instance is moved into the shift operation so that
the result can be stored in the same instance, then that result is
similarly consumed by the addition operation.
Licence
Just like GMP and MPFR, this crate is free software: you can redistribute it and/or modify it under the terms of either
- the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version, or
- the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
See LICENSE-LGPL and LICENSE-GPL for details.