From 4e771fdb544a0fa82b73c122221ffd48cfec629b Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Thu, 24 Sep 2020 11:06:28 +0200 Subject: [PATCH] [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 --- poincare/src/unit.cpp | 6 +++++- poincare/test/simplification.cpp | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/poincare/src/unit.cpp b/poincare/src/unit.cpp index 901085695..f92b0bc7a 100644 --- a/poincare/src/unit.cpp +++ b/poincare/src/unit.cpp @@ -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); } diff --git a/poincare/test/simplification.cpp b/poincare/test/simplification.cpp index 8ccc7718b..32b5bf580 100644 --- a/poincare/test/simplification.cpp +++ b/poincare/test/simplification.cpp @@ -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) {