Thread: double/float data types

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    double/float data types

    I just had a question about the double and float data types: what's the difference?

    for example:
    Code:
    double asdf = 1.123456789;
    cout << asdf;
    would print out > 1.12346, which is only 6 digits, yet double is supposed to have 10 i thought?

    if I were to declare the asdf var. as float, it would have printed the same thing. I thought double could hold more.

    Thanks to anyone who can un-confuse me

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    50
    AFAIK, the default precision is to 6 decimal places.

    To get more, you could use the setprecision manipulator.

    See URL here:
    http://www.cplusplus.com/ref/iostrea...precision.html

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    I think that the exact size of the data types is machine dependent.If I did this correctly in the following code, you can see what the size of each variable is on your machine in bytes. each byte holds 4 bits so if it says that a float is 4. You can do 4 X 4 = 16 digits of precision.

    Feel free to correct me if I am giving incorrect info.

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char c;
    	short s;
    	int i;
    	long l;
    	float f;
    	double d;
    	long double ld;
    	int array[ 20 ];
    	int *ptr = array;
    
    	cout << "sizeof c = " << sizeof c
    		 << "\tsizeof(char) = " << sizeof( char )
    		 << "\nsizeof s = " << sizeof s
    		 << "\tsizeof( short ) " << sizeof( short )
    		 << "\nsizeof i = " << sizeof i
    		 << "\tsizeof( int ) " << sizeof( int )
    		 << "\nsizeof l = " << sizeof l
    		 << "\tsizeof( long ) = " << sizeof( long )
    		 << "\nsizeof f = " << sizeof f
    		 << "\tsizeof( float ) = " << sizeof( float )
    		 << "\nsizeof d = " << sizeof d
    		 << "\tsizeof( double ) = " << sizeof( double )
    		 << "\nsizeof ld = " << sizeof ld
    		 << "\tsizeof( long double ) = " << sizeof( long double )
    		 << "\nsizeof array = " << sizeof array
    		 << "\nsizeof prt = " << sizeof ptr
    		 << endl;
    	return 0;
    }
    Almost forgot. Then you can display the precesion of the digits by using the setprecesion() .
    Last edited by big146; 06-28-2004 at 09:45 AM.
    big146

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    each byte holds 4 bits so if it says that a float is 4. You can do 4 X 4 = 16 digits of precision.

    Feel free to correct me if I am giving incorrect info.
    Each byte is four bits? What platform are you running? Each bytes is CHAR_BIT bits in size, and I'd be really really surprised if it was four.

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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think that the exact size of the data types is machine dependent.
    Yes, but there are minimum requirements. For float that requirement is 6 and for double it's 10 IIRC. But the original question most likely used the default value of cout.precision(), which is 6 unless I'm mistaken.

    >Each bytes is CHAR_BIT bits in size, and I'd be really really surprised if it was four.
    I would be too. CHAR_BIT is required to be at least 8.
    My best code is written with the delete key.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    nice :)

    very good example by big146 i must say
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also look at the members of numeric_limits<double>, declared in <limits>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM