#[cfg(feature = "num")]
use crate::{NumConst, Rem};
#[doc = crate::_tags!(quant)]
#[doc = crate::_doc_location!("num/quant")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ratio<N, D> {
pub n: N,
pub d: D,
}
#[rustfmt::skip]
impl<N, D> Ratio<N, D> {
pub const fn new(n: N, d: D) -> Self {
Self { n, d }
}
#[cfg(feature = "num")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "num")))]
pub fn new_checked(n: N, d: D) -> Option<Self> where D: NumConst<Num = D> + PartialEq {
let new = Self::new(n, d);
if let Some(zero) = D::NUM_ZERO {
if new.d == zero { None } else { Some(new) }
} else {
Some(new)
}
}
}
#[rustfmt::skip]
impl<N: Clone, D: Clone> Ratio<N, D> {
pub fn as_f32(&self) -> f32 where N: Into<f32>, D: Into<f32> {
self.n.clone().into() / self.d.clone().into()
}
pub fn as_f64(&self) -> f64 where N: Into<f64>, D: Into<f64> {
self.n.clone().into() / self.d.clone().into()
}
#[cfg(feature = "num")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "num")))]
pub fn is_whole(&self) -> Option<bool>
where
N: NumConst<Num = N> + Rem<D, Output = N> + PartialEq,
{
N::NUM_ZERO.map(|zero| (self.n.clone() % self.d.clone()) == zero)
}
}