2020-02-12 23:10:08 -05:00
|
|
|
//! # Rational Numbers (fractions)
|
2020-02-12 22:29:57 -05:00
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
use crate::num::Sign::*;
|
2020-02-19 15:07:40 -05:00
|
|
|
use crate::num::*;
|
2020-02-18 20:59:58 -05:00
|
|
|
use std::cmp::{Ord, Ordering, PartialOrd};
|
2020-02-18 16:38:26 -05:00
|
|
|
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
2020-02-12 23:10:08 -05:00
|
|
|
|
2020-02-18 16:38:26 -05:00
|
|
|
/// Type representing a fraction
|
2020-02-19 16:22:01 -05:00
|
|
|
///
|
|
|
|
/// There are three basic constructors:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use rusty_numbers::frac;
|
|
|
|
/// use rusty_numbers::rational::Frac;
|
|
|
|
///
|
|
|
|
/// // Macro
|
|
|
|
/// let reduced_macro = frac!(3 / 4);
|
|
|
|
///
|
|
|
|
/// // Frac::new (called by the macro)
|
|
|
|
/// let reduced = Frac::new(3, 4);
|
|
|
|
/// # assert_eq!(reduced_macro, reduced);
|
|
|
|
///
|
|
|
|
/// // Frac::new_unreduced
|
|
|
|
/// let unreduced = Frac::new_unreduced(4, 16);
|
|
|
|
/// ```
|
2020-02-14 17:24:51 -05:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
2020-02-12 22:29:57 -05:00
|
|
|
pub struct Frac<T: Unsigned = usize> {
|
|
|
|
numer: T,
|
|
|
|
denom: T,
|
2020-02-12 23:10:08 -05:00
|
|
|
sign: Sign,
|
2020-02-12 22:29:57 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 16:38:26 -05:00
|
|
|
/// Create a [Frac](rational/struct.Frac.html) type with signed or unsigned number literals
|
|
|
|
///
|
2020-02-19 16:22:01 -05:00
|
|
|
/// Example:
|
|
|
|
/// ```
|
|
|
|
/// use rusty_numbers::frac;
|
2020-02-18 16:38:26 -05:00
|
|
|
///
|
2020-02-19 16:22:01 -05:00
|
|
|
/// // Proper fractions
|
|
|
|
/// frac!(1 / 3);
|
|
|
|
///
|
|
|
|
/// // Improper fractions
|
|
|
|
/// frac!(4 / 3);
|
2020-02-18 16:38:26 -05:00
|
|
|
///
|
|
|
|
/// // Whole numbers
|
|
|
|
/// frac!(5u8);
|
|
|
|
///
|
|
|
|
/// // Whole numbers and fractions
|
|
|
|
/// frac!(1 1/2);
|
|
|
|
/// ```
|
2020-02-20 11:35:04 -05:00
|
|
|
#[macro_export]
|
2020-02-14 17:24:51 -05:00
|
|
|
macro_rules! frac {
|
2020-02-18 10:19:57 -05:00
|
|
|
($w:literal $n:literal / $d:literal) => {
|
|
|
|
frac!($w) + frac!($n / $d)
|
2020-02-14 17:24:51 -05:00
|
|
|
};
|
2020-02-14 23:41:14 -05:00
|
|
|
($n:literal / $d:literal) => {
|
2020-02-19 09:39:19 -05:00
|
|
|
$crate::rational::Frac::new($n, $d)
|
2020-02-14 17:24:51 -05:00
|
|
|
};
|
2020-02-18 10:19:57 -05:00
|
|
|
($w:literal) => {
|
2020-02-19 09:39:19 -05:00
|
|
|
$crate::rational::Frac::new($w, 1)
|
2020-02-18 10:19:57 -05:00
|
|
|
};
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 16:38:26 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
enum FracOp {
|
2020-02-19 14:08:43 -05:00
|
|
|
Addition,
|
2020-02-18 16:38:26 -05:00
|
|
|
Subtraction,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
impl<T: Unsigned> Frac<T> {
|
|
|
|
/// Create a new rational number from signed or unsigned arguments
|
|
|
|
///
|
|
|
|
/// Generally, you will probably prefer to use the [frac!](../macro.frac.html) macro
|
|
|
|
/// instead, as that is easier for mixed fractions and whole numbers
|
2020-02-19 14:08:43 -05:00
|
|
|
pub fn new<N: Int<Un = T>>(n: N, d: N) -> Frac<T> {
|
2020-02-19 09:39:19 -05:00
|
|
|
Self::new_unreduced(n, d).reduce()
|
2020-02-14 23:41:14 -05:00
|
|
|
}
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
/// Create a new rational number from signed or unsigned arguments
|
|
|
|
/// where the resulting fraction is not reduced
|
2020-02-19 14:08:43 -05:00
|
|
|
pub fn new_unreduced<N: Int<Un = T>>(n: N, d: N) -> Frac<T> {
|
2020-02-19 09:39:19 -05:00
|
|
|
if d.is_zero() {
|
|
|
|
panic!("Fraction can not have a zero denominator");
|
|
|
|
}
|
2020-02-14 23:41:14 -05:00
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
let mut sign = Positive;
|
2020-02-18 16:38:26 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
if n.is_neg() {
|
|
|
|
sign = !sign;
|
|
|
|
}
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
if d.is_neg() {
|
|
|
|
sign = !sign;
|
|
|
|
}
|
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
// Convert the possibly signed arguments to unsigned values
|
2020-02-19 09:39:19 -05:00
|
|
|
let numer = n.to_unsigned();
|
|
|
|
let denom = d.to_unsigned();
|
|
|
|
|
2020-02-19 15:07:40 -05:00
|
|
|
Frac { numer, denom, sign }
|
2020-02-18 21:29:40 -05:00
|
|
|
}
|
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
/// Create a new rational from all the raw parts
|
2020-02-19 09:39:19 -05:00
|
|
|
fn raw(n: T, d: T, s: Sign) -> Frac<T> {
|
2020-02-13 17:13:25 -05:00
|
|
|
if d.is_zero() {
|
|
|
|
panic!("Fraction can not have a zero denominator");
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:29:57 -05:00
|
|
|
Frac {
|
|
|
|
numer: n,
|
|
|
|
denom: d,
|
2020-02-14 17:24:51 -05:00
|
|
|
sign: s,
|
2020-02-19 15:07:40 -05:00
|
|
|
}
|
|
|
|
.reduce()
|
2020-02-13 17:13:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
/// Determine the output sign given the two input signs
|
2020-02-19 15:07:40 -05:00
|
|
|
fn get_sign(a: Self, b: Self, op: FracOp) -> Sign {
|
2020-02-19 21:10:53 -05:00
|
|
|
let mut output = Sign::default();
|
|
|
|
|
|
|
|
if op == FracOp::Addition && !(a.sign == Positive && b.sign == Positive) {
|
|
|
|
output = Negative;
|
2020-02-19 14:08:43 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
if a.sign != b.sign {
|
2020-02-19 21:10:53 -05:00
|
|
|
output = if op == FracOp::Subtraction && b.sign == Negative {
|
2020-02-19 14:08:43 -05:00
|
|
|
Positive
|
2020-02-18 16:38:26 -05:00
|
|
|
} else {
|
2020-02-19 14:08:43 -05:00
|
|
|
Negative
|
2020-02-18 16:38:26 -05:00
|
|
|
}
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
2020-02-19 21:10:53 -05:00
|
|
|
|
|
|
|
output
|
2020-02-13 17:13:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
/// Convert the fraction to its simplest form
|
2020-02-19 16:22:01 -05:00
|
|
|
pub fn reduce(mut self) -> Self {
|
2020-02-13 17:13:25 -05:00
|
|
|
let gcd = T::gcd(self.numer, self.denom);
|
|
|
|
self.numer /= gcd;
|
|
|
|
self.denom /= gcd;
|
|
|
|
|
|
|
|
self
|
2020-02-12 22:29:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> PartialOrd for Frac<T> {
|
2020-02-18 20:59:58 -05:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> Ord for Frac<T> {
|
2020-02-18 20:59:58 -05:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
2020-02-19 09:39:19 -05:00
|
|
|
if self.sign != other.sign {
|
2020-02-19 14:08:43 -05:00
|
|
|
return if self.sign == Positive {
|
2020-02-19 09:39:19 -05:00
|
|
|
Ordering::Greater
|
2020-02-18 20:59:58 -05:00
|
|
|
} else {
|
2020-02-19 09:39:19 -05:00
|
|
|
Ordering::Less
|
|
|
|
};
|
|
|
|
}
|
2020-02-18 21:29:40 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
if self.denom == other.denom {
|
|
|
|
return self.numer.cmp(&other.numer);
|
|
|
|
}
|
2020-02-18 20:59:58 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
let mut a = self.reduce();
|
|
|
|
let mut b = other.reduce();
|
2020-02-18 20:59:58 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
if a.denom == b.denom {
|
|
|
|
return a.numer.cmp(&b.numer);
|
2020-02-18 20:59:58 -05:00
|
|
|
}
|
2020-02-19 09:39:19 -05:00
|
|
|
|
|
|
|
let lcm = T::lcm(self.denom, other.denom);
|
|
|
|
let x = lcm / self.denom;
|
|
|
|
let y = lcm / other.denom;
|
|
|
|
|
|
|
|
a.numer *= x;
|
|
|
|
a.denom *= x;
|
|
|
|
|
|
|
|
b.numer *= y;
|
|
|
|
b.denom *= y;
|
|
|
|
|
|
|
|
a.numer.cmp(&b.numer)
|
2020-02-18 20:59:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> Mul for Frac<T> {
|
2020-02-14 12:30:09 -05:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn mul(self, rhs: Self) -> Self {
|
|
|
|
let numer = self.numer * rhs.numer;
|
|
|
|
let denom = self.denom * rhs.denom;
|
2020-02-18 16:38:26 -05:00
|
|
|
let sign = Self::get_sign(self, rhs, FracOp::Other);
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
Self::raw(numer, denom, sign)
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> MulAssign for Frac<T> {
|
2020-02-18 10:19:57 -05:00
|
|
|
fn mul_assign(&mut self, rhs: Self) {
|
2020-02-19 16:22:01 -05:00
|
|
|
*self = *self * rhs
|
2020-02-18 10:19:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> Div for Frac<T> {
|
2020-02-14 17:24:51 -05:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn div(self, rhs: Self) -> Self {
|
|
|
|
let numer = self.numer * rhs.denom;
|
|
|
|
let denom = self.denom * rhs.numer;
|
2020-02-18 16:38:26 -05:00
|
|
|
let sign = Self::get_sign(self, rhs, FracOp::Other);
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
Self::raw(numer, denom, sign)
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-14 12:30:09 -05:00
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> DivAssign for Frac<T> {
|
2020-02-18 10:19:57 -05:00
|
|
|
fn div_assign(&mut self, rhs: Self) {
|
2020-02-19 16:22:01 -05:00
|
|
|
*self = *self / rhs
|
2020-02-18 10:19:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> Add for Frac<T> {
|
2020-02-14 17:24:51 -05:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
|
|
let a = self;
|
|
|
|
let b = rhs;
|
|
|
|
|
|
|
|
// If the sign of one input differs,
|
|
|
|
// subtraction is equivalent
|
2020-02-19 14:08:43 -05:00
|
|
|
if a.sign == Negative && b.sign == Positive {
|
|
|
|
return b - -a;
|
|
|
|
} else if a.sign == Positive && b.sign == Negative {
|
|
|
|
return a - -b;
|
2020-02-14 12:30:09 -05:00
|
|
|
}
|
2020-02-14 17:24:51 -05:00
|
|
|
|
|
|
|
// Find a common denominator if needed
|
|
|
|
if a.denom != b.denom {
|
|
|
|
// Let's just use the simplest method, rather than
|
|
|
|
// worrying about reducing to the least common denominator
|
|
|
|
let numer = (a.numer * b.denom) + (b.numer * a.denom);
|
|
|
|
let denom = a.denom * b.denom;
|
2020-02-19 14:08:43 -05:00
|
|
|
let sign = Self::get_sign(a, b, FracOp::Addition);
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
return Self::raw(numer, denom, sign);
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let numer = a.numer + b.numer;
|
|
|
|
let denom = self.denom;
|
2020-02-19 14:08:43 -05:00
|
|
|
let sign = Self::get_sign(a, b, FracOp::Addition);
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 09:39:19 -05:00
|
|
|
Self::raw(numer, denom, sign)
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> AddAssign for Frac<T> {
|
2020-02-18 10:19:57 -05:00
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
2020-02-19 16:22:01 -05:00
|
|
|
*self = *self + rhs
|
2020-02-18 10:19:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> Sub for Frac<T> {
|
2020-02-14 17:24:51 -05:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
2020-02-18 20:59:58 -05:00
|
|
|
let a = self;
|
2020-02-18 16:38:26 -05:00
|
|
|
let b = rhs;
|
2020-02-14 17:24:51 -05:00
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
if a.sign == Positive && b.sign == Negative {
|
|
|
|
return a + -b;
|
|
|
|
} else if a.sign == Negative && b.sign == Positive {
|
2020-02-19 15:07:40 -05:00
|
|
|
return -(b + -a);
|
2020-02-19 14:08:43 -05:00
|
|
|
}
|
2020-02-18 20:59:58 -05:00
|
|
|
|
2020-02-18 10:19:57 -05:00
|
|
|
if a.denom != b.denom {
|
2020-02-19 14:08:43 -05:00
|
|
|
let (numer, overflowed) = (a.numer * b.denom).left_overflowing_sub(b.numer * a.denom);
|
|
|
|
|
2020-02-18 10:19:57 -05:00
|
|
|
let denom = a.denom * b.denom;
|
2020-02-19 15:07:40 -05:00
|
|
|
let sign = Self::get_sign(a, b, FracOp::Subtraction);
|
|
|
|
let res = Self::raw(numer, denom, sign);
|
|
|
|
return if overflowed { -res } else { res };
|
2020-02-18 10:19:57 -05:00
|
|
|
}
|
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
let (numer, overflowed) = a.numer.left_overflowing_sub(b.numer);
|
|
|
|
|
2020-02-18 10:19:57 -05:00
|
|
|
let denom = a.denom;
|
2020-02-19 15:07:40 -05:00
|
|
|
let sign = Self::get_sign(a, b, FracOp::Subtraction);
|
|
|
|
let res = Self::raw(numer, denom, sign);
|
2020-02-18 10:19:57 -05:00
|
|
|
|
2020-02-19 16:22:01 -05:00
|
|
|
if overflowed {
|
|
|
|
-res
|
|
|
|
} else {
|
|
|
|
res
|
|
|
|
}
|
2020-02-18 10:19:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
impl<T: Unsigned> SubAssign for Frac<T> {
|
2020-02-18 10:19:57 -05:00
|
|
|
fn sub_assign(&mut self, rhs: Self) {
|
2020-02-19 16:22:01 -05:00
|
|
|
*self = *self - rhs
|
2020-02-14 12:30:09 -05:00
|
|
|
}
|
2020-02-13 17:13:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-12 23:10:08 -05:00
|
|
|
impl<T: Unsigned> Neg for Frac<T> {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn neg(self) -> Self::Output {
|
2020-02-19 14:08:43 -05:00
|
|
|
let mut out = self;
|
2020-02-12 23:10:08 -05:00
|
|
|
out.sign = !self.sign;
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:29:57 -05:00
|
|
|
#[cfg(test)]
|
2020-03-05 21:53:33 -05:00
|
|
|
#[cfg_attr(tarpaulin, skip)]
|
2020-02-14 12:11:57 -05:00
|
|
|
mod tests {
|
2020-02-19 21:10:53 -05:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "Fraction can not have a zero denominator")]
|
|
|
|
fn zero_denom() {
|
|
|
|
Frac::raw(1u8, 0u8, Sign::default());
|
|
|
|
}
|
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
#[test]
|
|
|
|
fn macro_test() {
|
2020-02-14 23:41:14 -05:00
|
|
|
let frac1 = frac!(1 / 3);
|
2020-02-18 16:38:26 -05:00
|
|
|
let frac2 = frac!(1u32 / 3);
|
2020-02-14 17:24:51 -05:00
|
|
|
assert_eq!(frac1, frac2);
|
|
|
|
|
2020-02-14 23:41:14 -05:00
|
|
|
let frac1 = -frac!(1 / 2);
|
2020-02-18 16:38:26 -05:00
|
|
|
let frac2 = -frac!(1u32 / 2);
|
2020-02-14 17:24:51 -05:00
|
|
|
assert_eq!(frac1, frac2);
|
2020-02-18 10:19:57 -05:00
|
|
|
|
|
|
|
assert_eq!(frac!(3 / 2), frac!(1 1/2));
|
|
|
|
assert_eq!(frac!(3 / 1), frac!(3));
|
2020-02-19 21:10:53 -05:00
|
|
|
|
2020-02-19 21:23:07 -05:00
|
|
|
assert_eq!(-frac!(1 / 2), frac!(-1 / 2));
|
|
|
|
assert_eq!(-frac!(1 / 2), frac!(1 / -2));
|
|
|
|
assert_eq!(frac!(1 / 2), frac!(-1 / -2));
|
2020-02-14 17:24:51 -05:00
|
|
|
}
|
2020-02-14 12:11:57 -05:00
|
|
|
}
|