2012年4月5日 星期四

[C++] Marco PI 去哪裡了 ??? --> M_PI

一直記得C++內定義了PI的數值 , 怎麼這次使用PI找不到定義 ??!!
稍微Google了一下得到以下定義 :

/* Definitions of useful mathematical constants
 * M_E        - e
 * M_LOG2E    - log2(e)
 * M_LOG10E   - log10(e)
 * M_LN2      - ln(2)
 * M_LN10     - ln(10)
 * M_PI       - pi
 * M_PI_2     - pi/2
 * M_PI_4     - pi/4
 * M_1_PI     - 1/pi
 * M_2_PI     - 2/pi
 * M_2_SQRTPI - 2/sqrt(pi)
 * M_SQRT2    - sqrt(2)
 * M_SQRT1_2  - 1/sqrt(2)
 */


那就來使用M_PI搞定它 !!!!
...........
...........
還是沒有定義 (翻桌) !!!! 怎麼可能 !!!! 把math.h翻出來看 ,
/* Define _USE_MATH_DEFINES before including math.h to expose these macro
 * definitions for common math constants.  These are placed under an #ifdef
 * since these commonly-defined names are not part of the C/C++ standards.
 */

原來已經從C++ standard移除了 , 從此以後都要先定義_USE_MATH_DEFINES.
所以想要使用M_PI , 需要以下:

1. #define _USE_MATH_DEFINES
(在標頭檔定義)
2. #include "math.h"

Done.

Any problem , please leave your message.
Sean.

1 則留言:

  1. 其實我沒有什麼問題

    但您能將他整理出來,又有解釋

    真的很感謝,3Q

    回覆刪除