Modern Apps git

← Modern-Apps.git

calculator: ans and memory store the displayed absolute temperature

Commit
0917a418918883a96fad09749e24647a67fcea72
Author
Vayun Mathur <[email protected]>
When
2026-12-01 13:04 UTC (5 days ago)
Parents
f8b4d64
Quantity.value for an absolute temperature is only the
kelvin degree-size; the absolute magnitude is value +
tempOffsetK (displayValueIn already knew). Every scalar
reader used raw .value, so 212degF evaluated and displayed
373.15 K while ans kept 117.77... — neither the typed 212,
the 100 C, nor the displayed 373.15; ans - 273.15 then gave
-155.37 instead of 100. The same result spelled 100degC /
212degF / 373.15K seeded three different ans values, and
M+ had the identical defect.

Quantity.baseValue (absolute kelvin when an offset applies)
now feeds every scalar extraction: Expression.eval,
CalculatorTab (finiteness + lastAnswer), the widget answer,
and displayValueIn uses it instead of open-coding the
offset; delta arithmetic inside Quantity is untouched.
Regression tests pin ans/memory/widget across the three
spellings. Expression precedence, trig modes and unit
conversions were audited and cleared. Verified:
:calculator:testDevUnitTest 229 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 e940b6f7a3..5210673019 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/domain/Expression.kt@@ -87,11 +87,12 @@ class Expression private constructor(private val root: Node) {      /**      * Evaluate for a given free-variable value, [angle] mode and [ans], returning the numeric-     * magnitude only. Preserves the graphing path and every dimensionless caller: for a+     * magnitude only ([Quantity.baseValue], so an absolute temperature is its kelvin).+     * Preserves the graphing path and every dimensionless caller: for a      * dimensionless expression this is exactly the number, so nothing downstream changes.      */     fun eval(variable: Double = 0.0, angle: AngleMode = AngleMode.RADIANS, ans: Double = 0.0): Double =-        evalQuantity(variable, angle, ans).value+        evalQuantity(variable, angle, ans).baseValue      /** Evaluate to a full [Quantity], carrying units through the calculation. */     fun evalQuantity(variable: Double = 0.0, angle: AngleMode = AngleMode.RADIANS, ans: Double = 0.0): Quantity =
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 4efbca6d80..afcbed8dac 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/domain/Formatting.kt@@ -51,7 +51,7 @@ fun formatResultToken(value: Double): String = /** The magnitude of [q] expressed in [unit] (handling affine temperature and deltas). */ fun displayValueIn(q: Quantity, unit: UnitDef): Double =     if (q.dimension == Dimension.TEMPERATURE) {-        if (q.tempOffsetK != null) unit.fromBase(q.value + q.tempOffsetK) // absolute+        if (q.tempOffsetK != null) unit.fromBase(q.baseValue) // absolute         else q.value / unit.factorToBase // delta: degree-size only, no offset     } else {         unit.fromBase(q.value)
calculator/src/main/java/com/vayunmathur/calculator/domain/UnitsModel.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitsModel.kt b/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitsModel.ktindex cb8d55e71f..a5fab2d6d6 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitsModel.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitsModel.kt@@ -90,6 +90,18 @@ class Quantity(     val isDimensionless: Boolean get() = dimension.isDimensionless     private val isTemperature: Boolean get() = dimension == Dimension.TEMPERATURE +    /**+     * The magnitude in coherent base units, as a single number: for an absolute+     * temperature that is the absolute kelvin (`value + tempOffsetK`), and for+     * everything else (including temperature deltas) it is just [value]. This is+     * the only scalar a caller may store as "the result" (e.g. as `ans` or in the+     * memory register): [value] alone for `212degF` is 117.77… — the degree-size+     * scaled by 5/9 with the offset dropped — which is neither 212 °F nor the+     * 373.15 K the same quantity displays as.+     */+    val baseValue: Double+        get() = if (tempOffsetK != null) value + tempOffsetK else value+     operator fun unaryMinus() = Quantity(-value, dimension, tempOffsetK)      operator fun plus(other: Quantity) = addOrSub(other, 1.0)
calculator/src/main/java/com/vayunmathur/calculator/platform/CalculatorTab.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/platform/CalculatorTab.kt b/calculator/src/main/java/com/vayunmathur/calculator/platform/CalculatorTab.ktindex 20fa1f2edb..7e6d754793 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/platform/CalculatorTab.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/platform/CalculatorTab.kt@@ -97,7 +97,7 @@ class CalculatorTab {         // Non-finite results (NaN, 1/0 = ∞) have no re-parsable token form:         // recording one would replace the input with "∞"/"Undefined" and         // strand it there. Like NaN, they stay a live preview only.-        if (!quantity.value.isFinite()) return+        if (!quantity.baseValue.isFinite()) return         val isInstant = quantity.instant         val unit = if (quantity.isDimensionless || isInstant) {             null@@ -108,7 +108,9 @@ class CalculatorTab {         // The token form re-parses, so the result can seed the next calculation.         val token = formatQuantity(quantity, unit, useToken = true)         history.add(0, HistoryEntry(input, display, token))-        lastAnswer = quantity.value+        // baseValue, not value: for an absolute temperature value is only the+        // degree-size, so `ans` after `212degF` was 117.77… instead of 373.15.+        lastAnswer = quantity.baseValue         if (unit != null) selectedUnit = unit         updateInput(token)     }
calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorGlanceWidget.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorGlanceWidget.kt b/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorGlanceWidget.ktindex 123a40b03d..fdd4373d46 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorGlanceWidget.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorGlanceWidget.kt@@ -265,7 +265,7 @@ internal fun evaluateForWidget(input: String, ans: Double): WidgetResult? {     }     // Non-finite results have no re-parsable token form; storing "∞" as     // the widget input would strand it (the app's evaluate() does the same).-    if (!quantity.value.isFinite()) return null+    if (!quantity.baseValue.isFinite()) return null     val unit = if (quantity.isDimensionless || quantity.instant) {         null     } else {@@ -274,7 +274,9 @@ internal fun evaluateForWidget(input: String, ans: Double): WidgetResult? {     return WidgetResult(         display = formatQuantity(quantity, unit),         token = formatQuantity(quantity, unit, useToken = true),-        value = quantity.value,+        // Stored as the widget's `ans`, so it must be the base magnitude+        // (absolute kelvin for a temperature), not the raw degree-size.+        value = quantity.baseValue,     ) } 
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 bdc3116255..a8352ffcc8 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/HistoryMemoryTest.kt@@ -71,6 +71,38 @@ class HistoryMemoryTest {         assertEquals(HistoryEntry("ans*2", "10"), tab.history[0])     } +    @Test+    fun ansAfterAnAbsoluteTemperatureIsItsKelvinValueWhateverUnitWasTyped() {+        // Regression: the result displays as 373.15 K for all three spellings,+        // but `ans` stored the raw Quantity.value — 100 for Celsius and+        // 117.77… for Fahrenheit (212 × 5/9, offset dropped) — so a following+        // `ans` calculation silently used a number that was the answer in+        // no unit at all.+        for (src in listOf("100degC", "212degF", "373.15K")) {+            val tab = CalculatorTab()+            tab.updateInput(src)+            tab.evaluate()+            assertEquals("373.15\u202FK", tab.history[0].result, "display for $src")+            assertEquals(373.15, tab.lastAnswer, 1e-9, "ans after $src")+            tab.clear()+            tab.updateInput("ans-273.15")+            tab.evaluate()+            assertEquals(100.0, tab.lastAnswer, 1e-9, "ans-273.15 after $src")+        }+    }++    @Test+    fun memoryAfterAnAbsoluteTemperatureIsItsKelvinValueWhateverUnitWasTyped() {+        // Same stored-scalar bug through M+: the register folded the preview+        // via eval(), so it too must see the kelvin magnitude.+        for (src in listOf("100degC", "212degF", "373.15K")) {+            val tab = CalculatorTab()+            tab.updateInput(src)+            tab.memoryAdd()+            assertEquals(373.15, tab.memory, 1e-9, "memory after $src")+        }+    }+     @Test     fun fractionalPowerEvaluatesAndRecords() {         val tab = CalculatorTab()
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 963eca1bb6..527e7abe5b 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/UnitsTest.kt@@ -181,6 +181,18 @@ class UnitsTest {         assertClose(195.0, displayValueIn(r, unit("K")))     } +    @Test+    fun absoluteTemperatureBaseValueIsKelvinWhateverUnitWasTyped() {+        // The scalar magnitude of an absolute temperature is its kelvin+        // value; Quantity.value alone is only the degree-size (212degF+        // would give 117.77…), which no caller may store as the result.+        assertClose(373.15, q("100degC").baseValue)+        assertClose(373.15, q("212degF").baseValue)+        assertClose(373.15, q("373.15K").baseValue)+        assertClose(373.15, Expression.parse("212degF").eval())+        assertClose(273.15, Expression.parse("0degC").eval())+    }+     @Test     fun addingTwoCelsiusValuesAddsDegreeSizes() {         val r = q("5c + 3c")
calculator/src/test/java/com/vayunmathur/calculator/widget/WidgetLogicTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/widget/WidgetLogicTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/widget/WidgetLogicTest.ktindex f3fef0f27e..ae961c1da0 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/widget/WidgetLogicTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/widget/WidgetLogicTest.kt@@ -35,6 +35,13 @@ class WidgetLogicTest {         assertTrue(length?.display?.startsWith("2000") == true)     } +    @Test fun evaluateForWidgetStoresKelvinForAbsoluteTemperatures() {+        // The widget stores this value as its `ans`; it must be the kelvin+        // magnitude (373.15), not the raw degree-size (117.77… for 212degF).+        assertEquals(373.15, evaluateForWidget("212degF", 0.0)?.value ?: Double.NaN, 1e-9)+        assertEquals(373.15, evaluateForWidget("100degC", 0.0)?.value ?: Double.NaN, 1e-9)+    }+     @Test fun appendWidgetInputCapsLength() {         assertEquals("abc", appendWidgetInput("ab", "c"))         val long = "x".repeat(MAX_WIDGET_INPUT_LENGTH)