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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | Source/platform/DecimalTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 } 605 }
606 } 606 }
607 607
608 AlignedOperands alignedOperands; 608 AlignedOperands alignedOperands;
609 alignedOperands.exponent = exponent; 609 alignedOperands.exponent = exponent;
610 alignedOperands.lhsCoefficient = lhsCoefficient; 610 alignedOperands.lhsCoefficient = lhsCoefficient;
611 alignedOperands.rhsCoefficient = rhsCoefficient; 611 alignedOperands.rhsCoefficient = rhsCoefficient;
612 return alignedOperands; 612 return alignedOperands;
613 } 613 }
614 614
615 // Returns true if the 'n' least significant digits of
616 // the coefficient are all zero.
617 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
618 {
619 ASSERT(n >= 0);
620 while (n > 0) {
621 if (coefficient % 10) {
622 return false;
623 }
624 coefficient /= 10;
625 n--;
yosin_UTC9 2013/11/18 03:48:28 nit: --n Other parts of this file use prefix decre
626 }
627 return true;
628 }
629
615 // Round toward positive infinity. 630 // Round toward positive infinity.
616 // Note: Mac ports defines ceil(x) as wtf_ceil(x), so we can't use name "ceil" h ere. 631 // Note: Mac ports defines ceil(x) as wtf_ceil(x), so we can't use name "ceil" h ere.
617 Decimal Decimal::ceiling() const 632 Decimal Decimal::ceiling() const
618 { 633 {
619 if (isSpecial()) 634 if (isSpecial())
620 return *this; 635 return *this;
621 636
622 if (exponent() >= 0) 637 if (exponent() >= 0)
623 return *this; 638 return *this;
624 639
625 uint64_t result = m_data.coefficient(); 640 uint64_t result = m_data.coefficient();
626 const int numberOfDigits = countDigits(result); 641 const int numberOfDigits = countDigits(result);
627 const int numberOfDropDigits = -exponent(); 642 const int numberOfDropDigits = -exponent();
628 if (numberOfDigits < numberOfDropDigits) 643 if (numberOfDigits < numberOfDropDigits)
629 return isPositive() ? Decimal(1) : zero(Positive); 644 return isPositive() ? Decimal(1) : zero(Positive);
630 645
631 result = scaleDown(result, numberOfDropDigits - 1); 646 result = scaleDown(result, numberOfDropDigits);
632 if (sign() == Positive && result % 10 > 0) 647 if (isPositive() && !digitsAllZero(m_data.coefficient(), numberOfDropDigits) )
633 result += 10; 648 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.
634 result /= 10;
635 return Decimal(sign(), 0, result); 649 return Decimal(sign(), 0, result);
636 } 650 }
637 651
638 Decimal Decimal::compareTo(const Decimal& rhs) const 652 Decimal Decimal::compareTo(const Decimal& rhs) const
639 { 653 {
640 const Decimal result(*this - rhs); 654 const Decimal result(*this - rhs);
641 switch (result.m_data.formatClass()) { 655 switch (result.m_data.formatClass()) {
642 case EncodedData::ClassInfinity: 656 case EncodedData::ClassInfinity:
643 return result.isNegative() ? Decimal(-1) : Decimal(1); 657 return result.isNegative() ? Decimal(-1) : Decimal(1);
644 658
(...skipping 18 matching lines...) Expand all
663 677
664 if (exponent() >= 0) 678 if (exponent() >= 0)
665 return *this; 679 return *this;
666 680
667 uint64_t result = m_data.coefficient(); 681 uint64_t result = m_data.coefficient();
668 const int numberOfDigits = countDigits(result); 682 const int numberOfDigits = countDigits(result);
669 const int numberOfDropDigits = -exponent(); 683 const int numberOfDropDigits = -exponent();
670 if (numberOfDigits < numberOfDropDigits) 684 if (numberOfDigits < numberOfDropDigits)
671 return isPositive() ? zero(Positive) : Decimal(-1); 685 return isPositive() ? zero(Positive) : Decimal(-1);
672 686
673 result = scaleDown(result, numberOfDropDigits - 1); 687 result = scaleDown(result, numberOfDropDigits);
674 if (isNegative() && result % 10 > 0) 688 if (isNegative() && !digitsAllZero(m_data.coefficient(), numberOfDropDigits) )
675 result += 10; 689 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.
676 result /= 10;
677 return Decimal(sign(), 0, result); 690 return Decimal(sign(), 0, result);
678 } 691 }
679 692
680 Decimal Decimal::fromDouble(double doubleValue) 693 Decimal Decimal::fromDouble(double doubleValue)
681 { 694 {
682 if (std::isfinite(doubleValue)) 695 if (std::isfinite(doubleValue))
683 return fromString(String::numberToStringECMAScript(doubleValue)); 696 return fromString(String::numberToStringECMAScript(doubleValue));
684 697
685 if (std::isinf(doubleValue)) 698 if (std::isinf(doubleValue))
686 return infinity(doubleValue < 0 ? Negative : Positive); 699 return infinity(doubleValue < 0 ? Negative : Positive);
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 } 1038 }
1026 return builder.toString(); 1039 return builder.toString();
1027 } 1040 }
1028 1041
1029 Decimal Decimal::zero(Sign sign) 1042 Decimal Decimal::zero(Sign sign)
1030 { 1043 {
1031 return Decimal(EncodedData(sign, EncodedData::ClassZero)); 1044 return Decimal(EncodedData(sign, EncodedData::ClassZero));
1032 } 1045 }
1033 1046
1034 } // namespace WebCore 1047 } // namespace WebCore
OLDNEW
« 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