c语言中sqrt函数
C ++ sqrt()函数 (C++ sqrt() function)
sqrt() function is a library function of cmath header (<math.h> in earlier versions), it is used to find the square root of a given number, it accepts a number and returns the square root.
sqrt()函数是cmath标头(在早期版本中为<math.h> )的库函数,用于查找给定数字的平方根,它接受数字并返回平方根。
Note: If we provide a negative value, sqrt() function returns a domain error. (-nan).
注意:如果我们提供负值,则sqrt()函数将返回域错误。 ( -nan )。
Syntax of sqrt() function:
sqrt()函数的语法:
sqrt(x);
Parameter(s): x – a number whose square root to be calculated.
参数: x –要计算其平方根的数字。
Return value: double – it returns double value that is the square root of the given number x.
返回值: double-返回double值,它是给定数字x的平方根。
Example:
例:
Input:
int x = 2;
Function call:
sqrt(x);
Output:
1.41421
C ++代码演示sqrt()函数的示例 (C++ code to demonstrate the example of sqrt() function)
// C++ code to demonstrate the example of
// sqrt() function
#include <iostream>
#include <cmath>
using namespace std;
// main code section
int main()
{
float x;
//input the value
cout<<"Enter a number: ";
cin>>x;
// calculate the square root
float result = sqrt(x);
cout<<"square root of "<<x<<" is = "<<result;
cout<<endl;
return 0;
}
Output
输出量
First run:
Enter a number: 4
square root of 4 is = 2
Second run:
Enter a number: 10.234
square root of 10.234 is = 3.19906
Third run:
Enter a number: 0
square root of 0 is = 0
Fourth run:
Enter a number: -10
square root of -10 is = -nan
翻译自: https://www.includehelp.com/cpp-tutorial/sqrt-function-with-example.aspx
c语言中sqrt函数