Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
6e141a0132 |
|||
|
f9d4290659 |
|||
|
c108d3a2d2 |
|||
|
2b7884e802 |
2 changed files with 12 additions and 9 deletions
19
src/main.rs
19
src/main.rs
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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()?),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue