Skip to content

Commit 8855f26

Browse files
fix(stdlib): math.affine typecheck + borrow (#135 slice 12) (#184)
- to_float: `n + 0.0` is ill-typed under homogeneous arithmetic (Int + Float); use the `float` builtin (Int -> Float) as intended. - 8 loop accumulators reassigned without `let mut` (gcd x/y, is_prime i, fibonacci a/b/i, binomial result/i) -> add `mut`. math.affine now compiles resolve->typecheck->borrow. stdlib 13/19, full suite 233/233, zero regression. Refs #128
1 parent 6e182f9 commit 8855f26

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

stdlib/math.affine

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn cube(x: Int) -> Int {
106106

107107
/// Convert an integer to a float
108108
fn to_float(n: Int) -> Float {
109-
n + 0.0
109+
float(n)
110110
}
111111

112112
/// Fractional part of a float (x - trunc(x))
@@ -213,8 +213,8 @@ fn lerp(a: Float, b: Float, t: Float) -> Float {
213213

214214
/// Greatest common divisor via Euclid's algorithm
215215
fn gcd(a: Int, b: Int) -> Int {
216-
let x = abs(a);
217-
let y = abs(b);
216+
let mut x = abs(a);
217+
let mut y = abs(b);
218218

219219
while y != 0 {
220220
let temp = y;
@@ -254,7 +254,7 @@ fn is_prime(n: Int) -> Bool {
254254
if n % 2 == 0 || n % 3 == 0 {
255255
return false;
256256
}
257-
let i = 5;
257+
let mut i = 5;
258258
while i * i <= n {
259259
if n % i == 0 || n % (i + 2) == 0 {
260260
return false;
@@ -302,9 +302,9 @@ fn fibonacci(n: Int) -> Int {
302302
if n <= 1 {
303303
n
304304
} else {
305-
let a = 0;
306-
let b = 1;
307-
let i = 2;
305+
let mut a = 0;
306+
let mut b = 1;
307+
let mut i = 2;
308308
while i <= n {
309309
let temp = a + b;
310310
a = b;
@@ -332,8 +332,8 @@ fn binomial(n: Int, k: Int) -> Int {
332332
}
333333
// Use the smaller of k and n-k for efficiency
334334
let k_eff = if k > n - k { n - k } else { k };
335-
let result = 1;
336-
let i = 0;
335+
let mut result = 1;
336+
let mut i = 0;
337337
while i < k_eff {
338338
result = result * (n - i) / (i + 1);
339339
i = i + 1;

0 commit comments

Comments
 (0)