Expand description
A collection of numeric types and traits for Rust.
This includes new types for big integers, rationals, and complex numbers,
new traits for generic programming on numeric properties like Integer,
and generic range iterators.
§Example
This example uses the BigRational type and Newton’s method to approximate a square root to arbitrary precision:
use num::FromPrimitive;
use num::bigint::BigInt;
use num::rational::{Ratio, BigRational};
fn approx_sqrt(number: u64, iterations: usize) -> BigRational {
let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
let mut approx = start.clone();
for _ in 0..iterations {
approx = (&approx + (&start / &approx)) /
Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
}
approx
}
fn main() {
println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
}
§Compatibility
The num crate is tested for rustc 1.60 and greater.
Modules§
Structs§
- BigInt
- A big signed integer type.
- BigUint
- A big unsigned integer type.
- Complex
- A complex number in Cartesian form.
Traits§
- Bounded
- Numbers which have upper and lower bounds
- Checked
Add - Performs addition that returns
Noneinstead of wrapping around on overflow. - Checked
Div - Performs division that returns
Noneinstead of panicking on division by zero and instead of wrapping around on underflow and overflow. - Checked
Mul - Performs multiplication that returns
Noneinstead of wrapping around on underflow or overflow. - Checked
Sub - Performs subtraction that returns
Noneinstead of wrapping around on underflow. - Float
- Generic trait for floating point numbers
- From
Primitive - A generic trait for converting a number to a value.
- Integer
- Num
- The base trait for numeric types, covering
0and1values, comparisons, basic numeric operations, and string conversion. - NumCast
- An interface for casting between machine scalars.
- One
- Defines a multiplicative identity element for
Self. - PrimInt
- Generic trait for primitive integers.
- Saturating
- Saturating math operations. Deprecated, use
SaturatingAdd,SaturatingSubandSaturatingMulinstead. - Signed
- Useful functions for signed numbers (i.e. numbers that can be negative).
- ToPrimitive
- A generic trait for converting a value to a number.
- Unsigned
- A trait for values which cannot be negative
- Zero
- Defines an additive identity element for
Self.
Functions§
- abs
- Computes the absolute value.
- abs_sub
- The positive difference of two numbers.
- cast
- Cast from one machine scalar to another.
- checked_
pow - Raises a value to the power of exp, returning
Noneif an overflow occurred. - clamp
- A value bounded by a minimum and a maximum
- one
- Returns the multiplicative identity,
1. - pow
- Raises a value to the power of exp, using exponentiation by squaring.
- range
- Returns an iterator over the given range [start, stop) (that is, starting at start (inclusive), and ending at stop (exclusive)).
- range_
inclusive - Return an iterator over the range [start, stop]
- range_
step - Return an iterator over the range [start, stop) by
step. It handles overflow by stopping. - range_
step_ inclusive - Return an iterator over the range [start, stop] by
step. It handles overflow by stopping. - signum
- Returns the sign of the number.
- zero
- Returns the additive identity,
0.
Type Aliases§
- BigRational
- Alias for arbitrary precision rationals.
- Rational
Deprecated - Alias for a
Ratioof machine-sized integers. - Rational32
- Alias for a
Ratioof 32-bit-sized integers. - Rational64
- Alias for a
Ratioof 64-bit-sized integers.