Crate gmp_mpfr [] [src]

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.

To understand the exact operation of the functions in this crate, you can see the online documentation available at the GMP and MPFR pages.

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.

Basic use

There are three main types defined in this 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.

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 floating-point number with 16 bits of precision.
let flo = Float::from_prec(0xff00ff, 16);

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

Arithmetic operations with mixed arbitrary and primitive types are allowed.

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.

Structs

Float

A multi-precision floating-point number. The precision has to be set during construction.

Integer

Integer holds an arbitrary-precision integer. Standard arithmetic operations, bitwise operations and comparisons are supported. In standard arithmetic operations such as addition, you can mix Integer and primitive integer types; the result will be an Integer.

MutNumerDenom

Used to borrow the numerator and denominator of a Rational mutably. The Rational number is canonicalized when the borrow ends. See the Rational::as_mut_numer_denom() method.

Rational

An arbitrary-precision rational number.

Enums

Constant

The available floating-point constants.

Round

The rounding methods for floating-point values.

Special

Special floating-point values.

Traits

AddRound

Provides addition with a specified rounding method.

Assign

Assigns to a number from another value.

AssignRound

Assigns to a number from another value, applying the specified rounding method.

DivFromAssign

Divide and assign the result to the rhs operand. rhs.div_from_assign(lhs) has the same effect as rhs = lhs / rhs.

DivRound

Provides division with a specified rounding method.

FromPrec

Construct Self via a conversion with a specified precision.

FromPrecRound

Construct Self via a conversion with a specified precision, applying the specified rounding method.

MulRound

Provides multiplication with a specified rounding method.

NegAssign

Negates the value inside self.

NegRound

Provides negation with a specified rounding method.

Pow

Provides the power operation.

PowAssign

Provides the power operation inside self.

PowRound

Provides the power operation inside self with a specified rounding method.

ShlRound

Provides the left shift operation with a specified rounding method.

ShrRound

Provides the right shift operation with a specified rounding method.

SubFromAssign

Subtract and assigns the result to the rhs operand. rhs.sub_from_assign(lhs) has the same effect as rhs = lhs - rhs.

SubRound

Provides subtraction with a specified rounding method.

Functions

exp_max

Returns the maximum value for the exponent.

exp_min

Returns the minimum value for the exponent.

prec_max

Returns the maximum value for the precision.

prec_min

Returns the minimum value for the precision.

Type Definitions

BitCount

The type for the bit count of an Integer value.

Exp

The type for the exponent of a Float value.

Prec

The type for the precision of a Float value.