-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.ecl
More file actions
183 lines (148 loc) · 2.93 KB
/
math.ecl
File metadata and controls
183 lines (148 loc) · 2.93 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
// SPDX-License-Identifier: PMPL-1.0-or-later
// SPDX-FileCopyrightText: 2025 Jonathan D.A. Jewell
//! Math module providing mathematical functions and constants.
// Mathematical constants
const PI: Float = 3.141592653589793
const E: Float = 2.718281828459045
const TAU: Float = 6.283185307179586 // 2 * PI
// Basic functions
fn abs_int(x: Int) -> Int {
if x < 0 { -x } else { x }
}
fn abs_float(x: Float) -> Float {
if x < 0.0 { -x } else { x }
}
fn min_int(a: Int, b: Int) -> Int {
if a < b { a } else { b }
}
fn max_int(a: Int, b: Int) -> Int {
if a > b { a } else { b }
}
fn min_float(a: Float, b: Float) -> Float {
if a < b { a } else { b }
}
fn max_float(a: Float, b: Float) -> Float {
if a > b { a } else { b }
}
// Power and roots
fn pow_int(base: Int, exp: Int) -> Int {
if exp == 0 {
1
} else if exp == 1 {
base
} else if exp % 2 == 0 {
let half = pow_int(base, exp / 2)
half * half
} else {
base * pow_int(base, exp - 1)
}
}
fn sqrt(x: Float) -> Float {
@builtin("sqrt")
}
fn cbrt(x: Float) -> Float {
@builtin("cbrt")
}
// Trigonometric functions
fn sin(x: Float) -> Float {
@builtin("sin")
}
fn cos(x: Float) -> Float {
@builtin("cos")
}
fn tan(x: Float) -> Float {
@builtin("tan")
}
fn asin(x: Float) -> Float {
@builtin("asin")
}
fn acos(x: Float) -> Float {
@builtin("acos")
}
fn atan(x: Float) -> Float {
@builtin("atan")
}
fn atan2(y: Float, x: Float) -> Float {
@builtin("atan2")
}
// Hyperbolic functions
fn sinh(x: Float) -> Float {
@builtin("sinh")
}
fn cosh(x: Float) -> Float {
@builtin("cosh")
}
fn tanh(x: Float) -> Float {
@builtin("tanh")
}
// Exponential and logarithmic functions
fn exp(x: Float) -> Float {
@builtin("exp")
}
fn ln(x: Float) -> Float {
@builtin("ln")
}
fn log10(x: Float) -> Float {
@builtin("log10")
}
fn log2(x: Float) -> Float {
@builtin("log2")
}
fn log(x: Float, base: Float) -> Float {
ln(x) / ln(base)
}
// Rounding functions
fn floor(x: Float) -> Float {
@builtin("floor")
}
fn ceil(x: Float) -> Float {
@builtin("ceil")
}
fn round(x: Float) -> Float {
@builtin("round")
}
fn trunc(x: Float) -> Float {
@builtin("trunc")
}
// Factorial
fn factorial(n: Int) -> Int {
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
// GCD and LCM
fn gcd(a: Int, b: Int) -> Int {
if b == 0 {
abs_int(a)
} else {
gcd(b, a % b)
}
}
fn lcm(a: Int, b: Int) -> Int {
if a == 0 || b == 0 {
0
} else {
abs_int(a * b) / gcd(a, b)
}
}
// Clamping
fn clamp_int(value: Int, min: Int, max: Int) -> Int {
if value < min {
min
} else if value > max {
max
} else {
value
}
}
fn clamp_float(value: Float, min: Float, max: Float) -> Float {
if value < min {
min
} else if value > max {
max
} else {
value
}
}