sqrt() Function
The sqrt()
function computes the square root of a given number and returns the positive square root. It provides support for different floating-point precisions and handles domain errors when a negative value is passed.
Syntax of sqrt()
</>
Copy
double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);
Parameters
Parameter | Description |
---|---|
x | Value whose square root is computed. A domain error occurs if this value is negative. |
If the argument is negative, a domain error occurs. Depending on the error handling set in math_errhandling
, this will either set the global variable errno
to EDOM
or raise the floating-point exception FE_INVALID
.
Return Value
The function returns the square root of the given value. If the input value is negative, a domain error occurs as described above.
Examples for sqrt()
Example 1: Computing the Square Root of a Positive Number
Program
</>
Copy
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
double result = sqrt(num);
printf("Square root of 16.0 is: %f\n", result);
return 0;
}
Explanation:
- The program initializes a double variable
num
with the value 16.0. - The
sqrt()
function is used to compute the square root ofnum
. - The computed result is stored in
result
and printed to the console.
Program Output:
Square root of 16.0 is: 4.000000
Example 2: Handling a Negative Input for Domain Error
Program
</>
Copy
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
double num = -25.0;
errno = 0; // Reset errno before calling sqrt()
double result = sqrt(num);
if (errno == EDOM) {
printf("Error: Domain error occurred while computing sqrt.\n");
} else {
printf("Square root of -25.0 is: %f\n", result);
}
return 0;
}
Explanation:
- The program sets
num
to -25.0, an invalid input forsqrt()
. - The global variable
errno
is reset before the function call. - After calling
sqrt()
, the program checkserrno
to determine if a domain error occurred. - An error message is printed when a domain error is detected.
Program Output:
Error: Domain error occurred while computing sqrt.
Example 3: Using sqrtf() for Single-Precision Calculation
Program
</>
Copy
#include <stdio.h>
#include <math.h>
int main() {
float num = 25.0f;
float result = sqrtf(num);
printf("Square root of 25.0 is: %f\n", result);
return 0;
}
Explanation:
- A float variable
num
is initialized with the value 25.0f. - The
sqrtf()
function is used to compute the square root with single-precision. - The result is stored in
result
and then printed.
Program Output:
Square root of 25.0 is: 5.000000
Example 4: Using sqrtl() for Extended Precision Calculation
Program
</>
Copy
#include <stdio.h>
#include <math.h>
int main() {
long double num = 49.0L;
long double result = sqrtl(num);
printf("Square root of 49.0 is: %Lf\n", result);
return 0;
}
Explanation:
- A long double variable
num
is set to 49.0L. - The
sqrtl()
function computes the square root with extended precision. - The result is stored in
result
and printed using the long double format specifier.
Program Output:
Square root of 49.0 is: 7.000000