Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Unified Diff: Source/platform/Decimal.cpp

Issue 59603006: Fix Decimal.floor() + Decimal.ceiling() for most non-integral values. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Express integral check via is-power-of-10 predicate instead Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Source/platform/DecimalTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/platform/Decimal.cpp
diff --git a/Source/platform/Decimal.cpp b/Source/platform/Decimal.cpp
index 02dca8f3691710782a6f9cfc4b41c1d36f4b8272..e1537a793dc63c8a2af91e74ddcd24e5f895de76 100644
--- a/Source/platform/Decimal.cpp
+++ b/Source/platform/Decimal.cpp
@@ -612,6 +612,11 @@ Decimal::AlignedOperands Decimal::alignOperands(const Decimal& lhs, const Decima
return alignedOperands;
}
+static bool isMultiplePowersOfTen(uint64_t coefficient, int n)
+{
+ return !coefficient || !(coefficient % scaleUp(1, n));
+}
+
// Round toward positive infinity.
// Note: Mac ports defines ceil(x) as wtf_ceil(x), so we can't use name "ceil" here.
Decimal Decimal::ceiling() const
@@ -628,10 +633,9 @@ Decimal Decimal::ceiling() const
if (numberOfDigits < numberOfDropDigits)
return isPositive() ? Decimal(1) : zero(Positive);
- result = scaleDown(result, numberOfDropDigits - 1);
- if (sign() == Positive && result % 10 > 0)
- result += 10;
- result /= 10;
+ result = scaleDown(result, numberOfDropDigits);
+ if (isPositive() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits))
+ ++result;
return Decimal(sign(), 0, result);
}
@@ -670,10 +674,9 @@ Decimal Decimal::floor() const
if (numberOfDigits < numberOfDropDigits)
return isPositive() ? zero(Positive) : Decimal(-1);
- result = scaleDown(result, numberOfDropDigits - 1);
- if (isNegative() && result % 10 > 0)
- result += 10;
- result /= 10;
+ result = scaleDown(result, numberOfDropDigits);
+ if (isNegative() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits))
+ ++result;
return Decimal(sign(), 0, result);
}
« no previous file with comments | « no previous file | Source/platform/DecimalTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698