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-12 23:10:08 -05:00
|
|
|
use crate::{Sign, Unsigned};
|
|
|
|
use std::ops::Neg;
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-02-12 22:29:57 -05:00
|
|
|
pub enum FracType<T: Unsigned = usize> {
|
|
|
|
Proper(T, Frac),
|
|
|
|
Improper(Frac),
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:10:08 -05:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Unsigned> Frac<T> {
|
|
|
|
/// Create a new rational number
|
|
|
|
pub fn new(n: T, d: T) -> Self {
|
|
|
|
Frac {
|
|
|
|
numer: n,
|
|
|
|
denom: d,
|
2020-02-12 23:10:08 -05:00
|
|
|
sign: Sign::Positive,
|
2020-02-12 22:29:57 -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)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
}
|