-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.ltl
More file actions
206 lines (176 loc) · 4.48 KB
/
Copy pathmath.ltl
File metadata and controls
206 lines (176 loc) · 4.48 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Lateralus Standard Library — math.ltl (v1.5.0)
// Extended mathematical functions
module stdlib.math
pub const PI: float = 3.141592653589793
pub const E: float = 2.718281828459045
pub const TAU: float = 6.283185307179586
pub const PHI: float = 1.618033988749895
pub const INF: float = 1.7976931348623157e+308
pub const SQRT2: float = 1.4142135623730951
// -- Core arithmetic ----------------------------------------------------------
pub fn abs_val(x: float) -> float {
if x < 0.0 { return -x }
return x
}
pub fn sqrt(x: float) -> float {
if x < 0.0 {
throw ValueError("sqrt of negative number")
}
// Newton-Raphson
let mut guess = x / 2.0
let mut prev = 0.0
while abs_val(guess - prev) > 0.0000001 {
prev = guess
guess = (guess + x / guess) / 2.0
}
return guess
}
pub fn pow(base: float, exp: int) -> float {
if exp == 0 { return 1.0 }
if exp < 0 { return 1.0 / pow(base, -exp) }
let mut result = 1.0
let mut i = 0
while i < exp {
result = result * base
i += 1
}
return result
}
pub fn floor(x: float) -> int {
let n = x as int
if x < 0.0 && (x as float) != (n as float) {
return n - 1
}
return n
}
pub fn ceil(x: float) -> int {
let n = x as int
if x > 0.0 && (x as float) != (n as float) {
return n + 1
}
return n
}
pub fn round_val(x: float) -> int {
return floor(x + 0.5)
}
pub fn clamp(x: float, lo: float, hi: float) -> float {
if x < lo { return lo }
if x > hi { return hi }
return x
}
pub fn sign(x: float) -> int {
if x > 0.0 { return 1 }
if x < 0.0 { return -1 }
return 0
}
pub fn lerp(a: float, b: float, t: float) -> float {
return a + (b - a) * t
}
pub fn hypot(a: float, b: float) -> float {
return sqrt(a * a + b * b)
}
// -- Number theory ------------------------------------------------------------
pub fn factorial(n: int) -> int {
if n <= 1 { return 1 }
return n * factorial(n - 1)
}
pub fn gcd(a: int, b: int) -> int {
let mut aa = a
let mut bb = b
while bb != 0 {
let t = bb
bb = aa % bb
aa = t
}
return aa
}
pub fn lcm(a: int, b: int) -> int {
return (a * b) / gcd(a, b)
}
pub fn is_prime(n: int) -> bool {
if n < 2 { return false }
if n == 2 { return true }
if n % 2 == 0 { return false }
let mut i = 3
while i * i <= n {
if n % i == 0 { return false }
i += 2
}
return true
}
pub fn isqrt(n: int) -> int {
if n < 0 { throw ValueError("isqrt of negative") }
return floor(sqrt(n as float))
}
// Fibonacci (iterative, fast)
pub fn fibonacci(n: int) -> int {
if n <= 0 { return 0 }
if n == 1 { return 1 }
let mut a = 0
let mut b = 1
let mut i = 2
while i <= n {
let c = a + b
a = b
b = c
i += 1
}
return b
}
// -- Trigonometry (Taylor series) ---------------------------------------------
pub fn sin(x: float) -> float {
let mut r = x
while r > PI { r = r - TAU }
while r < -PI { r = r + TAU }
let mut term = r
let mut result = r
let mut i = 1
while abs_val(term) > 1e-12 {
term = -term * r * r / (((2*i) * (2*i + 1)) as float)
result = result + term
i += 1
}
return result
}
pub fn cos(x: float) -> float {
return sin(PI / 2.0 - x)
}
pub fn tan(x: float) -> float {
let c = cos(x)
if abs_val(c) < 1e-12 { throw ValueError("tan undefined at this value") }
return sin(x) / c
}
pub fn radians(deg: float) -> float {
return deg * PI / 180.0
}
pub fn degrees(rad: float) -> float {
return rad * 180.0 / PI
}
// -- Logarithms ---------------------------------------------------------------
pub fn ln(x: float) -> float {
if x <= 0.0 { throw ValueError("ln of non-positive number") }
let LN2: float = 0.6931471805599453
let mut k = 0
let mut m = x
while m > 1.5 { m = m / 2.0; k += 1 }
while m < 0.5 { m = m * 2.0; k -= 1 }
let y = (m - 1.0) / (m + 1.0)
let y2 = y * y
let mut s = 0.0
let mut term = y
let mut i = 1
while abs_val(term) > 1e-12 {
s = s + term / (i as float)
term = term * y2
i += 2
}
return 2.0 * s + (k as float) * LN2
}
pub fn log2(x: float) -> float {
if x <= 0.0 { throw ValueError("log2 of non-positive number") }
return ln(x) / ln(2.0)
}
pub fn log10(x: float) -> float {
if x <= 0.0 { throw ValueError("log10 of non-positive number") }
return ln(x) / ln(10.0)
}