Thread: direct computation

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Should be unsigned int .
    Code:
    printf("%u", UINT_MAX);

  2. #17
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Oh! Yeah, works fine now.

  3. #18
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    @ 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?

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  5. #20
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    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:
    Code:
    printf("Min long double value: %f\n", LDBL_MIN);
    printf("Max long double value: %f\n", LDBL_MAX);
    the first line prints: -0.0000000
    the second line prints: -1.#QNAN0

    What on earth is that?
    Last edited by Tool; 12-13-2009 at 10:29 AM.

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tool View Post
    I cant declare a unsigned float tho.
    Oh yeah. Guess I've never tried.

    BTW floats and doubles are SIGNED, for sure.
    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

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Tool View Post
    @ 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?
    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.

    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;
    }
    With the help of the "elbow room" above the maximum float value but not crashing the program, this works fine on my system.

    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.

  8. #23
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Could you please explain the above code a bit further?

    This really seems puzzling.

    Code:
    while(fnum > MAXFLOAT) {
        fnum /= 1.000001;
       }
    Wouldnt this crash the program? A float passing its max limit...


    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.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Tool View Post
    Could you please explain the above code a bit further?

    This really seems puzzling.

    Code:
    while(fnum > MAXFLOAT) {
        fnum /= 1.000001;
       }
    
    /* 
    fnum /= 4               would be the same thing as :
    fnum = fnum / 4
    
    C has this shorthand for all the arithmetic operators:
    
    fnum += 1   is the same as
    fnum = fnum + 1
    
    fnum *= 2 is the same as
    fnum = fnum * 2
    
    fnum -= 2 is the same as
    fnum = fnum - 2
    
    



    Wouldnt this crash the program? A float passing its max limit...

    Yes - but only if you go too far!


    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;
       }
    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


  10. #25
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    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.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  12. #27
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    It shows the same number as for FLT_MAX.

  13. #28
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Should i replace

    Code:
    while(fnum < FLT_MAX) {
         fnum *= 1.0000001;
         }
    
    with fnum < FLT_MAX - 1
    then?

    Or maybe multiply with a lower number?

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  15. #30
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    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.

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, 04:07 PM
  3. Direct X
    By MicroFiend in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2003, 02:34 PM
  4. Direct Music Illegal Static Member Call error
    By FwyWice in forum Game Programming
    Replies: 4
    Last Post: 11-30-2002, 05:14 PM
  5. Direct Music Trouble
    By FwyWice in forum Game Programming
    Replies: 5
    Last Post: 11-29-2002, 04:01 PM