From 4353548900eff2c56954e09f4f69030ab762f969 Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 3 Sep 2023 15:56:33 -0700 Subject: [PATCH] Fixed float printing --- src/quantity/scalar/f64base.rs | 85 ++++++++++++++++------------------ src/quantity/scalar/mod.rs | 3 +- src/tests.rs | 2 +- 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/quantity/scalar/f64base.rs b/src/quantity/scalar/f64base.rs index 0de90bd..4695e67 100644 --- a/src/quantity/scalar/f64base.rs +++ b/src/quantity/scalar/f64base.rs @@ -8,7 +8,8 @@ use std::ops::{ use std::cmp::Ordering; use super::ScalarBase; -use super::PRINT_LEN; +use super::SHOW_SIG; +use super::MAX_LEN; macro_rules! foward { ( $x:ident ) => { @@ -55,63 +56,57 @@ impl ToString for F64Base { } + // Pick significant digits and round + let mut s = String::from(s); + if s.len() > SHOW_SIG { + let round; + if s.len() != SHOW_SIG + 1 { + round = s[SHOW_SIG..SHOW_SIG+1].parse().unwrap(); + } else { round = 0; } + + s = String::from(&s[0..SHOW_SIG]); + + if round >= 5 { + let new = s[s.len()-1..s.len()].parse::().unwrap() + 1u8; + if new != 10 { + s = format!("{}{new}", &s[0..s.len()-1]); + } + } + } + + s = format!("{s}{}", "0".repeat(SHOW_SIG - s.len())); + // at this point, s is guaranteed to have exactly SHOW_SIG digits. + let neg = if neg {"-"} else {""}; - if (p.abs() as usize) < PRINT_LEN { - + if (p.abs() as usize) < MAX_LEN { if p >= 0 { let q = p as usize; - // Add zero padding - let t; - if s.len() < (q + 1) { - t = format!("{s}{}", "0".repeat(q + 1 - s.len())); - } else { t = s.to_string() } - - // Section before decimal point - let first = &t[0..q+1]; - - // The rest of the number, including a decimal point - let mut rest: String; - if first.len() == t.len() { - rest = String::from(""); + let first = &s[0..q+1]; + let mut rest = &s[q+1..]; + rest = rest.trim_end_matches('0'); + if rest == "" { + return format!("{neg}{first}"); } else { - rest = format!(".{}", &t[q+1..]); + return format!("{neg}{first}.{rest}"); } - - // Limit length of decimal portion - if rest.len() > PRINT_LEN { - rest = String::from(&rest[0..PRINT_LEN]); - } - - return format!("{neg}{first}{rest}"); } else { let q = p.abs() as usize; - - let t = format!("{neg}0.{}{s}", "0".repeat(q-1)); - - return if t.len() > PRINT_LEN { String::from(&t[0..PRINT_LEN]) } else {t}; + let t = format!("0.{}{s}", "0".repeat(q-1)); + return format!("{neg}{}", t.trim_end_matches('0')); } // Print full scientific notation } else { - // First (non-zero) digit of our number let first = &s[0..1]; - - // The rest of the number, including a decimal point - let mut rest: String; - if first.len() == s.len() { - rest = String::from(""); + let mut rest = &s[1..]; + rest = rest.trim_end_matches('0'); + if rest == "" { + return format!("{neg}{first}e{p}"); } else { - rest = format!(".{}", &s[1..]); + return format!("{neg}{first}.{rest}e{p}"); } - - // Limit length of decimal portion - if rest.len() > 5 { - rest = String::from(&rest[0..PRINT_LEN]); - } - - return format!("{neg}{first}{rest}e{p}"); } } } @@ -249,11 +244,11 @@ impl Rem for F64Base { fn rem(self, modulus: F64Base) -> Self::Output { if { - (!self.fract().unwrap().is_zero()) || - (!modulus.fract().unwrap().is_zero()) + (!self.is_int()) || + (!modulus.is_int()) } { panic!() } - F64Base{val : self.val.fract() % modulus.val.fract()} + F64Base{val : self.val.round() % modulus.val.round()} } } diff --git a/src/quantity/scalar/mod.rs b/src/quantity/scalar/mod.rs index b3f4f32..8f8b8ed 100644 --- a/src/quantity/scalar/mod.rs +++ b/src/quantity/scalar/mod.rs @@ -1,5 +1,6 @@ //const FLOAT_PRECISION: u32 = 1024; -const PRINT_LEN: usize = 5; // How many significant digits we will show in output +const SHOW_SIG: usize = 5; // How many significant digits we will show in output +const MAX_LEN: usize = 5; // If a scientific exponent is >= this value, do not use scientific notation. pub(in self) mod rationalbase; diff --git a/src/tests.rs b/src/tests.rs index 51342b3..d54f10f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -131,7 +131,7 @@ fn operators() { good_expr("125", "5^(+3)"); good_expr("125", "+5^3"); - good_expr("0.2148", "3 ^ (-1.4)"); + good_expr("0.21479", "3 ^ (-1.4)"); // Should parse as ((2^3)^4)^5 good_expr("1.1529e18", "2^3^4^5");