Index: cc/base/math_util.cc |
diff --git a/cc/base/math_util.cc b/cc/base/math_util.cc |
index 6e5ac0bd45db73b47d318a7d4b3aef0f42af0078..f3ad5ef7a967ad186d7fbbf6987c2ea8341d7560 100644 |
--- a/cc/base/math_util.cc |
+++ b/cc/base/math_util.cc |
@@ -120,6 +120,32 @@ static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, |
(*num_vertices_in_clipped_quad)++; |
} |
+float MathUtil::RoundToFixedPrecision(float value, int precision) { |
+ DCHECK_GE(precision, 0); |
+ |
+ // The value of std::numeric_limits<float>::digits10 is the number of base-10 |
+ // digits that can be represented by the type float without change. |
+ int max_digits10 = std::numeric_limits<float>::digits10; |
+ |
+ float integral_part; |
+ float fractional_part = std::modf(value, &integral_part); |
+ |
+ // Return if |value| can not be represented by float without change. |
+ int digits10_in_integral_part = |
+ integral_part > 0 ? static_cast<int>(std::log10(integral_part)) + 1 : 1; |
+ if (digits10_in_integral_part > max_digits10) |
+ return value; |
+ |
+ // Get maximum possible precision. |
+ int possible_precision = |
+ std::min((max_digits10 - digits10_in_integral_part), precision); |
+ DCHECK(possible_precision < max_digits10); |
+ |
+ float factor = std::pow(10, possible_precision); |
+ fractional_part = std::round(fractional_part * factor) / factor; |
+ return integral_part + fractional_part; |
+} |
+ |
gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, |
const gfx::Rect& src_rect) { |
if (transform.IsIdentityOrIntegerTranslation()) { |