Struct rug::rational::SmallRational
[−]
[src]
#[repr(C)]pub struct SmallRational { /* fields omitted */ }
A small rational number that does not require any memory allocation.
This can be useful when you have a numerator and denominator that
are primitive integer-types such as i64
or u8
, and you need a
reference to a Rational
.
Although no allocation is required, setting the value of a
SmallRational
does require some computation, as the numerator
and denominator need to be canonicalized.
The SmallRational
type can be coerced to a
Rational
, as it implements
Deref<Target = Rational>
.
Examples
use rug::Rational; use rug::rational::SmallRational; // `a` requires a heap allocation let mut a = Rational::from((100, 13)); // `b` can reside on the stack let b = SmallRational::from((-100, 21)); a /= &*b; assert_eq!(*a.numer(), -21); assert_eq!(*a.denom(), 13);
Methods
impl SmallRational
[src]
fn new() -> Self
[src]
Creates a SmallRational
with value 0.
Examples
use rug::rational::SmallRational; let r = SmallRational::new(); // Use r as if it were Rational. assert_eq!(*r.numer(), 0); assert_eq!(*r.denom(), 1);
unsafe fn as_nonreallocating_rational(&mut self) -> &mut Rational
[src]
Returns a mutable reference to a
Rational
number for simple
operations that do not need to allocate more space for the
numerator or denominator.
Safety
It is undefined behaviour to perform operations that
reallocate the internal data of the referenced
Rational
number or to swap it
with another number.
Examples
use rug::rational::SmallRational; let mut r = SmallRational::from((-15i32, 47i32)); let num_capacity = r.numer().capacity(); let den_capacity = r.denom().capacity(); // reciprocating this will not require reallocations unsafe { r.as_nonreallocating_rational().recip_mut(); } assert_eq!(*r, (-47, 15)); assert_eq!(r.numer().capacity(), num_capacity); assert_eq!(r.denom().capacity(), den_capacity);
unsafe fn from_canonical<Num, Den>(num: Num, den: Den) -> Self where
SmallInteger: Assign<Num> + Assign<Den>,
[src]
SmallInteger: Assign<Num> + Assign<Den>,
Creates a SmallRational
from a numerator and denominator,
assuming they are in canonical form.
Safety
This method leads to undefined behaviour if den
is zero or
if num
and den
have common factors.
Examples
use rug::rational::SmallRational; let from_unsafe = unsafe { SmallRational::from_canonical(-13, 10) }; // from_safe is canonicalized to the same form as from_unsafe let from_safe = SmallRational::from((130, -100)); assert_eq!(from_unsafe.numer(), from_safe.numer()); assert_eq!(from_unsafe.denom(), from_safe.denom());
unsafe fn from_canonicalized_32(neg: bool, num: u32, den: u32) -> Self
[src]
: use from_canonical
instead; for example SmallRational::from_cananoicalized_32(true, 13, 10)
can be replaced with SmallRational::from_canonical(-13, 10)
.
Creates a SmallRational
from a 32-bit numerator and
denominator, assuming they are in canonical form.
unsafe fn from_canonicalized_64(neg: bool, num: u64, den: u64) -> Self
[src]
: use from_canonical
instead; for example SmallRational::from_cananoicalized_64(true, 13, 10)
can be replaced with SmallRational::from_canonical(-13, 10)
.
Creates a SmallRational
from a 64-bit numerator and
denominator, assuming they are in canonical form.
unsafe fn assign_canonicalized_32(&mut self, neg: bool, num: u32, den: u32)
[src]
: use as_nonreallocating_rational
and assign_canonical
instead; for example r.assign_canonicalized_32(true, 13u32, 10u32)
can be replaced with r.as_nonreallocating_rational().assign_canonical(-13i32, 10u32)
.
Sets a SmallRational
to a 32-bit numerator and denominator,
assuming they are in canonical form.
unsafe fn assign_canonicalized_64(&mut self, neg: bool, num: u64, den: u64)
[src]
: use as_nonreallocating_rational
and assign_canonical
instead; for example r.assign_canonicalized_32(true, 13u64, 10u64)
can be replaced with r.as_nonreallocating_rational().assign_canonical(-13i64, 10u64)
.
Sets a SmallRational
to a 64-bit numerator and denominator,
assuming they are in canonical form.
Methods from Deref<Target = Rational>
fn to_integer(&self) -> Integer
[src]
Converts to an Integer
, rounding
towards zero.
Note that this method does not consume self
, and allocates a
new Integer
. If self
can be consumed, you should use
trunc
instead.
Examples
use rug::Rational; let pos = Rational::from((139, 10)); let posi = pos.to_integer(); assert_eq!(posi, 13); let neg = Rational::from((-139, 10)); let negi = neg.to_integer(); assert_eq!(negi, -13);
fn copy_to_integer(&self, i: &mut Integer)
[src]
: use trunc_ref
instead; r.copy_to_integer(&mut i)
can be replaced with i.assign(r.trunc_ref())
.
Converts to an Integer
inside i
,
rounding towards zero.
fn to_f32(&self) -> f32
[src]
Converts to an f32
, rounding towards zero.
Examples
use rug::Rational; use rug::rational::SmallRational; use std::f32; let min = Rational::from_f32(f32::MIN).unwrap(); let minus_small = min - &*SmallRational::from((7, 2)); // minus_small is truncated to f32::MIN assert_eq!(minus_small.to_f32(), f32::MIN); let times_three_two = minus_small * &*SmallRational::from((3, 2)); // times_three_two is too small assert_eq!(times_three_two.to_f32(), f32::NEG_INFINITY);
fn to_f64(&self) -> f64
[src]
Converts to an f64
, rounding towards zero.
Examples
use rug::Rational; use rug::rational::SmallRational; use std::f64; // An `f64` has 53 bits of precision. let exact = 0x1f_1234_5678_9aff_u64; let den = 0x1000_u64; let r = Rational::from((exact, den)); assert_eq!(r.to_f64(), exact as f64 / den as f64); // large has 56 ones let large = 0xff_1234_5678_9aff_u64; // trunc has 53 ones followed by 3 zeros let trunc = 0xff_1234_5678_9af8_u64; let j = Rational::from((large, den)); assert_eq!(j.to_f64(), trunc as f64 / den as f64); let max = Rational::from_f64(f64::MAX).unwrap(); let plus_small = max + &*SmallRational::from((7, 2)); // plus_small is truncated to f64::MAX assert_eq!(plus_small.to_f64(), f64::MAX); let times_three_two = plus_small * &*SmallRational::from((3, 2)); // times_three_two is too large assert_eq!(times_three_two.to_f64(), f64::INFINITY);
fn to_string_radix(&self, radix: i32) -> String
[src]
Returns a string representation for the specified radix
.
Examples
use rug::Rational; let r1 = Rational::from(0); assert_eq!(r1.to_string_radix(10), "0"); let r2 = Rational::from((15, 5)); assert_eq!(r2.to_string_radix(10), "3"); let r3 = Rational::from((10, -6)); assert_eq!(r3.to_string_radix(10), "-5/3"); assert_eq!(r3.to_string_radix(5), "-10/3");
Panics
Panics if radix
is less than 2 or greater than 36.
fn into_raw(self) -> mpq_t
[src]
Converts a Rational
into a GMP rational number.
The returned object should be freed to avoid memory leaks.
Examples
extern crate gmp_mpfr_sys; extern crate rug; use gmp_mpfr_sys::gmp; use rug::Rational; fn main() { let r = Rational::from((-145, 10)); let mut q = r.into_raw(); unsafe { let d = gmp::mpq_get_d(&q); assert_eq!(d, -14.5); // free object to prevent memory leak gmp::mpq_clear(&mut q); } }
fn as_raw(&self) -> *const mpq_t
[src]
Returns a pointer to the internal GMP rational number.
The returned pointer will be valid for as long as self
is
valid.
Examples
extern crate gmp_mpfr_sys; extern crate rug; use gmp_mpfr_sys::gmp; use rug::Rational; fn main() { let r = Rational::from((-145, 10)); let q_ptr = r.as_raw(); unsafe { let d = gmp::mpq_get_d(q_ptr); assert_eq!(d, -14.5); } // r is still valid assert_eq!(r, (-145, 10)); }
fn numer(&self) -> &Integer
[src]
Borrows the numerator as an Integer
.
Examples
use rug::Rational; let r = Rational::from((12, -20)); // r will be canonicalized to -3 / 5 assert_eq!(*r.numer(), -3)
fn denom(&self) -> &Integer
[src]
Borrows the denominator as an
Integer
.
Examples
use rug::Rational; let r = Rational::from((12, -20)); // r will be canonicalized to -3 / 5 assert_eq!(*r.denom(), 5);
fn as_numer_denom(&self) -> (&Integer, &Integer)
[src]
Borrows the numerator and denominator as
Integer
values.
Examples
use rug::Rational; let r = Rational::from((12, -20)); // r will be canonicalized to -3 / 5 let (num, den) = r.as_numer_denom(); assert_eq!(*num, -3); assert_eq!(*den, 5);
fn into_numer_denom(self) -> (Integer, Integer)
[src]
Converts into numerator and denominator integers.
This function reuses the allocated memory and does not allocate any new memory.
Examples
use rug::Rational; let r = Rational::from((12, -20)); // r will be canonicalized to -3 / 5 let (num, den) = r.into_numer_denom(); assert_eq!(num, -3); assert_eq!(den, 5);
fn as_neg(&self) -> BorrowRational
[src]
Borrows a negated copy of the Rational
number.
The returned object implements Deref<Target = Rational>
.
This method performs a shallow copy and negates it, and negation does not change the allocated data.
Examples
use rug::Rational; let r = Rational::from((7, 11)); let neg_r = r.as_neg(); assert_eq!(*neg_r, (-7, 11)); // methods taking &self can be used on the returned object let reneg_r = neg_r.as_neg(); assert_eq!(*reneg_r, (7, 11)); assert_eq!(*reneg_r, r);
fn as_abs(&self) -> BorrowRational
[src]
Borrows an absolute copy of the Rational
number.
The returned object implements Deref<Target = Rational>
.
This method performs a shallow copy and possibly negates it, and negation does not change the allocated data.
Examples
use rug::Rational; let r = Rational::from((-7, 11)); let abs_r = r.as_abs(); assert_eq!(*abs_r, (7, 11)); // methods taking &self can be used on the returned object let reabs_r = abs_r.as_abs(); assert_eq!(*reabs_r, (7, 11)); assert_eq!(*reabs_r, *abs_r);
fn as_recip(&self) -> BorrowRational
[src]
Borrows a reciprocal copy of the Rational
number.
The returned object implements Deref<Target = Rational>
.
This method performs some shallow copying, swapping numerator and denominator and making sure the sign is in the numerator.
Examples
use rug::Rational; let r = Rational::from((-7, 11)); let recip_r = r.as_recip(); assert_eq!(*recip_r, (-11, 7)); // methods taking &self can be used on the returned object let rerecip_r = recip_r.as_recip(); assert_eq!(*rerecip_r, (-7, 11)); assert_eq!(*rerecip_r, r);
Panics
Panics if the value is zero.
fn cmp0(&self) -> Ordering
[src]
Returns the same result as self.partial_cmp(&0).unwrap()
,
but is faster.
Examples
use rug::Rational; use std::cmp::Ordering; assert_eq!(Rational::from((-5, 7)).cmp0(), Ordering::Less); assert_eq!(Rational::from(0).cmp0(), Ordering::Equal); assert_eq!(Rational::from((5, 7)).cmp0(), Ordering::Greater);
fn cmp_abs(&self, other: &Self) -> Ordering
[src]
Compares the absolute values.
Examples
use rug::Rational; use std::cmp::Ordering; let a = Rational::from((-23, 10)); let b = Rational::from((-47, 5)); assert_eq!(a.cmp(&b), Ordering::Greater); assert_eq!(a.cmp_abs(&b), Ordering::Less);
fn abs(self) -> Self
[src]
Computes the absolute value.
Examples
use rug::Rational; let r = Rational::from((-100, 17)); let abs = r.abs(); assert_eq!(abs, (100, 17));
fn abs_ref(&self) -> AbsRef
[src]
Computes the absolute value.
Assign<Src> for Rational
and From<Src> for Rational
are implemented with the returned object as Src
.
Examples
use rug::Rational; let r = Rational::from((-100, 17)); let r_ref = r.abs_ref(); let abs = Rational::from(r_ref); assert_eq!(abs, (100, 17));
fn signum(self) -> Integer
[src]
Computes the signum.
- 0 if the value is zero
- 1 if the value is positive
- −1 if the value is negative
Examples
use rug::Rational; let r = Rational::from((-100, 17)); let signum = r.signum(); assert_eq!(signum, -1);
fn signum_ref(&self) -> SignumRef
[src]
Computes the signum.
- 0 if the value is zero
- 1 if the value is positive
- −1 if the value is negative
Examples
Assign<Src> for Integer
and From<Src> for Integer
are
implemented with the returned object as Src
.
use rug::{Integer, Rational}; let r = Rational::from((-100, 17)); let r_ref = r.signum_ref(); let signum = Integer::from(r_ref); assert_eq!(signum, -1);
fn clamp<'a, 'b, Min, Max>(self, min: &'a Min, max: &'b Max) -> Self where
Self: PartialOrd<Min> + PartialOrd<Max> + Assign<&'a Min> + Assign<&'b Max>,
[src]
Self: PartialOrd<Min> + PartialOrd<Max> + Assign<&'a Min> + Assign<&'b Max>,
Clamps the value within the specified bounds.
Examples
use rug::Rational; let min = (-3, 2); let max = (3, 2); let too_small = Rational::from((-5, 2)); let clamped1 = too_small.clamp(&min, &max); assert_eq!(clamped1, (-3, 2)); let in_range = Rational::from((1, 2)); let clamped2 = in_range.clamp(&min, &max); assert_eq!(clamped2, (1, 2));
Panics
Panics if the maximum value is less than the minimum value.
fn clamp_ref<'a, Min, Max>(
&'a self,
min: &'a Min,
max: &'a Max
) -> ClampRef<'a, Min, Max> where
Self: PartialOrd<Min> + PartialOrd<Max> + Assign<&'a Min> + Assign<&'a Max>,
[src]
&'a self,
min: &'a Min,
max: &'a Max
) -> ClampRef<'a, Min, Max> where
Self: PartialOrd<Min> + PartialOrd<Max> + Assign<&'a Min> + Assign<&'a Max>,
Clamps the value within the specified bounds.
Assign<Src> for Rational
and From<Src> for Rational
are
implemented with the returned object as Src
.
Examples
use rug::Rational; let min = (-3, 2); let max = (3, 2); let too_small = Rational::from((-5, 2)); let r1 = too_small.clamp_ref(&min, &max); let clamped1 = Rational::from(r1); assert_eq!(clamped1, (-3, 2)); let in_range = Rational::from((1, 2)); let r2 = in_range.clamp_ref(&min, &max); let clamped2 = Rational::from(r2); assert_eq!(clamped2, (1, 2));
Panics
Panics if the maximum value is less than the minimum value.
fn recip(self) -> Self
[src]
Computes the reciprocal.
Examples
use rug::Rational; let r = Rational::from((-100, 17)); let recip = r.recip(); assert_eq!(recip, (-17, 100));
Panics
Panics if the value is zero.
fn recip_ref(&self) -> RecipRef
[src]
Computes the reciprocal.
Assign<Src> for Rational
and From<Src> for Rational
are implemented with the returned object as Src
.
Examples
use rug::Rational; let r = Rational::from((-100, 17)); let r_ref = r.recip_ref(); let recip = Rational::from(r_ref); assert_eq!(recip, (-17, 100));
fn trunc(self) -> Integer
[src]
Rounds the number towards zero and returns it as an
Integer
.
Examples
use rug::Rational; // -3.7 let r1 = Rational::from((-37, 10)); let i1 = r1.trunc(); assert_eq!(i1, -3); // 3.3 let r2 = Rational::from((33, 10)); let i2 = r2.trunc(); assert_eq!(i2, 3);
fn trunc_ref(&self) -> TruncRef
[src]
Rounds the number towards zero.
Assign<Src> for Integer
and From<Src> for Integer
are
implemented with the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; let mut trunc = Integer::new(); // -3.7 let r1 = Rational::from((-37, 10)); trunc.assign(r1.trunc_ref()); assert_eq!(trunc, -3); // 3.3 let r2 = Rational::from((33, 10)); trunc.assign(r2.trunc_ref()); assert_eq!(trunc, 3);
fn fract(self) -> Self
[src]
: renamed to rem_trunc
Computes the fractional part of the number.
fn fract_ref(&self) -> RemTruncRef
[src]
: renamed to rem_trunc_ref
Computes the fractional part of the number.
fn rem_trunc(self) -> Self
[src]
Computes the fractional part of the number.
Examples
use rug::Rational; // -100/17 = -5 - 15/17 let r = Rational::from((-100, 17)); let rem = r.rem_trunc(); assert_eq!(rem, (-15, 17));
fn rem_trunc_ref(&self) -> RemTruncRef
[src]
Computes the fractional part of the number.
Assign<Src> for Rational
and From<Src> for Rational
are implemented with the returned object as Src
.
Examples
use rug::Rational; // -100/17 = -5 - 15/17 let r = Rational::from((-100, 17)); let r_ref = r.rem_trunc_ref(); let rem = Rational::from(r_ref); assert_eq!(rem, (-15, 17));
fn fract_trunc(self, trunc: Integer) -> (Self, Integer)
[src]
Computes the fractional and truncated parts of the number.
The initial value of trunc
is ignored.
Examples
use rug::{Integer, Rational}; // -100/17 = -5 - 15/17 let r = Rational::from((-100, 17)); let (fract, trunc) = r.fract_trunc(Integer::new()); assert_eq!(fract, (-15, 17)); assert_eq!(trunc, -5);
fn fract_trunc_ref(&self) -> FractTruncRef
[src]
Computes the fractional and truncated parts of the number.
Assign<Src> for (Rational, Integer)
,
Assign<Src> for (&mut Rational, &mut Integer)
and
From<Src> for (Rational, Integer)
are implemented with
the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; // -100/17 = -5 - 15/17 let r = Rational::from((-100, 17)); let r_ref = r.fract_trunc_ref(); let (mut fract, mut trunc) = (Rational::new(), Integer::new()); (&mut fract, &mut trunc).assign(r_ref); assert_eq!(fract, (-15, 17)); assert_eq!(trunc, -5);
fn ceil(self) -> Integer
[src]
Rounds the number upwards (towards plus infinity) and returns
it as an Integer
.
Examples
use rug::Rational; // -3.7 let r1 = Rational::from((-37, 10)); let i1 = r1.ceil(); assert_eq!(i1, -3); // 3.3 let r2 = Rational::from((33, 10)); let i2 = r2.ceil(); assert_eq!(i2, 4);
fn ceil_ref(&self) -> CeilRef
[src]
Rounds the number upwards (towards plus infinity).
Assign<Src> for Integer
and From<Src> for Integer
are
implemented with the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; let mut ceil = Integer::new(); // -3.7 let r1 = Rational::from((-37, 10)); ceil.assign(r1.ceil_ref()); assert_eq!(ceil, -3); // 3.3 let r2 = Rational::from((33, 10)); ceil.assign(r2.ceil_ref()); assert_eq!(ceil, 4);
fn rem_ceil(self) -> Self
[src]
Computes the non-positive remainder after rounding up.
Examples
use rug::Rational; // 100/17 = 6 - 2/17 let r = Rational::from((100, 17)); let rem = r.rem_ceil(); assert_eq!(rem, (-2, 17));
fn rem_ceil_ref(&self) -> RemCeilRef
[src]
Computes the non-positive remainder after rounding up.
Assign<Src> for Rational
and From<Src> for Rational
are implemented with the returned object as Src
.
Examples
use rug::Rational; // 100/17 = 6 - 2/17 let r = Rational::from((100, 17)); let r_ref = r.rem_ceil_ref(); let rem = Rational::from(r_ref); assert_eq!(rem, (-2, 17));
fn fract_ceil(self, ceil: Integer) -> (Self, Integer)
[src]
Computes the fractional and ceil parts of the number.
The fractional part cannot greater than zero. The initial
value of ceil
is ignored.
Examples
use rug::{Integer, Rational}; // 100/17 = 6 - 2/17 let r = Rational::from((100, 17)); let (fract, ceil) = r.fract_ceil(Integer::new()); assert_eq!(fract, (-2, 17)); assert_eq!(ceil, 6);
fn fract_ceil_ref(&self) -> FractCeilRef
[src]
Computes the fractional and ceil parts of the number.
The fractional part cannot be greater than zero.
Assign<Src> for (Rational, Integer)
,
Assign<Src> for (&mut Rational, &mut Integer)
and
From<Src> for (Rational, Integer)
are implemented with
the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; // 100/17 = 6 - 2/17 let r = Rational::from((100, 17)); let r_ref = r.fract_ceil_ref(); let (mut fract, mut ceil) = (Rational::new(), Integer::new()); (&mut fract, &mut ceil).assign(r_ref); assert_eq!(fract, (-2, 17)); assert_eq!(ceil, 6);
fn floor(self) -> Integer
[src]
Rounds the number downwards (towards minus infinity) and
returns it as an Integer
.
Examples
use rug::Rational; // -3.7 let r1 = Rational::from((-37, 10)); let i1 = r1.floor(); assert_eq!(i1, -4); // 3.3 let r2 = Rational::from((33, 10)); let i2 = r2.floor(); assert_eq!(i2, 3);
fn floor_ref(&self) -> FloorRef
[src]
Rounds the number downwards (towards minus infinity).
Assign<Src> for Integer
and From<Src> for Integer
are
implemented with the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; let mut floor = Integer::new(); // -3.7 let r1 = Rational::from((-37, 10)); floor.assign(r1.floor_ref()); assert_eq!(floor, -4); // 3.3 let r2 = Rational::from((33, 10)); floor.assign(r2.floor_ref()); assert_eq!(floor, 3);
fn rem_floor(self) -> Self
[src]
Computes the non-negative remainder after rounding down.
Examples
use rug::Rational; // -100/17 = -6 + 2/17 let r = Rational::from((-100, 17)); let rem = r.rem_floor(); assert_eq!(rem, (2, 17));
fn rem_floor_ref(&self) -> RemFloorRef
[src]
Computes the non-negative remainder after rounding down.
Assign<Src> for Rational
and From<Src> for Rational
are implemented with the returned object as Src
.
Examples
use rug::Rational; // -100/17 = -6 + 2/17 let r = Rational::from((-100, 17)); let r_ref = r.rem_floor_ref(); let rem = Rational::from(r_ref); assert_eq!(rem, (2, 17));
fn fract_floor(self, floor: Integer) -> (Self, Integer)
[src]
Computes the fractional and floor parts of the number.
The fractional part cannot be negative. The initial value of
floor
is ignored.
Examples
use rug::{Integer, Rational}; // -100/17 = -6 + 2/17 let r = Rational::from((-100, 17)); let (fract, floor) = r.fract_floor(Integer::new()); assert_eq!(fract, (2, 17)); assert_eq!(floor, -6);
fn fract_floor_ref(&self) -> FractFloorRef
[src]
Computes the fractional and floor parts of the number.
The fractional part cannot be negative.
Assign<Src> for (Rational, Integer)
,
Assign<Src> for (&mut Rational, &mut Integer)
and
From<Src> for (Rational, Integer)
are implemented with
the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; // -100/17 = -6 + 2/17 let r = Rational::from((-100, 17)); let r_ref = r.fract_floor_ref(); let (mut fract, mut floor) = (Rational::new(), Integer::new()); (&mut fract, &mut floor).assign(r_ref); assert_eq!(fract, (2, 17)); assert_eq!(floor, -6);
fn round(self) -> Integer
[src]
Rounds the number to the nearest integer and returns it as an
Integer
.
When the number lies exactly between two integers, it is rounded away from zero.
Examples
use rug::Rational; // -3.5 let r1 = Rational::from((-35, 10)); let i1 = r1.round(); assert_eq!(i1, -4); // 3.7 let r2 = Rational::from((37, 10)); let i2 = r2.round(); assert_eq!(i2, 4);
fn round_ref(&self) -> RoundRef
[src]
Rounds the number to the nearest integer.
When the number lies exactly between two integers, it is rounded away from zero.
Assign<Src> for Integer
and From<Src> for Integer
are
implemented with the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; let mut round = Integer::new(); // -3.5 let r1 = Rational::from((-35, 10)); round.assign(r1.round_ref()); assert_eq!(round, -4); // 3.7 let r2 = Rational::from((37, 10)); round.assign(r2.round_ref()); assert_eq!(round, 4);
fn rem_round(self) -> Self
[src]
Computes the remainder after rounding to the nearest integer.
Examples
use rug::Rational; // -3.5 = -4 + 0.5 = -4 + 1/2 let r1 = Rational::from((-35, 10)); let rem1 = r1.rem_round(); assert_eq!(rem1, (1, 2)); // 3.7 = 4 - 0.3 = 4 - 3/10 let r2 = Rational::from((37, 10)); let rem2 = r2.rem_round(); assert_eq!(rem2, (-3, 10));
fn rem_round_ref(&self) -> RemRoundRef
[src]
Computes the remainder after rounding to the nearest integer.
Assign<Src> for Rational
and From<Src> for Rational
are implemented with the returned object as Src
.
Examples
use rug::Rational; // -3.5 = -4 + 0.5 = -4 + 1/2 let r1 = Rational::from((-35, 10)); let r_ref1 = r1.rem_round_ref(); let rem1 = Rational::from(r_ref1); assert_eq!(rem1, (1, 2)); // 3.7 = 4 - 0.3 = 4 - 3/10 let r2 = Rational::from((37, 10)); let r_ref2 = r2.rem_round_ref(); let rem2 = Rational::from(r_ref2); assert_eq!(rem2, (-3, 10));
fn fract_round(self, round: Integer) -> (Self, Integer)
[src]
Computes the fractional and rounded parts of the number.
The fractional part is positive when the number is rounded down and negative when the number is rounded up. When the number lies exactly between two integers, it is rounded away from zero.
Examples
use rug::{Integer, Rational}; // -3.5 = -4 + 0.5 = -4 + 1/2 let r1 = Rational::from((-35, 10)); let (fract1, round1) = r1.fract_round(Integer::new()); assert_eq!(fract1, (1, 2)); assert_eq!(round1, -4); // 3.7 = 4 - 0.3 = 4 - 3/10 let r2 = Rational::from((37, 10)); let (fract2, round2) = r2.fract_round(Integer::new()); assert_eq!(fract2, (-3, 10)); assert_eq!(round2, 4);
fn fract_round_ref(&self) -> FractRoundRef
[src]
Computes the fractional and round parts of the number.
The fractional part is positive when the number is rounded down and negative when the number is rounded up. When the number lies exactly between two integers, it is rounded away from zero.
Assign<Src> for (Rational, Integer)
,
Assign<Src> for (&mut Rational, &mut Integer)
and
From<Src> for (Rational, Integer)
are implemented with
the returned object as Src
.
Examples
use rug::{Assign, Integer, Rational}; // -3.5 = -4 + 0.5 = -4 + 1/2 let r1 = Rational::from((-35, 10)); let r_ref1 = r1.fract_round_ref(); let (mut fract1, mut round1) = (Rational::new(), Integer::new()); (&mut fract1, &mut round1).assign(r_ref1); assert_eq!(fract1, (1, 2)); assert_eq!(round1, -4); // 3.7 = 4 - 0.3 = 4 - 3/10 let r2 = Rational::from((37, 10)); let r_ref2 = r2.fract_round_ref(); let (mut fract2, mut round2) = (Rational::new(), Integer::new()); (&mut fract2, &mut round2).assign(r_ref2); assert_eq!(fract2, (-3, 10)); assert_eq!(round2, 4);
fn square(self) -> Self
[src]
Computes the square.
Examples
use rug::Rational; let r = Rational::from((-13, 2)); let square = r.square(); assert_eq!(square, (169, 4));
fn square_ref(&self) -> SquareRef
[src]
Computes the square.
Assign<Src> for Rational
and From<Src> for Rational
are
implemented with the returned object as Src
.
Examples
use rug::Rational; let r = Rational::from((-13, 2)); assert_eq!(Rational::from(r.square_ref()), (169, 4));
Trait Implementations
impl Clone for SmallRational
[src]
fn clone(&self) -> SmallRational
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Default for SmallRational
[src]
impl Deref for SmallRational
[src]
type Target = Rational
The resulting type after dereferencing.
fn deref(&self) -> &Rational
[src]
Dereferences the value.
impl<Num> Assign<Num> for SmallRational where
SmallInteger: Assign<Num>,
[src]
SmallInteger: Assign<Num>,
impl<Num> From<Num> for SmallRational where
SmallInteger: Assign<Num>,
[src]
SmallInteger: Assign<Num>,
impl<Num, Den> Assign<(Num, Den)> for SmallRational where
SmallInteger: Assign<Num> + Assign<Den>,
[src]
SmallInteger: Assign<Num> + Assign<Den>,
impl<Num, Den> From<(Num, Den)> for SmallRational where
SmallInteger: Assign<Num> + Assign<Den>,
[src]
SmallInteger: Assign<Num> + Assign<Den>,