mirror of
https://github.com/PaddiM8/kalker.git
synced 2024-12-13 18:10:42 +01:00
fixed large rounding errors
This commit is contained in:
parent
c3768c7e34
commit
ed98a8d333
@ -16,11 +16,9 @@ pub fn eval(parser: &mut parser::Context, input: &str) {
|
||||
|
||||
let num = if exp <= 0 {
|
||||
// 0 < x < 1
|
||||
round(
|
||||
format!("0.{}{}", "0".repeat(exp.abs() as usize), digits)
|
||||
.trim_end_matches('0'),
|
||||
)
|
||||
.to_string()
|
||||
.trim_end_matches('0')
|
||||
.to_string()
|
||||
} else if use_sci_notation || result.fract() != 0 {
|
||||
// Insert the comma if there are supposed to be decimals.
|
||||
let mut chars: Vec<char> = digits
|
||||
@ -29,7 +27,7 @@ pub fn eval(parser: &mut parser::Context, input: &str) {
|
||||
.chars()
|
||||
.collect();
|
||||
chars.insert(comma_pos, '.');
|
||||
round(&chars.into_iter().collect::<String>()).to_string()
|
||||
chars.into_iter().collect::<String>()
|
||||
} else {
|
||||
// Regular number
|
||||
digits[..(exp as usize)].to_string()
|
||||
@ -51,39 +49,6 @@ pub fn print_err(msg: &str) {
|
||||
println!("{}", Red.paint(msg));
|
||||
}
|
||||
|
||||
fn round(value: &str) -> String {
|
||||
let mut value_iter = value.chars().into_iter().rev().skip(1);
|
||||
let last_char = value_iter.next().unwrap();
|
||||
let mut last_char_count = 1;
|
||||
|
||||
// Find out how many repeating trailing (equal) characters there are
|
||||
for c in value_iter {
|
||||
if c == last_char {
|
||||
last_char_count += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Don't round if there aren't that many
|
||||
if last_char_count < 5 {
|
||||
return value.to_string();
|
||||
}
|
||||
|
||||
// Remove zeroes.
|
||||
// If it's nines, round it up.
|
||||
// Otherwise, only show 3 of them and then three dots:
|
||||
match last_char {
|
||||
'0' => value[..value.len() - last_char_count - 1].to_string(),
|
||||
'9' => value.parse::<f32>().unwrap().ceil().to_string(),
|
||||
_ => format!(
|
||||
"{}{}...",
|
||||
&value[..value.len() - last_char_count - 1],
|
||||
last_char.to_string().repeat(3)
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn print_calc_err(err: CalcError) {
|
||||
print_err(&match err {
|
||||
IncorrectAmountOfArguments(expected, func, got) => format!(
|
||||
|
Loading…
Reference in New Issue
Block a user