sqrt, sqrtf, sqrtl

Header: <math.h>

1-3) Computes square root of arg.

# Declarations

float sqrtf( float arg );

(since C99)

double sqrt( double arg );
long double sqrtl( long double arg );

(since C99)

#define sqrt( arg )

(since C99)

# Parameters

# Return value

If no errors occur, square root of arg (({\small \sqrt{arg} })√arg), is returned.

# Notes

sqrt is required by the IEEE standard to be correctly rounded from the infinitely precise result. In particular, the exact result is produced if it can be represented in the floating-point type. The only other operations which require this are the arithmetic operators and the function fma. Other functions, including pow, are not so constrained.

# Example

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    // normal use
    printf("sqrt(100) = %f\n", sqrt(100));
    printf("sqrt(2) = %f\n", sqrt(2));
    printf("golden ratio = %f\n", (1 + sqrt(5)) / 2);
 
    // special values
    printf("sqrt(-0) = %f\n", sqrt(-0.0));
 
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("sqrt(-1.0) = %f\n", sqrt(-1));
    if (errno == EDOM)
        perror("    errno == EDOM");
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID was raised");
}

# See also