Logo

Programming-Idioms

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

Idiom #200 Return hypotenuse

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

local h = math.sqrt(x^2 + y^2)
local h=(x^2+y^2)^0.5
#include <cmath>
auto h = std::hypot(x, y);
double hypo(double x, double y)
{
    return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
import 'dart:math';
var h = sqrt(x * x + y * y);
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
var h = Math.sqrt(x*x + y*y);
const h = Math.hypot(x, y);
import static java.lang.Math.hypot;
double h = hypot(x, y);
return double h = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
import static java.math.MathContext.DECIMAL128;
import java.math.BigDecimal;
BigDecimal X = new BigDecimal(x).pow(2),
           Y = new BigDecimal(y).pow(2),
           h = X.add(Y).sqrt(DECIMAL128);
import static java.lang.Math.sqrt;
double h = sqrt((x * x) + (y * y));
$h = hypot($x, $y)

// before PHP 4.1
$h = sqrt($x*$x + $y*$y);
uses 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)
let h = x.hypot(y);
fn hypot(x:f64, y:f64)-> f64 {
    let num = x.powi(2) + y.powi(2);
    num.powf(0.5)
}

New implementation...
< >
Bart