Decimal.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef Decimal_h
  31. #define Decimal_h
  32. #include <stdint.h>
  33. #include <wtf/Assertions.h>
  34. #include <wtf/text/WTFString.h>
  35. namespace WebCore {
  36. namespace DecimalPrivate {
  37. class SpecialValueHandler;
  38. }
  39. // This class represents decimal base floating point number.
  40. //
  41. // FIXME: Once all C++ compiler support decimal type, we should replace this
  42. // class to compiler supported one. See below URI for current status of decimal
  43. // type for C++: // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1977.html
  44. class Decimal {
  45. WTF_MAKE_FAST_ALLOCATED;
  46. public:
  47. enum Sign {
  48. Positive,
  49. Negative,
  50. };
  51. // You should not use EncodedData other than unit testing.
  52. class EncodedData {
  53. // For accessing FormatClass.
  54. friend class Decimal;
  55. friend class DecimalPrivate::SpecialValueHandler;
  56. public:
  57. EncodedData(Sign, int exponent, uint64_t coefficient);
  58. bool operator==(const EncodedData&) const;
  59. bool operator!=(const EncodedData& another) const { return !operator==(another); }
  60. uint64_t coefficient() const { return m_coefficient; }
  61. int countDigits() const;
  62. int exponent() const { return m_exponent; }
  63. bool isFinite() const { return !isSpecial(); }
  64. bool isInfinity() const { return m_formatClass == ClassInfinity; }
  65. bool isNaN() const { return m_formatClass == ClassNaN; }
  66. bool isSpecial() const { return m_formatClass == ClassInfinity || m_formatClass == ClassNaN; }
  67. bool isZero() const { return m_formatClass == ClassZero; }
  68. Sign sign() const { return m_sign; }
  69. void setSign(Sign sign) { m_sign = sign; }
  70. private:
  71. enum FormatClass {
  72. ClassInfinity,
  73. ClassNormal,
  74. ClassNaN,
  75. ClassZero,
  76. };
  77. EncodedData(Sign, FormatClass);
  78. FormatClass formatClass() const { return m_formatClass; }
  79. uint64_t m_coefficient;
  80. int16_t m_exponent;
  81. FormatClass m_formatClass;
  82. Sign m_sign;
  83. };
  84. Decimal(int32_t = 0);
  85. Decimal(Sign, int exponent, uint64_t coefficient);
  86. Decimal(const Decimal&);
  87. Decimal& operator=(const Decimal&);
  88. Decimal& operator+=(const Decimal&);
  89. Decimal& operator-=(const Decimal&);
  90. Decimal& operator*=(const Decimal&);
  91. Decimal& operator/=(const Decimal&);
  92. Decimal operator-() const;
  93. bool operator==(const Decimal&) const;
  94. bool operator!=(const Decimal&) const;
  95. bool operator<(const Decimal&) const;
  96. bool operator<=(const Decimal&) const;
  97. bool operator>(const Decimal&) const;
  98. bool operator>=(const Decimal&) const;
  99. Decimal operator+(const Decimal&) const;
  100. Decimal operator-(const Decimal&) const;
  101. Decimal operator*(const Decimal&) const;
  102. Decimal operator/(const Decimal&) const;
  103. int exponent() const
  104. {
  105. ASSERT(isFinite());
  106. return m_data.exponent();
  107. }
  108. bool isFinite() const { return m_data.isFinite(); }
  109. bool isInfinity() const { return m_data.isInfinity(); }
  110. bool isNaN() const { return m_data.isNaN(); }
  111. bool isNegative() const { return sign() == Negative; }
  112. bool isPositive() const { return sign() == Positive; }
  113. bool isSpecial() const { return m_data.isSpecial(); }
  114. bool isZero() const { return m_data.isZero(); }
  115. Decimal abs() const;
  116. Decimal ceiling() const;
  117. Decimal floor() const;
  118. Decimal remainder(const Decimal&) const;
  119. Decimal round() const;
  120. double toDouble() const;
  121. // Note: toString method supports infinity and nan but fromString not.
  122. String toString() const;
  123. static Decimal fromDouble(double);
  124. // fromString supports following syntax EBNF:
  125. // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)?
  126. // | sign? '.' digit+ (exponent-marker sign? digit+)?
  127. // sign ::= '+' | '-'
  128. // exponent-marker ::= 'e' | 'E'
  129. // digit ::= '0' | '1' | ... | '9'
  130. // Note: fromString doesn't support "infinity" and "nan".
  131. static Decimal fromString(const String&);
  132. static Decimal infinity(Sign);
  133. static Decimal nan();
  134. static Decimal zero(Sign);
  135. // You should not use below methods. We expose them for unit testing.
  136. explicit Decimal(const EncodedData&);
  137. const EncodedData& value() const { return m_data; }
  138. private:
  139. struct AlignedOperands {
  140. uint64_t lhsCoefficient;
  141. uint64_t rhsCoefficient;
  142. int exponent;
  143. };
  144. Decimal(double);
  145. Decimal compareTo(const Decimal&) const;
  146. static AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs);
  147. static inline Sign invertSign(Sign sign) { return sign == Negative ? Positive : Negative; }
  148. Sign sign() const { return m_data.sign(); }
  149. EncodedData m_data;
  150. };
  151. } // namespace WebCore
  152. #endif // Decimal_h