Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-11-30 01:42:33 +0300
committerCampbell Barton <ideasman42@gmail.com>2009-11-30 01:42:33 +0300
commitd98093a91ac7ce96ab6b1e14168493f9f69f379d (patch)
treea89141e41ab7dfaa81b05b5f3cf97dd7baa8469e /source/blender/blenlib/intern/math_base.c
parentcd154da9732962870339952898499ed1b1c32d93 (diff)
- added a new math function double_round, useful for rounding a number to a number of decimal places.
- added Mathutils vector method, vec.asTuple(round), since this is tedious in python and fairly common task.
Diffstat (limited to 'source/blender/blenlib/intern/math_base.c')
-rw-r--r--source/blender/blenlib/intern/math_base.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/math_base.c b/source/blender/blenlib/intern/math_base.c
index f3fe09c088f..e1a53a09d40 100644
--- a/source/blender/blenlib/intern/math_base.c
+++ b/source/blender/blenlib/intern/math_base.c
@@ -109,3 +109,34 @@ float power_of_2(float val)
return (float)pow(2, ceil(log(val) / log(2)));
}
+/* from python 3.1 floatobject.c
+ * ndigits must be between 0 and 21 */
+double double_round(double x, int ndigits) {
+ double pow1, pow2, y, z;
+ if (ndigits >= 0) {
+ pow1 = pow(10.0, (double)ndigits);
+ pow2 = 1.0;
+ y = (x*pow1)*pow2;
+ /* if y overflows, then rounded value is exactly x */
+ if (!finite(y))
+ return x;
+ }
+ else {
+ pow1 = pow(10.0, (double)-ndigits);
+ pow2 = 1.0; /* unused; silences a gcc compiler warning */
+ y = x / pow1;
+ }
+
+ z = round(y);
+ if (fabs(y-z) == 0.5)
+ /* halfway between two integers; use round-half-even */
+ z = 2.0*round(y/2.0);
+
+ if (ndigits >= 0)
+ z = (z / pow2) / pow1;
+ else
+ z *= pow1;
+
+ /* if computation resulted in overflow, raise OverflowError */
+ return z;
+}