hypot

Compute the hypotenuse.

Usage

var hypot = require( '@stdlib/math/base/special/fast/hypot' );

hypot( x, y )

Computes the hypotenuse.

var h = hypot( -5.0, 12.0 );
// returns 13.0

Notes

  • For a sufficiently large x and/or y, computing the hypotenuse will overflow.

    var h = hypot( 1.0e154, 1.0e154 );
    // returns Infinity
    

    Similarly, for sufficiently small x and/or y, computing the hypotenuse will underflow.

    var h = hypot( 1e-200, 1.0e-200 );
    // returns 0.0
    

Examples

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var hypot = require( '@stdlib/math/base/special/fast/hypot' );

var x;
var y;
var h;
var i;

for ( i = 0; i < 100; i++ ) {
    x = round( randu()*100.0 ) - 50.0;
    y = round( randu()*100.0 ) - 50.0;
    h = hypot( x, y );
    console.log( 'hypot(%d,%d) = %d', x, y, h );
}

C APIs

Usage

#include "stdlib/math/base/special/fast/hypot.h

stdlib_base_fast_hypot( x, y )

Computes the hypotenuse.

double h = stdlib_base_fast_hypot( 5.0, 12.0 );
// returns 13.0

The function accepts the following arguments:

  • x: [in] double input value.
  • y: [in] double input value.
double stdlib_base_fast_hypot( const double x, const double y );

Examples

#include "stdlib/math/base/special/fast/hypot.h"
#include <stdio.h>

int main( void ) {
    const double x[] = { 3.0, 4.0, 5.0, 12.0 };

    double y;
    int i;
    for ( i = 0; i < 4; i += 2 ) {
        y = stdlib_base_fast_hypot( x[ i ], x[ i + 1 ] );
        printf( "hypot(%lf, %lf) = %lf\n", x[ i ], x[ i + 1 ], y );
    }
}
Did you find this page helpful?