Thread: Explain following code,doubt written in comments

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20

    Explain following code,doubt written in comments

    Code:
    /*As float(4 bytes) accepts a max integer value of 33554432, but if we give more than that max value it has to overflow but it is approximate answer ? */ 
    #include<stdio.h>
    int main()
    {
        float r=99999999;
        printf("%f",r);
        return 0;
    }
    
    
    0/p
    100000000.000000

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I really do not think it overflows; but, I have no facts to back up my view point.

    Tim S.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    How does a value written/stored in scientific notation overflow ?
    AFAIK the precision matters, not the size.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ackr1201 View Post
    Code:
    /*As float(4 bytes) accepts a max integer value of 33554432, but if we give more than that max value it has to overflow but it is approximate answer ? */ 
    #include<stdio.h>
    int main()
    {
        float r=99999999;
        printf("%f",r);
        return 0;
    }
    
    
    0/p
    100000000.000000
    Try it with 99999999.0 and see what happens

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Floating point values are represented with complicated binary patterns. I think in this case, the precision of the 99999999 value is jeopardized by the number of bits available in the "float" type. It is truncated and you are left with a different (albeit close) value. Though I could be wrong - it's been years since I learned about this.

    Changing it to a "double" gives you the precision to print out the proper value, though.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by manasij7479 View Post
    AFAIK the precision matters, not the size.
    So you're in the "It's not the size of the tool, it's how you use it!" camp?


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

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    :P
    Tools other than screwdrivers .

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by manasij7479 View Post
    How does a value written/stored in scientific notation overflow ?
    AFAIK the precision matters, not the size.
    The precision is tied to the size of the floating point type. How close you can get to an exact representation of your number depends on how many digits you have to represent something. How accurately can you represent pi in one decimal digit? The closest you can get is 3, which is off by about 4.5%. If I give you 3 digits, you can approximate it as 3.14, which is only off by around .05%. Note that the calculations of how precise you can be a little tricky when you have to account for the exponent, but regardless, you're limited to 32 bits of representation. Since you only have 32 bits to work with, and you can certainly come up with a number that wont fit in a mere 32 bits, it's easy to find a number that would overflow it, even taking advantage of scientific notation: 999999999999999999999999 * 2^9999999999999999. Put that in your IEEE 754 single-precision pipe and smoke it!

    Quote Originally Posted by ackr1201 View Post
    Code:
    /*As float(4 bytes) accepts a max integer value of 33554432, but if we give more than that max value it has to overflow but it is approximate answer ? */
    33554432 is not the largest integer representable by a single precision float, nor will it or 99999999 overflow the limits of a float (the upper limit is ~3.4*10^38). Here's a description IEEE 754 and one specifically on single precision floats. Also, this site has a nifty little calculator to break down your decimal number into it's IEEE 754 counterparts (it shows you single- and double-precision). Lastly, read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

    Notice that in IEEE 754, there is 1 sign bit, 8 exponent bits and 23 bits for the significand (there is actually 24 bits of precision because of the implied 1 at the beginning of any binary scientific notation number). Now, the value 99999999 when converted to binary becomes 1011 1110 1011 1100 0001 1111 111. If you count all the bits there, we would need 27 bits to represent it, but we don't have that many, so we would have to thow those last three 1's away. If they were zeros, we could correct for this in the exponent without loss of accuracy, but they're not, so what do we do? Time to turn to the standard.

    You specified an integer literal, so it obeys the following rules:
    Quote Originally Posted by C99 6.3.1.4p2
    When a value of integer type is converted to a real floating type, if the value being
    converted can be represented exactly in the new type, it is unchanged. If the value being
    converted is in the range of values that can be represented but cannot be represented
    exactly, the result is either the nearest higher or nearest lower representable value, chosen
    in an implementation-defined manner. If the value being converted is outside the range of
    values that can be represented, the behavior is undefined.
    Tater's addition of the .0 makes makes his 99999999 of real floating type, so his conversion follows 6.3.1.5p2, which has nearly identical wording, though his implementation may choose "nearest lower" when demoting real types, and yours may choose "nearest higher" when converting integral types, so you may see different behavior depending on the presence of a .0 (my system produced 100000000 in both cases).

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    it's easy to find a number that would overflow it, even taking advantage of scientific notation: 999999999999999999999999 * 2^9999999999999999. Put that in your IEEE 754 single-precision pipe and smoke it!
    I had the possibly wrong idea that the 9999... s would be further broken down in the scientific notation, making the expression fit in 32 bits. If it is not, why ?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by manasij7479 View Post
    I had the possibly wrong idea that the 9999... s would be further broken down in the scientific notation, making the expression fit in 32 bits. If it is not, why ?
    There is an maximum exponent size permitted; go above this size and it overflows.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 09-08-2009, 10:16 AM
  2. Code that looks for Comments
    By dgs414 in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2007, 06:36 PM
  3. Need comments on code
    By Desolation in forum C++ Programming
    Replies: 10
    Last Post: 01-02-2007, 09:17 PM
  4. Re-written code needs help
    By crag2804 in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:43 AM
  5. Code comments
    By salvelinus in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-17-2002, 01:27 AM