diff --git a/src/types.rs b/src/types.rs index 23987e7..3530588 100644 --- a/src/types.rs +++ b/src/types.rs @@ -105,6 +105,10 @@ impl Frac { return a; } + // Ideally builtin functions ( + - * / ) can operate with the values in any + // form without problems, occasional simplification is done for numeric + // stability, ensure to simplify after insert and before using any funcition + // (floor, ceil, etc.) pub fn simplify(&self) -> Frac { // Euclid's algorithm to reduce fraction // TODO: (decide if implementing this automathically once fraction @@ -130,13 +134,19 @@ impl Frac { // return Ok(Num(Frac::num(tk.parse::().unwrap()))); pub fn from_str(tk: &str) -> Self { - match tk.find("/") { + let frac = match tk.find("/") { Some(v) => Self { num: tk[0..v].parse::().unwrap(), den: tk[v + 1..tk.len()].parse::().unwrap(), }, None => Frac::num(tk.parse::().unwrap()), - } + }; + // Ensure that value is simplified before being inserted + // otherwise + // (/ 4 4) results in 1/1 + // 4/4 results in 4/4 + // this breaks some functions (like ceil) and doesn't make much sense + frac.simplify() } }