Math Frexp Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Frexp Function

Math Frexp Function

BLUF: Mastering Math Frexp Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Frexp Function

C++ is renowned for its efficiency. Learn how Math Frexp Function enables low-level control and high-performance computing in the tutorial below.

C++ Math frexp

This function separates the floating-point number into the binary significand and an integer exponent.

Let the floating point number be x then,

where 'e' represents the exponent and 'significand' stands for the binary significand

Syntax

Consider a floating-point value denoted as 'x' and a pointer indicated as 'exp':

Example

float frexp(float x, int* exp);
double frexp(double x, int* exp);
long double frexp(long double x, int* exp);
double frexp(integral x, int* exp);

Parameter

x : The value that needs to be broken down into the binary significand.

It represents a pointer to an integer that holds the value of the exponent.

Return value

It returns the binary significand, an absolute value that falls within the range of 0.5 (inclusive) and less than 1.

Parameter Significand exponent
x=0 zero zero
x>=1 positive number positive number
x>= -1 negative number positive number
-1<x<0 negative number negative number
0<x<1 positive number negative number

Example 1

Let's consider a basic scenario where the value of x exceeds 1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=2;
    int* e;
    cout<<"Value of x is : "<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

Output:

Output

Value of x is : 2
2=0.5 * 2^2

In this instance, the frexp function computes the binary mantissa of a floating-point value if x exceeds 1.

Example 2

Let's examine a basic scenario where the value of x equals zero.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=0;
    int* e;
    cout<<"Value of x is : "<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

Output:

Output

Value of x is : 0
0=0 * 2^0

In this instance, the frexp function computes the binary mantissa of a floating-point number if the input x equals zero.

Example 3

Let's examine a basic scenario where the value of x falls within the range of 0 and 1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=0.4;
    int* e;
    cout<<"Value of x is : "<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

Output:

Output

Value of x is : 0.4
0.4=0.8 * 2^-1

In this instance, the frexp function computes the binary mantissa of a floating-point value when x falls within the range of 0 and 1.

Example 4

Let's consider a straightforward scenario where the value of x falls within the range of -1 and 0.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x= -0.1;
    int* e;
    cout<<"Value of x is : "<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

Output:

Output

Value of x is : -0.1
-0.1=-0.8 * 2^-3

In this instance, the frexp function determines the binary fraction part of a floating-point value for input x within the range of -1 and 0.

Example 5

Let's examine a basic scenario where the variable x is below -1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x= -5;
    int* e;
    cout<<"Value of x is : "<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

Output:

Output

Value of x is : -5
-5=-0.625 * 2^3

In this instance, the frexp function computes the binary mantissa of a floating-point number if the input x is smaller than -1.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience