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.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
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 ;)).
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
mike_2000_17
Posting Virtuoso
2,137 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
I never wrote a game in my life, so I'll take your word for it :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343