C++: Extracting Exponent Value from a Double

  • Context: C/C++ 
  • Thread starter Thread starter madmike159
  • Start date Start date
  • Tags Tags
    Exponent Value
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
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)
 
Physics news on Phys.org
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