-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.affine
More file actions
111 lines (93 loc) · 2.09 KB
/
Copy pathMath.affine
File metadata and controls
111 lines (93 loc) · 2.09 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
// SPDX-License-Identifier: PMPL-1.0-or-later
// SPDX-FileCopyrightText: 2025 hyperpolymath
// Math - Mathematical functions
module Math;
// Constants (as functions since const declarations not yet supported)
pub fn pi() -> Float {
return 3.141592653589793;
}
pub fn e() -> Float {
return 2.718281828459045;
}
pub fn tau() -> Float {
return 6.283185307179586;
}
// Integer operations
pub fn abs(x: Int) -> Int {
return if x < 0 { return 0 - x; } else { return x; };
}
pub fn min(a: Int, b: Int) -> Int {
return if a < b { return a; } else { return b; };
}
pub fn max(a: Int, b: Int) -> Int {
return if a > b { return a; } else { return b; };
}
pub fn clamp(x: Int, low: Int, high: Int) -> Int {
return max(low, min(x, high));
}
// Power (integer exponentiation)
pub fn pow(base: Int, exp: Int) -> Int {
return if exp == 0 {
return 1;
} else {
return if exp == 1 {
return base;
} else {
let half = pow(base, exp / 2);
return if exp % 2 == 0 {
return half * half;
} else {
return base * half * half;
};
};
};
}
// GCD using Euclid's algorithm
pub fn gcd(a: Int, b: Int) -> Int {
let a_abs = abs(a);
let b_abs = abs(b);
return if b_abs == 0 {
return a_abs;
} else {
return gcd(b_abs, a_abs % b_abs);
};
}
// LCM
pub fn lcm(a: Int, b: Int) -> Int {
return if a == 0 {
return 0;
} else {
return if b == 0 {
return 0;
} else {
return abs(a * b) / gcd(a, b);
};
};
}
// Factorial
pub fn factorial(n: Int) -> Int {
return if n <= 1 {
return 1;
} else {
return n * factorial(n - 1);
};
}
// Fibonacci
pub fn fib(n: Int) -> Int {
return if n <= 1 {
return n;
} else {
return fib(n - 1) + fib(n - 2);
};
}
// Check if number is even
pub fn is_even(n: Int) -> Bool {
return n % 2 == 0;
}
// Check if number is odd
pub fn is_odd(n: Int) -> Bool {
return n % 2 != 0;
}
// Note: Float operations and transcendental functions (sin, cos, sqrt, etc.)
// would require FFI or builtin implementation
// Currently disabled due to type checker limitations with Float comparisons