# Extract exponent from a double value

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 06-15-2019
GReaper
I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.
• 06-15-2019
Click_here
Does it have to be ISO C that you are using?

Many compilers define behaviour of unions that might be able to be used - An example of this is XC8 c compiler and it would solve the problem without any arithmetic operators.
• 06-15-2019
Niccolo
Quote:

Originally Posted by GReaper
I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.

Ok, that occurred to me too, but how does one take a negative number and "normalize" it by +1022, so that -1022 becomes 0, or -1021 becomes 1?

Unless we're reading the question incorrectly, and that is likely.

The OP indicated unsigned int, offset by +1022.

The question doesn't say unsigned int, it says int.

Fine, so how about the nibble? The exponent is 12 bits.

Unless you have a 4 bit processor (and they did/do exist), how does one shift and sign extend that 12 bit value into a 16 or 32 or 64 bit integer?

I think that's the real question, now that I read both posts.

In other words, frexp extracts the exponent as an integer. Suppose they question says that without even including the library, and therefore not calling frexp, how do we extract those 12 bits without rolling, bit fiddling or all else that is excluded?

Maybe they expect us to do this with inline assembler. That may qualify to the literal text.

If so, it's trivial.
• 06-15-2019
stahta01
x = x +1022
• 06-15-2019
stahta01
Quote:

Originally Posted by GReaper
I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.

OK; I see your solution for this stupid problem.

Tim S.
• 06-16-2019
gawiellus
Thank you for yours answers. I know the solutions. In this case I have to use unions and bit fields. I gave me the proper solution.
• 06-16-2019
flp1969
Well....There is a pure mathematical solution.
If you want an expoent, you can use logarithm...

Code:

```int getexp( double d ) {  return floor(log2(fabs(d))); }```
But, of course, you have to add 1023...
• 06-16-2019
GReaper
It turns out that the "add 1022/1023" part is a misunderstanding, the challenge just wants you to extract the exponent part of the double value.
• 06-16-2019
Click_here
It was crewel of them to use implementation defined behaviour as a solution!
• 06-16-2019
GReaper
Quote:

Originally Posted by Click_here
It was crewel of them to use implementation defined behaviour as a solution!

Thank you, I now know what "crewel" means. :D
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12