From: younies Date: Sat, 13 Jun 2020 13:36:14 +0000 (+0200) Subject: add arounding epsilon to correct the error in the double operation X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=96c611a90eb360782250e562bc119a96e930ef5e;p=icu add arounding epsilon to correct the error in the double operation --- diff --git a/icu4c/source/i18n/complexunitsconverter.cpp b/icu4c/source/i18n/complexunitsconverter.cpp index b501a6bd49b..1ee2fed03cc 100644 --- a/icu4c/source/i18n/complexunitsconverter.cpp +++ b/icu4c/source/i18n/complexunitsconverter.cpp @@ -16,6 +16,10 @@ U_NAMESPACE_BEGIN +// The rounding factor +// TODO: Remove the rounding factor after using decNumber +#define EPSILON 1000000000.0 + ComplexUnitsConverter::ComplexUnitsConverter(const MeasureUnit inputUnit, const MeasureUnit outputUnits, const ConversionRates &ratesInfo, UErrorCode &status) { @@ -71,7 +75,7 @@ UBool ComplexUnitsConverter::greaterThanOrEqual(double quantity, double limit) c U_ASSERT(unitConverters_.length() > 0); // First converter converts to the biggest quantity. - double newQuantity = unitConverters_[0]->convert(quantity); + double newQuantity = roundl( unitConverters_[0]->convert(quantity) * EPSILON) / EPSILON; return newQuantity >= limit; } @@ -80,9 +84,9 @@ MaybeStackVector ComplexUnitsConverter::convert(double quantity, UError MaybeStackVector result; for (int i = 0, n = unitConverters_.length(); i < n; ++i) { - quantity = (*unitConverters_[i]).convert(quantity); + quantity = roundl((*unitConverters_[i]).convert(quantity) * EPSILON) / EPSILON; if (i < n - 1) { - int64_t newQuantity = quantity; + int64_t newQuantity = floor(quantity); Formattable formattableNewQuantity(newQuantity); // NOTE: Measure would own its MeasureUnit.