Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
140ce061a3 |
|||
|
c2b91fb774 |
|||
|
|
4a8a08f66a | ||
|
667f2e8eeb |
|||
|
aef4b4727a |
|||
|
7a1049fda9 |
|||
|
f60c8749a0 |
|||
|
|
42692d3ae4 |
1 changed files with 87 additions and 14 deletions
|
|
@ -11,22 +11,69 @@ pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
|
|||
let should_add_back_backslash = input.contains('\\');
|
||||
|
||||
let result = nodes::eval(&input)?;
|
||||
let display_digits = get_significant_digits(&result).clamp(
|
||||
if result >= 1000 || result < 1 {
|
||||
1
|
||||
} else if result >= 100 {
|
||||
3
|
||||
} else if result >= 10 {
|
||||
2
|
||||
// if result >= 1
|
||||
let display_digits = get_significant_digits(&result).clamp(1, max_digits);
|
||||
|
||||
// this helps improve the output formatting,
|
||||
// by not using scientific notation when it doesn't make sense.
|
||||
//
|
||||
// it does this by parsing the string, which is unfortunate.
|
||||
// ideally, this would be done by reimplementing the display code;
|
||||
// but neither I nor Edeth know how to do that yet.
|
||||
// (much like with `get_significant_digits`)
|
||||
let mut changing_string = format!("{result:.display_digits$}");
|
||||
if let Some(index) = changing_string.find('e')
|
||||
&& ((changing_string.chars().nth(index + 1).unwrap() == '-'
|
||||
&& changing_string
|
||||
.chars()
|
||||
.nth(index + 2)
|
||||
.unwrap()
|
||||
.to_digit(10)
|
||||
.unwrap()
|
||||
< 6
|
||||
&& changing_string.len() - index == 3)
|
||||
|| (changing_string.chars().nth(index + 1).unwrap() != '-'
|
||||
&& changing_string
|
||||
.chars()
|
||||
.nth(index + 1)
|
||||
.unwrap()
|
||||
.to_digit(10)
|
||||
.unwrap()
|
||||
< 6
|
||||
&& changing_string.len() - index == 2))
|
||||
{
|
||||
let exponent: isize = changing_string[index + 1..].parse().unwrap();
|
||||
changing_string.drain(index..);
|
||||
if exponent > 0 {
|
||||
let uexp = exponent as usize;
|
||||
if let Some(dot_pos) = changing_string.find('.') {
|
||||
if changing_string.len() - dot_pos - 1 == uexp {
|
||||
changing_string.remove(dot_pos);
|
||||
} else if changing_string.len() - dot_pos - 1 > uexp {
|
||||
let new_pos = dot_pos + uexp;
|
||||
changing_string.remove(dot_pos);
|
||||
changing_string.insert(new_pos, '.');
|
||||
} else {
|
||||
1
|
||||
},
|
||||
max_digits,
|
||||
let amount_of_zeros = uexp - (changing_string.len() - dot_pos - 1);
|
||||
changing_string.remove(dot_pos);
|
||||
changing_string.push_n('0', amount_of_zeros);
|
||||
}
|
||||
} else {
|
||||
changing_string.push_n('0', uexp);
|
||||
}
|
||||
} else {
|
||||
let uexp = exponent.unsigned_abs();
|
||||
if let Some(dot_pos) = changing_string.find('.') {
|
||||
changing_string.remove(dot_pos);
|
||||
}
|
||||
changing_string.insert_str(
|
||||
if changing_string.contains('-') { 1 } else { 0 },
|
||||
&format!("0.{}", "0".repeat(uexp - 1)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(format!(
|
||||
"{prefix}{deliminator}{result:.display_digits$}{}",
|
||||
"{prefix}{deliminator}{changing_string}{}",
|
||||
if should_add_back_backslash { " \\" } else { "" }
|
||||
)
|
||||
.replace('e', " * 10^"))
|
||||
|
|
@ -45,6 +92,16 @@ impl StrExt for str {
|
|||
.unwrap_or(("", "", self))
|
||||
}
|
||||
}
|
||||
trait StringExt {
|
||||
fn push_n(&mut self, c: char, n: usize);
|
||||
}
|
||||
impl StringExt for String {
|
||||
fn push_n(&mut self, c: char, n: usize) {
|
||||
for _ in 0..n {
|
||||
self.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_significant_digits(n: &Float) -> usize {
|
||||
let raw_string = n.to_string().replace(['.', '-'], "");
|
||||
|
|
@ -112,7 +169,7 @@ mod tests {
|
|||
assert_pretty_eval!("10");
|
||||
assert_pretty_eval!("100");
|
||||
assert_pretty_eval!("999");
|
||||
assert_pretty_eval!("1000", "1 * 10^3");
|
||||
assert_pretty_eval!("1000");
|
||||
|
||||
assert_pretty_eval!("11.111111");
|
||||
}
|
||||
|
|
@ -140,6 +197,22 @@ mod tests {
|
|||
assert_pretty_eval!("10_000 * 10_000 * 4", "4 * 10^8")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prettier_sci_notation() {
|
||||
assert_pretty_eval!("1 * 10^6");
|
||||
assert_pretty_eval!("10^5", "100000");
|
||||
assert_pretty_eval!("1 * 10^(-6)", "1 * 10^-6");
|
||||
assert_pretty_eval!("10^(-5)", "0.00001");
|
||||
assert_pretty_eval!("0.0001");
|
||||
assert_pretty_eval!("1.0901");
|
||||
assert_pretty_eval!("10901");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatting_minus() {
|
||||
assert_pretty_eval!("1/(2^3 * (-1)^(2 - 1))", "-0.125");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_get_significant_digits() {
|
||||
let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4");
|
||||
|
|
|
|||
Loading…
Reference in a new issue