Hey all, i have been reading some c++ books and they talk about a variable type called float but they also mention another one named double and another one named long double but what i don't understand is the difference, the furthest i get is that they tell me float is single precision, double is double precision and long double is extended precision.
But i don't see the difference and whats a precision?
Thank you in advanced.

PS:If you know a book which explains c++ 100%(or more then 70%) for a beginners pleas tell me the name of the book.

Recommended Answers

All 9 Replies

They are all "floating point" types. "double" is basically short for "double float" (but you can't say that). A float is usually 32 bits long whereas a double is 64 bits. A long double can be anywhere from 64 (same as double) to 128 bits.

I hear "C++ from the Ground Up" is good for beginners..

type float, 32 bits long, has a precision of 7 digits. While it may store values with very large or very small range (+/- 3.4 * 10^38 or * 10^-38), it has only 7 significant digits.

type double, 64 bits long, has a bigger range ( *10^+/-308) and 15 digits precision.

long double is nominally 80 bits, though a given compiler/OS pairing may store it as 12-16 bytes for alignment purposes. The long double has an exponent that just ridiculously huge, and should have 19 digits precision. M$, in their infinite wisdom, limits long double to 8 bytes, same as plain double.

Generally speaking, just use type double when you need a floating point value/variable. Literal floating point values used in expressions will be treated as doubles by default, and most of the math functions that return floating point values return doubles. You'll save yourself many headaches and typecastings if you just use double.

Thank you guys, that makes much more sense.

Some addition:
The newest C++ draft standard:

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.2) shall specify the maximum and minimum values of each arithmetic type for an implementation.

Take into account that the Standard does not specify explicitly precisions and ranges for these three floating point types. Moreover it's possible that all three FP types have the same internal representation (about MS VC++ 8-byte long double type ;)).

Hey all, i have been reading some c++ books and they talk about a variable type called float but they also mention another one named double and another one named long double but what i don't understand is the difference, the furthest i get is that they tell me float is single precision, double is double precision and long double is extended precision.
But i don't see the difference and whats a precision?
Thank you in advanced.

PS:If you know a book which explains c++ 100%(or more then 70%) for a beginners pleas tell me the name of the book.

I like Ivor Horton's Beginning Visual C++ 2005 or later. That is what I am currently starting to read. As for the answer to your question I think this sums it up nicely:

Typical implementations implement double to have considerably more precision than float and to cover a greater range of exponent values. Long double however may be implemented to be the same as double, or it may be larger.

So then the choice comes down to what precision you require and how efficient you need to be in space and/or time. While using the greatest precision and range that you can might seem an obvious choice, the down side of doing so is that operations using doubles take longer (and presumably a long double operation may take even longer!) than that on floats (assuming doubles and long doubles have greater precision and exponent range than floats). Doubles (and long doubles) will also take up more space in memory than a float.

On today's computer there is little or no performance difference between floats and doubles. Years ago all floats had to be converted to doubles before FPU processes them. In some cases that may still be true, but no all. I understand some FPUs can handle both floats and doubles without conversion.

Here is one thread that talkes about the performance differences. At no time has floats ever been faster than doubles.

At no time has floats ever been faster than doubles.

Well... that's absolutely true, if you restrict the discussion to the CPU. But floats have always been very popular for efficiency reasons in game programming because GPUs are (or at least "were", in my days) designed with custom floats (often 5 bytes for 4d vectors of 20 bytes in total). So, since conversion is inevitable and precision is of very little concern, floats being smaller means faster transfer rate from the main computer to the graphics card, which was one of the big bottlenecks in the rendering loop (since not enough memory was available on the graphics card to hold all the game data, there was a lot of memory-transfers). So a slightly higher cost of each FPO for the relatively small amount of operations done the CPU was a fair price to pay for increased throughput to the GPU.

I never wrote a game in my life, so I'll take your word for it :)

Indeed he is correct, openGL prefers floats over doubles for higher performance on the GPU, also from what i understand of SIMD SSE2 unless it has been expanded in later editions those instructions were optimal for floats also. If this has been changed (since 03 when the book i read that in was written so its likely) then my apologies but as i say from what i know where execution performance is paramount in graphics float is best but beware of the rounding errors which accumulate over time using floats. (also using doubles but its much slower owing to doubles' large precision)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.