From bc7ca057a084bcd8f9fa8d93d9769fb2ecb24c2d Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Fri, 25 Sep 2020 12:37:05 +0200 Subject: [PATCH] [apps/calculation] Add Joules to results on energy Change-Id: Ib775b1bd2bac1fd3bf755a9a4d3d5b3739a67cfa --- poincare/include/poincare/unit.h | 2 ++ poincare/src/unit.cpp | 17 ++++++++++++----- poincare/test/expression_properties.cpp | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/poincare/include/poincare/unit.h b/poincare/include/poincare/unit.h index d6c22f381..c44138a41 100644 --- a/poincare/include/poincare/unit.h +++ b/poincare/include/poincare/unit.h @@ -653,6 +653,8 @@ public: static_assert(strings_equal(k_temperatureRepresentatives[k_celsiusRepresentativeIndex].m_rootSymbol, "°C"), "Index for the Celsius Representative is incorrect."); static constexpr int k_fahrenheitRepresentativeIndex = 2; static_assert(strings_equal(k_temperatureRepresentatives[k_fahrenheitRepresentativeIndex].m_rootSymbol, "°F"), "Index for the Fahrenheit Representative is incorrect."); + static constexpr int k_jouleRepresentativeIndex = 0; + static_assert(strings_equal(k_energyRepresentatives[k_jouleRepresentativeIndex].m_rootSymbol, "J"), "Index for the Joule Representative is incorrect."); static constexpr int k_electronVoltRepresentativeIndex = 1; static_assert(strings_equal(k_energyRepresentatives[k_electronVoltRepresentativeIndex].m_rootSymbol, "eV"), "Index for the Electron Volt Representative is incorrect."); static constexpr int k_wattRepresentativeIndex = 0; diff --git a/poincare/src/unit.cpp b/poincare/src/unit.cpp index 52ba22f4d..237f45200 100644 --- a/poincare/src/unit.cpp +++ b/poincare/src/unit.cpp @@ -58,6 +58,7 @@ constexpr const int Unit::k_kelvinRepresentativeIndex, Unit::k_celsiusRepresentativeIndex, Unit::k_fahrenheitRepresentativeIndex, + Unit::k_jouleRepresentativeIndex, Unit::k_electronVoltRepresentativeIndex, Unit::k_wattRepresentativeIndex, Unit::k_hectareRepresentativeIndex, @@ -526,26 +527,32 @@ int UnitNode::TemperatureRepresentative::setAdditionalExpressions(double value, int UnitNode::EnergyRepresentative::setAdditionalExpressions(double value, Expression * dest, int availableLength, ExpressionNode::ReductionContext reductionContext) const { assert(availableLength >= 2); - /* 1. Convert into Wh + int index = 0; + /* 1. Convert into Joules + * As J is just a shorthand for _kg_m^2_s^-2, the value is used as is. */ + const Representative * joule = representativesOfSameDimension() + Unit::k_jouleRepresentativeIndex; + const Prefix * joulePrefix = joule->findBestPrefix(value, 1.); + dest[index++] = Multiplication::Builder(Float::Builder(value * std::pow(10., -joulePrefix->exponent())), Unit::Builder(joule, joulePrefix)); + /* 2. Convert into Wh * As value is expressed in SI units (ie _kg_m^2_s^-2), the ratio is that of * hours to seconds. */ const Representative * hour = TimeRepresentative::Default().representativesOfSameDimension() + Unit::k_hourRepresentativeIndex; const Representative * watt = PowerRepresentative::Default().representativesOfSameDimension() + Unit::k_wattRepresentativeIndex; double adjustedValue = value / hour->ratio() / watt->ratio(); const Prefix * wattPrefix = watt->findBestPrefix(adjustedValue, 1.); - dest[0] = Multiplication::Builder( + dest[index++] = Multiplication::Builder( Float::Builder(adjustedValue * std::pow(10., -wattPrefix->exponent())), Multiplication::Builder( Unit::Builder(watt, wattPrefix), Unit::Builder(hour, Prefix::EmptyPrefix()))); - /* 2. Convert into eV */ + /* 3. Convert into eV */ const Representative * eV = representativesOfSameDimension() + Unit::k_electronVoltRepresentativeIndex; adjustedValue = value / eV->ratio(); const Prefix * eVPrefix = eV->findBestPrefix(adjustedValue, 1.); - dest[1] = Multiplication::Builder( + dest[index++] = Multiplication::Builder( Float::Builder(adjustedValue * std::pow(10., -eVPrefix->exponent())), Unit::Builder(eV, eVPrefix)); - return 2; + return index; } const UnitNode::Representative * UnitNode::SurfaceRepresentative::standardRepresentative(double value, double exponent, ExpressionNode::ReductionContext reductionContext, const Prefix * * prefix) const { diff --git a/poincare/test/expression_properties.cpp b/poincare/test/expression_properties.cpp index b3d6aa5b5..d6a80ce21 100644 --- a/poincare/test/expression_properties.cpp +++ b/poincare/test/expression_properties.cpp @@ -413,8 +413,8 @@ QUIZ_CASE(poincare_expression_additional_results) { assert_additional_results_compute_to("-4×_°F", array17, 2); // Energy - const char * array8[2] = {"1×_kW×_h", "2.246943ᴇ13×_TeV"}; - assert_additional_results_compute_to("3.6×_MN_m", array8, 2); + const char * array8[3] = {"3.6×_MJ", "1×_kW×_h", "2.246943ᴇ13×_TeV"}; + assert_additional_results_compute_to("3.6×_MN_m", array8, 3); // Volume const char * array9[2] = {"264×_gal+1×_pt+0.7528377×_cup", "1000×_L"};