1. ## 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?

2. 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.

3. Originally Posted by Tool
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.

4. 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. Originally Posted by zacs7
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?

Your point is well taken, but what does that leave us with?

The instructor probably should have stated that the number representations can be assumed as is convenient.

7. Originally Posted by Tool
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...

8. 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

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?

9. Originally Posted by since
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?

10. Thx Adak and the rest, that explained it. Overflowing is undefined but it works normal with my compiler (the values seem to be ok).

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...

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?

11. What do you mean Tool?
Code:
```        unsigned int a = 0;
a--;
printf("%u\n",a);```
hmmm....

12. Code:
```union foo
{
unsigned type bar;
signed type baz;
} yay;

yay.bar = 0;
yay.bar--;

output( yay.baz );```
Cheat.

Quzah.

13. 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. 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. 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?