Home

Math Functions: The Square Root of a Number

     

Introduction

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:

//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "Exercise.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSquareRootClick(TObject *Sender)
{
	double Number = edtNumber->Text.ToDouble();
	double Result = sqrt(Number);

	edtSquareRoot->Text = Result;
}
//---------------------------------------------------------------------------
Square Root

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 © 2010-2016, FunctionX