Home

Arithmetic: The Square Root

 

There are two forms of calculating the square root of a Real positive number. When using any of these functions, make sure you include the math.h header file to your project.

The sqrt() function is used to calculate the square root of a double-precision number. Its syntax is:

double sqrt(double x);

This function takes one argument as a positive floating number. After the calculation, the function returns the square root of x:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	double Number = Edit1->Text.ToDouble();
	double Result = sqrt(Number);
	
	Edit2->Text = Result;
}
//---------------------------------------------------------------------------

For a large or larger number, you can use the sqrtl() function. Its syntax is:

long double sqrtl(long double x);

This second form takes a long double number as a variable and returns a long double number as the square root of x.

After the calculation, if the function succeeds, it would return the square root. If it fails, it would throw an error.

 
Home Copyright © 2004-2010 FunctionX, Inc.