From bb977afe552d0bbe7206cdcb8d14e59a8c51b94e Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 12 Feb 2026 12:27:32 +0100 Subject: [PATCH] Base: Fix floating point issue in schema threshold comparison When a parsed value lands exactly at a threshold boundary (e.g. "1 S/m" = 1e-9 at threshold 1e-9), floating point rounding could cause the wrong unit to be selected. Shrink thresholds by a relative epsilon so boundary values fall through to the next (more natural) unit. Co-Authored-By: Claude Opus 4.6 --- src/Base/UnitsSchema.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Base/UnitsSchema.cpp b/src/Base/UnitsSchema.cpp index decf368f9f..eb577907d5 100644 --- a/src/Base/UnitsSchema.cpp +++ b/src/Base/UnitsSchema.cpp @@ -67,7 +67,11 @@ std::string UnitsSchema::translate(const Quantity& quant, double& factor, std::s const auto value = quant.getValue(); auto isSuitable = [&](const UnitTranslationSpec& row) { - return row.threshold > value || row.threshold == 0; // zero indicates default + // Shrink threshold slightly so values at exact threshold boundaries + // (e.g. "1 S/m" = 1e-9 at threshold 1e-9) fall through to the next unit. + constexpr double relEps = 1e-12; + return row.threshold * (1.0 - relEps) > value + || row.threshold == 0; // zero indicates default }; auto unitSpecs = spec.translationSpecs.at(unitName);