Should be unsigned int .
Code:printf("%u", UINT_MAX);
Should be unsigned int .
Code:printf("%u", UINT_MAX);
Oh! Yeah, works fine now.
@ Adak
Ok, i printed the values of char, short, int, and long variables in all 3 ways: from the standard header, incrementing them (overflowing) and using sizeof() operator.
I also did print the max value of a float by using the header float.h which contains max float/double values.
But i dont know how to do the last part of this assignment which tells to compute floats. How can i compute floats, what do i need to do to get the max / min value?
For signed, I'd use the increment method and just keep comparing to 0.0f to find out when the value wraps around.
For unsigned, you can use fabsf(signed min) + signed max. Eg, with a char, that the absolute value of the signed minimum is 128, and the signed max is 127, so the unsigned max is 128+127=255.
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
I cant declare a unsigned float tho. I assume float/double types dont support it? Why? This is logical since the values represented from float.h say min value of a float is 0, so basicaly the float is itself an unsigned type?
Also:
the first line prints: -0.0000000Code:printf("Min long double value: %f\n", LDBL_MIN); printf("Max long double value: %f\n", LDBL_MAX);
the second line prints: -1.#QNAN0
What on earth is that?
Last edited by Tool; 12-13-2009 at 10:29 AM.
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
Floats are different - they don't overrun and go negative (at least not on my system). They crash the program when they overrun their maximum value - BUT they have a little "elbow room" above the maximum value, that we can use.
To my surprise, using addition, even addition of huge numbers, did not work for this. So, multiplication and division, to the rescue!
This is an example of finding the float maximum. Since it doesn't overflow back to negative values, you have to use the maximum float value from your header file, to make it work.
With the help of the "elbow room" above the maximum float value but not crashing the program, this works fine on my system.Code:/*find the largest float value on this system */ #include <stdio.h> #include <values.h> //MAXFLOAT, MINFLOAT your header file will be different int main(void) { int i, n; float fnum = 2.0; n = 34; for(i = 0; i < 10; i++) printf("\n\n\n"); printf("maximum float value to reach: %e\n", MAXFLOAT); do { fnum *= --n; if(n % 10 == 0) printf("\nThe float is now: %e", fnum); //3.87e15 }while(n > 1); printf("\nThe float is now: %e", fnum); while(fnum < MAXFLOAT) { fnum *= 1.000001; } printf("\nThe float is now: %e", fnum); while(fnum > MAXFLOAT) { fnum /= 1.000001; } printf("\nThe float is now: %e", fnum); while(fnum < MAXFLOAT) { fnum *= 1.0000001; } printf("\nThe float is now: %e", fnum); printf("\n\n\t Maximum float value is %e", fnum); printf("\n\n\t\t\t Press Enter When Ready "); n = getchar(); return 0; }
You may need (probably will need), to adjust n and other numbers, to make it run correctly AND in a reasonably short amount of time.
Last edited by Adak; 12-13-2009 at 07:18 PM.
Could you please explain the above code a bit further?
This really seems puzzling.
Wouldnt this crash the program? A float passing its max limit...Code:while(fnum > MAXFLOAT) { fnum /= 1.000001; }
And my program seems to get stuck counting after the first loop.
What number should i then put instead of 1.00001 to be faster?
Code:while(fnum < MAXFLOAT) { fnum *= 1.000001; }
Last edited by Tool; 12-14-2009 at 03:29 PM.
Take out a zero - maybe two, to speed it up! Each zero you remove will make it 10X faster. (larger)
The first thing is to look at your MAXFLOAT number. In the first while loop, you put n to the highest number it can go, without erroring, but still bringing fnum up very high.
You want fnum is > MAXFLOAT, but not enough to cause an error.
Now you need to bring fnum DOWN, so you divide by something > 1.0, to make it smaller. Start with 1.001 (sort of a mid value in this case), and adjust as needed by adding or removing a zero, from the right side of the decimal place.
You want to "ramp" the magnitude of the multiplier's and divisor's, so the granularity goes from very coarse, (big), to fine (small).
1.01
1.001
1.0001
etc., as needed
Code:int i, n; float fnum = 2.0; n = 34; do { fnum *= --n; if(n % 10 == 0) printf("\nThe float is now: %e", fnum); }while(n > 1); printf("\n\nThe float is now: %e\n", fnum); while(fnum < FLT_MAX) { fnum *= 1.0000001; } printf("\nThe float is now: %e", fnum); // prints 1.#INF00e+000, why?
Edit: i tried printing the values while computing them. And when i put
while(fnum < FLT_MAX) {
fnum *= 1.001; (the only way for it to work fast enough, else id have to wait for ages)
}
I always end up with a #INF00e+000
Maybe its my compiler?
Last edited by Tool; 12-15-2009 at 02:45 PM.
INF is compiler abbreviation for "Infinite".
so at that point, you have gone beyond the highest float number, but you've gone too far.
Here's something to do. Try and print FLT_MAX + 1, and see what it shows.
It shows the same number as for FLT_MAX.
Should i replace
then?Code:while(fnum < FLT_MAX) { fnum *= 1.0000001; } with fnum < FLT_MAX - 1
Or maybe multiply with a lower number?
Sorry, you already did the one past FLT_MAX.
And you got the SAME number as FLT_MAX???
OK, that's your compiler's version of "elbow room", I would suppose.
Now we need to know two things:
1) Can you "use" that number that was above FLT_MAX? I mean, can you lower it back down by some number, and have the lower number be correct, or will it just keep spitting out it's value as FLT_MAX?
So try either dividing the number fnum = (FLT_MAX + 1) / 10, and then print fnum and see if it shows the decimal point having been moved over 1 place.
2) How much "room" can we use above FLT_MAX, before it goes to INF and can't be used as a number, any more?
So fnum = (FLT_MAX + 1) * 1.0001, and then
fnum = fnum / 10
Then print fnum, and see if it's what it should be.
Last edited by Adak; 12-15-2009 at 03:53 PM.
1) Can you "use" that number that was above FLT_MAX? I mean, can you lower it back down by some number, and have the lower number be correct, or will it just keep spitting out it's value as FLT_MAX?
(see the attachment)
I can use it.
2) see attachment
Last edited by Tool; 12-16-2009 at 10:14 AM.