C++: Extracting Exponent Value from a Double

  • Context: C/C++ 
  • Thread starter Thread starter madmike159
  • Start date Start date
  • Tags Tags
    Exponent Value
Click For Summary
SUMMARY

The discussion focuses on extracting the exponent value from a double in C++ using the frexp() function from the math.h library. The internal representation of double values involves a sign bit, a significand, and an exponent based on base 2, rather than base 10. The example provided demonstrates how to use frexp() to separate the mantissa and exponent, allowing the exponent to be stored in an integer variable for further calculations.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with the math.h library functions
  • Knowledge of floating-point representation in computers
  • Basic experience with variable types and arithmetic operations in C++
NEXT STEPS
  • Explore the frexp() function in detail to understand its parameters and return values
  • Learn about floating-point representation and precision issues in C++
  • Investigate the log10() function for logarithmic calculations
  • Study the differences between base 2 and base 10 representations in numerical computing
USEFUL FOR

C++ developers, computer scientists, and anyone interested in numerical methods and floating-point arithmetic in programming.

madmike159
Gold Member
Messages
369
Reaction score
0
Is there a way to look at a double value in c++ and read the exponent only and put this into another variable?
So if I had 3.14159x10^300
I could take 300, put it into an int
Then divide 3.14159x10^300 by 1x10^(the int value)
 
Technology news on Phys.org
Here's a hint: log10(...)
 
The internal representation of doubles and floats does not use powers of 10. Instead, values of these types are stored with 1 bit for the sign, another group of bits for the mantissa (which is usually called the significand) and another group of bits for the exponent on 2.

The frexp() function in math.h might be what you're looking for.
This code and the output that is shown are from http://www.cplusplus.com/reference/clibrary/cmath/frexp/.
Code:
/* frexp example */
#include <stdio.h>
#include <math.h>

int main ()
{
  double param, result;
  int n;

  param = 8.0;
  result = frexp (param , &n);
  printf ("%lf * 2^%d = %f\n", result, n, param);
  return 0;
}
Output:
0.500000 * 2^4 = 8.000000
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K