时间:2024-11-05 10:34:44

(一) <math.h>

<math.h>文件中已经定义了M_PI,如下所示,用户可以直接使用;

//math.h
........................ #if defined(_USE_MATH_DEFINES) && !defined(_MATH_DEFINES_DEFINED)
#define _MATH_DEFINES_DEFINED /* 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.
*/ /* 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)
*/ #define M_E 2.71828182845904523536
#define M_LOG2E 1.44269504088896340736
#define M_LOG10E 0.434294481903251827651
#define M_LN2 0.693147180559945309417
#define M_LN10 2.30258509299404568402
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.785398163397448309616
#define M_1_PI 0.318309886183790671538
#define M_2_PI 0.636619772367581343076
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2 1.41421356237309504880
#define M_SQRT1_2 0.707106781186547524401 #endif /* _USE_MATH_DEFINES */

但必须在使用的文件中,

#include<math.h>之前,加入#define _USE_MATH_DEFINES,如下所示:

 //------------------------------------------------------------------------------
//>>>
//使用math.h中定义M_PI的定义
#define _USE_MATH_DEFINES
#include <math.h>
const double Rad2Deg = (180.0/M_PI);
//<<<
//------------------------------------------------------------------------------

(二)<float.h>

由于浮点数存在的精度误差,造成浮点数与0比较的问题,一般定义其误差值来解决。float.h中已经定义了float,double两种浮点数的误差值,用户可以直接使用。

//float.h

.....................

#define DBL_DIG         15                      /* # of decimal digits of precision */
#define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
#define DBL_MANT_DIG 53 /* # of bits in mantissa */
#define DBL_MAX 1.7976931348623158e+308 /* max value */
#define DBL_MAX_10_EXP 308 /* max decimal exponent */
#define DBL_MAX_EXP 1024 /* max binary exponent */
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */
#define DBL_MIN_10_EXP (-307) /* min decimal exponent */
#define DBL_MIN_EXP (-1021) /* min binary exponent */
#define _DBL_RADIX 2 /* exponent radix */
#define _DBL_ROUNDS 1 /* addition rounding: near */ #define FLT_DIG 6 /* # of decimal digits of precision */
#define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */
#define FLT_GUARD 0
#define FLT_MANT_DIG 24 /* # of bits in mantissa */
#define FLT_MAX 3.402823466e+38F /* max value */
#define FLT_MAX_10_EXP 38 /* max decimal exponent */
#define FLT_MAX_EXP 128 /* max binary exponent */
#define FLT_MIN 1.175494351e-38F /* min positive value */
#define FLT_MIN_10_EXP (-37) /* min decimal exponent */
#define FLT_MIN_EXP (-125) /* min binary exponent */
#define FLT_NORMALIZE 0
#define FLT_RADIX 2 /* exponent radix */
#define FLT_ROUNDS 1 /* addition rounding: near */

应用举例:

//------------------------------------------------------------------------------
//>>>
#include <float.h>
#define FLOAT_EQ(a,b) (fabs(a-b)<=FLT_EPSILON)
#define DOUBLE_EQ(a,b) (fabs(a-b)<=DBL_EPSILON)
//<<<
//------------------------------------------------------------------------------ float x,y; x=0.0; y=0.0000001; if(fabs(a)<FLT_EPSILON) //判断单精度类型变量x是否为零
...... if(fabls(a-b)<FLT_EPSILON) //判断单精度变量x,y是否相等
......

注意:对以上数学计算的宏定义,不要重复定义。