[apps/calculation] Add Joules to results on energy

Change-Id: Ib775b1bd2bac1fd3bf755a9a4d3d5b3739a67cfa
This commit is contained in:
Gabriel Ozouf
2020-09-25 12:37:05 +02:00
committed by Émilie Feral
parent 10f8e2f507
commit bc7ca057a0
3 changed files with 16 additions and 7 deletions

View File

@@ -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;

View File

@@ -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<double>::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<double>::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<double>::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 {

View File

@@ -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"};