use num_bigint::BigInt;
use num_rational::Ratio;
use num_traits::ToPrimitive;
pub trait Convertible: Clone {
fn add_refs(&self, other: &Self) -> Self;
fn mul_refs(&self, other: &Self) -> Self;
fn from_ratio_bigint(ratio: Ratio<BigInt>) -> Option<Self>;
fn reciprocal(&self) -> Self;
}
impl Convertible for Ratio<BigInt> {
fn mul_refs(&self, other: &Self) -> Self {
self * other
}
fn add_refs(&self, other: &Self) -> Self {
self + other
}
fn from_ratio_bigint(ratio: Ratio<BigInt>) -> Option<Self> {
Some(ratio)
}
fn reciprocal(&self) -> Self {
self.recip()
}
}
impl Convertible for f64 {
fn mul_refs(&self, other: &Self) -> Self {
self * other
}
fn add_refs(&self, other: &Self) -> Self {
self + other
}
fn from_ratio_bigint(ratio: Ratio<BigInt>) -> Option<Self> {
ratio.to_f64()
}
fn reciprocal(&self) -> Self {
self.recip()
}
}