English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Utilizzo e esempio della funzione sqrt() in C++

函数库cmathC++

La funzione sqrt() in C++ restituisce la radice quadrata del numero.

 √x = sqrt(x)

Questa funzione è disponibile in<cmath>Definito nel file di intestazione.

Prototipo di sqrt() [dalla versione C++ 11]

double sqrt(double x);
float sqrt(float x);
long double sqrt(long double x);
double sqrt(T x); // per interi

Parametro sqrt()

La funzione sqrt() accetta un singolo parametro non negativo.

Se si passa un parametro negativo alla funzione sqrt(), si verifica un errore.

Valore restituito da sqrt()

La funzione sqrt() restituisce la radice quadrata del parametro fornito.

Esempio 1: Come funziona sqrt() in C++?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double x = 10.25, result;
	result = sqrt(x);
	cout << "La radice quadrata di 10.25 è " << result << endl;
	return 0;
}

运行程序时,输出为:

La radice quadrata di 10.25 è 3.20156

Esempio 2: Funzione sqrt() con parametri interi

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	long x = 464453422;
	double result = sqrt(x);
	cout << "平方根是 464453422的" << result << endl;
	return 0;
}

运行程序时,输出为:

平方根是21551.2 464453422

 函数库cmathC++