Blender V5.3
math_base.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base_c.hh"
10
11#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
12
13namespace blender {
14
15int pow_i(int base, int exp)
16{
17 int result = 1;
18 BLI_assert(exp >= 0);
19 while (exp) {
20 if (exp & 1) {
21 result *= base;
22 }
23 exp >>= 1;
24 base *= base;
25 }
26
27 return result;
28}
29
30double double_round(double x, int ndigits)
31{
32 /* From Python 3.1 `floatobject.c`. */
33
34 double pow1, pow2, y, z;
35 if (ndigits >= 0) {
36 pow1 = pow(10.0, double(ndigits));
37 pow2 = 1.0;
38 y = (x * pow1) * pow2;
39 /* if y overflows, then rounded value is exactly x */
40 if (!isfinite(y)) {
41 return x;
42 }
43 }
44 else {
45 pow1 = pow(10.0, double(-ndigits));
46 pow2 = 1.0; /* unused; silences a gcc compiler warning */
47 y = x / pow1;
48 }
49
50 z = round(y);
51 if (fabs(y - z) == 0.5) {
52 /* halfway between two integers; use round-half-even */
53 z = 2.0 * round(y / 2.0);
54 }
55
56 if (ndigits >= 0) {
57 z = (z / pow2) / pow1;
58 }
59 else {
60 z *= pow1;
61 }
62
63 /* if computation resulted in overflow, raise OverflowError */
64 return z;
65}
66
67float floor_power_of_10(float f)
68{
69 BLI_assert(!(f < 0.0f));
70 if (f != 0.0f) {
71 return 1.0f / powf(10.0f, ceilf(log10f(1.0f / f)));
72 }
73 return 0.0f;
74}
75
76float ceil_power_of_10(float f)
77{
78 BLI_assert(!(f < 0.0f));
79 if (f != 0.0f) {
80 return 1.0f / powf(10.0f, floorf(log10f(1.0f / f)));
81 }
82 return 0.0f;
83}
84
85} // namespace blender
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define powf(x, y)
#define round
#define pow
#define exp
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
double double_round(double x, int ndigits)
Definition math_base.cc:30
float ceil_power_of_10(float f)
Definition math_base.cc:76
int pow_i(int base, int exp)
Definition math_base.cc:15
float floor_power_of_10(float f)
Definition math_base.cc:67
#define floorf
#define ceilf