max float

This is a discussion on max float within the C Programming forums, part of the General Programming Boards category; helloworld. i run into this problem, trying to get a max value for float, but reach "16777216.000000", if am adding ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    max float

    helloworld.

    i run into this problem, trying to get a max value for float, but reach "16777216.000000", if am adding 1, more if I add 10

    is this something to do with the registers on CPU?

    Code:
    #include <stdio.h>
    
    main()
    {
      float i;
      int j;
      
      i = 0;
      j = 0;
      
      while(1)
      {
        i = i + 1;
        j++;
        
        if(j > 10000)
        {
          printf("%f\n",i);
          j= 0;
    
        }
      }
      
    }
    thx, for ya help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    It's to do with the number of mantissa bits in a float.

    At some point, your float becomes so large that your 1 has too little precision to make any difference at all to the result.
    From then on, it's like adding zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by *nick View Post

    Code:
      
      while(1)
      {
        i = i + 1;
        j++;
        
        if(j > 10000)
        {
          printf("%f\n",i);
          j= 0;
    
        }
      }
      
    }
    The while statement must contain a logical expression as

    while(logical expression)
    {
    statement;
    ..............
    }

    yours is not a logical expression but a constant, first correct that

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by rakeshkool27 View Post
    The while statement must contain a logical expression as

    Code:
    while(logical expression)
    {
    statement;
    ..............
    }
    yours is not a logical expression but a constant, first correct that
    That has no bearing on the results observed by the OP whatsoever. The while statement must simply contain an expression that can be evaluated in a logical true/false context and as such an integer constant fulfills that role.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by rakeshkool27 View Post
    The while statement must contain a logical expression as

    while(logical expression)

    yours is not a logical expression but a constant, first correct that
    while(1) is fine if he wants an infinite loop. And he wants an infinite loop here...

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    You can just use limits.h and print FLT_MAX value.

    But if you want to compute it, you could do this;

    Code:
    /* Float value got by direct computation */
         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("\nThe float is now: %e\n\n", fnum);
        
         printf("Max value to reach: %f\n", FLT_MAX);
         while(fnum < FLT_MAX) {
         fnum *= 1.00000006; /* it works well for any value from 1.0000006 to 1.0000009 */
         }
         printf("The float is now:   %f\n\n", fnum);
    You need to use multiplying since adding wont work (Salem wrote why adding wont work).

    First you increase the variable close to FLT_MAX by multiplying it with itself 34 times: 2 * 34 * 33 * 32... until 1. This is because you dont wanna be waiting long time before it reaches close to FLT_MAX value.

    And then multiply that value with any number from 1.00000006 to 1.00000009 and you should get FLT_MAX value. Thats atleast how it worked for me.
    Last edited by Tool; 01-23-2010 at 06:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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