Compare commits

...
Sign in to create a new pull request.
Author SHA1 Message Date
140ce061a3
test: add real world example
this is what failed before the last fix;
it was formatted to "0.-125"
2026-11-18 19:24:59 -08:00
c2b91fb774
refactor: put back format!
I think this is still a little nicer to read
2026-11-18 19:24:25 -08:00
Edeth-Baguette
4a8a08f66a fix: account for negative sign 2026-11-18 19:16:39 -08:00
667f2e8eeb
refactor(formatting): no premature optimization 2026-11-16 15:27:31 -08:00
aef4b4727a
refactor(formatting): use helper function
this is probably much more efficient,
since `"0".repeat(n)` allocates a new string
(which is then immediately deallocated)
2026-11-16 15:08:56 -08:00
7a1049fda9
docs(formatting): add note 2026-11-15 21:39:59 -08:00
f60c8749a0
refactor(formatting): remove unneeded logic 2026-11-15 21:23:20 -08:00
Edeth-Baguette
42692d3ae4 feat: add (fully?) working sci notation correcter 2026-11-15 21:09:18 -08:00

View file

@ -11,22 +11,69 @@ pub fn pretty_eval(input: &str, max_digits: usize) -> Result<String, Error> {
let should_add_back_backslash = input.contains('\\'); let should_add_back_backslash = input.contains('\\');
let result = nodes::eval(&input)?; let result = nodes::eval(&input)?;
let display_digits = get_significant_digits(&result).clamp( let display_digits = get_significant_digits(&result).clamp(1, max_digits);
if result >= 1000 || result < 1 {
1 // this helps improve the output formatting,
} else if result >= 100 { // by not using scientific notation when it doesn't make sense.
3 //
} else if result >= 10 { // it does this by parsing the string, which is unfortunate.
2 // ideally, this would be done by reimplementing the display code;
// if result >= 1 // 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 {
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 { } else {
1 let uexp = exponent.unsigned_abs();
}, if let Some(dot_pos) = changing_string.find('.') {
max_digits, changing_string.remove(dot_pos);
); }
changing_string.insert_str(
if changing_string.contains('-') { 1 } else { 0 },
&format!("0.{}", "0".repeat(uexp - 1)),
);
}
}
Ok(format!( Ok(format!(
"{prefix}{deliminator}{result:.display_digits$}{}", "{prefix}{deliminator}{changing_string}{}",
if should_add_back_backslash { " \\" } else { "" } if should_add_back_backslash { " \\" } else { "" }
) )
.replace('e', " * 10^")) .replace('e', " * 10^"))
@ -45,6 +92,16 @@ impl StrExt for str {
.unwrap_or(("", "", self)) .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 { pub fn get_significant_digits(n: &Float) -> usize {
let raw_string = n.to_string().replace(['.', '-'], ""); let raw_string = n.to_string().replace(['.', '-'], "");
@ -112,7 +169,7 @@ mod tests {
assert_pretty_eval!("10"); assert_pretty_eval!("10");
assert_pretty_eval!("100"); assert_pretty_eval!("100");
assert_pretty_eval!("999"); assert_pretty_eval!("999");
assert_pretty_eval!("1000", "1 * 10^3"); assert_pretty_eval!("1000");
assert_pretty_eval!("11.111111"); assert_pretty_eval!("11.111111");
} }
@ -140,6 +197,22 @@ mod tests {
assert_pretty_eval!("10_000 * 10_000 * 4", "4 * 10^8") 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] #[test]
fn basic_get_significant_digits() { fn basic_get_significant_digits() {
let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4"); let many_zeros = eval("14.00643 + 1.00784 * 4 + 35.446 + 15.9994 * 4");