-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.k
103 lines (76 loc) · 1.77 KB
/
math.k
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
96
97
98
99
100
101
102
103
$ ----
*
* math.k -- source for KAPPA math library
*
* Authored by Karl "p0lyh3dron" Kreuze on September 4th, 2023
*
* This file is part of the KAPPA library.
*
* The KAPPA library is an open-source hybrid
* scripting and programming language.
*
* Definitions for various mathematic functions can be found here.
*
---- $
f32: cos_approx(f32: t) { return (9.86960440109 - 4.0*t*t) / (9.86960440109 + t*t); };
f32: cos(f32: t) {
if t < 0.0 do t = 0.0 - t;
while t > 6.28318530718 do t = t - 6.28318530718;
if t < 1.57079632679 do return cos_approx(t);
return 0.0 - cos_approx(t - 3.14159265359);
};
f32: sin (f32: t) { return cos(t - 1.57079632679); };
f32: ipow(f32: t, u32: p) {
f32: ret = 1.0;
while p > 0 do {
ret = ret * t;
p = p - 1;
};
return ret;
};
u32: fact(u32: t) {
if t == 0 do return 1;
return t * fact(t - 1);
};
f32: exp(f32: t) {
f32: ret = 1.0;
f32: frac = 0.0;
u32: i = 0;
while t > 1.0 do {
t = t - 1.0;
ret = ret * 2.71828182846;
};
while t < 0.0 do {
t = t + 1.0;
ret = ret * 0.367879441171;
};
while i < 8 do {
frac = frac + ipow(t, i) / fact(i);
i = i + 1;
};
return ret * frac;
};
f32: log(f32: t) {
f32: ret = 0.0;
f32: frac = 0.0;
u32: i = 1;
while t > 1.0 do {
t = t / 2.71828182846;
ret = ret + 1.0;
};
t = t - 1;
while i < 8 do {
frac = frac + ipow(0.0 - 1.0, i) * ipow(t, i) / i;
i = i + 1;
};
return ret - frac;
};
f32: pow(f32: b, f32: e) {
return exp(e * log(b));
};
f32: cosh(f32: t) {
return (exp(t) + exp(0.0 - t)) / 2.0;
};
f32: sinh(f32: t) {
return (exp(t) - exp(0.0 - t)) / 2.0;
};