Compare commits
2 changed files with 9 additions and 12 deletions
19
src/main.rs
19
src/main.rs
|
|
@ -28,8 +28,9 @@ pub fn parse_and_calc(s: &str) -> Float {
|
|||
}
|
||||
|
||||
pub fn get_significant_digits(n: &Float) -> usize {
|
||||
let raw_string: String = format!("{n}").chars().filter(|c| *c != '.').collect();
|
||||
let raw_string = format!("{n}");
|
||||
|
||||
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() {
|
||||
|
|
@ -46,18 +47,20 @@ pub fn get_significant_digits(n: &Float) -> usize {
|
|||
}
|
||||
|
||||
if consecutive_zeros >= 10 {
|
||||
return i + 1 - consecutive_zeros;
|
||||
significant_digits = i - consecutive_zeros;
|
||||
break;
|
||||
} else if consecutive_nines >= 10 {
|
||||
return i + 1 - consecutive_nines;
|
||||
significant_digits = i - consecutive_nines;
|
||||
break;
|
||||
}
|
||||
|
||||
if !raw_string[i..raw_string.len()].contains(|c| matches!(c, '1'..='9')) {
|
||||
return i;
|
||||
significant_digits = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// all digits are significant
|
||||
raw_string.len()
|
||||
significant_digits
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -68,12 +71,8 @@ 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ 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()?),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue