Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #200 Return hypotenuse

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

const h = Math.sqrt(a**2 + b**2)

// Before ES7
const h = Math.sqrt(a*a + b*b)

// Before ES6
var h = Math.sqrt(a*a + b*b)
double hypo(double x, double y)
{
    return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
import :math
def sq(x) do
  x*x
end

def hypo(a,b) do
  sqrt(sq(a) + sq(b))
end
h = hypot(x,y)
import "math"
h := math.Hypot(x, y)
hypo x y = sqrt $ x**2 + y**2
$h = hypot($x, $y)

// before PHP 4.1
$h = sqrt($x*$x + $y*$y);
math
h := hypot(x,y);
use Math::AnyNum qw(hypot);
my $h = hypot $x, $y;
import math
h = math.hypot(x, y)
include Math
h = hypot(x, y)
fn hypot(x:f64, y:f64)-> f64 {
    let num = x.powi(2) + y.powi(2);
    num.powf(0.5)
}

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Bart