OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 23 matching lines...) Expand all Loading... |
34 #include <math.h> | 34 #include <math.h> |
35 | 35 |
36 #include <google/protobuf/stubs/common.h> | 36 #include <google/protobuf/stubs/common.h> |
37 #include <google/protobuf/stubs/logging.h> | 37 #include <google/protobuf/stubs/logging.h> |
38 #include <google/protobuf/stubs/mathlimits.h> | 38 #include <google/protobuf/stubs/mathlimits.h> |
39 | 39 |
40 namespace google { | 40 namespace google { |
41 namespace protobuf { | 41 namespace protobuf { |
42 namespace internal { | 42 namespace internal { |
43 template<typename T> | 43 template<typename T> |
44 bool IsNan(T value) { | |
45 return false; | |
46 } | |
47 template<> | |
48 inline bool IsNan(float value) { | |
49 #ifdef _MSC_VER | |
50 return _isnan(value); | |
51 #else | |
52 return isnan(value); | |
53 #endif | |
54 } | |
55 template<> | |
56 inline bool IsNan(double value) { | |
57 #ifdef _MSC_VER | |
58 return _isnan(value); | |
59 #else | |
60 return isnan(value); | |
61 #endif | |
62 } | |
63 | |
64 template<typename T> | |
65 bool AlmostEquals(T a, T b) { | 44 bool AlmostEquals(T a, T b) { |
66 return a == b; | 45 return a == b; |
67 } | 46 } |
68 template<> | 47 template<> |
69 inline bool AlmostEquals(float a, float b) { | 48 inline bool AlmostEquals(float a, float b) { |
70 return fabs(a - b) < 32 * FLT_EPSILON; | 49 return fabs(a - b) < 32 * FLT_EPSILON; |
71 } | 50 } |
72 | 51 |
73 template<> | 52 template<> |
74 inline bool AlmostEquals(double a, double b) { | 53 inline bool AlmostEquals(double a, double b) { |
75 return fabs(a - b) < 32 * DBL_EPSILON; | 54 return fabs(a - b) < 32 * DBL_EPSILON; |
76 } | 55 } |
77 } // namespace internal | 56 } // namespace internal |
78 | 57 |
79 class MathUtil { | 58 class MathUtil { |
80 public: | 59 public: |
81 template<typename T> | 60 template<typename T> |
82 static T Sign(T value) { | 61 static T Sign(T value) { |
83 if (value == T(0) || ::google::protobuf::internal::IsNan<T>(value)) { | 62 if (value == T(0) || MathLimits<T>::IsNaN(value)) { |
84 return value; | 63 return value; |
85 } | 64 } |
86 return value > T(0) ? 1 : -1; | 65 return value > T(0) ? 1 : -1; |
87 } | 66 } |
88 | 67 |
89 template<typename T> | 68 template<typename T> |
90 static bool AlmostEquals(T a, T b) { | 69 static bool AlmostEquals(T a, T b) { |
91 return ::google::protobuf::internal::AlmostEquals(a, b); | 70 return ::google::protobuf::internal::AlmostEquals(a, b); |
92 } | 71 } |
93 | 72 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } | 132 } |
154 T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y))); | 133 T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y))); |
155 return AbsDiff(x, y) <= Max(margin, relative_margin); | 134 return AbsDiff(x, y) <= Max(margin, relative_margin); |
156 } | 135 } |
157 } | 136 } |
158 | 137 |
159 } // namespace protobuf | 138 } // namespace protobuf |
160 } // namespace google | 139 } // namespace google |
161 | 140 |
162 #endif // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_ | 141 #endif // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_ |
OLD | NEW |