如题所述
有的数学库中定义了PI. 如:
# define M_PI 3.14159265358979323846 /* pi */
# define M_PI_2 1.57079632679489661923 /* pi/2 */
# define M_PI_4 0.78539816339744830962 /* pi/4 */
# define M_1_PI 0.31830988618379067154 /* 1/pi */
# define M_2_PI 0.63661977236758134308 /* 2/pi */
但不同的数学库定义的方式不同。所以最好自己定义一下。
原回答可能没大说清楚,先说用法:在需要用PI的文件中添加下列代码,即可应用M_PI用于计算了:
#ifndef _USE_MATH_DEFINES#define _USE_MATH_DEFINES
#endif
#include "math.h"
下边是原答案========================
在 math。h中有定义
但是如果使用这个定义需要做两步
1,头文件引用 math.h
2,在程序中做一个定义
#defined _USE_MATH_DEFINES
这样才可以把 M_PI当做pi来用。
下面是math.h中的定义,可以看出,必须再定义一个宏才能使用,否则即使引用了 math.h也无法使用
#if defined _USE_MATH_DEFINES && !defined _MATH_DEFINES_DEFINED
#define _MATH_DEFINES_DEFINED
// Definitions of useful mathematical constants
//
// 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 or C++ standards
#define M_E 2.71828182845904523536 // e
#define M_LOG2E 1.44269504088896340736 // log2(e)
#define M_LOG10E 0.434294481903251827651 // log10(e)
#define M_LN2 0.693147180559945309417 // ln(2)
#define M_LN10 2.30258509299404568402 // ln(10)
#define M_PI 3.14159265358979323846 // pi
#define M_PI_2 1.57079632679489661923 // pi/2
#define M_PI_4 0.785398163397448309616 // pi/4
#define M_1_PI 0.318309886183790671538 // 1/pi
#define M_2_PI 0.636619772367581343076 // 2/pi
#define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
#define M_SQRT2 1.41421356237309504880 // sqrt(2)
#define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)
#endif
本回答被网友采纳#define PI 3.14
或者全局变量
double PI=3.14
大小写都可以
下面有用到,就不用在定义了,直接调用就可以
大写还是小写?
追答小写