Thread: Extract exponent from a double value

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    Extract exponent from a double value

    Hi i have a question. How to extract exponent from a doble value as a unsigned int. I try with function frexp it gave me exopnent from range
    -1022 - +1023. My task is to offset this exponent about value 1022 to have unsigned int. I can't use any arithmetic or bitwise operators.
    Can you give me some advice or explanations please
    Code:
    int exponent(double d)
    {
      int result;
      frexp(d,&result);
      return result;
    }

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    The output from frexp gives you an exponent and a "normalized" fraction (which you're not reading here).

    If you can't use / or *, you can turn that fraction into an integer. It's pretty much as simple as that.

    However, you might find what you're looking for documentation in the various versions of frexp and related functions in the standard library.

    I'm just not that familiar with how this would be reasonably done without / or *

    Maybe you want modf?

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Definitley I don't need modf. For me it's look like adding two numbers without any arithmetic or bitwise operators. I think if I am not allowed to use this operators I have to use functions. The questions is which functions.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Do you mean that you (a) want the absolute value or (b) that you want to shift the result +1022 out of the negative?

    a) Simply use fabs()
    b) Now, this can be difficult without any arithmetic or bitwise operators... I guess you could do this:
    Code:
    int result;
    frexp(d, &result);
    return (int)&((char*)result)[1022];
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    You are GREAT and BRILLIANT and PHENOMENAL. It seems to be fine. Can you explain me this line with return. I have to understand this code. But i also can't do any pointers addres operations
    Last edited by gawiellus; 06-15-2019 at 12:51 PM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Sure. I take advantage of the fact that array indexing just adds an offset to a pointer. I convert the integer returned by frexp() into a pointer, index it and immediately take that "element's" address. Of course, there is no such element, but the compiler doesn't care, since you never dereference the pointer. Then I convert the pointer back into an integer and it's done.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Thank you very much I didn't know about this. It's very inventive. I read the task again and I am not allowed to use any pointers and adres operations is well. But thank you very much i have to remember this expression

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by gawiellus View Post
    I read the task again and I am not allowed to use any pointers and adres operations is well.
    What are you allowed to use? This is ridiculous... I don't think it's possible, at least not without some assembly hacking.

    EDIT: I just had a wacky idea. How about this? It uses a file counter to add to the final result.
    Code:
    int exponent(double d)
    {
      int result;
      frexp(d,&result);
    
      FILE* tmpFile = fopen("tmpFile", "w");
    
      fseek(tmpFile, 1022, SEEK_SET);
      fseek(tmpFile, result, SEEK_CUR);
      result = ftell(tmpFile);
    
      fclose(tmpFile);
      remove("tmpFile");
    
      return result;
    }
    Oh, it uses pointers... This may actually be impossible.
    Last edited by GReaper; 06-15-2019 at 01:30 PM.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    I am not allowed to use any operators +-*/%&|^~<>#? I am not allowed to use any pointers address operations and I can't include any extra files. Sorry for that.


    Last edited by gawiellus; 06-15-2019 at 01:21 PM.

  10. #10
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Quote Originally Posted by GReaper View Post
    What are you allowed to use? This is ridiculous... I don't think it's possible, at least not without some assembly hacking.

    EDIT: I just had a wacky idea. How about this? It uses a file counter to add to the final result.
    Code:
    int exponent(double d)
    {
      int result;
      frexp(d,&result);
    
      FILE* tmpFile = fopen("tmpFile", "w");
    
      fseek(tmpFile, 1022, SEEK_SET);
      fseek(tmpFile, result, SEEK_CUR);
      result = ftell(tmpFile);
    
      fclose(tmpFile);
      remove("tmpFile");
    
      return result;
    }
    Oh, it uses pointers... This may actually be impossible.
    This is task from codewars site. Anti cheat test doesn't let me use instructions
    frexp(d,&result)
    because of character &
    I have no idea how to solve that
    By the way thank you very much for your help
    Last edited by gawiellus; 06-15-2019 at 01:52 PM.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What does the task say, exactly? Can you give a link or something?
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    ask

    A double precision floating-point type (double) has the following format:
    > Sign (1 bit)
    | > Exponent (11 bits)
    | | > Mantissa (52 bits)
    | | |
    s eeeeeeeeeee mmmmmmmmmmmmmmmmm...mmGiven a double value, extract its exponent part and return the value as an int.
    Restrictions

    You can't use any of the characters +-*/%&|^~<>#? in your code, which means:

    • You can't do any arithmetic/bitwise operations.
    • You can't do any pointer/address operations.
    • You can't include any extra files.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gawiellus
    But thank you very much i have to remember this expression
    It's inventive, but honestly, forget it: it's also code obfuscation, and unless you're planning to do that as a sport, by and large this is harmful.

    I will reserve final judgement until you have a good solution or a non-obfuscated "model answer", but provisionally given the suggestions by people here that apparently don't satisfy the contrived requirements, this looks suspiciously like a trivia question where the question asker has some particular trick in mind and you're supposed to discover it, and it all has nothing to do with good programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by gawiellus View Post

    You can't use any of the characters +-*/%&|^~<>#? in your code, which means:

    • You can't do any arithmetic/bitwise operations.
    • You can't do any pointer/address operations.
    • You can't include any extra files.
    Well... in that case, you cannot use frexp() either (it's on math.h as you have to include using #include <math.h>, which uses #, < and >).
    And if you cannot use any arithmetic or logical operators (+, -, /, %, &, |, ^ and ~), you have an impossible task at hand...

  15. #15
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by gawiellus View Post

    You can't use any of the characters +-*/%&|^~<>#? in your code, which means:

    • You can't do any arithmetic/bitwise operations.
    • You can't do any pointer/address operations.
    • You can't include any extra files.
    Actually, there is one solution, but it isn't pretty and match all the requirements. You are doing arithmetic and logical instructions, but not in C!
    You can use assembly (not inline assembly, because you will have to use % for this). So, using NASM you can:
    Code:
    // test.c
    extern int getexp( double );
    
    int main( void )
    {
      // test value
      double d = 3.14;
    
      // this is the single line of code using '%' char
      // you can create your own itoa() function to overcome this this way:
      //
      //    char buff[8];
      //    int n = getexp( d );
      //    itoa( buff, n );
      //    fputs( "exp = ", stdout );
      //    puts( buff );
      printf( "exp = %d\n", getexp( d ) );
    }
    Code:
    ; getexp.asm
    bits 64
    default rel
    
    section .text
    
    global getexp
    getexp:
      movq rax,xmm0
      shr  rax,52
      and eax,0x7ff
      sub eax,1023
      ret
    Compile and run with:
    Code:
    $ cc -include stdio.h -c -o test.o test.c
    $ nasm -felf64 -o getexp.o getexp.asm
    $ cc -o test test.o getexp.o
    $ ./test
    1

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