Sign up ×

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;
share|improve this question
This is implementation-defined. – user1203803 Feb 16 '12 at 15:43
@daknøk: then an answer covering the most common implementations (VC, GCC, Clang, TCC, Borland) would be nice. – orlp Feb 16 '12 at 15:47

4 Answers 4

up vote 1 down vote accepted

Assuming IEEE 754 standard floating point numbers, which are commonplace nowadays, you get roughly 7.22 digits of accuracy for a float and 15.95 for a double, so aim for 7 digits after the decimal point for a float π constant and 15 for double to be on the safe side. 128-bit long double gives 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 double is quite often not an IEEE format; x86-32 typically has 80-bit, not 128-bit, long double. In MSVC, it's apparently a synonym for double, so 64-bit.

share|improve this answer
35 digits should be fine in every sane case then, right? – orlp Feb 16 '12 at 15:59
@nightcracker: yes, that should be fine. Depending on the application, you'll likely get round-off errors and noise from other sources anyway. – larsmans Feb 16 '12 at 16:01
@nightcracker: oh and do check whether your <math.h> has the (non-standard) M_PI macro. – larsmans Feb 16 '12 at 16:02
As I recall, to uniquely identify an IEEE-754 single-precision number, you need a total of 9 decimal digits, and to uniquely identify an IEEE-754 double-precision number you need a total of 17 decimal digits [Sorry, I don't have the source handy, probably a paper by professor Kahan]. Note that this does not imply that for conversions in the opposite direction you can expect that many valid decimal digits, as has already been pointed out by larsmans. – njuffa Feb 16 '12 at 21:01

Use as many digits as you can (within reasonable limits: your float PI_F above is ok). If it's more data than the type can hold, the compiler will strip excess data away; and if it's less, you will be happy to have written all you knew.

share|improve this answer

Assuming you have 32-bit floats and 64-bit doubles, "Floats are good for 6 digits of accuracy but doubles offer 15."

share|improve this answer

The "normal" mathematical constants are defined in the C header , like M_PI and such. For them it is best to use your system-provided value. For others; if they are reasonably cheap to compute/approximate, do that in your program for robustness, or compute them out of line to (a bit more than) the needed precision and copy the value.

BTW, for almost all "real world" uses 3 or 4 digits precision is plenty, don't fret too much over this.

share|improve this answer

Your Answer


