When I define constants in C that cannot be accurately represented in the decimal system, say for example π, how many digits will be taken into account by the compiler for actually creating the float or double in memory?
For example:
/* Overkill? */
const float PI_F = 3.1415926535897932384626433832795f;
/* Too few digits? */
const double PI_D = 3.14159;
Assuming IEEE 754 standard floating point numbers, which are commonplace nowadays, you get roughly 7.22 digits of accuracy for a
floatand 15.95 for adouble, so aim for 7 digits after the decimal point for afloatπ constant and 15 fordoubleto be on the safe side. 128-bitlong doublegives 34.02 precision, so 33 positions after the decimal point in π.(*)However, excess precision won’t hurt, the compiler will simply cut it off.
(*) Note that
long doubleis quite often not an IEEE format; x86-32 typically has 80-bit, not 128-bit,long double. In MSVC, it’s apparently a synonym fordouble, so 64-bit.