Modern Apps git

← Modern-Apps.git

calculator: instant tokens no longer truncate fractional seconds

Commit
09663c9a63003c48c1f41004f694e069429b3caa
Author
Vayun Mathur <[email protected]>
When
2026-12-01 01:41 UTC (5 days ago)
Parents
a9b8cbe
Evaluating '#1000000 + 500ms' produced the instant
1000000.5, but formatInstant's token form wrote back
'#1000000' — toLong() discarded the fraction while claiming
resultToken is 'the same value in re-parsable form'. Pressing
= again, or reusing the result from history, silently moved
the instant by up to a second. The display side compounded
it: INSTANT_TOKEN matched digits only, leaving the '.5'
dangling in the input line (Jan 12, 1970, 5:46 AM.5).

Token form now emits the shortest round-tripping decimal
(whole seconds unchanged, non-finite keeps the old fallback),
and the token regex consumes the fraction, which the parser
already accepted. Three regression tests failed pre-fix on
exactly these losses. Expression precedence, units,
temperature conversion and rounding display were audited and
cleared. Verified: :calculator:testDevUnitTest 222 tests
pass; lint errors are pre-existing on untouched files.

Diff

calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.kt b/calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.ktindex 61caa1df9e..4efbca6d80 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.kt@@ -80,7 +80,7 @@ fun formatQuantity(q: Quantity, unit: UnitDef?, useToken: Boolean = false): Stri  * midnight check is what makes `date + duration` show a time only once one is actually present.  */ fun formatInstant(epochSeconds: Double, useToken: Boolean = false): String {-    if (useToken) return "#" + epochSeconds.toLong()+    if (useToken) return "#" + epochSecondsToken(epochSeconds)     // A user-typed `#<digits>` token can name an epoch far outside the     // range java.time.Instant supports (or overflow to Infinity, whose     // toLong() saturates to Long.MAX_VALUE): ofEpochSecond then throws@@ -99,7 +99,23 @@ fun formatInstant(epochSeconds: Double, useToken: Boolean = false): String {     return zoned.format(formatter) } -private val INSTANT_TOKEN = Regex("#(-?\\d+)")+/**+ * The re-parsable spelling of an epoch-seconds value. Whole seconds keep the+ * plain integer form (`#1000000`); a fractional part (an instant plus a+ * sub-second duration, e.g. `#1000000 + 500ms`) must survive, because the+ * parser reads it back and truncating it would silently move the instant.+ * [Double.toString] gives the shortest string that round-trips to the same+ * value, and [BigDecimal.toPlainString] expands its scientific form into the+ * plain digits the parser's instant literal accepts.+ */+private fun epochSecondsToken(epochSeconds: Double): String =+    if (epochSeconds.isFinite()) {+        BigDecimal(epochSeconds.toString()).stripTrailingZeros().toPlainString()+    } else {+        epochSeconds.toLong().toString()+    }++private val INSTANT_TOKEN = Regex("#(-?\\d+(?:\\.\\d+)?)")  /** Replaces raw `#<epoch>` date tokens in an expression string with their localized text, for  * showing the input line. Non-date parts are left untouched. */
calculator/src/test/java/com/vayunmathur/calculator/util/FormattingEdgeTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/util/FormattingEdgeTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/util/FormattingEdgeTest.ktindex 6767adb870..cedc03772f 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/FormattingEdgeTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/FormattingEdgeTest.kt@@ -104,6 +104,25 @@ class FormattingEdgeTest {         assertEquals("#-5", formatInstant(-5.0, useToken = true))     } +    @Test+    fun instantTokenFormPreservesFractionalSeconds() {+        // A date plus a sub-second duration (e.g. `#1000000 + 500ms`) is an+        // instant at 1000000.5. The token form claims to be the same value in+        // re-parsable form, so it must not truncate the fraction away.+        assertEquals("#1000000.5", formatInstant(1000000.5, useToken = true))+        assertEquals("#-4.5", formatInstant(-4.5, useToken = true))+        val reparsed = Expression.parse(formatInstant(1000000.5, useToken = true)).evalQuantity()+        assertTrue(reparsed.instant)+        assertEquals(1000000.5, reparsed.value, 0.0)+    }++    @Test+    fun renderInputReplacesFractionalEpochTokensWholly() {+        val rendered = renderInputForDisplay("#1000000.5")+        assertTrue(!rendered.contains("#"), "epoch token was not replaced: $rendered")+        assertTrue(!rendered.contains(".5"), "fraction was left dangling: $rendered")+    }+     @Test     fun renderInputReplacesEpochTokens() {         val rendered = renderInputForDisplay("a #1000000 b")
calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.ktindex d56442555b..bdc3116255 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.kt@@ -185,6 +185,25 @@ class HistoryMemoryTest {         assertEquals("5", tab.input)     } +    @Test+    fun fractionalInstantSurvivesEvaluateAndHistoryReuse() {+        val tab = CalculatorTab()+        tab.updateInput("#1000000 + 500ms")+        tab.evaluate()+        assertEquals(1000000.5, tab.lastAnswer, 0.0)+        // The recorded token and the re-seeded input must still name the+        // half-second instant, not the whole second below it.+        assertEquals("#1000000.5", tab.history[0].resultToken)+        assertEquals("#1000000.5", tab.input)+        val entry = tab.history[0]+        // Pressing = again on the seeded result must not move the answer.+        tab.evaluate()+        assertEquals(1000000.5, tab.lastAnswer, 0.0)+        tab.clear()+        tab.useHistory(entry)+        assertEquals("#1000000.5", tab.input)+    }+     @Test     fun insertInstantAppendsEpochToken() {         val tab = CalculatorTab()