use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rational {
pub num: i32,
pub den: i32,
}
impl Rational {
pub const ZERO: Rational = Rational { num: 0, den: 1 };
pub const fn new(num: i32, den: i32) -> Rational {
Rational { num, den }
}
pub fn as_f64(self) -> f64 {
if self.den == 0 {
f64::NAN
} else {
self.num as f64 / self.den as f64
}
}
pub fn inverse(self) -> Rational {
Rational {
num: self.den,
den: self.num,
}
}
}
impl Default for Rational {
fn default() -> Self {
Rational::ZERO
}
}
impl fmt::Display for Rational {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.num, self.den)
}
}