-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.c
executable file
·95 lines (84 loc) · 1.63 KB
/
math.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "math.h"
static unsigned int g_seed = 0;
int fastrand() {
++g_seed;
g_seed = (214013*g_seed+2531011);
return (g_seed>>16)&0x7FFF;
}
#define PI 3.1415926536
float abs(float x)
{
if (x > 0) return x;
return -x;
}
float pow(float a, int b)
{
float r = a;
if (b > 0) {
while (--b)
r *= a;
}
else if (b < 0) {
while (++b)
r *= a;
r = 1.0 / r;
}
else r = 0;
return r;
}
float sqrt(float number) {
float new_guess;
float last_guess;
if (number < 0) {
return -1;
}
new_guess = 1;
do {
last_guess = new_guess;
new_guess = (last_guess + number / last_guess) / 2;
} while (new_guess != last_guess);
return new_guess;
}
float cos(float x)
{
float fl = 1;
if (x > 2 * PI || x < -2 * PI) x -= (int)(x / (2 * PI)) * 2 * PI;
if (x > PI) x -= 2 * PI;
if (x < -PI) x += 2 * PI;
if (x > PI / 2)
{
x -= PI;
fl *= -1;
}
if (x < -PI / 2)
{
x += PI;
fl *= -1;
}
if (x > PI / 4) return fl * sin(PI / 2 - x);
else return fl * (1 - pow(x, 2) / 2 + pow(x, 4) / 24 - pow(x, 6) / 720 + pow(x, 8) / 40320);//̩�չ�ʽ
}
float sin(float x)
{
float fl = 1;
if (x > 2 * PI || x < -2 * PI) x -= (int)(x / (2 * PI)) * 2 * PI;
if (x > PI) x -= 2 * PI;
if (x < -PI) x += 2 * PI;
if (x > PI / 2)
{
x -= PI;
fl *= -1;
}
if (x < -PI / 2)
{
x += PI;
fl *= -1;
}
if (x < 0)
{
x *= -1;
fl *= -1;
}
if (x > PI / 4) return fl * cos(PI / 2 - x);
else return fl * (x - pow(x, 3) / 6 + pow(x, 5) / 120 - pow(x, 7) / 5040 + pow(x, 9) / 362880);//̩�չ�ʽ
}