pub enum GenericFraction<T>where
    T: Clone + Integer,
{ Rational(SignRatio<T>), Infinity(Sign), NaN, }
Expand description

Generic implementation of the fraction type

Examples

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(SignRatio<T>)

§

Infinity(Sign)

§

NaN

Implementations§

Constructs a new fraction with the specified numerator and denominator Handles gracefully signed integers even if the storage type is unsigned and vise versa The arguments can be of any integer types imlementing the necessary traits

Examples
use fraction::{GenericFraction, Sign};
type F = GenericFraction<u16>;

let f12 = F::new_generic(Sign::Plus, 1i8, 2u8).unwrap();
let f34 = F::new_generic(Sign::Plus, 3i16, 4u32).unwrap();
let f56 = F::new_generic(Sign::Plus, 5i64, 6u128).unwrap();
let f78 = F::new_generic(Sign::Plus, 7usize, 8isize).unwrap();

assert_eq! ((*f12.numer().unwrap(), *f12.denom().unwrap()), (1u16, 2u16));
assert_eq! ((*f34.numer().unwrap(), *f34.denom().unwrap()), (3u16, 4u16));
assert_eq! ((*f56.numer().unwrap(), *f56.denom().unwrap()), (5u16, 6u16));
assert_eq! ((*f78.numer().unwrap(), *f78.denom().unwrap()), (7u16, 8u16));

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 allows explicitly set sign.

Examples
use fraction::{GenericFraction, Sign};
type F = GenericFraction<u8>;

let _f = F::new_raw_signed(Sign::Minus, 1u8, 2u8);

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::infinity ();
assert_eq! (Sign::Plus, fra.sign ().unwrap ());

let fra = F::neg_infinity ();
assert_eq! (Sign::Minus, fra.sign ().unwrap ());


let fra = F::nan ();
assert_eq! (None, fra.sign ());

Generates a GenericFraction from GenericFraction where T: From

use fraction::{ Fraction, GenericFraction };
type F8 = GenericFraction<u8>;

let fra8 = F8::new (5u8, 6u8);
assert_eq! (Fraction::new (5u64, 6u64), Fraction::from_fraction(fra8));

Generates a GenericFraction from GenericFraction where T: Into

use fraction::{ Fraction, GenericFraction };
type F8 = GenericFraction<u8>;

let fra8 = F8::new (5u8, 6u8);
assert_eq! (Fraction::new (5u64, 6u64), fra8.into_fraction());

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§

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Performs the += operation. Read more
Returns the smallest finite number this type can represent
Returns the largest finite number this type can represent
Adds two numbers, checking for overflow. If overflow happens, None is returned. Read more
Divides two numbers, checking for underflow, overflow and division by zero. If any of that happens, None is returned. Read more
Multiplies two numbers, checking for underflow or overflow. If underflow or overflow happens, None is returned. Read more
Subtracts two numbers, checking for underflow. If underflow happens, None is returned. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Performs the conversion.
Performs the conversion from an absent value (e.g. to distinguish between implicit and explicit null). The default implementation just uses from_input_value as if an explicit null were provided. This conversion must not fail. Read more
Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more
Determines if a value of this type can be created from the specified Postgres Type. Read more
Creates a new value of this type from a NULL SQL value. Read more
A convenience function that delegates to from_sql and from_sql_null depending on the value of raw. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Returns name of this GraphQLType to expose. Read more
Returns MetaType representing this GraphQLType.
Context type for this GraphQLValue. Read more
Type that may carry additional schema information for this GraphQLValue. Read more
Returns name of the GraphQLType exposed by this GraphQLValue. Read more
Resolves the provided selection_set against this GraphQLValue. Read more
Resolves the value of a single field on this GraphQLValue. Read more
Resolves this GraphQLValue (being an interface or an union) into a concrete downstream object type. Read more
Returns the concrete GraphQLType name for this GraphQLValue being an interface, an union or an object. Read more
Resolves the provided selection_set against this GraphQLValueAsync. Read more
Resolves the value of a single field on this GraphQLValueAsync. Read more
Resolves this GraphQLValueAsync (being an interface or an union) into a concrete downstream object type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
An arbitrary function without meaning. Read more
An arbitrary function without meaning. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
Convert from a string and radix (typically 2..=36). Read more
Returns the multiplicative identity element of Self, 1. Read more
Sets self to the multiplicative identity element of Self, 1.
Returns true if self is equal to the multiplicative identity. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
See the trait documentation
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
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
Method which takes an iterator and generates Self from the elements by multiplying the items. Read more
Method which takes an iterator and generates Self from the elements by multiplying the items. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
Performs the %= operation. Read more
Performs the %= operation. Read more
Serialize this value into the given Serde serializer. Read more
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.
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more
Performs the -= operation. Read more
Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more
Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more
Performs the conversion.
Converts the value of self to an i64. If the value cannot be represented by an i64, then None is returned. Read more
Converts the value of self to a u64. If the value cannot be represented by a u64, then None is returned. Read more
Converts the value of self to an f64. Overflows may map to positive or negative inifinity, otherwise None is returned if the value cannot be represented by an f64. Read more
Converts the value of self to an isize. If the value cannot be represented by an isize, then None is returned. Read more
Converts the value of self to an i8. If the value cannot be represented by an i8, then None is returned. Read more
Converts the value of self to an i16. If the value cannot be represented by an i16, then None is returned. Read more
Converts the value of self to an i32. If the value cannot be represented by an i32, then None is returned. Read more
Converts the value of self to an i128. If the value cannot be represented by an i128 (i64 under the default implementation), then None is returned. Read more
Converts the value of self to a usize. If the value cannot be represented by a usize, then None is returned. Read more
Converts the value of self to a u8. If the value cannot be represented by a u8, then None is returned. Read more
Converts the value of self to a u16. If the value cannot be represented by a u16, then None is returned. Read more
Converts the value of self to a u32. If the value cannot be represented by a u32, then None is returned. Read more
Converts the value of self to a u128. If the value cannot be represented by a u128 (u64 under the default implementation), then None is returned. Read more
Converts the value of self to an f32. Overflows may map to positive or negative inifinity, otherwise None is returned if the value cannot be represented by an f32. Read more
Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more
Determines if a value of this type can be converted to the specified Postgres Type. Read more
An adaptor method used internally by Rust-Postgres. Read more
Specify the encode format
Returns the additive identity element of Self, 0. Read more
Returns true if self is equal to the additive identity.
Sets self to the additive identity element of Self, 0.

Copy semantics to be applied for the target type, but only if T also has it.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Returns a reference to self as a ToSql trait object.
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns the smallest finite number this type can represent
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Returns the largest finite number this type can represent