Crate rug [−] [src]
Arbitrary-precision numbers
The rug
crate provides integers and floating-point numbers with
arbitrary precision and correct rounding. Its main features are:
- big integers with arbitrary precision,
- big rational numbers with arbitrary precision,
- multi-precision floating-point numbers with correct rounding, and
- multi-precision complex numbers with correct rounding.
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 the full text of the GNU LGPL and GNU GPL for details.
This crate depends on the low-level bindings in the
gmp-mpfr-sys
crate, which provides Rust FFI bindings for:
- the GNU Multiple Precision Arithmetic Library (GMP),
- the GNU MPFR Library, a library for multiple-precision floating-point computations, and
- GNU MPC, a library for the arithmetic of complex numbers with arbitrarily high precision.
It can be helpful to refer to the documentation of the GMP, MPFR and MPC libraries.
Examples
extern crate rug; use rug::{Assign, Integer}; fn main() { // Create an integer initialized as zero. let mut int = Integer::new(); assert_eq!(int, 0); int.assign(14); assert_eq!(int, 14); let hex_160 = "ffff0000ffff0000ffff0000ffff0000ffff0000"; int.assign_str_radix(hex_160, 16).unwrap(); assert_eq!(int.significant_bits(), 160); int = (int >> 128) - 1; assert_eq!(int, 0xfffe_ffff_u32); }
Usage
This crate requires rustc version 1.18.0 or later.
To use rug
in your crate, add extern crate rug;
to the crate
root and add rug
as a dependency in Cargo.toml
:
[dependencies]
rug = "0.9.3"
The rug
crate depends on the low-level bindings in the
gmp-mpfr-sys
crate. This should be transparent on
GNU/Linux and macOS, but may need some work on Windows. See the
gmp-mpfr-sys
documentation for some details.
Optional features
The rug
crate has six optional features:
integer
, enabled by default.rational
, enabled by default. This feature requires theinteger
feature.float
, enabled by default.complex
, enabled by default. This feature requires thefloat
feature.rand
, enabled by default. This features requires theinteger
feature.serde
, disabled by default. This provides serialization support for theInteger
,Rational
,Float
andComplex
types, providing that they are enabled.
The first five optional features are enabled by default; to
disable them add this to Cargo.toml
:
[dependencies.rug]
version = "0.9.3"
default-features = false
If none of the first five optional features are selected, the
gmp-mpfr-sys
crate is not required and thus not enabled.
To use features selectively, you can add this to Cargo.toml
:
[dependencies.rug]
version = "0.9.3"
default-features = false
# Pick which features to use
features = ["integer", "float", "rand"]
Modules
complex |
Multi-precision complex numbers with correct rounding. |
float |
Multi-precision floating-point numbers with correct rounding. |
integer |
Aribtrary-precision integers. |
ops |
Operations on numbers. |
rand |
Random number generation. |
rational |
Arbitrary-precision rational numbers. |
Structs
Complex |
A multi-precision complex number with arbitrarily large precision and correct rounding. |
Float |
A multi-precision floating-point number with arbitrarily large precision and correct rounding |
Integer |
An arbitrary-precision integer. |
Rational |
An arbitrary-precision rational number. |
Traits
Assign |
Assigns to a number from another value. |