-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.py
More file actions
351 lines (284 loc) · 9.56 KB
/
Copy pathmath.py
File metadata and controls
351 lines (284 loc) · 9.56 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
@namespace("math")
from Promethium import ValueError, List
# The module namespace is intentionally explicit: consumers use the normal
# Python-shaped `import math` spelling rather than the project implementation
# namespace.
#
# Where a function can be implemented with pure Promethium arithmetic
# (fabs/isnan/isinf/isfinite/degrees/radians/copysign/gcd/factorial/fmod),
# it is, exactly like the original fabs/isnan — no per-target branching
# needed, and no dependency on any native math library existing or being
# spelled consistently across targets.
#
# Everything else (roots, logs, trig, powers) genuinely needs a native call:
# Elements compiles this same source to four very different backends, and
# each spells its math library differently. Confirmed call surfaces:
# - Echoes: System.Math.Sqrt/.Floor/.Ceiling/.Truncate/.Pow/.Log/.Log10/
# .Exp/.Sin/.Cos/.Tan/.Asin/.Acos/.Atan/.Atan2 (RTL2's own
# Math.pas maps identically; no Log2 pre-.NET6, use Log(x, 2)).
# - Island: RemObjects.Elements.System.Math with the same Sqrt/Floor/
# Ceiling/Truncate/Pow/Log/Log2/Log10/Exp/Sin/Cos/Tan/Asin/
# Acos/Atan/Atan2 names (IslandRTL's own Math unit).
# - Cooper: Math.sqrt/.floor/.ceil/.pow/.log/.log10/.exp/.sin/.cos/.tan/
# .asin/.acos/.atan/.atan2 (java.lang.Math, lowercase methods,
# no explicit import needed).
# - Toffee: rtl.sqrt/.floor/.ceil/.pow/.log/.log10/.exp/.sin/.cos/.tan/
# .asin/.acos/.atan/.atan2 — the `rtl.`-qualified spelling is
# used deliberately instead of the bare global C functions
# (`sqrt(...)` etc., which also exist in Toffee mode): a bare
# call to `sqrt` from *inside a function this module itself
# defines and also names `sqrt`* would resolve to itself
# (infinite recursion) rather than the C library function.
# `System.Math.Sqrt(value)`-style fully-qualified native calls follow the
# same pattern already proven in PromethiumBaseLibrary's own Builtins.py,
# whose `print` calls `RemObjects.Elements.System.writeLn(value)` directly.
#
# `int(...)`/`float(...)` casts are avoided entirely here — there's no
# confirmed-working cast syntax in this codebase yet, so every function
# below either returns float directly from a native call or works with
# int arithmetic throughout (gcd, factorial).
def fabs(value: float) -> float:
if value == 0.0:
return 0.0
if value < 0.0:
return -value
return value
def isnan(value: float) -> bool:
return value != value
def isinf(value: float) -> bool:
if isnan(value):
return False
difference: float = value - value
return isnan(difference)
def isfinite(value: float) -> bool:
return not isnan(value) and not isinf(value)
def copysign(magnitude: float, sign: float) -> float:
result: float = fabs(magnitude)
if sign < 0.0:
return -result
return result
def fmod(x: float, y: float) -> float:
return x % y
def degrees(value: float) -> float:
return value * (180.0 / 3.141592653589793)
def radians(value: float) -> float:
return value * (3.141592653589793 / 180.0)
def gcd(a: int, b: int) -> int:
x: int = a
if x < 0:
x = -x
y: int = b
if y < 0:
y = -y
while y != 0:
remainder: int = x % y
x = y
y = remainder
return x
def factorial(n: int) -> int:
if n < 0:
raise ValueError("factorial() not defined for negative values")
result: int = 1
value: int = 2
while value <= n:
result *= value
value += 1
return result
def sqrt(value: float) -> float:
if defined("ECHOES"):
return System.Math.Sqrt(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Sqrt(value)
elif defined("COOPER"):
return Math.sqrt(value)
else:
return rtl.sqrt(value)
def hypot(x: float, y: float) -> float:
return sqrt(x * x + y * y)
def floor(value: float) -> float:
if defined("ECHOES"):
return System.Math.Floor(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Floor(value)
elif defined("COOPER"):
return Math.floor(value)
else:
return rtl.floor(value)
def ceil(value: float) -> float:
if defined("ECHOES"):
return System.Math.Ceiling(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Ceiling(value)
elif defined("COOPER"):
return Math.ceil(value)
else:
return rtl.ceil(value)
def trunc(value: float) -> float:
if defined("ECHOES"):
return System.Math.Truncate(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Truncate(value)
elif defined("COOPER"):
if value < 0.0:
return Math.ceil(value)
else:
return Math.floor(value)
else:
return rtl.trunc(value)
def pow(base: float, exponent: float) -> float:
if defined("ECHOES"):
return System.Math.Pow(base, exponent)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Pow(base, exponent)
elif defined("COOPER"):
return Math.pow(base, exponent)
else:
return rtl.pow(base, exponent)
def exp(value: float) -> float:
if defined("ECHOES"):
return System.Math.Exp(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Exp(value)
elif defined("COOPER"):
return Math.exp(value)
else:
return rtl.exp(value)
def log(value: float) -> float:
if defined("ECHOES"):
return System.Math.Log(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Log(value)
elif defined("COOPER"):
return Math.log(value)
else:
return rtl.log(value)
def log10(value: float) -> float:
if defined("ECHOES"):
return System.Math.Log10(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Log10(value)
elif defined("COOPER"):
return Math.log10(value)
else:
return rtl.log10(value)
def log2(value: float) -> float:
if defined("ISLAND"):
return RemObjects.Elements.System.Math.Log2(value)
else:
return log(value) / log(2.0)
def sin(value: float) -> float:
if defined("ECHOES"):
return System.Math.Sin(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Sin(value)
elif defined("COOPER"):
return Math.sin(value)
else:
return rtl.sin(value)
def cos(value: float) -> float:
if defined("ECHOES"):
return System.Math.Cos(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Cos(value)
elif defined("COOPER"):
return Math.cos(value)
else:
return rtl.cos(value)
def tan(value: float) -> float:
if defined("ECHOES"):
return System.Math.Tan(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Tan(value)
elif defined("COOPER"):
return Math.tan(value)
else:
return rtl.tan(value)
def asin(value: float) -> float:
if defined("ECHOES"):
return System.Math.Asin(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Asin(value)
elif defined("COOPER"):
return Math.asin(value)
else:
return rtl.asin(value)
def acos(value: float) -> float:
if defined("ECHOES"):
return System.Math.Acos(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Acos(value)
elif defined("COOPER"):
return Math.acos(value)
else:
return rtl.acos(value)
def atan(value: float) -> float:
if defined("ECHOES"):
return System.Math.Atan(value)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Atan(value)
elif defined("COOPER"):
return Math.atan(value)
else:
return rtl.atan(value)
def atan2(y: float, x: float) -> float:
if defined("ECHOES"):
return System.Math.Atan2(y, x)
elif defined("ISLAND"):
return RemObjects.Elements.System.Math.Atan2(y, x)
elif defined("COOPER"):
return Math.atan2(y, x)
else:
return rtl.atan2(y, x)
def isqrt(n: int) -> int:
if n < 0:
raise ValueError("isqrt() argument must be nonnegative")
if n == 0:
return 0
x: int = n
y: int = (x + 1) / 2
while y < x:
x = y
y = (x + n / x) / 2
return x
def comb(n: int, k: int) -> int:
if k < 0 or n < 0:
raise ValueError("comb() arguments must be non-negative")
if k > n:
return 0
if k > n - k:
k = n - k
result: int = 1
index: int = 0
while index < k:
result = result * (n - index) / (index + 1)
index += 1
return result
def perm(n: int, k: int) -> int:
if k < 0 or n < 0:
raise ValueError("perm() arguments must be non-negative")
if k > n:
return 0
result: int = 1
index: int = 0
while index < k:
result *= (n - index)
index += 1
return result
def prod(values: List[int]) -> int:
result: int = 1
index: int = 0
while index < len(values):
result *= values.__getitem__(index)
index += 1
return result
def prod(values: List[float]) -> float:
result: float = 1.0
index: int = 0
while index < len(values):
result *= values.__getitem__(index)
index += 1
return result
def dist(p: tuple[float, float], q: tuple[float, float]) -> float:
dx: float = p[0] - q[0]
dy: float = p[1] - q[1]
return sqrt(dx * dx + dy * dy)