From bddc5133e33ee9f29e0b341347f73df1f09ac6a9 Mon Sep 17 00:00:00 2001 From: Younies Mahmoud Date: Wed, 6 May 2020 04:28:35 +0200 Subject: [PATCH] fix substituteConstans function --- icu4c/source/i18n/unitconverter.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/icu4c/source/i18n/unitconverter.cpp b/icu4c/source/i18n/unitconverter.cpp index 07b0b8ad312..2899cd5909a 100644 --- a/icu4c/source/i18n/unitconverter.cpp +++ b/icu4c/source/i18n/unitconverter.cpp @@ -125,22 +125,16 @@ struct Factor { constantsValues[CONSTANT_GAL_IMP2M3] = 0.00454609; for (int i = 0; i < CONSTANTS_COUNT; i++) { - if (this->constants[i] == 0) continue; + if (this->constants[i] == 0) { continue;} - substituteSingleConstant(this->constants[i], constantsValues[i]); - this->constants[i] = 0; - } - } + auto absPower = std::abs(this->constants[i]); + SigNum powerSig = this->constants[i] < 0 ? SigNum::NEGATIVE : SigNum::POSITIVE; + double absConstantValue = std::pow(constantsValues[i], absPower); - void substituteSingleConstant(int32_t constantPower, - double constantValue /* the constant actual value, - e.g. G= 9.88888 */) { - constantValue = std::pow(constantValue, std::abs(constantPower)); + if (powerSig == SigNum::NEGATIVE) { this->factorDen *= absConstantValue;} + else { this->factorNum *= absConstantValue;} - if (constantPower < 0) { - this->factorDen *= constantValue; - } else { - this->factorNum *= constantValue; + this->constants[i] = 0; } } }; @@ -222,6 +216,7 @@ MeasureUnit extractCompoundBaseUnit(const MeasureUnit &source, const ConversionR return result; } +// TODO: Load those constant from units data. /* * Adds a single factor element to the `Factor`. e.g "ft3m", "2.333" or "cup2m3". But not "cup2m3^3". */ -- 2.40.0