0

When to use double, float, and long double?

so I'm taking a c++ class and i have this chinese professor who doesn't know how to explain. So i know you use float, double and long double when you are dealing with decimals but what is the difference between them? why should i use double and not float?

8th Oct 2017, 12:21 AM
Johnny Loves Steel Balls
Johnny Loves Steel Balls - avatar
3 Respuestas
0
double has more precision, but at the cost of being 4 bytes larger than float and possibly slower when doing operations/calculations with it. Same thing for long double, it's 2 bytes longer than just double, but has more precision. In many cases, float is good enough and the most supported on platforms, but if you are experiencing rounding errors or need more precision then you want to try double and/or long double.
8th Oct 2017, 12:51 AM
aklex
aklex - avatar
0
On most modern hardware you should use double. If you need more precision use long double. Double calculations will be faster than float (this is because when calculations with float are done they are cast to double first, then calculation is performed, then cast back to float). You might want to use float if your code is used in low memory system, such as an embedded system.
8th Oct 2017, 2:38 AM
Jared Bird
Jared Bird - avatar
0
@Jared Bird That is not true at all. If the architecture supports it, there will be separate instruction sets for handling single point and double point operations. No casting is needed
8th Oct 2017, 2:52 AM
aklex
aklex - avatar