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.

After execution, the function returns the mantissa such that:

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;


The Frexp() Function

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;
Home Copyright © 2004-2016, FunctionX, Inc.