Arbitrary-precision numbers
Rug provides integers and floating-point numbers with arbitrary precision and correct rounding:
Integer
is a bignum integer with arbitrary precision,Rational
is a bignum rational number with arbitrary precision,Float
is a multi-precision floating-point number with correct rounding, andComplex
is a multi-precision complex number with correct rounding.
Rug is a high-level interface to the following GNU libraries:
- GMP for integers and rational numbers,
- MPFR for floating-point numbers, and
- MPC for complex numbers.
Rug 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 the full text of the GNU LGPL and GNU GPL for details.
What’s new
Version 1.11.0 news (2020-09-03)
- The gmp-mpfr-sys dependency was updated to version 1.4.
- Now it is possible to display
Float
numbers with only one siginificant digit.
Version 1.10.0 news (2020-07-23)
- The gmp-mpfr-sys dependency was updated to version 1.3.
- Now Option<Integer> has
the same size as
Integer
; and similar forRational
,Float
,Complex
,RandState
andThreadRandState
.
Other releases
Details on other releases can be found in RELEASES.md.
Quick example
use ;
let mut int = new;
assert_eq!;
int.assign;
assert_eq!;
let decimal = "98_765_432_109_876_543_210";
int.assign;
assert!;
let hex_160 = "ffff0000ffff0000ffff0000ffff0000ffff0000";
int.assign;
assert_eq!;
int = - 1;
assert_eq!;
- Integer::new creates a new
Integer
intialized to zero. - To assign values to Rug types, we use the
Assign
trait and its methodAssign::assign
. We do not use the assignment operator=
as that would drop the left-hand-side operand and replace it with a right-hand-side operand of the same type, which is not what we want here. - Arbitrary precision numbers can hold numbers that are too large to fit in a primitive type. To assign such a number to the large types, we use strings rather than primitives; in the example this is done using Integer::parse and Integer::parse_radix.
- We can compare Rug types to primitive types or to other Rug types
using the normal comparison operators, for example
int > 100_000_000
. - Most arithmetic operations are supported with Rug types and
primitive types on either side of the operator, for example
int >> 128
.
Using with primitive types
With Rust primitive types, arithmetic operators usually operate on two
values of the same type, for example 12i32 + 5i32
. Unlike primitive
types, conversion to and from Rug types can be expensive, so the
arithmetic operators are overloaded to work on many combinations of
Rug types and primitives. More details are available in the
documentation.
Operators
Operators are overloaded to work on Rug types alone or on a combination of Rug types and Rust primitives. When at least one operand is an owned value of a Rug type, the operation will consume that value and return a value of the Rug type. For example
use Integer;
let a = from;
let b = 5 - a;
assert_eq!;
Here a
is consumed by the subtraction, and b
is an owned
Integer
.
If on the other hand there are no owned Rug types and there are references instead, the returned value is not the final value, but an incomplete-computation value. For example
use Integer;
let = ;
let incomplete = &a - &b;
// This would fail to compile: assert_eq!(incomplete, -10);
let sub = from;
assert_eq!;
Here a
and b
are not consumed, and incomplete
is not the final
value. It still needs to be converted or assigned into an Integer
.
This is covered in more detail in the documentation’s
Incomplete-computation values section.
More details on operators are available in the documentation.
Using Rug
Rug is available on crates.io. To use Rug in your crate, add it as a dependency inside Cargo.toml:
[]
= "1.11"
Rug requires rustc version 1.37.0 or later.
Rug also depends on the GMP, MPFR and MPC libraries through the low-level FFI bindings in the gmp-mpfr-sys crate, which needs some setup to build; the gmp-mpfr-sys documentation has some details on usage under GNU/Linux, macOS and Windows.
Optional features
The Rug crate has six optional features:
integer
, enabled by default. Required for theInteger
type and its supporting features.rational
, enabled by default. Required for theRational
number type and its supporting features. This feature requires theinteger
feature.float
, enabled by default. Required for theFloat
type and its supporting features.complex
, enabled by default. Required for theComplex
number type and its supporting features. This feature requires thefloat
feature.rand
, enabled by default. Required for theRandState
type and its supporting features. This feature requires theinteger
feature.serde
, disabled by default. This provides serialization support for theInteger
,Rational
,Float
andComplex
number types, providing that they are enabled. This feature requires the serde crate.
The first five optional features are enabled by default; to use features selectively, you can add the dependency like this to Cargo.toml:
[]
= "1.11"
= false
= ["integer", "float", "rand"]
Here only the integer
, float
and rand
features are enabled. If
none of the features are selected, the gmp-mpfr-sys crate
is not required and thus not enabled. In that case, only the
Assign
trait and the traits that are in the ops
module are
provided by the crate.