Enum fraction::GenericFraction
[−]
[src]
pub enum GenericFraction<T> where
T: Clone + Integer, { Rational(Sign, Ratio<T>), Infinity(Sign), NaN, }
Generic implementation of the fraction type
Even though it is implemented as enum
, you should not use enum variants explicitly. Use new
, new_*
and from
methods insdead.
Although there are two main specializations of this type (Fraction and BigFraction), you can easily define your own ones.
use fraction::GenericFraction; type F = GenericFraction<u8>; let first = F::new (1u8, 2u8); let second = F::new (2u8, 8u8); assert_eq! (first + second, F::new (3u8, 4u8));
Since GenericFraction keeps its sign explicitly and independently of the numerics, it is not recommended to use signed types, although it's completely valid with the cost of target type capacity.
use fraction::GenericFraction; type F = GenericFraction<i8>; let first = F::new (1, 2); let second = F::new (2, 8); assert_eq! (first + second, F::new (3, 4));
Variants
Rational(Sign, Ratio<T>)
Infinity(Sign)
NaN
Methods
impl<T: Clone + Integer> GenericFraction<T>
[src]
fn new<N, D>(num: N, den: D) -> GenericFraction<T> where
N: Into<T>,
D: Into<T>,
N: Into<T>,
D: Into<T>,
Constructs a new fraction with the specified numerator and denominator
The arguments must me either of T
type, or implement Into<T>
trait.
Examples
use fraction::GenericFraction; type F = GenericFraction<u16>; let f = F::new (1u8, 2u16);
fn new_neg<N, D>(num: N, den: D) -> GenericFraction<T> where
N: Into<T>,
D: Into<T>,
N: Into<T>,
D: Into<T>,
Constructs a new negative fraction with the specified numerator and denominator
The arguments must be either of T
type, or implement Into<T>
trait.
Examples
use fraction::GenericFraction; type F = GenericFraction<u16>; let f = F::new_neg (1u8, 2u16);
fn new_raw(num: T, den: T) -> GenericFraction<T>
Constructs a new fraction without types casting, checking for denom == 0 and reducing numbers.
You must be careful with this function because all the other functionality parts rely on the numbers to be reduced. That said, in the normal case 2/4 has to be reduced to 1/2, but it will not happen with new_raw.
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let f = F::new_raw (1u8, 2u8);
fn new_raw_neg(num: T, den: T) -> GenericFraction<T>
The same as fn new_raw, but produces negative fractions.
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let f = F::new_raw_neg (1u8, 2u8);
fn new_nan() -> GenericFraction<T>
Constructs NaN value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let nan = F::new_nan ();
fn new_inf() -> GenericFraction<T>
Constructs INF value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let nan = F::new_inf ();
fn new_inf_neg() -> GenericFraction<T>
Constructs negative INF value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let nan = F::new_inf_neg ();
fn numer(&self) -> Option<&T>
Returns a reference to the numerator value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let fra = F::new (5u8, 6u8); assert_eq! (5, *fra.numer ().unwrap ());
fn denom(&self) -> Option<&T>
Returns a reference to the denominator value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; let fra = F::new (5u8, 6u8); assert_eq! (6, *fra.denom ().unwrap ());
fn sign(&self) -> Option<&Sign>
Returns a reference to the sign value
Examples
use fraction::{ GenericFraction, Sign }; type F = GenericFraction<u8>; let fra = F::new (5u8, 6u8); assert_eq! (Sign::Plus, *fra.sign ().unwrap ()); let fra = F::new_neg (5u8, 6u8); assert_eq! (Sign::Minus, *fra.sign ().unwrap ()); let fra = F::new_inf (); assert_eq! (Sign::Plus, *fra.sign ().unwrap ()); let fra = F::new_inf_neg (); assert_eq! (Sign::Minus, *fra.sign ().unwrap ()); let fra = F::new_nan (); assert_eq! (None, fra.sign ());
fn into_big(self) -> BigFraction where
T: Into<BigUint>,
T: Into<BigUint>,
Generates a new BigFraction from the current one
Examples
use fraction::{ BigFraction, GenericFraction }; type F = GenericFraction<u8>; let fra = F::new (5u8, 6u8).into_big (); assert_eq! (BigFraction::new (5u8, 6u8), fra);
fn format_as_float(&self) -> Option<String> where
T: From<u8> + Into<BigUint> + ToPrimitive + Display,
T: From<u8> + Into<BigUint> + ToPrimitive + Display,
Returns a float representation of the fraction
If you have a fraction "1/2", in float it should be "0.5".
Since native floats can lose precision, we try to avoid converting to floats as long as possible. However, the current implementation is not ideal and still performs casting into native floats in case we have a "bad" number, like "1/3", "2/3", "5/6" etc. Please, feel free to PR fix if you feel power to solve this issue.
Returns None in case it's a "bad" number and numerator or denominator so big that cannot be converted into f64.
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! ("0.5", &F::new (1u8, 2u8).format_as_float ().unwrap ()); assert_eq! ("0.8", &F::new (8u8, 10u8).format_as_float ().unwrap ());
impl<T: Clone + Integer> GenericFraction<T>
[src]
fn nan() -> Self
Returns NaN value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::nan (), F::new (0, 0));
fn infinity() -> Self
Returns positive Infinity value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::infinity (), F::new (1, 0));
fn neg_infinity() -> Self
Returns negative Infinity value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::neg_infinity (), F::new_neg (1, 0));
fn neg_zero() -> Self
Returns zero with negative sign
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::neg_zero (), F::new_neg (0, 1));
fn min_positive_value() -> Self where
T: Bounded,
T: Bounded,
Returns minimal value greater than zero
Examples
use fraction::GenericFraction; type F8 = GenericFraction<u8>; type F16 = GenericFraction<u16>; assert_eq! (F8::min_positive_value (), F8::new (1u8, 255u8)); assert_eq! (F16::min_positive_value (), F16::new (1u16, 65535u16));
fn is_nan(&self) -> bool
Returns true if the value is NaN
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert! (F::nan ().is_nan ()); assert! (F::new (0, 0).is_nan ());
fn is_infinite(&self) -> bool
Returns true if the value is Infinity (does not matter positive or negative)
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert! (F::infinity ().is_infinite ()); assert! (F::new (1u8, 0).is_infinite ()); assert! (F::new_neg (1u8, 0).is_infinite ());
fn is_finite(&self) -> bool
Returns true if the value is not Infinity (does not matter positive or negative)
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert! (! F::infinity ().is_finite ()); assert! (! F::new (1u8, 0).is_finite ()); assert! (! F::new_neg (1u8, 0).is_finite ());
fn is_normal(&self) -> bool
Returns true if the number is neither zero, Infinity or NaN
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert! (! F::nan ().is_normal ()); assert! (! F::infinity ().is_normal ()); assert! (! F::neg_infinity ().is_normal ()); assert! (! F::new (0, 1u8).is_normal ()); assert! (! F::neg_zero ().is_normal ());
fn classify(&self) -> FpCategory
Returns the floating point category of the number
Examples
use std::num::FpCategory; use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::nan ().classify (), FpCategory::Nan); assert_eq! (F::infinity ().classify (), FpCategory::Infinite); assert_eq! (F::new (0, 1u8).classify (), FpCategory::Zero); assert_eq! (F::new (1u8, 1u8).classify (), FpCategory::Normal);
fn floor(&self) -> Self
Returns the largest integer less than or equal to the value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (7u8, 5u8).floor (), F::new (5u8, 5u8));
fn ceil(&self) -> Self
Returns the smallest integer greater than or equal to the value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (7u8, 5u8).ceil (), F::new (10u8, 5u8));
fn round(&self) -> Self
Returns the nearest integer to the value (.5 goes up)
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (7u8, 5u8).round (), F::new (5u8, 5u8)); assert_eq! (F::new (8u8, 5u8).round (), F::new (10u8, 5u8)); assert_eq! (F::new (3u8, 2u8).round (), F::new (4u8, 2u8)); assert_eq! (F::new (1u8, 2u8).round (), F::new (2u8, 2u8));
fn trunc(&self) -> Self
Returns the integer part of the value
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (7u8, 5u8).trunc (), F::new (5u8, 5u8)); assert_eq! (F::new (8u8, 5u8).trunc (), F::new (5u8, 5u8));
fn fract(&self) -> Self
Returns the fractional part of a number
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (7u8, 5u8).fract (), F::new (2u8, 5u8)); assert_eq! (F::new (8u8, 5u8).fract (), F::new (3u8, 5u8));
fn abs(&self) -> Self
Returns the absolute value of self
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::nan ().abs (), F::nan ()); assert_eq! (F::infinity ().abs (), F::infinity ()); assert_eq! (F::neg_infinity ().abs (), F::infinity ()); assert_eq! (F::new (1u8, 2u8).abs (), F::new (1u8, 2u8)); assert_eq! (F::new_neg (1u8, 2u8).abs (), F::new (1u8, 2u8));
fn signum(&self) -> Self
Returns a number that represents the sign of self
- 1.0 if the number is positive, +0.0 or INFINITY
- -1.0 if the number is negative, -0.0 or NEG_INFINITY
- NAN if the number is NAN
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (1u8, 2u8).signum (), F::new (1u8, 1u8)); assert_eq! (F::new (0u8, 1u8).signum (), F::new (1u8, 1u8)); assert_eq! (F::infinity ().signum (), F::new (1u8, 1u8)); assert_eq! (F::new_neg (1u8, 2u8).signum (), F::new_neg (1u8, 1u8)); assert_eq! (F::neg_zero ().signum (), F::new_neg (1u8, 1u8)); assert_eq! (F::neg_infinity ().signum (), F::new_neg (1u8, 1u8)); assert_eq! (F::nan ().signum (), F::nan ());
fn is_sign_positive(&self) -> bool
Returns true if the sign is positive
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert! (F::new (1u8, 2u8).is_sign_positive ()); assert! (F::infinity ().is_sign_positive ()); assert! (! F::nan ().is_sign_positive ());
fn is_sign_negative(&self) -> bool
Returns true if the sign is negative
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert! (F::new_neg (1u8, 2u8).is_sign_negative ()); assert! (F::neg_zero ().is_sign_negative ()); assert! (F::neg_infinity ().is_sign_negative ()); assert! (! F::nan ().is_sign_negative ());
fn mul_add(&self, a: Self, b: Self) -> Self
self.clone () * a + b
Added for interface compatibility with float types
fn recip(&self) -> Self
Takes the reciprocal (inverse) of the value (1/x)
Examples
use fraction::GenericFraction; type F = GenericFraction<u8>; assert_eq! (F::new (1u8, 2u8).recip (), F::new (2u8, 1u8)); assert_eq! (F::new (0u8, 1u8).recip (), F::infinity ()); assert_eq! (F::infinity ().recip (), F::new (0u8, 1u8)); assert_eq! (F::nan ().recip (), F::nan ());
Trait Implementations
impl<T: Clone> Clone for GenericFraction<T> where
T: Clone + Integer,
[src]
T: Clone + Integer,
fn clone(&self) -> GenericFraction<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: Hash> Hash for GenericFraction<T> where
T: Clone + Integer,
[src]
T: Clone + Integer,
fn hash<__HT: Hasher>(&self, __arg_0: &mut __HT)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T: Debug> Debug for GenericFraction<T> where
T: Clone + Integer,
[src]
T: Clone + Integer,
impl<T> Copy for GenericFraction<T> where
T: Copy + Integer,
[src]
T: Copy + Integer,
Copy semantics to be applied for the target type, but only if T also has it.
impl<T: Bounded + Clone + Integer> Bounded for GenericFraction<T>
[src]
fn min_value() -> Self
returns the smallest finite number this type can represent
fn max_value() -> Self
returns the largest finite number this type can represent
impl<T: Clone + Integer> PartialEq for GenericFraction<T>
[src]
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<T: Clone + Integer> Eq for GenericFraction<T>
[src]
impl<T: Clone + Integer> PartialOrd for GenericFraction<T>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<T: Clone + Integer> Neg for GenericFraction<T>
[src]
type Output = GenericFraction<T>
The resulting type after applying the -
operator
fn neg(self) -> Self
The method for the unary -
operator
impl<T: Clone + Integer> Add for GenericFraction<T>
[src]
type Output = Self
The resulting type after applying the +
operator
fn add(self, other: Self) -> Self
The method for the +
operator
impl<T: Clone + Integer> AddAssign for GenericFraction<T>
[src]
fn add_assign(&mut self, other: Self)
The method for the +=
operator
impl<T: Clone + Integer> Sub for GenericFraction<T>
[src]
type Output = Self
The resulting type after applying the -
operator
fn sub(self, other: Self) -> Self
The method for the -
operator
impl<T: Clone + Integer> SubAssign for GenericFraction<T>
[src]
fn sub_assign(&mut self, other: Self)
The method for the -=
operator
impl<T: Clone + Integer> Mul for GenericFraction<T>
[src]
type Output = Self
The resulting type after applying the *
operator
fn mul(self, other: Self) -> Self
The method for the *
operator
impl<T: Clone + Integer> MulAssign for GenericFraction<T>
[src]
fn mul_assign(&mut self, other: Self)
The method for the *=
operator
impl<T: Clone + Integer> Div for GenericFraction<T>
[src]
type Output = Self
The resulting type after applying the /
operator
fn div(self, other: Self) -> Self
The method for the /
operator
impl<T: Clone + Integer> DivAssign for GenericFraction<T>
[src]
fn div_assign(&mut self, other: Self)
The method for the /=
operator
impl<T: Clone + Integer> Rem for GenericFraction<T>
[src]
type Output = Self
The resulting type after applying the %
operator
fn rem(self, other: Self) -> Self
The method for the %
operator
impl<T: Clone + Integer> RemAssign for GenericFraction<T>
[src]
fn rem_assign(&mut self, other: Self)
The method for the %=
operator
impl<T: Clone + Integer> Zero for GenericFraction<T>
[src]
fn zero() -> Self
Returns the additive identity element of Self
, 0
. Read more
fn is_zero(&self) -> bool
Returns true
if self
is equal to the additive identity.
impl<T: Clone + Integer> One for GenericFraction<T>
[src]
impl<T: Clone + Integer> Num for GenericFraction<T>
[src]
type FromStrRadixErr = ParseRatioError
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
Convert from a string and radix <= 36. Read more
impl<T: Clone + Integer> Signed for GenericFraction<T>
[src]
fn abs(&self) -> Self
Computes the absolute value. Read more
fn abs_sub(&self, other: &Self) -> Self
The positive difference of two numbers. Read more
fn signum(&self) -> Self
Returns the sign of the number. Read more
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
impl<T: Clone + Integer + PartialEq + ToPrimitive> ToPrimitive for GenericFraction<T>
[src]
fn to_i64(&self) -> Option<i64>
Converts the value of self
to an i64
.
fn to_u64(&self) -> Option<u64>
Converts the value of self
to an u64
.
fn to_f64(&self) -> Option<f64>
Converts the value of self
to an f64
.
fn to_isize(&self) -> Option<isize>
Converts the value of self
to an isize
.
fn to_i8(&self) -> Option<i8>
Converts the value of self
to an i8
.
fn to_i16(&self) -> Option<i16>
Converts the value of self
to an i16
.
fn to_i32(&self) -> Option<i32>
Converts the value of self
to an i32
.
fn to_usize(&self) -> Option<usize>
Converts the value of self
to a usize
.
fn to_u8(&self) -> Option<u8>
Converts the value of self
to an u8
.
fn to_u16(&self) -> Option<u16>
Converts the value of self
to an u16
.
fn to_u32(&self) -> Option<u32>
Converts the value of self
to an u32
.
fn to_f32(&self) -> Option<f32>
Converts the value of self
to an f32
.
impl<T: Display + Eq + One + Clone + Integer> Display for GenericFraction<T>
[src]
impl<T: Clone + Integer> From<u8> for GenericFraction<T> where
u8: Into<T>,
[src]
u8: Into<T>,
fn from(val: u8) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<u16> for GenericFraction<T> where
u16: Into<T>,
[src]
u16: Into<T>,
fn from(val: u16) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<u32> for GenericFraction<T> where
u32: Into<T>,
[src]
u32: Into<T>,
fn from(val: u32) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<u64> for GenericFraction<T> where
u64: Into<T>,
[src]
u64: Into<T>,
fn from(val: u64) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<BigUint> for GenericFraction<T> where
BigUint: Into<T>,
[src]
BigUint: Into<T>,
fn from(val: BigUint) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<i8> for GenericFraction<T>
[src]
fn from(val: i8) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<i16> for GenericFraction<T>
[src]
fn from(val: i16) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<i32> for GenericFraction<T>
[src]
fn from(val: i32) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<i64> for GenericFraction<T>
[src]
fn from(val: i64) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<isize> for GenericFraction<T>
[src]
fn from(val: isize) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer> From<usize> for GenericFraction<T>
[src]
fn from(val: usize) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer + CheckedAdd + CheckedMul> From<f32> for GenericFraction<T>
[src]
fn from(val: f32) -> GenericFraction<T>
Performs the conversion.
impl<T: Clone + Integer + CheckedAdd + CheckedMul> From<f64> for GenericFraction<T>
[src]
fn from(val: f64) -> GenericFraction<T>
Performs the conversion.
impl<T> From<BigInt> for GenericFraction<T> where
T: Clone + Integer + From<BigUint>,
[src]
T: Clone + Integer + From<BigUint>,
impl<T, N, D> From<(N, D)> for GenericFraction<T> where
T: Clone + Integer,
N: Display,
D: Display,
[src]
T: Clone + Integer,
N: Display,
D: Display,
fn from(pair: (N, D)) -> GenericFraction<T>
Performs the conversion.