6.11.175 sqrtl Function
Calculates the square root of a long double precision floating-point value.
Include
<math.h>
Prototype
long double sqrtf(long double x);
Argument
x
- a non-negative floating-point value
Return Value
Returns the non-negative square root of x
.
Remarks
If x
is less than 0, a domain error will occur and
errno
will be set to EDOM
.
When targeting Cortex-M devices, MPLAB XC32 uses different algorithms for
sqrtl()
, based on the optimization level selected. When level
s
optimizations (-Os
) have been specified, square
roots are performed using the Newton algorithm, which can produce smaller code. For all
other optimization levels, the Kahan algorithm is used. The square root for some values
might differ slightly based on the algorithm chosen, but in both cases, they should
produce answers ±1 ulp (unit of least precision).
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x;
errno = 0;
x = sqrtl(0.0F);
if (errno)
perror("Error");
printf("The square root of 0.0F is %Lf\n", x);
errno = 0;
x = sqrtl(9.5F);
if (errno)
perror("Error");
printf("The square root of 9.5F is %Lf\n", x);
errno = 0;
x = sqrtl(-25.0F);
if (errno)
perror("Error");
printf("The square root of -25F is %Lf\n", x);
}
Example Output
The square root of 0.0F is 0.000000
The square root of 9.5F is 3.082207
Error: domain error
The square root of -25F is nan