View Raw SPL
/*****************************************************************************
*                                                                            *
*   HYPOT.SPL    Copyright (C) 2015 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Computes the hypotenuse of a right-angle triangle           *
*                                                                            *
*   Revisions:   21 Oct 2015  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_HYPOT

    HYPOT

    Purpose: Computes the square root of the sum of squares.

    Syntax:  HYPOT(x, y)
              
              x - A scalar or series.

              y - A scalar or series.

    Returns: A scalar or series, the right triangle hypotenuse formula
             sqrt(x*x + y*y) computed with underflow and overflow protection.

    Example:
             hypot(3, 4)

             returns 5, the hypotenuse of a 3-4-5 triangle.

    Example:
             a = 1e200;
             b = sqrt(a*a + a*a);
             c = hypot(a, a);

             b == inf
             c == 1.414214e+200

             Variable a contains a large real value. The straightforward
             computation overflows, but HYPOT returns the correct
             value without overflow.

    Example:
             a = 1e-200;
             b = sqrt(a*a + a*a);
             c = hypot(a, a);

             b == 0.0
             c == 1.414214e-200

             Variable a contains a small real value. The straightforward
             computation underflows, but HYPOT returns the correct
             value without underflow.

    Example:
             W1: 1..5
             W2: {3, 1, inf, 3, 5}
             W3: hypot(w1, w2)

             W3 == {3.162278, 2.236068, inf, 5.000000, 7.071068}

    Remarks:
             HYPOT(x, y) computes the right triangle hypotenuse formula
             sqrt(x*x + y*y) with underflow and overflow protection using
             a routine compatible with the IEEE 754 hypot function.

             For complex X or Y, HYPOT effectively computes:

             sqrt(mag(x)*mag(x) + mag(y)*mag(y))

    See Also:
             Abs
             Mag
             Makecartesian
             Norm
#endif


/* hypotenuse */
hypot(a, b)
{
        if (argc < 2) error("hypot - two input values required");

        /* use MAG */
        return(mag(makecartesian(mag(a), mag(b))));
}