View Full Version : Data Types in C++

04-23-2004, 07:20 PM
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?


04-23-2004, 08:05 PM
I don't know myself... maybe your instructor would know?

If I were you, the only reason I'd be curious to know the answer from my professor as he/she will be the one asking the question, and I'd like to know what answer he/she expects!


04-24-2004, 02:29 AM
The thing is I don't really have an instructor, it's an internet course :thumbsup:.

The reason I'm asking is because my text book later asks a question along the lines of:


float x=3.0392039, y=483.33;

what are the values that are stored in memory?

04-24-2004, 02:50 AM
I don't have C++ installed on my computer.. but I used Java to produce the following output:

public class TestFloat {
public static void main(String[] args) {
float x=(float)3.0392039, y=(float)483.33;
System.out.println(x+" "+y);

3.039204 483.33

So anyway, try something similar in C++ and see what you come up with.


04-24-2004, 04:13 AM
The precision of float and double is not a definite number of decimals. The floating point numbers are constructed using two binary components, and depending on the actual numerical value being represented, those binary components can be converted into a variable number of decimals.

04-24-2004, 06:48 AM

I actually tried that code (in c++) earlier:

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;

q = y *a+a *c;
r = z%x+b/a;
s = x+z/y*c;
t = b/a * b*x + c;

cout<< a << b << c << x << y << z << q << r << s << t;

The output doesn't help at all, when it displays the value for 'a' (float=4.0) it displays '4' (no decimal). But what's output on the screen doesn't necessarily indicate what's stored in memory...right? :confused: Thanks for your reply.


I apologize, I'm having a hard time understanding your reply (I'm fairly new to programming :cool: ) but I think that's the kind of answer I'm looking for. Could you perhaps elaborate on what you mean, maybe even an example? Thanks again.

04-24-2004, 05:04 PM
I just know the basics behind it myself, I never got further into how it is actually encoded. However, Google is your friend when it comes to finding this type of data.

Easy to understand if you know how to think binary: <http://www.webpronews.com/webdevelopment/webapplications/wpn-27-20040331UnderstandingFloatingPointFormats.html>

More technical, but explains both single and double precision:

Even more technichal:

04-25-2004, 08:47 AM
Here is a nice explanation (http://webster.cs.ucr.edu/AoA/Windows/HTML/MoreDataRepresentation.html#998834) about the floating point format.

The actual precision of floats and doubles is machine dependent.


04-25-2004, 02:29 PM
Yes, it is machine independent. However, the standard that almost all processor manufacturers use today is IEEE 754, which was outlined by the documents I pointed to. Both the x86 and the PPC uses IEEE 754 as their native floating point format, so there's your standard. Also, IEEE 754 does not vary depending on OS or processor architecture: Single precision and double precision are both defined to be a certain bit size in the spec.

04-29-2004, 03:50 AM
Thanks for your help and the links!