C library functions -sqrt()

C standard library - <math.h>

Description

C Library Functionsdouble sqrt(double x)returnxthe square root.

sqrt()is the C standard library<math.h>A function used to calculate the square root of a non-negative number. This function is frequently used in mathematics and engineering.

Declaration

Below is the declaration of the sqrt() function.

#include <math.h>

double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);

Parameter

  • x: A non-negative number whose square root is to be calculated.

Return Value

  • returnxthe square root.
  • ifxIf it is a negative number, NaN is returned, and theerrnoisEDOM。

Error Handling

  • ifxis a negative number,sqrt()The function will return NaN and seterrnoisEDOM。

Example

The following example demonstrates the usage of the sqrt() function.

Example

#include <stdio.h>
#include <math.h>

int main ()
{

   printf("The square root of %lf is %lf\n", 4.0, sqrt(4.0) );
   printf("The square root of %lf is %lf\n", 5.0, sqrt(5.0) );
   
   return(0);
}

Let us compile and run the above program, this will produce the following results:

4.000000 的平方根是 2.000000
5.000000 的平方根是 2.236068

Processing the square roots of multiple values

The following example demonstrates how to handle the square root calculation of multiple values:

Example

#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double values[] = {16.0, 25.0, 100.0, -1.0, 0.0};
    int num_values = sizeof(values) / sizeof(values[0]);

    for (int i = 0; i < num_values; i++) {
        double x = values[i];
        errno = 0;  // Reset errno
        double result = sqrt(x);

        if (errno == EDOM) {
            printf("Domain error: sqrt(%f) is not defined\n", x);
        } else {
            printf("sqrt(%f) = %f\n", x, result);
        }
    }

    return 0;
}

Let us compile and run the above program, this will produce the following results:

sqrt(16.000000) = 4.000000
sqrt(25.000000) = 5.000000
sqrt(100.000000) = 10.000000
sqrt(-1.000000) = nan
sqrt(0.000000) = 0.000000

Code explanation

  • Define an array containing multiple floating-point numbersvalues。
  • UsageforLoop through each value and callsqrt(x)perform the calculation.
  • reseterrnoSet to 0 and check for possible errors
  • Print the square root calculation result for each value, or an error message.

Use Case

sqrt()The function has a wide range of applications in many fields, including but not limited to:

  • Calculate distances in geometry.
  • Calculating velocity, acceleration, etc. in physics.
  • Solve mathematical problems related to square roots.

Summary

sqrt()The function is used to calculate the square root of non-negative numbers, making it an important tool for mathematical computations. Through reasonable usesqrt(), square root calculations can be implemented in scientific computing, engineering applications, and mathematical problems, while ensuring proper handling of possible error conditions.

C standard library - <math.h>

other extensions