M_PI は C の標準仕様ではない | M_PI Is Non-Standard in C
GCC または Clang で M_PI
を使う場合、 _XOPEN_SOURCE=500
またはより一般的な機能選択マクロが定義されていなければならない。
デフォルトでは定義されているので問題なく使える。
M_PI
is defined by default with GCC or Clang, so you can use it without any problems.
M_PI
Pi, the ratio of a circle’s circumference to its diameter.
...
These constants come from the Unix98 standard and were also available in 4.4BSD; therefore they are only defined if
_XOPEN_SOURCE=500
, or a more general feature select macro, is defined. The default set of features includes these constants. See Feature Test Macros.
VC++ で M_PI
を使うには、math.h
をインクルードする前に _USE_MATH_DEFINES
を定義する必要がある (ソースコードを引用する方法がわからない):
To use M_PI
with VC++, you must define _USE_MATH_DEFINES
before including math.h
(I don't know how to quote source code):
#define _USE_MATH_DEFINES // for C++ #include <cmath> #define _USE_MATH_DEFINES // for C #include <math.h>
...
Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h.
まあ、気になるのであれば、こんな感じにすればたぶん大丈夫:
If you are concerned about portability, you may want to define it like:
/* #ifdef M_PI #undef M_PI #endif */ #ifndef M_PI #define M_PI 3.14159265358979323846 #endif /* !M_PI */
宿題で間違ったのでメモ。