[poincare/unit] Missing prefix reset

When tuning the representative without tuning the prefix, the prefix
would not be reset to its default value, causing a problem for masses,
whose base prefix depend on the representative (k for grams, none for
the rest).
Fixes a crash when typing sqrt(_N) with country set to US.

Change-Id: Iade900de5fb31e06fdfafef34ae27da3f36983fa
This commit is contained in:
Gabriel Ozouf
2020-09-24 11:06:28 +02:00
committed by Émilie Feral
parent d862d5503c
commit 4e771fdb54
2 changed files with 6 additions and 1 deletions

View File

@@ -922,11 +922,15 @@ void Unit::chooseBestRepresentativeAndPrefix(double * value, double exponent, Ex
double baseValue = *value * std::pow(node()->representative()->ratio() * std::pow(10., node()->prefix()->exponent() - node()->representative()->basePrefix()->exponent()), exponent);
const Prefix * bestPrefix = (optimizePrefix) ? Prefix::EmptyPrefix() : nullptr;
const Representative * bestRepresentative = node()->representative()->standardRepresentative(baseValue, exponent, reductionContext, &bestPrefix);
if (!optimizePrefix) {
bestPrefix = bestRepresentative->basePrefix();
}
if (bestRepresentative != node()->representative()) {
*value = *value * std::pow(node()->representative()->ratio() / bestRepresentative->ratio() * std::pow(10., bestRepresentative->basePrefix()->exponent() - node()->representative()->basePrefix()->exponent()), exponent);
node()->setRepresentative(bestRepresentative);
}
if (optimizePrefix && bestPrefix != node()->prefix()) {
if (bestPrefix != node()->prefix()) {
*value = *value * std::pow(10., exponent * (node()->prefix()->exponent() - bestPrefix->exponent()));
node()->setPrefix(bestPrefix);
}

View File

@@ -536,6 +536,7 @@ QUIZ_CASE(poincare_simplification_units) {
assert_parsed_expression_simplify_to("cos(1_s/1_s)", "cos(1)");
assert_parsed_expression_simplify_to("1_m+π_m+√(2)_m-cos(15)_m", "6.3154941288217×_m");
assert_parsed_expression_simplify_to("√(16×_m^2)", "4×_m");
assert_parsed_expression_simplify_to("1×_A_kg", "2.2046226218488×_A×_lb", User, Radian, Imperial);
}
QUIZ_CASE(poincare_simplification_power) {