sample-code: update for new WIP syntax (it's more persnickety)

This commit is contained in:
2025-10-19 19:27:09 -04:00
parent 6b24980fc7
commit b54826cdd5
7 changed files with 46 additions and 42 deletions

View File

@@ -6,24 +6,28 @@ const EPSILON: f64 = 8.8541878188 / 1000000000000.0;
/// Calcuates the absolute value of a number
fn f64_abs(n: f64) -> f64 {
let n = n as f64
if n < (0.0) { -n } else { n }
let n = n as f64;
if n < (0.0) {
-n
} else {
n
}
}
/// Square root approximation using Newton's method
fn sqrt(n: f64) -> f64 {
let n = n as f64
let n = n as f64;
if n < 0.0 {
return 0.0 / 0.0 // TODO: NaN constant
return 0.0 / 0.0; // TODO: NaN constant
}
if n == 0.0 {
return 0.0
return 0.0;
}
let z = n
let z = n;
loop {
let adj = (z * z - n) / (2.0 * z)
z -= adj
let adj = (z * z - n) / (2.0 * z);
z -= adj;
if adj.f64_abs() < EPSILON {
break z;
}
@@ -37,7 +41,9 @@ fn pythag(a: f64, b: f64) -> f64 {
/// Quadratic formula: (-b ± (b² - 4ac)) / 2a
fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
let a = a as f64; let b = b as f64; let c = c as f64;
let a = a as f64;
let b = b as f64;
let c = c as f64;
(
(-b + sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
(-b - sqrt(b * b - 4.0 * a * c)) / 2.0 * a,
@@ -46,11 +52,11 @@ fn quadratic(a: f64, b: f64, c: f64) -> (f64, f64) {
fn main() {
for i in 0..10 {
println("sqrt(",i,") ≅ ",sqrt(i as f64))
println("sqrt(", i, ") ≅ ", sqrt(i as f64))
}
println("\nPythagorean Theorem")
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0))
println("\nPythagorean Theorem");
println("Hypotenuse of ⊿(5, 12): ", pythag(5.0, 12.0));
println("\nQuadratic formula")
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0))
println("\nQuadratic formula");
println("Roots of 10x² + 4x - 1: ", quadratic(10.0, 40, -1.0));
}