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

ParameterDescription
xValue 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:

  1. The program initializes a double variable num with the value 16.0.
  2. The sqrt() function is used to compute the square root of num.
  3. 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:

  1. The program sets num to -25.0, an invalid input for sqrt().
  2. The global variable errno is reset before the function call.
  3. After calling sqrt(), the program checks errno to determine if a domain error occurred.
  4. 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:

  1. A float variable num is initialized with the value 25.0f.
  2. The sqrtf() function is used to compute the square root with single-precision.
  3. 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:

  1. A long double variable num is set to 49.0L.
  2. The sqrtl() function computes the square root with extended precision.
  3. The result is stored in result and printed using the long double format specifier.

Program Output:

Square root of 49.0 is: 7.000000