• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 #include <float.h>
34 #include <math.h>
35 
36 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/mathlimits.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace internal {
43 template<typename T>
AlmostEquals(T a,T b)44 bool AlmostEquals(T a, T b) {
45   return a == b;
46 }
47 template<>
AlmostEquals(float a,float b)48 inline bool AlmostEquals(float a, float b) {
49   return fabs(a - b) < 32 * FLT_EPSILON;
50 }
51 
52 template<>
AlmostEquals(double a,double b)53 inline bool AlmostEquals(double a, double b) {
54   return fabs(a - b) < 32 * DBL_EPSILON;
55 }
56 }  // namespace internal
57 
58 class MathUtil {
59  public:
60   template<typename T>
Sign(T value)61   static T Sign(T value) {
62     if (value == T(0) || MathLimits<T>::IsNaN(value)) {
63       return value;
64     }
65     return value > T(0) ? 1 : -1;
66   }
67 
68   template<typename T>
AlmostEquals(T a,T b)69   static bool AlmostEquals(T a, T b) {
70     return internal::AlmostEquals(a, b);
71   }
72 
73   // Largest of two values.
74   // Works correctly for special floating point values.
75   // Note: 0.0 and -0.0 are not differentiated by Max (Max(0.0, -0.0) is -0.0),
76   // which should be OK because, although they (can) have different
77   // bit representation, they are observably the same when examined
78   // with arithmetic and (in)equality operators.
79   template<typename T>
Max(const T x,const T y)80   static T Max(const T x, const T y) {
81     return MathLimits<T>::IsNaN(x) || x > y ? x : y;
82   }
83 
84   // Absolute value of x
85   // Works correctly for unsigned types and
86   // for special floating point values.
87   // Note: 0.0 and -0.0 are not differentiated by Abs (Abs(0.0) is -0.0),
88   // which should be OK: see the comment for Max above.
89   template<typename T>
Abs(const T x)90   static T Abs(const T x) {
91     return x > T(0) ? x : -x;
92   }
93 
94   // Absolute value of the difference between two numbers.
95   // Works correctly for signed types and special floating point values.
96   template<typename T>
AbsDiff(const T x,const T y)97   static typename MathLimits<T>::UnsignedType AbsDiff(const T x, const T y) {
98     // Carries out arithmetic as unsigned to avoid overflow.
99     typedef typename MathLimits<T>::UnsignedType R;
100     return x > y ? R(x) - R(y) : R(y) - R(x);
101   }
102 
103   // If two (usually floating point) numbers are within a certain
104   // fraction of their magnitude or within a certain absolute margin of error.
105   // This is the same as the following but faster:
106   //   WithinFraction(x, y, fraction)  ||  WithinMargin(x, y, margin)
107   // E.g. WithinFraction(0.0, 1e-10, 1e-5) is false but
108   //      WithinFractionOrMargin(0.0, 1e-10, 1e-5, 1e-5) is true.
109   template<typename T>
110   static bool WithinFractionOrMargin(const T x, const T y,
111                                      const T fraction, const T margin);
112 };
113 
114 template<typename T>
WithinFractionOrMargin(const T x,const T y,const T fraction,const T margin)115 bool MathUtil::WithinFractionOrMargin(const T x, const T y,
116                                       const T fraction, const T margin) {
117   // Not just "0 <= fraction" to fool the compiler for unsigned types.
118   GOOGLE_DCHECK((T(0) < fraction || T(0) == fraction) &&
119          fraction < T(1) &&
120          margin >= T(0));
121 
122   // Template specialization will convert the if() condition to a constant,
123   // which will cause the compiler to generate code for either the "if" part
124   // or the "then" part.  In this way we avoid a compiler warning
125   // about a potential integer overflow in crosstool v12 (gcc 4.3.1).
126   if (MathLimits<T>::kIsInteger) {
127     return x == y;
128   } else {
129     if (!MathLimits<T>::IsFinite(x) || !MathLimits<T>::IsFinite(y)) {
130       return false;
131     }
132     T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y)));
133     return AbsDiff(x, y) <= Max(margin, relative_margin);
134   }
135 }
136 
137 }  // namespace protobuf
138 }  // namespace google
139 
140 #endif  // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_
141