Blender V4.3
math_base.c
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
9#include "BLI_math_base.h"
10
11#include "BLI_strict_flags.h" /* Keep last. */
12
13int pow_i(int base, int exp)
14{
15 int result = 1;
16 BLI_assert(exp >= 0);
17 while (exp) {
18 if (exp & 1) {
19 result *= base;
20 }
21 exp >>= 1;
22 base *= base;
23 }
24
25 return result;
26}
27
28double double_round(double x, int ndigits)
29{
30 /* From Python 3.1 `floatobject.c`. */
31
32 double pow1, pow2, y, z;
33 if (ndigits >= 0) {
34 pow1 = pow(10.0, (double)ndigits);
35 pow2 = 1.0;
36 y = (x * pow1) * pow2;
37 /* if y overflows, then rounded value is exactly x */
38 if (!isfinite(y)) {
39 return x;
40 }
41 }
42 else {
43 pow1 = pow(10.0, (double)-ndigits);
44 pow2 = 1.0; /* unused; silences a gcc compiler warning */
45 y = x / pow1;
46 }
47
48 z = round(y);
49 if (fabs(y - z) == 0.5) {
50 /* halfway between two integers; use round-half-even */
51 z = 2.0 * round(y / 2.0);
52 }
53
54 if (ndigits >= 0) {
55 z = (z / pow2) / pow1;
56 }
57 else {
58 z *= pow1;
59 }
60
61 /* if computation resulted in overflow, raise OverflowError */
62 return z;
63}
64
65float floor_power_of_10(float f)
66{
67 BLI_assert(!(f < 0.0f));
68 if (f != 0.0f) {
69 return 1.0f / powf(10.0f, ceilf(log10f(1.0f / f)));
70 }
71 return 0.0f;
72}
73
74float ceil_power_of_10(float f)
75{
76 BLI_assert(!(f < 0.0f));
77 if (f != 0.0f) {
78 return 1.0f / powf(10.0f, floorf(log10f(1.0f / f)));
79 }
80 return 0.0f;
81}
#define BLI_assert(a)
Definition BLI_assert.h:50
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
pow(value.r - subtrahend, 2.0)") .do_static_compilation(true)
#define powf(x, y)
#define ceilf(x)
#define floorf(x)
int pow_i(int base, int exp)
Definition math_base.c:13
float floor_power_of_10(float f)
Definition math_base.c:65
float ceil_power_of_10(float f)
Definition math_base.c:74
double double_round(double x, int ndigits)
Definition math_base.c:28
ccl_device_inline float2 fabs(const float2 a)
ccl_device_inline float3 exp(float3 v)