Sqrt c что это
Стандартная библиотека языка Си предоставляет ряд математических функций, которые определены в разных заголовочных файлах, но в основном определены в файле math.h .
Степерь числа. pow
Функция pow() , определенная в заголовочном файле math.h , возвращает степень числа:
Стоит отметить, что эта функция возвращает число типа double .
Для возведения в степень чисел типа float и long double также определены функции powf() и powl() соответственно.
Округление числа. round
Функция round() округляет число до ближайшего целого. В качестве параметра она принимает число типа double :
Квадратный корень. sqrt
Функция sqrt() возвращает квадратный корень числа типа double :
Для получения квадратного корня числа float также есть функция sqrtf() , а для числа типа long double — функция sqrtl()
sqrt() in C
The sqrt() function in C returns the square root of the number. It is defined in the math.h header file.
Syntax for sqrt() in C
The syntax for the sqrt function in C is given below:
Parameters for sqrt() in C
The sqrt() function takes a single number(in double) as the input.
Return Values for sqrt() in C
The sqrt() function returns a double number value as the output. However, if the input is negative, it returns -nan . To understand why that happens, see Exceptions.
Exceptions for sqrt() in C
When the input number is negative, the sqrt function in C returns -nan (not a number). This is because the square root of a negative number is imaginary, and the sqrt() function doesn't support an imaginary number. This is a domain error, i.e., the input is not within the function's domain.
Output
Example to Illustrate the Working of sqrt() Function in C
Output
What is sqrt() Function in C?
The sqrt() function in C returns the square root of the number. It is defined in the math.h header file. It takes the input (in double) and returns the output(in double). If the input is negative, it returns a domain error -nan .
How to Use the sqrt() Function in C?
- If we want to use the sqrt function in C for int , float , etc., we can typecast these into double values and then pass them into the function.
- If the input is not a double value (e.g., int , float , etc.), it is automatically typecasted into a double . The sqrt() function in C is defined such that if the input is not a double value(e.g., int , float , etc.), it is typecasted into double , and then its square root is calculated.
- We can also use the sqrtf() for float values and sqrtl() for long double values.
Note: Since the sqrt() function returns double values, it is also prone to small roundoff errors.
More Examples of sqrt() in C
- Program to get the square root of a number using the sqrt() function in C.
Output
Explanation The program declares three variables a , b , and c and computes the square root of these numbers using the sqrt() function. Note that variables a and b are integers. When they are passed into the sqrt() function, it typecasts them into a double value.
- Program to take a number from the user and get its square root using the sqrt() function in C.
Output
Explanation
In this program, we take a non-negative number from the user and compute its square root using the sqrt() function. If the user passes a negative number, it will result in a domain error. See Exception for more details.
- Program to get the square root of a number using the pow() function.
Output
Explanation
In this program, we calculate the square root of a number using the pow() function. The pow() function takes two parameters, the base, and the exponent. Therefore, when we compute pow(a, 0.5) , it is the same as the square root of a .
- Program to get the square root of a number using a user-defined function without using the sqrt() function.
Output
Explanation
In this program, we write a user-defined function to calculate the square root of the number. The user enters a number, a , and we pass this number to the function squareRoot() . We store a / 2 in j . Initially, i = 0 . We then assign j to i and update the value of j . This goes on until j is not equal to i . This method is called the Newton Raphson Method.
C Language: sqrt function
(Square Root)
In the C Programming Language, the sqrt function returns the square root of x.
Syntax
The syntax for the sqrt function in the C Language is:
Parameters or Arguments
Returns
The sqrt function returns the square root of x. If x is negative, the sqrt function will return a domain error.
Required Header
In the C Language, the required header for the sqrt function is:
Applies To
In the C Language, the sqrt function can be used in the following versions:
C sqrt()
The sqrt() function computes the square root of a number.
Function prototype of sqrt()
The sqrt() function takes a single argument (in double ) and returns its square root (also in double ).
The sqrt() function is defined in math.h header file.
To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator.
You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.