Logo

Programming-Idioms

  • D
  • Smalltalk
  • Ada
  • Rust

Idiom #200 Return hypotenuse

Compute the hypotenuse h of the triangle where the sides adjacent to the square angle have lengths x and y.

fn hypot(x:f64, y:f64)-> f64 {
    let num = x.powi(2) + y.powi(2);
    num.powf(0.5)
}
let h = x.hypot(y);

Where x and y are floating-point values (f32 or f64).
#include <cmath>
auto h = std::hypot(x, y);

h, x, y may have types float, double, long double

New implementation...
< >
Bart