C/C++ C++: Extracting Exponent Value from a Double

  • Thread starter Thread starter madmike159
  • Start date Start date
  • Tags Tags
    Exponent Value
AI Thread Summary
To extract the exponent from a double value in C++, the frexp() function from the math.h library is recommended. This function decomposes a floating-point number into its normalized fraction and an exponent of two, allowing the exponent to be stored in an integer variable. The internal representation of doubles uses a binary format, not decimal, which means the exponent is based on powers of two rather than ten. For example, using frexp() with a value like 8.0 will yield a normalized fraction and an exponent, making it straightforward to manipulate the exponent as needed. This method is efficient for obtaining the exponent and performing further calculations with the original value.
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top