Data Types in C++ (Homework Help)

Discussion in 'Computer Science & Culture' started by Nebula, Apr 22, 2004.

Thread Status:
Not open for further replies.
  1. Nebula Occasionally Frequent Registered Senior Member

    Messages:
    906
    I'm taking an introductory C++ course right now but I'm having some difficulty with data types, specifically float and double.

    From my textbook, C++ Programming Today:

    Float: data type for variables containing up to five digits of decimal precision.

    Double: data type for variables containing up to ten digits of decimal precision.

    Alright, pretty straightforward. But on the next page there is a table comparing all of the data types. Here it says:

    Float contains a number with six to seven digits of decimal precision, ie 14.937453.

    Double contains a number with thirteen to fourteen digits of decimal precision, ie 3.14159265294753

    These statements seem contradictory to me, and it continues throughout the text. I've looked a bunch of stuff up on google trying to find an answer but none of the results were really helpful to me; the only useful information I got was that it seems the ranges vary depending on what compiler you're using. I'm using MS Visual C++ (course requirement).

    Can anyone clarify this for me, offer any suggestions or provide any useful links?

    Thanks.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. okinrus Registered Senior Member

    Messages:
    2,669
    Yes, they have to be inaccurate as well. Stroustrupt says the meaning of floats and doubles is implementation defined with the constraint sizeof(float) <= size(double) <= sizeof(long double).

    I think the confusion is the number of digits printed out by a cout call but your book is definitly wrong on this matter. Even if it was specific to Visual Studio, floats are 32 bits and doubles are 64 bits.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Nebula Occasionally Frequent Registered Senior Member

    Messages:
    906
    Thanks okinrus,

    Sorry, I should have identified the contradiction. I realize "sizeof(float) <= size(double) <= sizeof(long double)" & the 32/64 bits.

    What confuses me is the number of digits of precision. One part of my textbook says a float contains up to 5 digits of decimal precision, and another part says a float contains six to seven digits of precision. Which is it? Am I misunderstanding something here?

    I'm sorry if your reply has answered this question okinrus, but if it has then I still don't understand...
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Nebula Occasionally Frequent Registered Senior Member

    Messages:
    906
    This may help...

    Basically I'm asking this because I was stumped by one of the questions in my textbook. It asks what values will be stored in memory for each variable. I'm having trouble with the floats (I think the rest are alright). The nuts of the program look like this:

    Code:
    	float a = 4.0, b = 8.0, c= 1.5;
    	int x = 5, y = 7.5, z = 19.0;
    	float q, r;
    	int s, t;
    
    	s = x + z / y * c;
    	t = b / a * b * x + c;
    	q = y * a + a * c;
    	r = z % x + b / a;
    Here are the values I got:
    a = 4.000000
    b = 8.000000
    c = 1.500000
    x = 5
    y = 7
    z = 19
    q = 34
    r = 6
    s= 8
    t = 81

    Hopefully this helps illustrate where I am confused

    Please Register or Log in to view the hidden image!

    .

    *edit formatting*
     
  8. okinrus Registered Senior Member

    Messages:
    2,669
    Intel uses the IEE754 format which is fairly complex, I think. I can't imagine they would ask a question like that but one simply way to find out what values are in memory is to write something like this where printBits prints out the bits
    Code:
    int main(void)
    {
         union {
               float f;
               long val;
         } u;
    
         u.f = 4.0;
    
         printBits(u.val);
         return 0;
    }
    
    I'm pretty certain your text books means to just have you print out the floating point numbers and then record how many decimal points to the right. Problem is that the internal storage is a format like sign * mantissa * 2^exp, which means that there are greater gaps between large numbers than between smaller numbers. Not to mention that very small numbers are handled differently by IEEE574 and there's normalization involved. Consequently this the precision is something you do not want to calculate yourself. This book says between 6 and 9 for floats http://www.particle.kth.se/~lindsey/JavaCourse/Book/Tech/Chapter02/floatingPt.html
     
  9. LeHKN Registered Member

    Messages:
    5
    Nebula,
    I am guessing that you are confused because your program prints out integers for your float values q and r???

    The value stored in each variable, according to your output is as follows:
    a = 4.000000
    b = 8.000000
    c = 1.500000
    x = 5
    y = 7
    z = 19

    The values for q and r are questionable. Since they are floats, they should
    have 6 decimals behind them. I think the problem is you are doing arithmetic with integers. That have the tendency to round things up to whole numbers. To resolve this, type cast your int values as floats like so:
    q = (float) (y * a + a * c);
    r = (float) (z % x + b / a);
     
  10. Nebula Occasionally Frequent Registered Senior Member

    Messages:
    906
    yeah, i know it would do that. the thing is I was given the code and i'm just supposed to say what will be stored in memory. But yeah, I've read other parts of the textbook and I think they're simply wanting me to show that the float values will be stored with 6 digits after the decimal (if if they are all '0' as a result of the integer arithmetic).

    and i think:
    q = 34.000000
    r = 6.000000
     
Thread Status:
Not open for further replies.

Share This Page