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 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
3f17f2f01f
commit
bb977afe55
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user