From 09c99c91699d8701733052937b1a2c2f395e1132 Mon Sep 17 00:00:00 2001 From: bakk Date: Tue, 18 May 2021 20:02:21 +0200 Subject: [PATCH] Fixed mod.rs errors for when rug is not used, and added some unit tests there --- kalk/src/kalk_num/mod.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/kalk/src/kalk_num/mod.rs b/kalk/src/kalk_num/mod.rs index 2049efd..97cedfa 100644 --- a/kalk/src/kalk_num/mod.rs +++ b/kalk/src/kalk_num/mod.rs @@ -49,13 +49,13 @@ impl KalkNum { let integer = self.value.clone().trunc(); // If it's an integer, there's nothing that would be done to it. - if fract == 0 { + if fract == 0f64 { return None; } // Eg. 0.5 to 1/2 let as_abs_string = self.to_string().trim_start_matches("-").to_string(); - let sign = if self.value < 0 { "-" } else { "" }; + let sign = if self.value < 0f64 { "-" } else { "" }; let fract_as_string = fract.to_string(); if as_abs_string.starts_with("0.5") { if as_abs_string.len() == 3 || (as_abs_string.len() > 6 && &as_abs_string[3..5] == "00") @@ -65,7 +65,7 @@ impl KalkNum { } // Eg. 1.33333333 to 1 + 1/3 - if fract_as_string.len() >= 5 { + if fract_as_string.len() >= 7 { let first_five_decimals = &fract_as_string[2..7]; if first_five_decimals == "33333" || first_five_decimals == "66666" { let fraction = match first_five_decimals.as_ref() { @@ -74,7 +74,7 @@ impl KalkNum { _ => "?", }; - if integer == 0 { + if integer == 0f64 { return Some(format!("{}{}", sign, fraction)); } else { let explicit_sign = if sign == "" { "+" } else { "-" }; @@ -102,7 +102,7 @@ impl KalkNum { /// Basic up/down rounding from 0.00xxx or 0.999xxx or xx.000xxx, etc. pub fn round(&self) -> Option { - let sign = if self.value < 0 { -1 } else { 1 }; + let sign = if self.value < 0f64 { -1f64 } else { 1f64 }; let fract = self.value.clone().abs().fract(); let integer = self.value.clone().abs().trunc(); @@ -196,6 +196,27 @@ mod tests { (-0.665f64, None), (100f64, None), (-100f64, None), + (1f64, None), + (0.12f64, None), + (0.1f64, None), + (1.2f64, None), + (1.23f64, None), + (1.234f64, None), + (1.2345f64, None), + (1.23456f64, None), + (1.234567f64, None), + (1.2345678f64, None), + (1.23456789f64, None), + (-0.12f64, None), + (-0.1f64, None), + (-1.2f64, None), + (-1.23f64, None), + (-1.234f64, None), + (-1.2345f64, None), + (-1.23456f64, None), + (-1.234567f64, None), + (-1.2345678f64, None), + (-1.23456789f64, None), ]; for (input, output) in in_out {