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

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: 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..45f69c3ae8a99f5e288cb2ee202fd24b1d6fe77f 100644
--- a/Source/platform/Decimal.cpp
+++ b/Source/platform/Decimal.cpp
@@ -612,6 +612,21 @@ Decimal::AlignedOperands Decimal::alignOperands(const Decimal& lhs, const Decima
return alignedOperands;
}
+// Returns true if the 'n' least significant digits of
+// the coefficient are all zero.
+static bool digitsAllZero(uint64_t coefficient, int n)
yosin_UTC9 2013/11/18 03:48:28 How about this? static bool isMultipleOfTenPowerN
sof 2013/11/18 06:48:27 Yes, clearer too. Done + took the opportunity to s
+{
+ ASSERT(n >= 0);
+ while (n > 0) {
+ if (coefficient % 10) {
+ return false;
+ }
+ coefficient /= 10;
+ n--;
yosin_UTC9 2013/11/18 03:48:28 nit: --n Other parts of this file use prefix decre
+ }
+ return true;
+}
+
// 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 +643,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() && !digitsAllZero(m_data.coefficient(), numberOfDropDigits))
+ result++;
yosin_UTC9 2013/11/18 03:48:28 nit: ++result Other parts of this file use prefix
sof 2013/11/18 06:48:27 Done.
return Decimal(sign(), 0, result);
}
@@ -670,10 +684,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() && !digitsAllZero(m_data.coefficient(), numberOfDropDigits))
+ result++;
yosin_UTC9 2013/11/18 03:48:28 nit: ++result Other parts of this file use prefix
sof 2013/11/18 06:48:27 Done.
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