From 3c20174ecb952d359b0b4a342e7daa8187eb6118 Mon Sep 17 00:00:00 2001 From: electria Date: Wed, 30 Sep 2026 20:10:03 -0700 Subject: [PATCH 1/5] feat: prettyify scientific notation this way it's also typst-compatible! --- src/formatting.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/formatting.rs b/src/formatting.rs index 5292282..fa18bd2 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -34,7 +34,8 @@ pub fn pretty_eval(mut input: String) -> Result { "" }, if should_add_back_backslash { " \\" } else { "" } - )) + ) + .replace('e', " * 10^")) } #[test] fn test_character_readdition() { From ef9d07923e24c4a1854dd7b3d311486260785af3 Mon Sep 17 00:00:00 2001 From: electria Date: Fri, 2 Oct 2026 13:09:43 -0700 Subject: [PATCH 2/5] refactor: simplify raw_string creation --- src/formatting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formatting.rs b/src/formatting.rs index fa18bd2..e893d9b 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -46,7 +46,7 @@ fn test_character_readdition() { } pub fn get_significant_digits(n: &Float) -> usize { - let raw_string: String = format!("{n}").chars().filter(|c| *c != '.').collect(); + let raw_string = n.to_string().replace('.', ""); let mut consecutive_zeros = 0; let mut consecutive_nines = 0; From c745d6b5d2e9960c44be9542ac5d6a674f60ed06 Mon Sep 17 00:00:00 2001 From: electria Date: Fri, 2 Oct 2026 13:22:49 -0700 Subject: [PATCH 3/5] feat: don't print scientific notation for n < 1000 unfortunately it isn't this easy for n > 0.001, a similar strategy seems to prefer printing eg 1.0 * 10^-1 --- src/formatting.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/formatting.rs b/src/formatting.rs index e893d9b..cf3f6e5 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -19,10 +19,19 @@ pub fn pretty_eval(mut input: String) -> Result { .is_some(); let result = nodes::eval(&input)?; - let significant_digits = get_significant_digits(&result).min(MAX_DIGITS); + let display_digits = if result >= 1000 || result < 1 { + get_significant_digits(&result).min(MAX_DIGITS) + } else if result >= 100 { + 3 + } else if result >= 10 { + 2 + // if result >= 1 + } else { + 1 + }; Ok(format!( - "{}{}{result:.significant_digits$}{}", + "{}{}{result:.display_digits$}{}", " ".repeat(leading_spaces), if should_add_back_equals && should_add_back_alignment { "& = " @@ -44,6 +53,14 @@ fn test_character_readdition() { assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11"); assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\"); } +#[test] +fn test_basic_up_to_999() { + assert_eq!(pretty_eval("1".into()).unwrap(), "1"); + assert_eq!(pretty_eval("10".into()).unwrap(), "10"); + assert_eq!(pretty_eval("100".into()).unwrap(), "100"); + assert_eq!(pretty_eval("999".into()).unwrap(), "999"); + assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3"); +} pub fn get_significant_digits(n: &Float) -> usize { let raw_string = n.to_string().replace('.', ""); From 098df2aef2b7100bae6277f763588a439ce4fb93 Mon Sep 17 00:00:00 2001 From: electria Date: Fri, 2 Oct 2026 13:32:53 -0700 Subject: [PATCH 4/5] fix: ignoring decimal place accidentally --- src/formatting.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/formatting.rs b/src/formatting.rs index cf3f6e5..b157564 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -19,16 +19,19 @@ pub fn pretty_eval(mut input: String) -> Result { .is_some(); let result = nodes::eval(&input)?; - let display_digits = if result >= 1000 || result < 1 { - get_significant_digits(&result).min(MAX_DIGITS) - } else if result >= 100 { - 3 - } else if result >= 10 { - 2 - // if result >= 1 - } else { - 1 - }; + let display_digits = + get_significant_digits(&result) + .min(MAX_DIGITS) + .max(if result >= 1000 || result < 1 { + 1 + } else if result >= 100 { + 3 + } else if result >= 10 { + 2 + // if result >= 1 + } else { + 1 + }); Ok(format!( "{}{}{result:.display_digits$}{}", @@ -60,6 +63,8 @@ fn test_basic_up_to_999() { assert_eq!(pretty_eval("100".into()).unwrap(), "100"); assert_eq!(pretty_eval("999".into()).unwrap(), "999"); assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3"); + + assert_eq!(pretty_eval("11.111111".into()).unwrap(), "11.111111"); } pub fn get_significant_digits(n: &Float) -> usize { From ba33a715012c81cc8bedeeb29fcf3664aed5388d Mon Sep 17 00:00:00 2001 From: electria Date: Fri, 2 Oct 2026 13:37:16 -0700 Subject: [PATCH 5/5] feat: increase PREC 512 -> 2048 --- src/nodes/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index f435db4..da7c34e 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -25,7 +25,7 @@ pub fn eval(s: &str) -> Result { Ok(Nodes::from_str(s)?.evaluate()?) } -pub const PREC: u32 = 512; +pub const PREC: u32 = 2048; #[derive(Debug, Default, Clone, PartialEq)] pub struct Nodes(Vec);