#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Monrovia, Los Angeles County, California
    Posts
    46
    Rep Power
    12

    A rundown of the Float Datatype


    I'm not sure if this belongs here, but I've been working with C++ and my tutorial/book is very very helpful yet, it hasn't explained the float datatype. I know char and int, but we've used float with almost no explenation. If someone could help me out, I would be very grateful. Thankyou.

    - Obscurity
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    char ch='a';
    cout<<'a'<<endl;

    int number= 5;
    cout<<number/4<<endl; //answer is 1

    What if I want to use numbers with decimal points, i.e floating point numbers, for more accuracy:

    float number = 5.0f; //f for float
    cout<<number/4<<endl; //answer is 1.25;
    Last edited by 7stud; June 5th, 2003 at 11:14 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Monrovia, Los Angeles County, California
    Posts
    46
    Rep Power
    12
    Ah thanks alot man, I owe you one. Do you know where float ends though (such as char 128 -127 and int 32767 -32767)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Originally posted by Obscurity
    Ah thanks alot man, I owe you one. Do you know where float ends though (such as char 128 -127 and int 32767 -32767)
    float is floating-point, so it's a fractional part out to so many decimals (binaries, actually) and its exponent ranges from +/- whatever.

    If you are using Visual C++, the ranges should be in the help file. I'm not sure what the ranges are for gcc.

    BTW, a greater precision floating-point type is double, which is usually the default floating-point type.

    EDIT:
    From VC++6 help file:
    Table 2.4 Sizes of Fundamental Types

    Type Size
    char, unsigned char, signed char 1 byte
    short, unsigned short 2 bytes
    int, unsigned int 4 bytes
    long, unsigned long 4 bytes
    float 4 bytes
    double 8 bytes
    long double1 8 bytes

    Maximum representable floating-point number.
    FLT_MAX 3.402823466e+38F
    DBL_MAX 1.7976931348623158e+308
    LDBL_MAX 1.7976931348623158e+308

    Minimum positive value.
    FLT_MIN 1.175494351e38F
    DBL_MIN 2.2250738585072014e308
    LDBL_MIN 2.2250738585072014e308
    VC++ 1.52 help has a much better page. I'll edit it into here tomorrow.
    Last edited by dwise1_aol; June 6th, 2003 at 01:28 AM.
  8. #5
  9. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    It should be noted that the ranges for float are not exactly restricted in the same manner as integers... A 2 byte signed integer can be from -32,768 .. 32,767, and be any integer within these ranges. A float cannot simply be any float between the ranges of a float, because the floating point data type only has so much precision. Think about a float as a number like so:
    1.957934 * 10 ^ 45
    or
    1.957934 E 45
    A certain number of bits are used to store the exponent (45 in this case) and a certain number of bits are used to store the mantissa (1.957934 in this case). The exponent has a limited range, which is basically what sets the limits of a floating point data type. BUT, the mantissa also has its limits. If you attempt to add 1.0 to the number above, you'll get the same number, because only so many digits of the mantissa are stored. For example:

    1.957934 E 45 + 1 = 1.957934000000000000000000000000000000000000001 E 45 (no I didn't count the number of 0s, so it is probably off by a few)

    since we can only store 7 or 8 digits of the mantissa in a float data type, this is rounded to:

    1.957934 E 45

    and thus, this is what is stored for the result of the addition.

    Also, since floating point types are stored in binary format, some non repeating decimal numbers such as 0.1 cannot be stored accurately in a float, since they are repeating in binary. 0.1 decimal = 0.0001100110011.... binary. We can only store so many binary digits, and the rest are cut off. It's similar to attempting to store 1/3 which is 0.33333333333... with a limited number of decimal digits. Eventually, we have to cut off the rest, and we do not accurately store 1/3.

    Try making a program that keeps adding 0.1 to itself (and keeps adding 1 to an integer variable), and see if the float remains 1/10th the size of the integer... it won't. Accumulative error is to blame, since you are not actually adding 0.1 every time.

    Hope this helps.
    Last edited by Jason Doucette; June 6th, 2003 at 08:45 AM.
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    not sure how spot on this is or not, but it'll at least roughly work out the max and min vals of float:

    Code:
    #include <stdio.h>
    
    main()
    {
    	float i;
    	float j;
    	
    	j=i=0.0;
    	while (++i>j)
    		j++;
    	printf("float max: %f\n", j);
    	
    	j=i=0.0;
    	while (--i<j)
    		j--;
    	printf("float min: %f\n", j);
    }
    also there's a header file called limits.h that defines maxes and mins, but double and float aren't in there, not my one, for some reason.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Here is the Visual C++ 1.52 help page on data types:
    Code:
     Data Types
    
    C/C++ recognizes the types shown in the table below.
    -------------------------------------------
    Type Name	Bytes	Other Names	Range of Values
    
    int                            *              signed,                    	System dependent
                                                    signed int
    
    unsigned int            *              unsigned                  System dependent
    
    char                         1              signed char             -128 to 127
    
    unsigned char         1	none                       0 to 255
    
    short                       2               short int,                 -32,768 to 32,767
                                                    signed short int	 
    
    unsigned short        2              unsigned short int   0 to 65,535
    
    long                         4              long int,                    -2,147,483,648 to 2,147,483,647
                                                    signed long int	
    
    unsigned long         4               unsigned long int     0 to 4,294,967,295
    
    enum                       2              none                        -32,768 to 32,767
    
    float                        4               none                        3.4E +/- 38 (7 digits)
    
    double                    8               none                        1.7E +/- 308 (15 digits)
    
    long double           10              none                        1.2E +/- 4932 (19 digits)
    
    Signed and unsigned are modifiers that can be used with any integral type. 
    The char type is signed by default, but you can specify /J to make it unsigned by default.
    
    The int and unsigned int types have the size of the system word.
    This is two bytes (the same as short and unsigned short) on MS-DOS and 16-bit versions of Windows. 
    However, portable code should not depend on the size of int.
    Please also note that the format of floating-point numbers is specified in IEEE 754; http://research.microsoft.com/~holla...ieeefloat.html .

    If you are going to inspect your floating-point numbers on the bit level (eg, in hex) and you are using an Intel machine, keep in mind that Intel machines are little-endian, such that the bytes of a multi-byte value are in reverse order, with the least significant byte first.
    Last edited by dwise1_aol; June 6th, 2003 at 10:05 AM.
  14. #8
  15. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    The range of values for floating point types (float, double, long double) is implementation defined, so it will vary between different compilers. To find out the range for your compiler output the max and min values:

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
        cout << "Maximum floating point values" << endl;
        cout << "=============================" << endl;
        cout << "Max float: " << numeric_limits<float>::max() << endl;
        cout << "Max double: " << numeric_limits<double>::max() << endl;
        cout << "Max long double: " << numeric_limits<long double>::max() << endl;
        cout << "\nMinimum floating point values:" << endl;
        cout << "=============================" << endl;
        cout << "Min float: " << numeric_limits<float>::min() << endl;
        cout << "Min double: " << numeric_limits<double>::min() << endl;
        cout << "Min long double: " << numeric_limits<long double>::min() << endl;
    }

IMN logo majestic logo threadwatch logo seochat tools logo