诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 rivejä
5.3 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_
  31. #define GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_
  32. #include <float.h>
  33. #include <math.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/stubs/logging.h>
  36. #include <google/protobuf/stubs/mathlimits.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace internal {
  40. template<typename T>
  41. bool AlmostEquals(T a, T b) {
  42. return a == b;
  43. }
  44. template<>
  45. inline bool AlmostEquals(float a, float b) {
  46. return fabs(a - b) < 32 * FLT_EPSILON;
  47. }
  48. template<>
  49. inline bool AlmostEquals(double a, double b) {
  50. return fabs(a - b) < 32 * DBL_EPSILON;
  51. }
  52. } // namespace internal
  53. class MathUtil {
  54. public:
  55. template<typename T>
  56. static T Sign(T value) {
  57. if (value == T(0) || MathLimits<T>::IsNaN(value)) {
  58. return value;
  59. }
  60. return value > T(0) ? 1 : -1;
  61. }
  62. template<typename T>
  63. static bool AlmostEquals(T a, T b) {
  64. return internal::AlmostEquals(a, b);
  65. }
  66. // Largest of two values.
  67. // Works correctly for special floating point values.
  68. // Note: 0.0 and -0.0 are not differentiated by Max (Max(0.0, -0.0) is -0.0),
  69. // which should be OK because, although they (can) have different
  70. // bit representation, they are observably the same when examined
  71. // with arithmetic and (in)equality operators.
  72. template<typename T>
  73. static T Max(const T x, const T y) {
  74. return MathLimits<T>::IsNaN(x) || x > y ? x : y;
  75. }
  76. // Absolute value of x
  77. // Works correctly for unsigned types and
  78. // for special floating point values.
  79. // Note: 0.0 and -0.0 are not differentiated by Abs (Abs(0.0) is -0.0),
  80. // which should be OK: see the comment for Max above.
  81. template<typename T>
  82. static T Abs(const T x) {
  83. return x > T(0) ? x : -x;
  84. }
  85. // Absolute value of the difference between two numbers.
  86. // Works correctly for signed types and special floating point values.
  87. template<typename T>
  88. static typename MathLimits<T>::UnsignedType AbsDiff(const T x, const T y) {
  89. // Carries out arithmetic as unsigned to avoid overflow.
  90. typedef typename MathLimits<T>::UnsignedType R;
  91. return x > y ? R(x) - R(y) : R(y) - R(x);
  92. }
  93. // If two (usually floating point) numbers are within a certain
  94. // fraction of their magnitude or within a certain absolute margin of error.
  95. // This is the same as the following but faster:
  96. // WithinFraction(x, y, fraction) || WithinMargin(x, y, margin)
  97. // E.g. WithinFraction(0.0, 1e-10, 1e-5) is false but
  98. // WithinFractionOrMargin(0.0, 1e-10, 1e-5, 1e-5) is true.
  99. template<typename T>
  100. static bool WithinFractionOrMargin(const T x, const T y,
  101. const T fraction, const T margin);
  102. };
  103. template<typename T>
  104. bool MathUtil::WithinFractionOrMargin(const T x, const T y,
  105. const T fraction, const T margin) {
  106. // Not just "0 <= fraction" to fool the compiler for unsigned types.
  107. GOOGLE_DCHECK((T(0) < fraction || T(0) == fraction) &&
  108. fraction < T(1) &&
  109. margin >= T(0));
  110. // Template specialization will convert the if() condition to a constant,
  111. // which will cause the compiler to generate code for either the "if" part
  112. // or the "then" part. In this way we avoid a compiler warning
  113. // about a potential integer overflow in crosstool v12 (gcc 4.3.1).
  114. if (MathLimits<T>::kIsInteger) {
  115. return x == y;
  116. } else {
  117. if (!MathLimits<T>::IsFinite(x) || !MathLimits<T>::IsFinite(y)) {
  118. return false;
  119. }
  120. T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y)));
  121. return AbsDiff(x, y) <= Max(margin, relative_margin);
  122. }
  123. }
  124. } // namespace protobuf
  125. } // namespace google
  126. #endif // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_