[Bf-blender-cvs] [c1e177ad292] master: BLI_math: add simple helper to get amount of 'integer' digits in a float number.

Bastien Montagne noreply at git.blender.org
Tue Aug 1 16:45:15 CEST 2017


Commit: c1e177ad29249a9508ee4f1c7b09f51fba9ac9e9
Author: Bastien Montagne
Date:   Tue Aug 1 16:34:02 2017 +0200
Branches: master
https://developer.blender.org/rBc1e177ad29249a9508ee4f1c7b09f51fba9ac9e9

BLI_math: add simple helper to get amount of 'integer' digits in a float number.

===================================================================

M	source/blender/blenlib/BLI_math_base.h
M	source/blender/blenlib/intern/math_base_inline.c

===================================================================

diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h
index 0126e30d900..c44b666faea 100644
--- a/source/blender/blenlib/BLI_math_base.h
+++ b/source/blender/blenlib/BLI_math_base.h
@@ -138,6 +138,9 @@ MINLINE int signum_i(float a);
 
 MINLINE float power_of_2(float f);
 
+MINLINE int integer_digits_f(const float f);
+MINLINE int integer_digits_d(const double d);
+
 /* these don't really fit anywhere but were being copied about a lot */
 MINLINE int is_power_of_2_i(int n);
 MINLINE int power_of_2_max_i(int n);
diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c
index 8d2d80c2a35..6574c001a23 100644
--- a/source/blender/blenlib/intern/math_base_inline.c
+++ b/source/blender/blenlib/intern/math_base_inline.c
@@ -314,6 +314,21 @@ MINLINE int signum_i(float a)
 	else          return  0;
 }
 
+/** Returns number of (base ten) *significant* digits of integer part of given float
+ * (negative in case of decimal-only floats, 0.01 returns -1 e.g.). */
+MINLINE int integer_digits_f(const float f)
+{
+	return (f == 0.0f) ? 0 : (int)floor(log10(fabs(f))) + 1;
+}
+
+/** Returns number of (base ten) *significant* digits of integer part of given double
+ * (negative in case of decimal-only floats, 0.01 returns -1 e.g.). */
+MINLINE int integer_digits_d(const double d)
+{
+	return (d == 0.0) ? 0 : (int)floor(log10(fabs(d))) + 1;
+}
+
+
 /* Internal helpers for SSE2 implementation.
  *
  * NOTE: Are to be called ONLY from inside `#ifdef __SSE2__` !!!




More information about the Bf-blender-cvs mailing list