aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index cbe9716..0fc4328 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,4 +1,5 @@
use std::ops::*;
+use std::fmt::{Debug, Formatter, Error};
#[derive(Copy, Clone)]
pub struct Rational
@@ -15,6 +16,14 @@ impl Default for Rational
fn default() -> Rational { Rational::from_int(0) }
}
+impl Debug for Rational
+{
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
+ {
+ f.write_fmt(format_args!("{}/{}", self.n, self.d))
+ }
+}
+
impl Add for Rational
{
type Output = Self;
@@ -70,6 +79,11 @@ impl From<u8> for Rational
fn from(v: u8) -> Self { Rational::from_int(v as i64) }
}
+impl From<Rational> for f64
+{
+ fn from(v: Rational) -> Self { v.n as f64 / v.d as f64 }
+}
+
impl Rational
{
fn from_int(v: i64) -> Rational {Rational{n: v, d: 1}}