blob: 3a1ef8a8cbb3b3fe509d97922ef92794ea5ae0aa [file] [log] [blame]
Feng Xiaoe96ff302015-06-15 18:21:48 -07001// 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>
Jisi Liu51053122015-07-17 15:22:05 -070037#include <google/protobuf/stubs/logging.h>
Feng Xiaoe96ff302015-06-15 18:21:48 -070038#include <google/protobuf/stubs/mathlimits.h>
39
40namespace google {
41namespace protobuf {
42namespace internal {
43template<typename T>
44bool IsNan(T value) {
45 return false;
46}
47template<>
Bo Yangff7bdad2015-08-23 10:45:14 -070048inline bool IsNan(float value) {
49#ifdef _MSC_VER
50 return _isnan(value);
51#else
52 return isnan(value);
53#endif
54}
Feng Xiaoe96ff302015-06-15 18:21:48 -070055template<>
Bo Yangff7bdad2015-08-23 10:45:14 -070056inline bool IsNan(double value) {
57#ifdef _MSC_VER
58 return _isnan(value);
59#else
60 return isnan(value);
61#endif
62}
Feng Xiaoe96ff302015-06-15 18:21:48 -070063
64template<typename T>
65bool AlmostEquals(T a, T b) {
66 return a == b;
67}
68template<>
69inline bool AlmostEquals(float a, float b) {
70 return fabs(a - b) < 32 * FLT_EPSILON;
71}
72
73template<>
74inline bool AlmostEquals(double a, double b) {
75 return fabs(a - b) < 32 * DBL_EPSILON;
76}
77} // namespace internal
78
79class MathUtil {
80 public:
81 template<typename T>
82 static T Sign(T value) {
83 if (value == T(0) || ::google::protobuf::internal::IsNan<T>(value)) {
84 return value;
85 }
86 return value > T(0) ? value : -value;
87 }
88
89 template<typename T>
90 static bool AlmostEquals(T a, T b) {
91 return ::google::protobuf::internal::AlmostEquals(a, b);
92 }
93
94 // Largest of two values.
95 // Works correctly for special floating point values.
96 // Note: 0.0 and -0.0 are not differentiated by Max (Max(0.0, -0.0) is -0.0),
97 // which should be OK because, although they (can) have different
98 // bit representation, they are observably the same when examined
99 // with arithmetic and (in)equality operators.
100 template<typename T>
101 static T Max(const T x, const T y) {
102 return MathLimits<T>::IsNaN(x) || x > y ? x : y;
103 }
104
105 // Absolute value of x
106 // Works correctly for unsigned types and
107 // for special floating point values.
108 // Note: 0.0 and -0.0 are not differentiated by Abs (Abs(0.0) is -0.0),
109 // which should be OK: see the comment for Max above.
110 template<typename T>
111 static T Abs(const T x) {
112 return x > T(0) ? x : -x;
113 }
114
115 // Absolute value of the difference between two numbers.
116 // Works correctly for signed types and special floating point values.
117 template<typename T>
118 static typename MathLimits<T>::UnsignedType AbsDiff(const T x, const T y) {
119 // Carries out arithmetic as unsigned to avoid overflow.
120 typedef typename MathLimits<T>::UnsignedType R;
121 return x > y ? R(x) - R(y) : R(y) - R(x);
122 }
123
124 // If two (usually floating point) numbers are within a certain
125 // fraction of their magnitude or within a certain absolute margin of error.
126 // This is the same as the following but faster:
127 // WithinFraction(x, y, fraction) || WithinMargin(x, y, margin)
128 // E.g. WithinFraction(0.0, 1e-10, 1e-5) is false but
129 // WithinFractionOrMargin(0.0, 1e-10, 1e-5, 1e-5) is true.
130 template<typename T>
131 static bool WithinFractionOrMargin(const T x, const T y,
132 const T fraction, const T margin);
133};
134
135template<typename T>
136bool MathUtil::WithinFractionOrMargin(const T x, const T y,
137 const T fraction, const T margin) {
138 // Not just "0 <= fraction" to fool the compiler for unsigned types.
139 GOOGLE_DCHECK((T(0) < fraction || T(0) == fraction) &&
140 fraction < T(1) &&
141 margin >= T(0));
142
143 // Template specialization will convert the if() condition to a constant,
144 // which will cause the compiler to generate code for either the "if" part
145 // or the "then" part. In this way we avoid a compiler warning
146 // about a potential integer overflow in crosstool v12 (gcc 4.3.1).
147 if (MathLimits<T>::kIsInteger) {
148 return x == y;
149 } else {
150 // IsFinite checks are to make kPosInf and kNegInf not within fraction
151 if (!MathLimits<T>::IsFinite(x) && !MathLimits<T>::IsFinite(y)) {
152 return false;
153 }
154 T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y)));
155 return AbsDiff(x, y) <= Max(margin, relative_margin);
156 }
157}
158
159} // namespace protobuf
160} // namespace google
161
162#endif // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_