Numerical computing trade-offs
Quick answer
Numerical programs need correct shapes, suitable precision and an explicit error policy before optimization. A faster calculation is not useful if its numerical results are unreliable.
Shape, precision and order
A dot product multiplies paired elements and adds the products; reject mismatched lengths rather than silently truncating with zip. Floating-point addition is not associative in general, so parallel reduction can change results. Choose tolerances based on scale and error requirements, and account for NaN and infinity. Libraries such as ndarray and nalgebra offer richer abstractions; verify target and version compatibility before use.
fn dot(a: &[f64], b: &[f64]) -> Option<f64> { if a.len() != b.len() { return None; } Some(a.iter().zip(b).map(|(x, y)| x * y).sum())}fn main() { println!("{:?} {:?}", dot(&[2., 3.], &[4., 5.]), dot(&[1.], &[])); }Expected stdout (not executed):
Some(23.0) NoneThis small model permits non-finite inputs; stricter applications must reject them.
fn main() { let (a, b, c) = (1e16_f64, -1e16_f64, 1.0_f64); println!("{} {}", (a + b) + c, a + (b + c));}Expected stdout (not executed):
1 0The small term is lost in one grouping.
Common mistake
Do not use exact equality as a general test for results of floating-point algorithms.
Try it yourself
Reject non-finite values in dot and write a scale-aware comparison for finite results.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Numerical computing trade-offs — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A dot-product implementation uses zip on two unequal-length vectors. What should it do under an equal-shape contract?
Numerical computing trade-offs — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn dot(a: &[f64], b: &[f64]) -> Option<f64> { if a.len() != b.len() { return None; } Some(a.iter().zip(b).map(|(x, y)| x * y).sum()) } fn main() { println!("{:?} {:?}", dot(&[2., 3.], &[4., 5.]), dot(&[1.], &[])); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Numerical computing trade-offs — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let (a, b, c) = (1e16_f64, -1e16_f64, 1.0_f64); println!("{} {}", (a + b) + c, a + (b + c)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.