Home

Arithmetic Functions

 

Absolute Values

 

The abs Function

The decimal numeric system counts from minus infinity to infinity. This means that numbers are usually negative or positive, depending on their position from 0, which is considered as neutral. In some operations, the number considered will need to be only positive even if it is provided in a negative format. The absolute value of a number x is x if the number is (already) positive. If the number is negative, its absolute value is its positive equivalent. For example, the absolute value of 12 is 12, while the absolute value of 12 is 12.

To get the absolute value of a number, you can use one of the C/C++ abs() function. Its syntax is:

int abs(int x);

This function takes an integer as the argument and returns its absolute value equivalent. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnAbsoluteClick(TObject *Sender)
{
	int Number = Edit1->Text.ToInt();
	
	Edit2->Text = abs(Number);
}
//---------------------------------------------------------------------------

The labs Function

If you want to find the absolute value of a number that is larger than the regular integer, you can use the labs() function. Its syntax is:

long labs(long int x);

This function takes a long integer as argument and returns its equivalent absolute value:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnLongAbsoluteClick(TObject *Sender)
{
	int Longer = StrToInt(edtNumber->Text);
	
	edtResult->Text = labs(Longer);
}
//---------------------------------------------------------------------------

 

Home Copyright 2004-2007 FunctionX, Inc.