-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.cpp
58 lines (51 loc) · 976 Bytes
/
Math.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Math.h"
#include <cmath>
// see https://en.wikipedia.org/wiki/Exponentiation_by_squaring
float Math::exp_by_squaring(float x, uint32_t n)
{
if(n < 0)
{
return exp_by_squaring(1 / x, -n);
}
else if(n == 0)
{
return 1;
}
else if(n == 1)
{
return x;
}
else if(n % 2 == 0)
{
return exp_by_squaring(x * x, n / 2);
}
else
{
return x * exp_by_squaring(x * x, (n - 1) / 2);
}
}
float Math::fast_pow(float a, float b)
{
float in = fabs(a);
union
{
double d;
int x[2];
} u = { in };
u.x[1] = (int)(b * (u.x[1] - 1072632447) + 1072632447);
u.x[0] = 0;
return u.d;
}
float Math::fast_sqrt(float x)
{
unsigned int i = *(unsigned int*) &x;
// adjust bias
i += 127 << 23;
// approximation of square root
i >>= 1;
return *(float*) &i;
}
float Math::theta_from_cartesian(float x, float y)
{
return 0;
}