2020-02-12 23:10:08 -05:00
|
|
|
//! # Rational Numbers (fractions)
|
|
|
|
//!
|
|
|
|
//! Traits to implement:
|
|
|
|
//! * Add
|
|
|
|
//! * AddAssign
|
|
|
|
//! * Div
|
|
|
|
//! * DivAssign
|
|
|
|
//! * Mul
|
|
|
|
//! * MulAssign
|
|
|
|
//! * Neg
|
|
|
|
//! * Sub
|
|
|
|
//! * SubAssign
|
2020-02-12 22:29:57 -05:00
|
|
|
|
2020-02-14 10:14:22 -05:00
|
|
|
use crate::num::*;
|
2020-02-14 17:24:51 -05:00
|
|
|
use std::ops::{Add, Div, Mul, Neg, Sub};
|
2020-02-12 23:10:08 -05:00
|
|
|
|
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-14 17:24:51 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! frac {
|
|
|
|
($n:literal / $d:literal) => {
|
|
|
|
Frac::new_conv($n, $d)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* macro_rules! impl_from_signed {
|
|
|
|
($(($in_type: ty, $out_type: ty)),* ) => {
|
|
|
|
$(
|
|
|
|
impl Frac<$in_type> {
|
|
|
|
pub fn new(n: $in_type, d: $in_type) -> Frac<$out_type> {
|
|
|
|
// Converting from signed to unsigned should always be safe
|
|
|
|
// when using the absolute value, especially since I'm converting
|
|
|
|
// between the same bit size
|
|
|
|
let mut sign = $crate::num::Sign::Positive;
|
|
|
|
let numer = <$out_type>::try_from(n.abs()).unwrap();
|
|
|
|
let denom = <$out_type>::try_from(d.abs()).unwrap();
|
|
|
|
|
|
|
|
if n < 0 {
|
|
|
|
sign = !sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
if d < 0 {
|
|
|
|
sign = !sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
Frac::new_signed(numer, denom, sign)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_signed(n: $in_type, d: $in_type, _: $crate::num::Sign) -> Frac<$out_type> {
|
|
|
|
Self::new(n, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
impl_from_signed!((i8, u8), (i16, u16), (i32, u32), (i64, u64), (i128, u128), (isize, usize)); */
|
|
|
|
|
|
|
|
impl<T: Signed, U: Unsigned> Frac<U> {
|
|
|
|
pub fn new_conv(n: T, d: T) -> Self {
|
|
|
|
// Converting from signed to unsigned should always be safe
|
|
|
|
// when using the absolute value, especially since I'm converting
|
|
|
|
// between the same bit size
|
|
|
|
let mut sign = Sign::Positive;
|
|
|
|
let numer:T::Un = n.to_unsigned();
|
|
|
|
let denom:T::Un = d.to_unsigned();
|
|
|
|
|
|
|
|
if n.is_neg() {
|
|
|
|
sign = !sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.is_neg() {
|
|
|
|
sign = !sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::new(numer, denom, sign)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:29:57 -05:00
|
|
|
impl<T: Unsigned> Frac<T> {
|
|
|
|
/// Create a new rational number
|
2020-02-14 17:24:51 -05:00
|
|
|
pub fn new(n: T, d: T, s: Sign) -> Self {
|
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-12 22:29:57 -05:00
|
|
|
}
|
2020-02-13 17:13:25 -05:00
|
|
|
.reduce()
|
|
|
|
}
|
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
/// Determine the output sign given the two input signs
|
|
|
|
fn get_sign(a: Self, b: Self) -> Sign {
|
|
|
|
if a.sign != b.sign {
|
|
|
|
Sign::Negative
|
|
|
|
} else {
|
|
|
|
Sign::Positive
|
|
|
|
}
|
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-13 17:13:25 -05:00
|
|
|
fn reduce(mut self) -> Self {
|
|
|
|
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-14 12:30:09 -05:00
|
|
|
impl<T: Unsigned + Mul<Output = T>> Mul for Frac<T> {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn mul(self, rhs: Self) -> Self {
|
|
|
|
let numer = self.numer * rhs.numer;
|
|
|
|
let denom = self.denom * rhs.denom;
|
2020-02-14 17:24:51 -05:00
|
|
|
let sign = Self::get_sign(self, rhs);
|
|
|
|
|
|
|
|
Self::new_signed(numer, denom, sign)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Unsigned + Mul<Output = T>> Div for Frac<T> {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn div(self, rhs: Self) -> Self {
|
|
|
|
let numer = self.numer * rhs.denom;
|
|
|
|
let denom = self.denom * rhs.numer;
|
|
|
|
let sign = Self::get_sign(self, rhs);
|
|
|
|
|
|
|
|
Self::new_signed(numer, denom, sign)
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 12:30:09 -05:00
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
impl<T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T>> Add for Frac<T> {
|
|
|
|
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-14 12:30:09 -05:00
|
|
|
if self.sign != rhs.sign {
|
2020-02-14 17:24:51 -05:00
|
|
|
if a.numer > b.numer {
|
|
|
|
return a - b
|
|
|
|
} else if a.numer < b.numer {
|
|
|
|
return b - a
|
|
|
|
}
|
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;
|
|
|
|
let sign = Self::get_sign(a, b);
|
|
|
|
|
|
|
|
return Self::new_signed(numer, denom, sign);
|
|
|
|
}
|
|
|
|
|
|
|
|
let numer = a.numer + b.numer;
|
|
|
|
let denom = self.denom;
|
|
|
|
let sign = Self::get_sign(a, b);
|
|
|
|
|
|
|
|
Self::new_signed(numer, denom, sign)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Unsigned + Sub<Output = T>> Sub for Frac<T> {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
|
|
|
let a = self;
|
|
|
|
let b = rhs;
|
|
|
|
|
|
|
|
unimplemented!()
|
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 {
|
|
|
|
let mut out = self.clone();
|
|
|
|
out.sign = !self.sign;
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:29:57 -05:00
|
|
|
#[cfg(test)]
|
2020-02-14 12:11:57 -05:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mul_test() {
|
2020-02-14 17:24:51 -05:00
|
|
|
let frac1 = Frac::new(1u8, 3u8, Sign::Positive);
|
|
|
|
let frac2 = Frac::new(2u8, 3u8, Sign::Positive);
|
2020-02-14 12:11:57 -05:00
|
|
|
|
2020-02-14 17:24:51 -05:00
|
|
|
let expected = Frac::new(2u8, 9u8, Sign::Positive);
|
2020-02-14 12:11:57 -05:00
|
|
|
|
|
|
|
assert_eq!(frac1 * frac2, expected);
|
|
|
|
}
|
2020-02-14 17:24:51 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_test() {
|
|
|
|
assert_eq!(frac!(5u8 / 6), frac!(1 / 3) + frac!(1 / 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_test() {
|
|
|
|
let frac1 = frac!(1u8 / 3);
|
|
|
|
let frac2 = Frac::new(1u8, 3, Sign::Positive);
|
|
|
|
assert_eq!(frac1, frac2);
|
|
|
|
|
|
|
|
let frac1 = -frac!(1u8 / 2);
|
|
|
|
let frac2 = Frac::new(1u8, 2, Sign::Negative);
|
|
|
|
assert_eq!(frac1, frac2);
|
|
|
|
}
|
2020-02-14 12:11:57 -05:00
|
|
|
}
|