Thread: Extract exponent from a double value

  1. #16
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    I think I came up with a solution. It involves a union, a struct and a bitfield... think about it.
    Devoted my life to programming...

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    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.

  3. #18
    Registered User
    Join Date
    May 2019
    Posts
    209
    Quote Originally Posted by GReaper View Post
    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.
    Last edited by Niccolo; 06-15-2019 at 09:39 PM.

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    3,687
    x = x +1022
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    3,687
    Quote Originally Posted by GReaper View Post
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #21
    Registered User
    Join Date
    Jun 2019
    Posts
    9
    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.

  7. #22
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    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...
    Last edited by flp1969; 06-16-2019 at 07:07 AM.

  8. #23
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    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.
    Devoted my life to programming...

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    It was crewel of them to use implementation defined behaviour as a solution!

  10. #25
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by Click_here View Post
    It was crewel of them to use implementation defined behaviour as a solution!
    Thank you, I now know what "crewel" means.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exponent and C4550
    By Opjeezzeey in forum C Programming
    Replies: 9
    Last Post: 12-16-2013, 12:28 AM
  2. Using an Exponent in C++
    By NismoT in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2011, 09:11 AM
  3. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 07:31 PM
  4. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 01:34 AM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM

Tags for this Thread