devela/num/frac/wrapper/impl_frac.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
// devela::num::frac::wrapper::impl_frac
//
//! implements non-owning fraction-related methods
//
// TOC
// - prim. array | Int array:
// - numerator | num
// - denominator | den
// - is_valid
// - is_proper
// - is_reduced
// - reduce
// - gcd
// - lcm
// - add
#[cfg(doc)]
use crate::NumError::{self, Overflow};
#[cfg(_int··)]
use crate::{Frac, Int, NumResult as Result};
// $i: the integer type.
// $self: the fractional self type.
// $fout: the fractionsl output type.
// $cap: the capability feature that enables the given implementation. E.g "_int_i8".
macro_rules! impl_frac {
[] => {
impl_frac![
i8:"_int_i8", i16:"_int_i16", i32:"_int_i32",
i64:"_int_i64", i128:"_int_i128", isize:"_int_isize",
u8:"_int_u8", u16:"_int_u16", u32:"_int_u32",
u64:"_int_u64", u128:"_int_u128", usize:"_int_usize"
];
};
($( $i:ty : $cap:literal ),+ ) => {
$(
impl_frac![@array $i:$cap, [$i; 2], Frac<[$i; 2]>];
impl_frac![@int_array $i:$cap, [Int<$i>; 2], Frac<[Int<$i>; 2]>];
// impl_frac![@slice $i:$cap, &[$i], [$i; 2]]; // MAYBE
)+
};
// both for signed and unsigned
(@array $i:ty : $cap:literal, $self:ty, $fout:ty) => { $crate::paste! {
#[doc = crate::doc_availability!(feature = $cap)]
///
#[doc = "# Fraction related methods for `[" $i "; 2]`\n\n"]
#[cfg(feature = $cap )]
// #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
impl Frac<$self> {
/// Returns the numerator (the first number of the sequence).
#[must_use]
pub const fn numerator(self) -> $i { self.0[0] }
/// Alias of [`numerator`][Self::numerator].
#[must_use]
pub const fn num(self) -> $i { self.0[0] }
/// Returns the denominator (the second number of the sequence).
#[must_use]
pub const fn denominator(self) -> $i { self.0[1] }
/// Alias of [`denominator`][Self::denominator].
#[must_use]
pub const fn den(self) -> $i { self.0[0] }
/// Retuns `true` if the fraction is valid `(denominator != 0)`.
/// # Examples
/// ```
/// # use devela::Frac;
#[doc = "assert![Frac([2_" $i ", 1]).is_valid()];"]
#[doc = "assert![!Frac([2_" $i ", 0]).is_valid()];"]
/// ```
#[must_use]
pub const fn is_valid(self) -> bool { self.0[1] != 0 }
/// Returns `true` if the fraction is proper
/// `(numerator.abs() < denominator.abs())`.
/// # Examples
/// ```
/// # use devela::Frac;
#[doc = "assert![Frac([2_" $i ", 3]).is_proper()];"]
#[doc = "assert![!Frac([3_" $i ", 3]).is_proper()];"]
#[doc = "assert![!Frac([4_" $i ", 3]).is_proper()];"]
/// ```
#[must_use]
pub const fn is_proper(self) -> bool { Int(self.0[0]).abs().0 < Int(self.0[1]).abs().0 }
/// Retuns `true` if the fraction is in the simplest possible form `(gcd() == 1)`.
#[must_use]
pub const fn is_reduced(self) -> bool { self.gcd() == 1 }
/// Simplify a fraction.
#[must_use]
pub const fn reduce(self) -> $fout {
let g = self.gcd();
Frac([self.0[0] / g, self.0[1] / g])
}
/// Returns the <abbr title="Greatest Common Divisor">GCD</abbr>
/// between the numerator and the denominator.
#[must_use]
pub const fn gcd(self) -> $i { Int(self.0[0]).gcd(self.0[1]).0 }
/// Returns the <abbr title="Least Common Multiple">LCM</abbr>
/// between the numerator and the denominator.
/// # Errors
/// Could [`Overflow`].
pub const fn lcm(self) -> Result<$i> {
match Int(self.numerator()).lcm(self.denominator()) {
Ok(res) => Ok(res.0),
Err(e) => Err(e)
}
}
/// Adds two fractions.
#[allow(clippy::should_implement_trait)]
pub fn add(self, other: $self) -> Result<$fout> {
let [num1, den1, num2, den2] = [self.0[0], self.0[1], other[0], other[1]];
let lcm_denom = match Int(den1).lcm(den2) {
Ok(res) => res.0,
Err(e) => { return Err(e); }
};
let num = num1 * (lcm_denom / den1) + num2 * (lcm_denom / den2);
Ok(Frac([num, lcm_denom]).reduce())
}
}
}};
(@int_array $i:ty : $cap:literal, $self:ty, $fout:ty) => { $crate::paste! {
#[doc = crate::doc_availability!(feature = $cap)]
///
#[doc = "# Fraction related methods for `[Int<" $i ">; 2]`\n\n"]
#[cfg(feature = $cap )]
// #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
impl Frac<$self> {
/// Returns the numerator (the first number of the sequence).
#[must_use]
pub const fn numerator(self) -> Int<$i> { self.0[0] }
/// Returns the denominator (the second number of the sequence).
#[must_use]
pub const fn denominator(self) -> Int<$i> { self.0[1] }
/// Retuns `true` if the fraction is valid `(denominator != 0)`.
/// # Examples
/// ```
/// # use devela::{Frac, Int};
#[doc = "assert![Frac([Int(2_" $i "), Int(1)]).is_valid()];"]
#[doc = "assert![!Frac([Int(2_" $i "), Int(0)]).is_valid()];"]
/// ```
#[must_use]
pub const fn is_valid(self) -> bool { self.0[1].0 != 0 }
/// Returns `true` if the fraction is proper
/// `(numerator.abs() < denominator.abs())`.
/// # Examples
/// ```
/// # use devela::{Frac, Int};
#[doc = "assert![Frac([Int(2_" $i "), Int(3)]).is_proper()];"]
#[doc = "assert![!Frac([Int(3_" $i "), Int(3)]).is_proper()];"]
#[doc = "assert![!Frac([Int(4_" $i "), Int(3)]).is_proper()];"]
/// ```
#[must_use]
pub const fn is_proper(self) -> bool { self.0[0].abs().0 < self.0[1].abs().0 }
/// Retuns `true` if the fraction is in the simplest possible form `(gcd() == 1)`.
#[must_use]
pub const fn is_reduced(self) -> bool { self.gcd().0 == 1 }
/// Simplify a fraction.
#[must_use]
pub const fn reduce(self) -> $fout {
let g = self.gcd().0;
Frac([Int(self.0[0].0 / g), Int(self.0[1].0 / g)])
}
/// Returns the <abbr title="Greatest Common Divisor">GCD</abbr>
/// between the numerator and the denominator.
#[must_use]
pub const fn gcd(self) -> Int<$i> { self.0[0].gcd(self.0[1].0) }
/// Returns the <abbr title="Least Common Multiple">LCM</abbr>
/// between the numerator and the denominator.
/// # Errors
/// Could [`Overflow`].
pub const fn lcm(self) -> Result<Int<$i>> {
match self.numerator().lcm(self.denominator().0) {
Ok(res) => Ok(res),
Err(e) => Err(e)
}
}
/// Adds two fractions.
#[allow(clippy::should_implement_trait)]
pub fn add(self, other: $self) -> Result<$fout> {
let [num1, den1, num2, den2] = [self.0[0].0, self.0[1].0, other[0].0, other[1].0];
let lcm_denom = match Int(den1).lcm(den2) {
Ok(res) => res.0,
Err(e) => { return Err(e); }
};
let num = num1 * (lcm_denom / den1) + num2 * (lcm_denom / den2);
Ok(Frac([Int(num), Int(lcm_denom)]).reduce())
}
}
}};
}
impl_frac!();