OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_BASE_MATH_UTIL_H_ | 5 #ifndef CC_BASE_MATH_UTIL_H_ |
6 #define CC_BASE_MATH_UTIL_H_ | 6 #define CC_BASE_MATH_UTIL_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <cmath> | 9 #include <cmath> |
10 #include <vector> | 10 #include <vector> |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; } | 90 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; } |
91 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; } | 91 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; } |
92 | 92 |
93 static float Round(float f) { | 93 static float Round(float f) { |
94 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); | 94 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); |
95 } | 95 } |
96 static double Round(double d) { | 96 static double Round(double d) { |
97 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); | 97 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); |
98 } | 98 } |
99 | 99 |
| 100 // Rounds the given |value| to fixed precision. This makes almost equal |
| 101 // floats (which differ by some threshold magnitude of floating point epsilon) |
| 102 // to be considered as same. |
| 103 // The |precision| should be >= 0, otherwise crashes in debug builds. |
| 104 // The max number of digits that can be represented without change by float |
| 105 // is 6. The maximum possible digits in the fraction part is computed by |
| 106 // considering digits in integral part and digits required for |precision|. |
| 107 // e.g. If intergral part has 4 digits and precision in fractional part |
| 108 // requested is 5, then max allowed precision for fraction part is 2. |
| 109 // Examples: |
| 110 // RoundToFixedPrecision(7.33907556533813, 4) returns 7.339000225067138671875. |
| 111 // RoundToFixedPrecision(7.33907508850098, 4) returns 7.339000225067138671875. |
| 112 static float RoundToFixedPrecision(float value, int precision); |
| 113 |
100 // Returns true if rounded up value does not overflow, false otherwise. | 114 // Returns true if rounded up value does not overflow, false otherwise. |
101 template <typename T> | 115 template <typename T> |
102 static bool VerifyRoundup(T n, T mul) { | 116 static bool VerifyRoundup(T n, T mul) { |
103 return mul && (n <= (std::numeric_limits<T>::max() - | 117 return mul && (n <= (std::numeric_limits<T>::max() - |
104 (std::numeric_limits<T>::max() % mul))); | 118 (std::numeric_limits<T>::max() % mul))); |
105 } | 119 } |
106 | 120 |
107 // Rounds up a given |n| to be a multiple of |mul|, but may overflow. | 121 // Rounds up a given |n| to be a multiple of |mul|, but may overflow. |
108 // Examples: | 122 // Examples: |
109 // - RoundUp(123, 50) returns 150. | 123 // - RoundUp(123, 50) returns 150. |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 template <typename T> | 330 template <typename T> |
317 static T RoundDownInternal(T n, T mul) { | 331 static T RoundDownInternal(T n, T mul) { |
318 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 | 332 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 |
319 : ((n - mul + 1) / mul) * mul; | 333 : ((n - mul + 1) / mul) * mul; |
320 } | 334 } |
321 }; | 335 }; |
322 | 336 |
323 } // namespace cc | 337 } // namespace cc |
324 | 338 |
325 #endif // CC_BASE_MATH_UTIL_H_ | 339 #endif // CC_BASE_MATH_UTIL_H_ |
OLD | NEW |