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);
}
//---------------------------------------------------------------------------
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);
}
//---------------------------------------------------------------------------
Consider a floating number such as 12.155. As you can see, this number is between integer 12 and integer
13
In the same way, consider a number such as –24.06. As this number is
negative, it is between –24 and –25, with –24 being greater.
In arithmetic, the ceiling of a number is the closest integer that is
greater or higher than the number considered. In the first case, the
ceiling of 12.155 is 13 because 13 is the closest integer greater than
or equal to 12.155. The ceiling of –24.06 is –24.
|
|
In C++, the function used to obtain the ceiling of a number uses the following syntax:
double ceil(double Value);
The function takes one argument, which is the floating number to be evaluated, and the function returns a double-precision number that is the integer that is greater than or equal to Value. Here is an example:
|
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsusedint main(int argc, char* argv[])
{
double Value1 = 155.55; double Value2 = -24.06;
cout << "The ceiling of " << Value1 << " is " << ceil(Value1) << endl;
cout << "The ceiling of " << Value2 << " is " << ceil(Value2) << endl;
cout << "\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------
This would produce:
The ceiling of -24.06 is -24
Press any key to continue...
In C++ Builder, the function used to get the ceiling of a number is:
int __fastcall Ceil(Extended Value);
The Ceil() function takes an argument that represents a long
double value. The function returns the greater or equal integer of
Value. To use the Ceil() function, include the math.hpp header to your
program. Here is an example:
|
//---------------------------------------------------------------------------
#include <iostream.h>
#include <math.hpp>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
Extended Value1 = 312.44;
Extended Value2 = -4002.35;
cout << "The ceiling of " << Value1 << " is" << Ceil(Value1) << endl;
cout << "The ceiling of " << Value2 << " is" << Ceil(Value2);
cout << "\n\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------
This would produce:
The ceiling of 312.44 is 313
The ceiling of -4002.35 is -4002
Press any key to continue...
 |
Arithmetic: The Floor of a Number
|
|
Consider two floating numbers such as 128.44 and -36.72. The number
128.44 is between 128 and 129 with 128 being the lower. The number
–36.72 is between –37 and –36 with –37 being the lower. The lowest but
closest integer value of a number is referred to as its floor. Based on
this, the floor of 128.44 is 128. The floor of –36.72 is –37.
|
In C++, to obtain the floor of a number, use the following function:
double floor(double Value);
The floor() function takes the considered value as the argument
and returns the integer that is less than or equal to Value. Here is an
example:
|
double Value1 = 1540.25;
double Value2 = -360.04;
cout << "The floor of " << Value1 << " is " << floor(Value1) << endl;
cout << "The floor of " << Value2 << " is " << floor(Value2) << endl;
This would produce:
The floor of 1540.25 is 1540
The floor of -360.04 is -361
Press any key to continue...
When using C++ Builder, you can use the Floor() function to find the floor of a number. The syntax of the function is:
int __fastcall Floor(Extended Value);
The Value argument of the function represents the number that is being
considered. The function returns the integer that is less than or equal
to Value. Here is an example:
|
Extended Value1 = 312.44;
Extended Value2 = -4002.35;
cout << "The floor of " << Value1 << " is " << Floor(Value1) << endl;
cout << "The floor of " << Value2 << " is " << Floor(Value2) << endl;
This would produce:
The floor of 312.44 is 312
The floor of -4002.35 is -4003
Press any key to continue...
 |
Arithmetic: The Exponent of a Number
|
|
The frexp() and the frexpl() Functions
|
|
double frexp(double Number, int *Exp);
long double frexpl(long double Number, int *Exp);
The C++ frexp() and frexpl() functions are used to get the mantissa and the exponent portions of a floating- point number. Each of these functions takes two arguments. The Number argument represents the value that will be examined. For the
frexp() function, this value is a double-precision number. If the number is larger, then use the
frexpl() version whose argument is a long double. The Exp
argument is passed as a pointer to an integer. This allows the function
to return a second value.
Mantissa = frexp(Number, Exp);
The result returned, Mantissa, is a double (frexp) or a long double (frexpl) number in the range 0.5 (included) to 1 (excluded). The Exp argument, passed as a pointer, is returned as
Number = Mantissa * 2Exp
For the following example, a form is equipped with three Edit controls
named edtNumber, edtMantissa, and edtExponent. It also has a Button
control named btnCalculate with the Default property
set to true. The user must type a number in the Number edit box and
press Enter. Then the OnClick event of the button executes to perform
the frexp() function which leads to displaying the results in the
appropriate edit boxes :
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalculateClick(TObject *Sender)
{
double Mant, Number = edtNumber->Text.ToDouble();
int Exp; Mant = frexp(Number, &Exp);
edtMantissa->Text = Mant;
edtExponent->Text = Exp;
}//---------------------------------------------------------------------------
|
|
void __fastcall Frexp(Extended Number, Extended &Mantissa, int &Exp);
The Frexp() function is the VCL’s version of the frexp()
function. This function takes three arguments. The number to be examined
is the Number argument passed as a long double. The number to be
returned, also a long double, is the Mnatissa argument, also passed by
reference. The Exp argument, also passed as a reference, is returned as
the exponent value. The numbers are dealt with according to the formula:
|
Number = Mantissa * 2Exp
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Extended Number, Mant;
int Exp;
Number = StrToFloat(Edit1->Text);
Frexp(Number, Mant, Exp);
Edit2->Text = FloatToStr(Mant);
Edit3->Text = Exp;
}
//--------------------------------------------------------------------------- |
|
|
|
0 comments:
Post a Comment