Enum fraction::GenericFraction [] [src]

pub enum GenericFraction<T> where
    T: Clone + Integer
{ Rational(SignRatio<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

Methods

impl<T: Clone + Integer> GenericFraction<T>
[src]

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);

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);

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);

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);

Constructs NaN value

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

let nan = F::new_nan ();

Constructs INF value

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

let nan = F::new_inf ();

Constructs negative INF value

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

let nan = F::new_inf_neg ();

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 ());

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 ());

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 ());

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);

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]

Returns NaN value

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

assert_eq! (F::nan (), F::new (0, 0));

Returns positive Infinity value

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

assert_eq! (F::infinity (), F::new (1, 0));

Returns negative Infinity value

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

assert_eq! (F::neg_infinity (), F::new_neg (1, 0));

Returns zero with negative sign

Examples

use fraction::GenericFraction;
type F = GenericFraction<u8>;

assert_eq! (F::neg_zero (), F::new_neg (0, 1));

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));

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 ());

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 ());

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 ());

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 ());

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);

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));

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));

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));

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));

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));

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));

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 ());

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 ());

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 ());

self.clone () * a + b

Added for interface compatibility with float types

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]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: Hash> Hash for GenericFraction<T> where
    T: Clone + Integer
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<T: Debug> Debug for GenericFraction<T> where
    T: Clone + Integer
[src]

Formats the value using the given formatter.

impl<T> Copy for GenericFraction<T> where
    T: Copy + Integer
[src]

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]

returns the smallest finite number this type can represent

returns the largest finite number this type can represent

impl<T: Clone + Integer> PartialEq for GenericFraction<T>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Clone + Integer> Eq for GenericFraction<T>
[src]

impl<T: Clone + Integer> PartialOrd for GenericFraction<T>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

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]

The resulting type after applying the - operator

The method for the unary - operator

impl<T: Clone + Integer> Add for GenericFraction<T>
[src]

The resulting type after applying the + operator

The method for the + operator

impl<T: Clone + Integer> AddAssign for GenericFraction<T>
[src]

The method for the += operator

impl<T: Clone + Integer> Sub for GenericFraction<T>
[src]

The resulting type after applying the - operator

The method for the - operator

impl<T: Clone + Integer> SubAssign for GenericFraction<T>
[src]

The method for the -= operator

impl<T: Clone + Integer> Mul for GenericFraction<T>
[src]

The resulting type after applying the * operator

The method for the * operator

impl<T: Clone + Integer> MulAssign for GenericFraction<T>
[src]

The method for the *= operator

impl<T: Clone + Integer> Div for GenericFraction<T>
[src]

The resulting type after applying the / operator

The method for the / operator

impl<T: Clone + Integer> DivAssign for GenericFraction<T>
[src]

The method for the /= operator

impl<T: Clone + Integer> Rem for GenericFraction<T>
[src]

The resulting type after applying the % operator

The method for the % operator

impl<T: Clone + Integer> RemAssign for GenericFraction<T>
[src]

The method for the %= operator

impl<T: Clone + Integer> Zero for GenericFraction<T>
[src]

Returns the additive identity element of Self, 0. Read more

Returns true if self is equal to the additive identity.

impl<T: Clone + Integer> One for GenericFraction<T>
[src]

Returns the multiplicative identity element of Self, 1. Read more

impl<T: Clone + Integer> Num for GenericFraction<T>
[src]

Convert from a string and radix <= 36. Read more

impl<T: Clone + Integer> Signed for GenericFraction<T>
[src]

Computes the absolute value. Read more

The positive difference of two numbers. Read more

Returns the sign of the number. Read more

Returns true if the number is positive and false if the number is zero or negative.

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]

Converts the value of self to an i64.

Converts the value of self to an u64.

Converts the value of self to an f64.

Converts the value of self to an isize.

Converts the value of self to an i8.

Converts the value of self to an i16.

Converts the value of self to an i32.

Converts the value of self to a usize.

Converts the value of self to an u8.

Converts the value of self to an u16.

Converts the value of self to an u32.

Converts the value of self to an f32.

impl<T: Display + Eq + One + Clone + Integer> Display for GenericFraction<T>
[src]

Formats the value using the given formatter. Read more

impl<T: Clone + Integer> From<u8> for GenericFraction<T> where
    u8: Into<T>, 
[src]

Performs the conversion.

impl<T: Clone + Integer> From<u16> for GenericFraction<T> where
    u16: Into<T>, 
[src]

Performs the conversion.

impl<T: Clone + Integer> From<u32> for GenericFraction<T> where
    u32: Into<T>, 
[src]

Performs the conversion.

impl<T: Clone + Integer> From<u64> for GenericFraction<T> where
    u64: Into<T>, 
[src]

Performs the conversion.

impl<T: Clone + Integer> From<BigUint> for GenericFraction<T> where
    BigUint: Into<T>, 
[src]

Performs the conversion.

impl<T: Clone + Integer> From<i8> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer> From<i16> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer> From<i32> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer> From<i64> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer> From<isize> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer> From<usize> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer + CheckedAdd + CheckedMul> From<f32> for GenericFraction<T>
[src]

Performs the conversion.

impl<T: Clone + Integer + CheckedAdd + CheckedMul> From<f64> for GenericFraction<T>
[src]

Performs the conversion.

impl<T> From<BigInt> for GenericFraction<T> where
    T: Clone + Integer + From<BigUint>, 
[src]

Performs the conversion.

impl<T, N, D> From<(N, D)> for GenericFraction<T> where
    T: Clone + Integer,
    N: Display,
    D: Display
[src]

Performs the conversion.