]> granicus.if.org Git - icu/commitdiff
add arounding epsilon to correct the error in the double operation
authoryounies <younies@chromium.org>
Sat, 13 Jun 2020 13:36:14 +0000 (15:36 +0200)
committeryounies <younies@chromium.org>
Sat, 13 Jun 2020 13:36:14 +0000 (15:36 +0200)
icu4c/source/i18n/complexunitsconverter.cpp

index b501a6bd49b500960e50e2ce1f924367c4cd7ee9..1ee2fed03ccd0711662d4f49435b17a5aaf00d41 100644 (file)
 
 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<Measure> ComplexUnitsConverter::convert(double quantity, UError
     MaybeStackVector<Measure> 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.