fix findAndMerge function

This commit is contained in:
younies 2020-06-16 20:14:44 +02:00
parent 3290a661d3
commit 5e28c0c941
2 changed files with 14 additions and 12 deletions

View file

@ -769,15 +769,16 @@ MeasureUnit MeasureUnit::product(const MeasureUnit& other, UErrorCode& status) c
return std::move(impl).build(status);
}
namespace {
/**
* Searches the `simplifiedUnits` for a unit with the same base identifier and the same SI prefix.
* For example, `square-meter` and `cubic-meter` but not `meter` and `centimeter`.
* After that, the matched units will be merged.
* Searches the `simplifiedUnits` for a unit with the same base identifier and the same SI prefix
* (For example, `square-meter` and `cubic-meter` but not `meter` and `centimeter`). After that, the
* matched units will be merged. Otherwise, the `newUnitImpl` will be appended.
*/
void findAndMerge(MeasureUnitImpl &simplifiedUnitsImpl, const SingleUnitImpl &newUnitImpl,
UErrorCode &status) {
for (int i = 0, n = simplifiedUnitsImpl.units.length(); i < n; i++) {
auto& singleSimplifiedUnitImpl = *(simplifiedUnitsImpl.units[i]);
auto &singleSimplifiedUnitImpl = *(simplifiedUnitsImpl.units[i]);
if (singleSimplifiedUnitImpl.identifier == newUnitImpl.identifier &&
singleSimplifiedUnitImpl.siPrefix == newUnitImpl.siPrefix) {
@ -785,9 +786,9 @@ void findAndMerge(MeasureUnitImpl &simplifiedUnitsImpl, const SingleUnitImpl &ne
return;
}
}
simplifiedUnitsImpl.units.emplaceBackAndCheckErrorCode(status, newUnitImpl);
simplifiedUnitsImpl.append(newUnitImpl, status);
}
} // namespace
MeasureUnit MeasureUnit::simplify(UErrorCode &status) const {
if (this->getComplexity(status) == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED) {
@ -797,10 +798,9 @@ MeasureUnit MeasureUnit::simplify(UErrorCode &status) const {
MeasureUnitImpl resultImpl;
int32_t outCount;
auto singleUnits = this->splitToSingleUnits(outCount, status);
for (int i = 0; i < outCount; ++i) {
findAndMerge(resultImpl, SingleUnitImpl::forMeasureUnit(singleUnits[i], status), status);
const auto &units = (*MeasureUnitImpl::get(*this)).units;
for (int i = 0, n = units.length(); i < n; ++i) {
findAndMerge(resultImpl, *units[i], status);
}
return std::move(resultImpl).build(status);

View file

@ -495,8 +495,10 @@ double UnitConverter::convert(double inputValue) const {
result = 1.0 / result;
}
// Multiply the result by 1.000,000,001 to fix the deterioration from using `double`
// TODO: remove the multiplication by 1.000,000,001 after using `decNumber`
// TODO: remove the multiplication by 1.000,000,000,001 after using `decNumber`
// Multiply the result by 1.000,000,000,001 to fix the deterioration from using `double` (the
// deterioration is around 15 to 17 decimal digit).
return result * 1.000000000001;
}