direct computation

This is a discussion on direct computation within the C Programming forums, part of the General Programming Boards category; Write a program to determine the ranges of char , short , int , and long variables, both signed and ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    direct computation

    Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.

    What is direct computation? Does it means using INT_MAX?

    How can i determine ranges of floating types on my machine? Using C?
    Last edited by Tool; 12-11-2009 at 05:39 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) Get the number of bits using sizeof.
    2) Consider the sign.
    3) Set the bits to represent the highest possible value: for unsigned, that's all of them, for signed, all but the MSB.
    4) The lowest value of a signed type is just the MSB set, ie, the inverse of the highest value.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Tool View Post
    Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.

    What is direct computation? Does it means using INT_MAX?

    How can i determine ranges of floating types on my machine? Using C?
    What they want you to do is to use the standard header files to print out the ranges of these data types. Yes, INT_MAX is one of them.

    You'll want to include headers like limits.h

    Then the second part of your assignment is to find these values yourself, on your system, using direct computation.

    pseudo code for find char limit would be something like this:

    Code:
    #include <stdio.h>
    
    int main() {
     char limit = 0;
    
      while(++limit > 0);
    
       now limit has passed it's limit, and gone negative,
       so back it up by 1
    
       just for the heck of it, print the negative value of limit, before you
       back it up by 1
    
       --limit;
       
      limit should now be at it's maximum value.
    
      You can use similar logic to find the max value of any data type. If the type is
      unsigned, the specifics change a bit, but not much.
    
      return 0;
    }
    For larger data types, you can speed it up a great deal by having the variable increment by say, 10,000 each time, then use a while loop to back it down, instead of just subtracting one. Big time saver.

    The sizeof method mentioned above is good, and much faster. Since there would still be some multiplication involved, I think that would also be a "direct computation".

    It's great to know how your data type is laid out in bits and bytes, and learning about MSB, and such.
    Last edited by Adak; 12-11-2009 at 06:35 PM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Overflowing signed integers is not defined by the standard, that is it is undefined. And using bitwise operations on signed integers is either undefined or implementation defined. As well as signed integers could be excess-k, two's complement, signed magnitude... anything!

    The above of course means nothing if your implementation defines such behaviour. limits.h does exist for a reason.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacs7 View Post
    Overflowing signed integers is not defined by the standard, that is it is undefined. And using bitwise operations on signed integers is either undefined or implementation defined. As well as signed integers could be excess-k, two's complement, signed magnitude... anything!

    The above of course means nothing if your implementation defines such behaviour. limits.h does exist for a reason.
    OK, but how would the OP "directly compute" the maximum value of these data types?

    Your point is well taken, but what does that leave us with? I'm not talking about getting the values from limits.h. That is the FIRST part of the assignment. I'm talking about the second part of the assignment where the instructor asks for a program requiring "direct computation".

    MK27's bit method, and my overflow method, are the only two ways I know of to directly compute these maximum (or minimum), values.

    What do you recommend then?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Adak
    Your point is well taken, but what does that leave us with?
    A bad assignment question?

    The instructor probably should have stated that the number representations can be assumed as is convenient.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Quote Originally Posted by Tool View Post
    determine the ranges of the various floating-point types.
    -infinity to +infinity -> done!
    Or if you include NANs: "To infinity and beyond!"

    Presumably the instructor would mean to exclude infinities...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    How is SizeOf implemented? That's obviously what he wants to know, unless that question can't be answered?

    edit; never mind, looks like it's something the compiler does. OP; you should find the place in the standard where it talks about sizeof as support for why you have to use sizeof and vola, you can use sizeof and you're done.

    Take the number of bytes which is the result of some sizeof(type), and fill that unsigned type with that many number of '1' bits and print the number, same for unsigned and just don't set the most significant bit.

    Tip: http://www.cprogramming.com/faq/cgi-...wer=1074727388


    What do you guys think about this? http://www.dreamincode.net/code/snippet1134.htm
    Two other forum posts suggest it's undefined behavior, but apparently it works reliably, seems like the list of exceptions where this would fail is few or uncommon?
    Last edited by since; 12-12-2009 at 09:29 AM.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by since View Post
    Take the number of bytes which is the result of some sizeof(type), and fill that unsigned type with that many number of '1' bits and print the number, same for unsigned and just don't set the most significant bit.
    Which if you took the time to read a thread instead of just the first post, you wouldn't have to waste your time giving the EXACT same answer as post #2 -- and, further, someone explains why that answer isn't so satisfactory anyway in post #4.

    A lot of people do this ( iMalc comes immediately to mind... ), I always find it very strange...I don't have time to learn about the wheel, I'm too busy re-inventing it?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Thx Adak and the rest, that explained it. Overflowing is undefined but it works normal with my compiler (the values seem to be ok).

    What about for unsigned chars:

    is it enough to just initialize a unsigned char test = 0; and then decrement test, and we get the MAX_SIZE of unsigned char? Is that considered a 'direct computation'? The values seem to be good for me...


    And what about floating types.

    Can they even have a max value? If so why isnt it defined in limits.h? What should i do with this part of the assignment?
    Last edited by Tool; 12-12-2009 at 06:42 PM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What do you mean Tool?
    Code:
            unsigned int a = 0;
            a--;
            printf("%u\n",a);
    hmmm....
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union foo
    {
        unsigned type bar;
        signed type baz;
    } yay;
    
    yay.bar = 0;
    yay.bar--;
    
    output( yay.baz );
    Cheat.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    You are allowed to convert any pointer to character type and examine the object byte by byte. You are allowed to subtract an array from one past the end of the array (single objects count as an array of one object for this purpose). Are you allowed to combine them? I don't know. I can't imagine there are too many, if any, systems where this won't work, but it doesn't matter too much because you can just use sizeof instead.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you've gotten the max and min values these 3 ways:

    1) Gotten them from the standard library macro's (like INT_MAX), in limits.h).

    2) Used the sizeof() method (where 2^n - 1 is max, +- the MSB number).

    3) Incremented or decremented the variable to the max or min value.

    Then you've done all you can.

    Every variable has a maximum and minimum value, no matter what the type. It might take adjustments to the #2 and #3 methods listed above, but there definitely ARE max and min limits.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    So if i want a min value of a char using sizeof(), this is how it should go?
    int tmp = 0;
    tmp = pow(2, (sizeof(char)*8)-1)*-1;

    And for the char max value:
    tmp = pow(2, (sizeof(char)*8)-1) - 1;

    Is this the correct way?
    Last edited by Tool; 12-13-2009 at 06:44 AM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct Input shutting down improperly
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 06-14-2005, 06:54 AM
  2. Direct x 8 include files
    By Zoalord in forum Game Programming
    Replies: 2
    Last Post: 03-17-2004, 03:07 PM
  3. Direct X
    By MicroFiend in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2003, 01:34 PM
  4. Direct Music Illegal Static Member Call error
    By FwyWice in forum Game Programming
    Replies: 4
    Last Post: 11-30-2002, 04:14 PM
  5. Direct Music Trouble
    By FwyWice in forum Game Programming
    Replies: 5
    Last Post: 11-29-2002, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21