Crate num [−] [src]
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:
extern crate num; #[cfg(all(feature = "bigint", feature="rational"))] mod test { use num::FromPrimitive; use num::bigint::BigInt; use num::rational::{Ratio, BigRational}; pub 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 } } #[cfg(not(all(feature = "bigint", feature="rational")))] mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } } use test::approx_sqrt; fn main() { println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 }extern crate num; 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 } Run
Reexports
pub extern crate num_traits; |
pub extern crate num_integer; |
pub extern crate num_iter; |
pub extern crate num_complex; |
pub extern crate num_bigint; |
pub extern crate num_rational; |
Modules
bigint |
A Big integer (signed version: |
cast | |
complex |
Complex numbers. |
integer |
Integer trait and functions. |
iter |
External iterators for generic mathematics |
rational |
Rational numbers |
traits |
Numeric traits for generic mathematics |
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 |
CheckedAdd |
Performs addition that returns |
CheckedDiv |
Performs division that returns |
CheckedMul |
Performs multiplication that returns |
CheckedSub |
Performs subtraction that returns |
Float | |
FromPrimitive |
A generic trait for converting a number to a value. |
Integer | |
Num |
The base trait for numeric types |
NumCast |
An interface for casting between machine scalars. |
One |
Defines a multiplicative identity element for |
PrimInt | |
Saturating |
Saturating math operations |
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 |
Functions
abs |
Computes the absolute value. |
abs_sub |
The positive difference of two numbers. |
checked_pow |
Raises a value to the power of exp, returning |
one |
Returns the multiplicative identity, |
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 |
range_step_inclusive |
Return an iterator over the range [start, stop] by |
signum |
Returns the sign of the number. |
zero |
Returns the additive identity, |
Type Definitions
BigRational |
Alias for arbitrary precision rationals. |
Rational |
Alias for a |