If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Data Types in C++

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?

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.

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? Thanks for your reply.

liorean

I apologize, I'm having a hard time understanding your reply (I'm fairly new to programming ) 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.

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.