diff --git a/src/main.rs b/src/main.rs index 368ef3d..5a73914 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,9 +28,8 @@ pub fn parse_and_calc(s: &str) -> Float { } pub fn get_significant_digits(n: &Float) -> usize { - let raw_string = format!("{n}"); + let raw_string: String = format!("{n}").chars().filter(|c| *c != '.').collect(); - let mut significant_digits = raw_string.len() - 1; let mut consecutive_zeros = 0; let mut consecutive_nines = 0; for (i, c) in raw_string.char_indices() { @@ -47,20 +46,18 @@ pub fn get_significant_digits(n: &Float) -> usize { } if consecutive_zeros >= 10 { - significant_digits = i - consecutive_zeros; - break; + return i + 1 - consecutive_zeros; } else if consecutive_nines >= 10 { - significant_digits = i - consecutive_nines; - break; + return i + 1 - consecutive_nines; } if !raw_string[i..raw_string.len()].contains(|c| matches!(c, '1'..='9')) { - significant_digits = i; - break; + return i; } } - significant_digits + // all digits are significant + raw_string.len() } #[test] @@ -71,8 +68,12 @@ fn test_get_significant_digits() { assert_eq!(many_nines.to_f64(), 44.0084); let zero_nine_zero = parse_and_calc("2+12321(5+123)"); assert_eq!(zero_nine_zero.to_f64(), 1577090.0); + let test_4 = + parse_and_calc("12.0096 + 1.00784 * 3 + 12.0096 + 1.00784 * 2 + 15.9994 + 1.00784"); + assert_eq!(test_4.to_f64(), 46.06564); assert_eq!(get_significant_digits(&many_zeros), 8); assert_eq!(get_significant_digits(&many_nines), 6); assert_eq!(get_significant_digits(&zero_nine_zero), 6); + assert_eq!(get_significant_digits(&test_4), 7); } diff --git a/src/nodes/from_str.rs b/src/nodes/from_str.rs index 94a4af4..ba94dfa 100644 --- a/src/nodes/from_str.rs +++ b/src/nodes/from_str.rs @@ -11,6 +11,8 @@ impl Node { "c" => Self::Number(Float::with_val(PREC, 299_792_458)), "A" => Self::Number(Float::with_val(PREC, 6.02214076e23)), "pi" => Self::Number(Float::with_val(PREC, f64::consts::PI)), + // Planck's constant, in J*s (joule-seconds) + "h" => Self::Number(Float::with_val(PREC, 6.62607015e-34)), _ => Self::Function(s.parse()?), }) }