Chromium Code Reviews| 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 amlost euqal | |
|
vmpstr
2015/10/19 18:58:38
typo: amlost euqal -> 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 >= 1 and | |
| 104 // <= (std::numeric_limits<float>::digits10 - 1), i.e. 5, as floating point | |
| 105 // precision of higher than that gives different values. For other | |
| 106 // |precision|, this function returns the same |value|. | |
| 107 // Examples: | |
| 108 // RoundToFixedPrecision(7.33907556533813, 4) returns 7.339000225067138671875. | |
| 109 // RoundToFixedPrecision(7.33907508850098, 4) returns 7.339000225067138671875. | |
| 110 static float RoundToFixedPrecision(float value, int precision); | |
| 111 | |
| 100 // Returns true if rounded up value does not overflow, false otherwise. | 112 // Returns true if rounded up value does not overflow, false otherwise. |
| 101 template <typename T> | 113 template <typename T> |
| 102 static bool VerifyRoundup(T n, T mul) { | 114 static bool VerifyRoundup(T n, T mul) { |
| 103 return mul && (n <= (std::numeric_limits<T>::max() - | 115 return mul && (n <= (std::numeric_limits<T>::max() - |
| 104 (std::numeric_limits<T>::max() % mul))); | 116 (std::numeric_limits<T>::max() % mul))); |
| 105 } | 117 } |
| 106 | 118 |
| 107 // Rounds up a given |n| to be a multiple of |mul|, but may overflow. | 119 // Rounds up a given |n| to be a multiple of |mul|, but may overflow. |
| 108 // Examples: | 120 // Examples: |
| 109 // - RoundUp(123, 50) returns 150. | 121 // - RoundUp(123, 50) returns 150. |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 template <typename T> | 328 template <typename T> |
| 317 static T RoundDownInternal(T n, T mul) { | 329 static T RoundDownInternal(T n, T mul) { |
| 318 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 | 330 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 |
| 319 : ((n - mul + 1) / mul) * mul; | 331 : ((n - mul + 1) / mul) * mul; |
| 320 } | 332 } |
| 321 }; | 333 }; |
| 322 | 334 |
| 323 } // namespace cc | 335 } // namespace cc |
| 324 | 336 |
| 325 #endif // CC_BASE_MATH_UTIL_H_ | 337 #endif // CC_BASE_MATH_UTIL_H_ |
| OLD | NEW |