32

I'm using C and trying to get access to the constant M_PI (3.14159...). I have imported the math.h header file, but the M_PI constant was still undefined. Through some searching on StackOverflow I have found that I need to add #define _USE_MATH_DEFINES to my code (see example code below). This works fine when compiling normally, but I need to be able to compile with the std=c89 flag for the work that I'm doing.

How should I access M_PI from some C89 code?

5 Answers 5

42

A conforming standard library file math.h is not only not required to, but actually must not define M_PI by default. In this context 'by default' means that M_PI must only get defined through compiler-specific tricks, most often undefined behavior through the use of reserved identifiers.

Just define the constant yourself (you can use the name M_PI freely, but should you want to be able to compile the code with a non-conforming compiler, you must first check that M_PI is not already defined). For convention's sake, do not define M_PI as anything other than (the approximation of) pi.

4
  • 6
    @Jason S: it's simply a consequence of the fact that the standard library implementation is not allowed to "pollute" namespace (i.e. must only use reserved identifiers as per standard).
    – eq-
    Commented Feb 16, 2011 at 17:42
  • Is that so? That would mean a C implementation can never conform to C89 and C99 at the same time, since C99 adds identifiers to the library.
    – Fred Foo
    Commented Mar 7, 2012 at 11:11
  • 3
    @larsmans: C99 is careful to add identifiers that were reserved to the implementation in C89 (see in particular section 4.13 FUTURE LIBRARY DIRECTIONS in C89), or in new header files that were not defined in C89. However there are some corner cases of differing library behaviour between C89 and C99 - for example strtod() which must handle hexadecimal floating point in C99 and cannot in C89.
    – caf
    Commented Mar 7, 2012 at 11:27
  • 1
    @FredFoo That is correct; it is not possible to conform to C89 and C99 at the same time. That's why we have compiler switches to select one or the other. A C89 implementation can provide some C99 features as conforming extensions; the obvious situation is allowing the program to call C99 functions, or to use long long (as long as a diagnostic is emitted).
    – Kaz
    Commented Sep 12, 2018 at 22:14
32

I would go for

#ifndef M_PI
#    define M_PI 3.14159265358979323846
#endif
3
  • The fact that I would have to do something like that, instead of it just being guaranteed that PI is defined, makes me angry.
    – CPlus
    Commented Oct 25, 2022 at 1:08
  • 2
    @user16217248 As you can see in the first answer, a conforming implementation actually must not define M_PI, so it's kind of guaranteed it's not defined. I'd still go with the code in this answer, since there is no harm to checking whether it's already defined first. Commented Oct 25, 2022 at 7:09
  • @sven, It's actually the third answer that says M_PI must not be defined by default. Perhaps that one was shown first for you, but remember that won't necessarily be the same for other readers (and it can change over time, depending on user settings). Commented Jan 18, 2023 at 8:49
9

M_PI is not required by the C standard, it's just a common extension, so if you want to be standard you shouldn't rely on it. However, you can easily define your own #define for it, last time I checked it was a universal constant so there's not much space for confusion. :)

9

I fail to see what the problem is here; there is no incompatability between -std=c89 and _USE_MATH_DEFINES, one defines what language the compiler will compile, the other defines what parts of math.h get enabled.

Those parts that are enabled are not defined as part of the ISO C standard library, but that is not the same thing as not being standard C language, language and library are separate entities in C. It is no less C89 compliant than it would be if you had defined your own macros in your own header.

I would however suggest that you define the macro on the command-line rather than in the code:

-std=c89 -D_USE_MATH_DEFINES

If you ever encounter a math.h implementation that does not define M_PI, then that is easily fixed without code modification by similarly using command line defined macros:

-std=c89 -DM_PI=3.14159265358979323846
2
  • 1
    _USE_* is part of glibc internals, meant to be defined only be features.h as a result of some public feature-test macro being defined. Use -D_GNU_SOURCE or to be more portable, -D_XOPEN_SOURCE=700 or similar. Commented Jun 9, 2013 at 23:07
  • @R..: It was the OP that determined to use the macro after finding it elsewhere on StackOverflow - he failed to specify where unfortunately. The GNU documentation says to define "_BSD_SOURCE or _XOPEN_SOURCE=500, or a more general feature select macro", while Microsoft's C Runtime documentation says to define _USE_MATH_DEFINES, so I'd stick with the latter just for compatibility; the question does not mention any specific compiler.
    – Clifford
    Commented Jun 10, 2013 at 12:16
0

In addition to M_PI definition missing, recall that the following only defines a double constant.

#ifndef M_PI
  #define M_PI 3.141592653589793238462643383279502984
  //           1 23456789 123456789 123456789 1234567
#endif

Even though the 3.14159... in code is good for about 113-bits, the precision typically becomes 53-bit as it is a double constant.

This can be quite important for long double objects and math.

Be sure to use L when declaring a long double constant

#include <math.h>
#include <stdio.h>
#ifndef M_PI
  #define M_PI 3.141592653589793238462643383279502984
#endif
#define M_PI_L 3.141592653589793238462643383279502984L

#define T(s) #s
#define S(s) T(s)

int main() {
  printf("acos(-1):             %.*g\n", DBL_DECIMAL_DIG, acos(-1));
  printf("M_PI(string):         %s\n", S(M_PI));
  printf("M_PI(double):         %.*g\n", DBL_DECIMAL_DIG, M_PI);
  printf("M_PI(long double):    %.*Lg\n", LDBL_DECIMAL_DIG, (long double) M_PI);
  printf("acosl(-1):            %.*Lg\n", LDBL_DECIMAL_DIG, acosl(-1));
  printf("M_PI_L(string):       %s\n", S(M_PI_L));
  printf("M_PI_L(long double):  %.*Lg\n", LDBL_DECIMAL_DIG, M_PI_L);
}

Sample output:

acos(-1):             3.1415926535897931
M_PI(string):         3.1415926535897932384626433832795
M_PI(double):         3.1415926535897931
M_PI(long double):    3.141592653589793116
acosl(-1):            3.14159265358979323851
M_PI_L(string):       3.141592653589793238462643383279502984L
M_PI_L(long double):  3.14159265358979323851

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.