office: signed-exponent number literals parse as one number
- Commit
f7b478c405960c0d19df765ddcc58668a6f4c15f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 11:48 UTC (4 days ago)
- Parents
273e09d
The formula number scanner consumed digits/./E but stopped at the exponent sign: =1E-3 scanned '1E' (parse fails, falls back to 0.0), then '-3' parsed as binary subtraction — result -3 instead of 0.001. =1E+3 gave 3, =2.5E-2 likewise wrong. The tokenizer now consumes E/e plus an optional sign only when a digit follows, plus the exponent digits, so the sign stays inside the literal; a bare trailing E is still not consumed. Rust regression tests cover 1E-3, 1E+3, 2.5E-2, a compound expression and an unsigned guard, plus a Kotlin native test through OfficeNative. One hunk was compacted to keep the file under the 500-line ratchet. Verified: cargo test -p office_engine 105 passed; :office:testDevUnitTest 0 failures; lint errors are pre-existing on untouched files.
Diff
office/src/main/rust/src/formula_part3.rs
diff --git a/office/src/main/rust/src/formula_part3.rs b/office/src/main/rust/src/formula_part3.rsindex 474e5dac2f..29e4f4de89 100644--- a/office/src/main/rust/src/formula_part3.rs+++ b/office/src/main/rust/src/formula_part3.rs@@ -264,12 +264,22 @@ impl Parser { let start = self.pos; while self.pos < self.s.len() { let c = self.s[self.pos];- if c.is_ascii_digit() || c == '.' || c == 'E' || c == 'e' {+ if c.is_ascii_digit() || c == '.' { self.pos += 1; } else { break; } }+ // A signed exponent belongs to the number ("1E-3", "2.5E+2"): consume E/e+ // plus its optional sign only when digits follow, lest '-' turn binary.+ if self.pos < self.s.len() && matches!(self.s[self.pos], 'E' | 'e') {+ let mut exp = self.pos + 1;+ if exp < self.s.len() && matches!(self.s[exp], '+' | '-') { exp += 1; }+ if exp < self.s.len() && self.s[exp].is_ascii_digit() {+ self.pos = exp;+ while self.pos < self.s.len() && self.s[self.pos].is_ascii_digit() { self.pos += 1; }+ }+ } let mut value = self.substr(start, self.pos).parse::<f64>().unwrap_or(0.0); self.skip_ws(); if self.peek() == '%' {
office/src/main/rust/src/formula_tests.rs
diff --git a/office/src/main/rust/src/formula_tests.rs b/office/src/main/rust/src/formula_tests.rsindex 34264ac976..78d52e60df 100644--- a/office/src/main/rust/src/formula_tests.rs+++ b/office/src/main/rust/src/formula_tests.rs@@ -106,6 +106,17 @@ fn extended_math() { assert_eq!(eval("of:=POWER(2;10)"), "1024"); } +#[test]+fn signed_exponent_literals() {+ // Sheet1!C1 holds each probe; Excel/OpenFormula read a signed exponent as+ // part of the number, not as a binary +/- operator.+ assert_eq!(eval("of:=1E-3"), "0.001");+ assert_eq!(eval("of:=1E+3"), "1000");+ assert_eq!(eval("of:=2.5E-2"), "0.025");+ assert_eq!(eval("of:=1.5E2*2"), "300");+ assert_eq!(eval("of:=1E3"), "1000");+}+ #[test] fn trig_and_hyperbolic() { assert_eq!(eval("of:=SIN(0)"), "0");
office/src/test/java/com/vayunmathur/office/ui/Slice4SheetsTest.kt
diff --git a/office/src/test/java/com/vayunmathur/office/ui/Slice4SheetsTest.kt b/office/src/test/java/com/vayunmathur/office/ui/Slice4SheetsTest.ktindex 9c97218fb0..bc39be422b 100644--- a/office/src/test/java/com/vayunmathur/office/ui/Slice4SheetsTest.kt+++ b/office/src/test/java/com/vayunmathur/office/ui/Slice4SheetsTest.kt@@ -263,6 +263,19 @@ class Slice4SheetsTest { } } + @Test fun signedExponentLiteralEvaluates() {+ // Grid: A1 holds =1E-3 (as its ODF form). A signed exponent is part of+ // the number, so the display value is 0.001, not -3 (0 - 3).+ val h = OfficeNative.createWorkbook(+ listOf(wbSheet(listOf(OdfCell("", formula = "of:=1E-3")))), 0L)+ try {+ assertEquals("0.001", OfficeNative.nativeDisplayValue(h, 0, 0, 0))+ assertTrue(OfficeNative.nativeIsNumeric(h, 0, 0, 0))+ } finally {+ OfficeNative.nativeFree(h)+ }+ }+ @Test fun outOfRangeIsEmpty() { val h = OfficeNative.createWorkbook(listOf(wbSheet(listOf(OdfCell("a")))), 0L) try {