當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Math sqrt()用法及代碼示例

此函數查找給定數字的平方根。

考慮一個參數 'arg':

square_root of a number=√arg

用法

語法是:

double sqrt(double arg);
float sqrt(float arg);
long double sqrt(long double arg);
double sqrt(integral arg);

參數

arg:它是浮點型或整數型的值。

返回值

它返回參數 arg 的平方根。

例子1

當參數 'arg' 是整數類型時,讓我們看一個簡單的例子。

#include <iostream>
#include<cmath>
using namespace std;
int main()
{ int arg=16;
 std::cout << "Square root of a number is:" <<sqrt(arg);
  return 0;
}

輸出:

Square root of a number is:

例子2

當參數 'arg' 是浮點類型時,讓我們看一個簡單的例子

#include <iostream>
#include<cmath>
using namespace std;
int main()
{float arg=8.5;
 std::cout << "Square root of a number is:" <<sqrt(arg);
 return 0;
}

輸出:

Square root of a number is:2.91548 





相關用法


注:本文由純淨天空篩選整理自 C++ Math sqrt()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。