Thread: Floating-Point Numbers

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    Floating-Point Numbers

    Greetings All-

    I am currently reading about floating-point numbers. As per the C Primer Plus, a floating-point representation involves breaking up a number into a fractional part and an exponent part and storing the parts separately.

    Example mentioned was how 7.0 is written as fractional part and exponent part:

    7.0 as 0.7E1. 0.7 as the fractional part and 1 as the exponent part.

    My question is:

    Is it incorrect to break 7.0 as 7.0E0, 7.0 as the fractional part and 0 as the exponent part?


    Your thoughts would be greatly appreciated.

    Cheers
    MindLess

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Whether you display 7.0 or 7.0E0 is a presentation issue, not a storage issue.

    Internally, floats always have a fractional part and an exponent part.
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    fractional part and an exponent part
    isn't quite right. There's an exponent and a significand/mantissa.
    Think of the mantissa as a value in the range [1.0, 2.0). Each bit represents a binary fraction.
    The first bit is always a 1.
    The next bit optionally adds half.
    The next bit optionally adds a quarter.
    The next bit optionally adds an eighth.
    The next bit optionally adds a 1/16...
    Then according to the exponent, all of those bits are doubled or halved some number of times.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    Greetings All-

    Thank you Salem and iMalc for your replies. I am struggling with the concept of floating-point data types.

    Example:
    Code:
    float salary;
    From what I read, float uses 32-bits and accurately represents the first 6 significant digits and has a range from 10^-37 to 10^+37. I used the sizeof() operator to see how many bytes float is represented with. The result was 4 bytes i.e. 32-bits.

    And 8-bits for Exponent [ for sign/value ], and 24-bits for the Mantissa/Significand [ for sign/value ].

    Here are my questions:

    8-bits for Exponent: Shouldn't the valid range be from -128 to +127?
    24-bits for Mantissa: Does it mean the valid values are from -8388608 to +8388607?


    In my example code, say a user inputs 5000 for salary. Would it be correct to say that the Mantissa would be 5000 and the Exponent would be 0?

    /Edit: I tried reading the information in the link
    http://www.cprogramming.com/tutorial...ing_point.html I am not able to understand the floats. Not sure what to do with current situation.

    Cheers
    MindLess
    Last edited by MindLess; 06-24-2007 at 09:29 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 8-bits for Exponent: Shouldn't the valid range be from -128 to +127?
    pow(2,127) is about the same as pow(10,37)

    > 24-bits for Mantissa: Does it mean the valid values are from -8388608 to +8388607?
    23 bits, and it represents a fraction, not a whole number.

    > In my example code, say a user inputs 5000 for salary.
    > Would it be correct to say that the Mantissa would be 5000 and the Exponent would be 0?
    The mantissa represents a normalised value between [1.0,2.0), and raised to some power of two (the exponent). You can see from the program below that powers of two have all the mantissa bits zero, and increasing by a power of two just increments the exponent. Note that the exponent is biased so that exponents don't need to store an explicit sign bit (exponents of 00 and FF are reserved for things like infinity)

    Code:
    #include<stdio.h>
    int main ( ) {
        float arr[] = { 0.125, 0.25, 0.5, 1, 2, 4, 44 };
        union {
            float           f;
            unsigned long   d;
        } a;
        int i;
        for ( i = 0 ; i < sizeof(arr)/sizeof(*arr) ; i++ ) {
            unsigned long sign, exponent, mantissa;
            a.f = arr[i];
            sign     = ( a.d >> 31 ) & 0x01;
            exponent = ( a.d >> 23 ) & 0xFF;
            mantissa = a.d & 0x7FFFFF;
            /* sign is only ever 0 or 1 */
            /* exponent is 23 bits, but 24 are printed, so ignore the MSB */
            printf("&#37;1lx:%02lx:%06lx is %f\n", sign, exponent, mantissa, a.f );
        }
    
    	return 0;
    }
    
    0:7c:000000 is 0.125000
    0:7d:000000 is 0.250000
    0:7e:000000 is 0.500000
    0:7f:000000 is 1.000000
    0:80:000000 is 2.000000
    0:81:000000 is 4.000000
    0:84:300000 is 44.000000
    42 being 32 + 8 + 4
    Taking the first nibble of the exponent (0011), we ignore the first bit (see the code comment), then we're left with 011

    As a fraction, this is no half, + 1 quarter + 1 eighth (=0.375). Since we don't store the most significant bit, the true value of the mantissa is 1.375.

    To get back to our decimal value, we now multiply this by some power of two (in this case, 2^5, or 32).
    1.375 * 32 is indeed 44
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  3. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM