Modern Apps git

← Modern-Apps.git

calculator: unit division groups its divisor (10m/2h is metres per hour)

Commit
1e617048bc7a7d6e99319bc24f9020d1fa0b2884
Author
Vayun Mathur <[email protected]>
When
2026-12-01 08:23 UTC (5 days ago)
Parents
9960b32
parseTermStep parsed the right operand of / and % as a
single factor (just the number), so the trailing unit was
consumed by the outer term loop as implicit multiplication
applied to the whole quotient: 10m/2h computed (10m/2)*h =
18000 with dimension LENGTH*TIME instead of 0.00138889 m/s
(SPEED); 10m%3m gave 1 m^2 instead of 1 m. Parenthesised
10m/(2h) always worked, which is why the existing
(all-parenthesised) tests missed it.

The divisor is now parsed by parseDivisor, which absorbs a
directly-following unit into the divisor — narrow on
purpose: only unit tokens bind that way (excluding like-
named function calls), so scalar semantics are unchanged
(1/2x still means (1/2)*x, 6/2(1+2) still equals 9; both
pinned by a new test). Regression tests cover 10m/2h,
1nmi/1h, 10m/4s dimensions and 10m%3m. Precedence, trig,
currency and formatting were audited and cleared. Verified:
:calculator:testDevUnitTest 225 tests pass; lint errors are
pre-existing on untouched files.

Diff

calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.kt b/calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.ktindex 7e72e3cb4d..e940b6f7a3 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.kt@@ -311,7 +311,34 @@ class Expression private constructor(private val root: Node) {                 '(', '|' -> '*'                 else -> if (isImplicitFactorStart(c)) '*' else return null             }-            return Binary(op, left, parseFactor())+            return if (op == '/' || op == '%') Binary(op, left, parseDivisor()) else Binary(op, left, parseFactor())+        }++        /**+         * The right operand of `/` or `%`: a factor plus any unit directly following it.+         * A number and its unit are one quantity (`2h` is two hours), so that unit belongs+         * to the divisor — letting the term loop take it instead implicit-multiplies the+         * whole quotient, which made `10m/2h` metres-times-hours rather than metres-per-hour+         * and `10m%3m` an area rather than a length. Only units bind this way: a following+         * variable, group or function stays a term-level implicit factor, so `1/2x` and+         * `6/2(1+2)` keep their left-to-right meaning.+         */+        private fun parseDivisor(): Node {+            var node = parseFactor()+            while (isUnitAhead()) node = Binary('*', node, parseFactor())+            return node+        }++        /** Whether the next token is a unit spelling (not a call to a like-named function). */+        private fun isUnitAhead(): Boolean {+            var i = pos+            while (i < src.length && src[i].isWhitespace()) i+++            if (i >= src.length || !src[i].isLetter()) return false+            val start = i+            while (i < src.length && (src[i].isLetterOrDigit() || src[i] == '_')) i+++            if (src.substring(start, i) !in UnitRegistry.parseTokens) return false+            while (i < src.length && src[i].isWhitespace()) i+++            return i >= src.length || src[i] != '('         }          /** Whether [c] can start an implicit-multiplication factor (`2x`, `2√x`, `2θ`). */
calculator/src/test/java/com/vayunmathur/calculator/util/ExpressionTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/util/ExpressionTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/util/ExpressionTest.ktindex 7ab3f32cc8..b1011ca588 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/ExpressionTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/ExpressionTest.kt@@ -94,6 +94,14 @@ class ExpressionTest {         assertEquals(7.0, eval("1 + 2x", x = 3.0))     } +    @Test+    fun divisionByAVariableOrGroupStaysLeftToRight() {+        // Only a unit binds to the divisor's number (see UnitsTest); a variable+        // or group after a division remains a term-level implicit factor.+        assertEquals(2.0, eval("1/2x", x = 4.0))+        assertEquals(9.0, eval("6/2(1+2)"))+    }+     // ---- numbers ----      @Test
calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.ktindex 0f42593117..963eca1bb6 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.kt@@ -124,6 +124,35 @@ class UnitsTest {         assertClose(1.0, q("1N").value)     } +    @Test+    fun dividedUnitQuantityStaysInTheDenominator() {+        // Regression: the divisor parsed only its number, so the unit after it was+        // implicit-multiplied into the whole quotient — `10m/2h` came out as+        // metres-times-hours (18000) and `1nmi/1h` as 6667200 of them.+        val speed = q("10m/2h")+        assertEquals(Dimension.SPEED, speed.dimension)+        assertClose(10.0 / 7200.0, speed.value, tolerance = 1e-12)+        assertClose(0.005, displayValueIn(speed, unit("kmh")), tolerance = 1e-12)+        // The parenthesised form always worked and must agree.+        assertClose(speed.value, q("10m/(2h)").value, tolerance = 1e-12)++        val knots = q("1nmi/1h")+        assertEquals(Dimension.SPEED, knots.dimension)+        assertClose(1852.0 / 3600.0, knots.value, tolerance = 1e-12)++        val metresPerSecond = q("10m/4s")+        assertEquals(Dimension.SPEED, metresPerSecond.dimension)+        assertClose(2.5, metresPerSecond.value)+    }++    @Test+    fun remainderUnitQuantityStaysInTheDivisor() {+        // Same mis-grouping on `%`: unparenthesised this returned 1 m², not 1 m.+        val r = q("10m%3m")+        assertEquals(Dimension.LENGTH, r.dimension)+        assertClose(1.0, r.value)+    }+     @Test     fun multiplyingUnitsProducesADerivedDimension() {         // force × length = energy