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> |
44 bool AlmostEquals(T a, T b) { | 65 bool AlmostEquals(T a, T b) { |
45 return a == b; | 66 return a == b; |
46 } | 67 } |
47 template<> | 68 template<> |
48 inline bool AlmostEquals(float a, float b) { | 69 inline bool AlmostEquals(float a, float b) { |
49 return fabs(a - b) < 32 * FLT_EPSILON; | 70 return fabs(a - b) < 32 * FLT_EPSILON; |
50 } | 71 } |
51 | 72 |
52 template<> | 73 template<> |
53 inline bool AlmostEquals(double a, double b) { | 74 inline bool AlmostEquals(double a, double b) { |
54 return fabs(a - b) < 32 * DBL_EPSILON; | 75 return fabs(a - b) < 32 * DBL_EPSILON; |
55 } | 76 } |
56 } // namespace internal | 77 } // namespace internal |
57 | 78 |
58 class MathUtil { | 79 class MathUtil { |
59 public: | 80 public: |
60 template<typename T> | 81 template<typename T> |
61 static T Sign(T value) { | 82 static T Sign(T value) { |
62 if (value == T(0) || MathLimits<T>::IsNaN(value)) { | 83 if (value == T(0) || ::google::protobuf::internal::IsNan<T>(value)) { |
63 return value; | 84 return value; |
64 } | 85 } |
65 return value > T(0) ? 1 : -1; | 86 return value > T(0) ? 1 : -1; |
66 } | 87 } |
67 | 88 |
68 template<typename T> | 89 template<typename T> |
69 static bool AlmostEquals(T a, T b) { | 90 static bool AlmostEquals(T a, T b) { |
70 return ::google::protobuf::internal::AlmostEquals(a, b); | 91 return ::google::protobuf::internal::AlmostEquals(a, b); |
71 } | 92 } |
72 | 93 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 153 } |
133 T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y))); | 154 T relative_margin = static_cast<T>(fraction * Max(Abs(x), Abs(y))); |
134 return AbsDiff(x, y) <= Max(margin, relative_margin); | 155 return AbsDiff(x, y) <= Max(margin, relative_margin); |
135 } | 156 } |
136 } | 157 } |
137 | 158 |
138 } // namespace protobuf | 159 } // namespace protobuf |
139 } // namespace google | 160 } // namespace google |
140 | 161 |
141 #endif // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_ | 162 #endif // GOOGLE_PROTOBUF_STUBS_MATHUTIL_H_ |
OLD | NEW |