gmp-mpfr 0.4.1

Integer, rational, floating-point and complex numbers based on GMP, MPFR and MPC
docs.rs failed to build gmp-mpfr-0.4.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: gmp-mpfr-0.5.2

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. GNU MPC is used for complex numbers.

Documentation

Documentation is available at [https://tspiteri.gitlab.io/gmp-mpfr-doc/].

It can also be helpful to reference the online documentation available at the GMP, MPFR and MPC pages.

There are four main types defined in the crate:

  • Integer which holds an arbitrary precision integer,
  • Rational which holds an arbitrary precision rational number, and
  • Float which holds a floating-point number with an exact precision.
  • Complex which holds a complex 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 gmp_mpfr::{Float, FromPrec, Integer, Rational};

// Create an integer initialized as zero.
let int = Integer::new();
// Create a rational number, -22 / 4.
let rat = Rational::from((-22, 4));
// Create a floating-point number with 16 bits of precision.
let flo = Float::from_prec(0xff00ff, 16);
// Create complex number with 16 bits of precision.
let mut com = Complex::new((16, 16));
com.assign((1.5, 3.5));

assert!(int.to_u32() == 0);
assert!(int == 0);
assert!(rat.numer().to_i32() == -11);
assert!(*rat.denom() == 2);
assert!(rat.to_f32() == -5.5);
assert!(flo.to_f64() == 0xff0100 as f64);
assert!(*com.real() == 1.5);
assert!(*com.imag() == 3.5);

Arithmetic operations with mixed arbitrary and primitive types are allowed. However, the supported operations are not exhaustive.

use gmp_mpfr::Integer;

let mut a = Integer::from(0xc);
a = (a << 80) + 0xffee;
assert!(a.to_string_radix(16) == "c0000000000000000ffee");
//                                 ^   ^   ^   ^   ^
//                                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.

Usage

To use this crate, add gmp-mpfr as a dependency in Cargo.toml:

[dependencies]
gmp-mpfr = "0.4"

This crate depends on the low-level bindings in the crate gmp-mpfr-sys. This should be transparent on GNU/Linux and macOS, but may need some work on Windows. See the gmp-mpfr-sys README for some details.

License

This crate is free software: you can redistribute it and/or modify it under the terms of 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.

See LICENSE-LGPL and LICENSE-GPL for details.