Compare commits

...
Author SHA1 Message Date
6e141a0132
refactor(significant_digits): obsolete temp value
also use the correct value for if all digits are significant,
since we filter out the dot(s). in practice though, this is very rare.
2026-07-21 13:29:38 -07:00
f9d4290659
fix(significant_digits): ignore dot completely 2026-07-21 13:21:03 -07:00
c108d3a2d2
fix(significant_digits): account for no dot or many dot 2026-07-21 13:20:47 -07:00
2b7884e802
feat: add Plank's constant 'h' 2026-07-21 10:31:00 -07:00
2 changed files with 12 additions and 9 deletions

View file

@ -28,9 +28,8 @@ pub fn parse_and_calc(s: &str) -> Float {
} }
pub fn get_significant_digits(n: &Float) -> usize { 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_zeros = 0;
let mut consecutive_nines = 0; let mut consecutive_nines = 0;
for (i, c) in raw_string.char_indices() { for (i, c) in raw_string.char_indices() {
@ -47,20 +46,18 @@ pub fn get_significant_digits(n: &Float) -> usize {
} }
if consecutive_zeros >= 10 { if consecutive_zeros >= 10 {
significant_digits = i - consecutive_zeros; return i + 1 - consecutive_zeros;
break;
} else if consecutive_nines >= 10 { } else if consecutive_nines >= 10 {
significant_digits = i - consecutive_nines; return i + 1 - consecutive_nines;
break;
} }
if !raw_string[i..raw_string.len()].contains(|c| matches!(c, '1'..='9')) { if !raw_string[i..raw_string.len()].contains(|c| matches!(c, '1'..='9')) {
significant_digits = i; return i;
break;
} }
} }
significant_digits // all digits are significant
raw_string.len()
} }
#[test] #[test]
@ -71,8 +68,12 @@ fn test_get_significant_digits() {
assert_eq!(many_nines.to_f64(), 44.0084); assert_eq!(many_nines.to_f64(), 44.0084);
let zero_nine_zero = parse_and_calc("2+12321(5+123)"); let zero_nine_zero = parse_and_calc("2+12321(5+123)");
assert_eq!(zero_nine_zero.to_f64(), 1577090.0); 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_zeros), 8);
assert_eq!(get_significant_digits(&many_nines), 6); assert_eq!(get_significant_digits(&many_nines), 6);
assert_eq!(get_significant_digits(&zero_nine_zero), 6); assert_eq!(get_significant_digits(&zero_nine_zero), 6);
assert_eq!(get_significant_digits(&test_4), 7);
} }

View file

@ -11,6 +11,8 @@ impl Node {
"c" => Self::Number(Float::with_val(PREC, 299_792_458)), "c" => Self::Number(Float::with_val(PREC, 299_792_458)),
"A" => Self::Number(Float::with_val(PREC, 6.02214076e23)), "A" => Self::Number(Float::with_val(PREC, 6.02214076e23)),
"pi" => Self::Number(Float::with_val(PREC, f64::consts::PI)), "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()?), _ => Self::Function(s.parse()?),
}) })
} }