Struct oxygen_quark::fraction::Fraction
source · Fields§
§numerator: i64
The numerator
denominator: i64
The denominator
Implementations§
source§impl Fraction
impl Fraction
sourcepub fn new_from_decimal(decimal: f64) -> Fraction
pub fn new_from_decimal(decimal: f64) -> Fraction
Generates a new Fraction
from an f64
It returns a Fraction
in its fully reduced form,
after calculating the greatest common divisor.
Useful for when calculating square-roots, sine, cosine, tangent functions and more.
Examples
let x = Fraction::new(3, 1);
let y = Fraction::new(4, 1);
// Prints out "3, 4"
println!("{}, {}", x, y);
Panics
Panics if the numerator is too large, ‘attempt to multiply with overflow’.
sourcepub fn add(self, other: Fraction) -> Fraction
pub fn add(self, other: Fraction) -> Fraction
Adds two Fraction
s and returns their sum, another Fraction
.
Examples
let f1 = Fraction::new(3, 1);
let f2 = Fraction::new(4, 1);
// Prints out "7"
println!("{}", f1.add(f2));
// It also works like this:
println!("{}", f1 + f2);
sourcepub fn subtract(self, other: Fraction) -> Fraction
pub fn subtract(self, other: Fraction) -> Fraction
Subtracts two Fraction
s and returns their difference, another Fraction
.
Examples
let f1 = Fraction::new(3, 1);
let f2 = Fraction::new(4, 1);
// Prints out "-1"
println!("{}", f1.subtract(f2));
// It also works like this:
println!("{}", f1 - f2);
sourcepub fn multiply(self, other: Fraction) -> Fraction
pub fn multiply(self, other: Fraction) -> Fraction
Multiplies two Fraction
s and returns their product, another Fraction
.
Examples
let f1 = Fraction::new(3, 1);
let f2 = Fraction::new(4, 1);
// Prints out "12"
println!("{}", f1.multiply(f2));
// It also works like this:
println!("{}", f1 * f2);
sourcepub fn divide(self, other: Fraction) -> Fraction
pub fn divide(self, other: Fraction) -> Fraction
Divides two Fraction
s and returns their quotient, another Fraction
.
Examples
let f1 = Fraction::new(3, 1);
let f2 = Fraction::new(4, 1);
// Prints out "(3/4)"
println!("{}", f1.divide(f2));
// It also works like this:
println!("{}", f1 / f2);
sourcepub fn greatest_common_divisor(numerator: i64, denominator: i64) -> i64
pub fn greatest_common_divisor(numerator: i64, denominator: i64) -> i64
Returns the greatest common divisor, as an i64
.
Examples
let n1 = 50;
let n2 = 20;
// Prints out "10"
println!("{}", Fraction::greatest_common_divisor(n1, n2));
sourcepub fn reciprocal(self) -> Fraction
pub fn reciprocal(self) -> Fraction
sourcepub fn zero() -> Fraction
pub fn zero() -> Fraction
Returns a Fraction
with numerator 0 and denominator 1.
Examples
let fraction = Fraction::zero();
// Prints out "0"
println!("{}", fraction);
sourcepub fn sqrt(&self) -> Fraction
pub fn sqrt(&self) -> Fraction
Returns the square-root of a Fraction
. Result is a Fraction
Examples
let fraction = Fraction::new(25, 36);
// Prints out "(5/6)"
println!("{}", fraction.sqrt());
sourcepub fn cosine(self) -> Fraction
pub fn cosine(self) -> Fraction
Returns an approximated Fraction
of the cosine (cos) of the Fraction
Examples
let fraction = Fraction::new(1, 1);
// Prints out "(5403/10000)"
println!("{}", fraction.sqrt());
sourcepub fn arc_cosine(self) -> Fraction
pub fn arc_cosine(self) -> Fraction
Returns an approximated Fraction
of the arc cosine (cos^-1) of the Fraction
Examples
let fraction = Fraction::new(1, 1);
// Prints out "+0"
println!("{}", fraction.sqrt());
sourcepub fn sine(self) -> Fraction
pub fn sine(self) -> Fraction
Returns an approximated Fraction
of the sine (sin) of the Fraction
Examples
let fraction = Fraction::new(1, 1);
// Prints out "(84147/100000)"
println!("{}", fraction.sqrt());
sourcepub fn arc_sine(self) -> Fraction
pub fn arc_sine(self) -> Fraction
Returns an approximated Fraction
of the arc sine (sin^-1) of the Fraction
Examples
let fraction = Fraction::new(1, 1);
// Prints out "(3927/2500)"
println!("{}", fraction.sqrt());
sourcepub fn tangent(self) -> Fraction
pub fn tangent(self) -> Fraction
Returns an approximated Fraction
of the tangent (tan) of the Fraction
Examples
let fraction = Fraction::new(1, 1);
// Prints out "(155741/100000)"
println!("{}", fraction.sqrt());
sourcepub fn arc_tangent(self) -> Fraction
pub fn arc_tangent(self) -> Fraction
Returns an approximated Fraction
of the arc tangent (tan^-1) of the Fraction
Examples
let fraction = Fraction::new(1, 1);
// Prints out "(3927/5000)"
println!("{}", fraction.sqrt());
Trait Implementations§
source§impl AddAssign<Fraction> for Fraction
impl AddAssign<Fraction> for Fraction
source§fn add_assign(&mut self, other: Fraction)
fn add_assign(&mut self, other: Fraction)
+=
operation. Read moresource§impl DivAssign<Fraction> for Fraction
impl DivAssign<Fraction> for Fraction
source§fn div_assign(&mut self, other: Fraction)
fn div_assign(&mut self, other: Fraction)
/=
operation. Read moresource§impl MulAssign<Fraction> for Fraction
impl MulAssign<Fraction> for Fraction
source§fn mul_assign(&mut self, other: Fraction)
fn mul_assign(&mut self, other: Fraction)
*=
operation. Read moresource§impl Ord for Fraction
impl Ord for Fraction
source§impl PartialOrd<Fraction> for Fraction
impl PartialOrd<Fraction> for Fraction
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<Fraction> for Fraction
impl SubAssign<Fraction> for Fraction
source§fn sub_assign(&mut self, other: Fraction)
fn sub_assign(&mut self, other: Fraction)
-=
operation. Read more