[go: up one dir, main page]

Struct fraction::Fraction [] [src]

pub struct Fraction {
    // some fields omitted
}

Fraction is the main structure that keeps number representation internally.

Possible options for instatiation are as follows:

  • Fraction::new - creates a number with custom numerator and denominator
  • Fraction::zero - creates a new zero value (0/1)
  • Fraction::one - creates a new value (1/1)
  • Fraction::from - converts a native rust number into a Fraction

Possible options for extracting the value:

  • Fraction::to_f64 - returns a native f64 representation of the value
  • Fraction::unpack - returns a tuple with (sign, numerator, denominator) values

Examples

Instantiation

use fraction::Fraction;

assert_eq! (
    Fraction::zero (), // 0/1
    Fraction::from (0) // 0/1
);

assert_eq! (
    Fraction::one (),  // 1/1
    Fraction::from (1) // 1/1
);

assert_eq! (
    Fraction::new  (1, 5),     // 1/5
    Fraction::from (1.0 / 5.0) // 1/5
);

Extracting

use fraction::Fraction;

assert_eq! (
    Fraction::new (3, 4).unpack (),
    (true, 3, 4)
);

assert_eq! (
    Fraction::new (3, 4).to_f64 (),
    0.75
);

Methods

impl Fraction
[src]

fn new(numerator: u64, denominator: u64) -> Fraction

Creates a new fraction with custom numerator and denominator

Examples

use fraction::Fraction;

Fraction::new (1, 1); // 1/1
Fraction::new (1, 2); // 1/2

fn zero() -> Fraction

Creates a new fraction with value 0/1

Examples

use fraction::Fraction;

let zero = Fraction::zero (); // = 0

assert_eq! (zero, Fraction::new (0, 1));

fn one() -> Fraction

Creates a new fraction with value 1/1

Examples

use fraction::Fraction;

let one = Fraction::one (); // = 1

assert_eq! (one, Fraction::new (1, 1));

fn nan() -> Fraction

Creates a new fraction with value NaN

Examples

use fraction::Fraction;
use std::f32;

let nan = Fraction::nan ();

assert_eq! (nan, Fraction::new (1, 0)); // 1/0 is NaN
assert_eq! (nan, Fraction::from (f32::NAN));

fn inf() -> Fraction

Creates a new fraction with value INF

Examples

use fraction::Fraction;
use std::f32;

let inf = Fraction::inf ();

assert_eq! (inf, Fraction::from (f32::INFINITY));
assert_eq! (-inf, Fraction::from (f32::NEG_INFINITY));

fn neg_inf() -> Fraction

Creates a new fraction with value -INF

Examples

use fraction::Fraction;
use std::f32;

let inf = Fraction::neg_inf ();

assert_eq! (inf, Fraction::from (f32::NEG_INFINITY));
assert_eq! (-inf, Fraction::from (f32::INFINITY));

fn unpack(&self) -> (bool, u64, u64)

Unpacks fraction into a tuple of (sign, numerator, denominator) values

Examples

use fraction::Fraction;

assert_eq! ((true, 1, 4), Fraction::from (1.0/4.0).unpack ());
assert_eq! ((false, 5, 4), Fraction::from (-1.25).unpack ());

fn is_nan(&self) -> bool

Checks whether the value is NaN

Examples

use fraction::Fraction;
use std::f32;

assert! (Fraction::nan ().is_nan ());
assert! (Fraction::from (f32::NAN).is_nan ());
assert! (Fraction::new (1, 0).is_nan ());

fn is_infinite(&self) -> bool

Checks whether the value is INF

Examples

use fraction::Fraction;
use std::f32;

assert! (Fraction::inf ().is_infinite ());
assert! (Fraction::from (f32::INFINITY).is_infinite ());
assert! (Fraction::from (f32::NEG_INFINITY).is_infinite ());

fn is_finite(&self) -> bool

Checks whether the value is not INF and not is NaN

Examples

use fraction::Fraction;
use std::f32;

assert! (! Fraction::inf ().is_finite ());
assert! (! Fraction::nan ().is_finite ());
assert! (! Fraction::from (f32::INFINITY).is_finite ());

fn is_neg(&self) -> bool

Checks whether the value is negative

Examples

use fraction::Fraction;
use std::f32;

assert! (Fraction::from (-1).is_neg ());
assert! (Fraction::from (f32::NEG_INFINITY).is_neg ());

fn to_f64(&self) -> f64

Returns native f64 representation of the fraction

Examples

use fraction::Fraction;
use std::f64;

assert_eq! (1.5f64, Fraction::from (1.5f32).to_f64 ());
assert! (Fraction::nan ().to_f64 ().is_nan ());
assert! (Fraction::inf ().to_f64 ().is_infinite ());
assert! (Fraction::neg_inf ().to_f64 ().is_infinite ());

Trait Implementations

impl Hash for Fraction
[src]

fn hash<__H: Hasher>(&self, __arg_0: &mut __H)

Feeds this value into the state given, updating the hasher as necessary.

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0

Feeds a slice of this type into the state provided.

impl Debug for Fraction
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl Clone for Fraction
[src]

fn clone(&self) -> Fraction

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 Copy for Fraction
[src]

impl From<u8> for Fraction
[src]

fn from(val: u8) -> Fraction

Performs the conversion.

impl From<i8> for Fraction
[src]

fn from(val: i8) -> Fraction

Performs the conversion.

impl From<u16> for Fraction
[src]

fn from(val: u16) -> Fraction

Performs the conversion.

impl From<i16> for Fraction
[src]

fn from(val: i16) -> Fraction

Performs the conversion.

impl From<u32> for Fraction
[src]

fn from(val: u32) -> Fraction

Performs the conversion.

impl From<i32> for Fraction
[src]

fn from(val: i32) -> Fraction

Performs the conversion.

impl From<u64> for Fraction
[src]

fn from(val: u64) -> Fraction

Performs the conversion.

impl From<i64> for Fraction
[src]

fn from(val: i64) -> Fraction

Performs the conversion.

impl From<usize> for Fraction
[src]

fn from(val: usize) -> Fraction

Performs the conversion.

impl From<f32> for Fraction
[src]

fn from(val: f32) -> Fraction

Performs the conversion.

impl From<f64> for Fraction
[src]

fn from(val: f64) -> Fraction

Performs the conversion.

impl Neg for Fraction
[src]

type Output = Fraction

The resulting type after applying the - operator

fn neg(self) -> Fraction

The method for the unary - operator

impl Add for Fraction
[src]

type Output = Fraction

The resulting type after applying the + operator

fn add(self, oth: Fraction) -> Fraction

The method for the + operator

impl Sub for Fraction
[src]

type Output = Fraction

The resulting type after applying the - operator

fn sub(self, oth: Fraction) -> Fraction

The method for the - operator

impl Mul for Fraction
[src]

type Output = Fraction

The resulting type after applying the * operator

fn mul(self, oth: Fraction) -> Fraction

The method for the * operator

impl Div for Fraction
[src]

type Output = Fraction

The resulting type after applying the / operator

fn div(self, oth: Fraction) -> Fraction

The method for the / operator

impl Ord for Fraction
[src]

fn cmp(&self, oth: &Fraction) -> Ordering

This method returns an Ordering between self and other. Read more

impl PartialOrd for Fraction
[src]

fn partial_cmp(&self, oth: &Fraction) -> 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 Eq for Fraction
[src]

impl PartialEq for Fraction
[src]

fn eq(&self, oth: &Fraction) -> 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 Display for Fraction
[src]

fn fmt(&self, formatter: &mut Formatter) -> Result

Formats the value using the given formatter.