+ 2

Why is it advisory to use "float" instead o"double" in C++?

Float Vs Double

10th Mar 2018, 6:12 AM
Ronald Mbanjwa
Ronald Mbanjwa - avatar
4 Answers
+ 6
Your answer shouldn't be "why" it's advisory, it should be "when" it's advisory. Double usually has a lot more precision, but uses more bytes. How much depends on the compiler and the hardware, so if you want constant precision on every system, use float. However, that's rarely the case. It could be problematic if you convert double to float and vice versa, so try not to. Modern computers don't have any space or speed issues when preferring double over float, most compilers even use double automatically if you don't use a float literal. (1 is int, 0.0 is double, 0.0f is float) You might have memory issues on embedded systems (microchips etc), so perhaps float is advisory there if you don't need high precision.
10th Mar 2018, 8:37 AM
Chris
Chris - avatar
+ 6
The C++ standard adds: The value representation of floating-point types is implementation-defined. ... A double is 64 and single precision (float) is 32 bits. The double has a bigger mantissa (the integer bits of the real number).
10th Mar 2018, 6:25 AM
Scooby
Scooby - avatar
+ 2
👍good explanation @Chris I now understand. @Kinshuk Vasisht @SD thanks I now have a clear understanding
10th Mar 2018, 9:43 AM
Ronald Mbanjwa
Ronald Mbanjwa - avatar