您的位置:首页 > 编程语言 > C语言/C++

C++标准库——cmath,climits,limits

2015-12-04 20:18 1316 查看

1. cmath

功能:cmath头文件是原生支持的内建数学函数。

http://www.cplusplus.com/reference/cmath/。这是C++对C语言中的math.h头文件的一个封装,充分考虑了不同类型进行处理的效率问题,undefine了所有C语言的全局数学函数,同时将所有函数在std命名空间内进行了封装。是标准C++风格的头文件。

类似定义如下:

...
#include <math.h>
...
#undef ceil
...

std{
...
using ::ceil;

inline float
ceil(float __x)
{ return __builtin_ceilf(__x); }

inline long double
ceil(long double __x)
{ return __builtin_ceill(__x); }

template<typename _Tp>
inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
ceil(_Tp __x)
{ return __builtin_ceil(__x); }
...
}


其他数学函数都有上述类似定义。因此使用时都优先考虑cmath头文件。所有数学函数速查如下:

#undef abs
#undef div
#undef acos
#undef asin
#undef atan
#undef atan2
#undef ceil
#undef cos
#undef cosh
#undef exp
#undef fabs
#undef floor
#undef fmod
#undef frexp
#undef ldexp
#undef log
#undef log10
#undef modf
#undef pow
#undef sin
#undef sinh
#undef sqrt
#undef tan
#undef tanh

#undef fpclassify
#undef isfinite
#undef isinf
#undef isnan
#undef isnormal
#undef signbit
#undef isgreater
#undef isgreaterequal
#undef isless
#undef islessequal
#undef islessgreater
#undef isunordered


2. climits

功能:定义了所有基本数据类型的界限和范围常量。

http://www.cplusplus.com/reference/climits/。climits也是C语言中的limits.h头文件的封装,由于基本数据类型二者相同,因此该头文件的内容仅仅是包含了limit.h头文件,但是所有常量都是定义在了标准C++的std命名空间中。

#include <limits.h>


limits.h头文件定义的基本数据界限范围常量列举如下:

#define PATH_MAX    259    //定义路径的字符串最大长度

//定义字符类型的范围
#define CHAR_BIT    8      //单字符的bit数
#define MB_LEN_MAX  2      //宽字符最长字符数
#define SCHAR_MIN   (-128) //有符号char类型数的最小值
#define SCHAR_MAX   127    //有符号char类型数的最大值
#define UCHAR_MAX   255    //无符号char类型的最大值

//整数值范围
#define INT_MAX     2147483647
#define INT_MIN     (-INT_MAX-1)
#define UINT_MAX    0xffffffff

#define SHRT_MAX    32767
#define SHRT_MIN    (-SHRT_MAX-1)
#define USHRT_MAX   0xffff

#define LONG_MAX    2147483647L
#define LONG_MIN    (-LONG_MAX-1)
#define ULONG_MAX   0xffffffffUL

//for ISO C9x
#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX - 1)
#define ULLONG_MAX (2ULL * LLONG_MAX + 1)

//for GNU C compiler
#define LONG_LONG_MAX   9223372036854775807LL
#define LONG_LONG_MIN   (-LONG_LONG_MAX-1)
#define ULONG_LONG_MAX  (2ULL * LONG_LONG_MAX + 1)

//for MSVC compatibility
#define _I64_MIN LONG_LONG_MIN
#define _I64_MAX LONG_LONG_MAX
#define _UI64_MAX ULONG_LONG_MAX


3. limits

功能提供与基本数据类型相关的定义。

http://www.cplusplus.com/reference/limits/。对于每个数值数据类型,它定义了可以表示出来的最大值和最小值以及二进制数字的位数。C++标准一共支持13种基本算术类型:

//   * integers
//         bool                             (1)
//         char, signed char, unsigned char         (3)
//         short, unsigned short                (2)
//         int, unsigned                    (2)
//         long, unsigned long                  (2)
//
//   * floating points
//         float                        (1)
//         double                       (1)
//         long double                      (1)


另外有GNU C++标准通过C标准库支持另外两种整数类型:

//   * integer
//         long long, unsigned long long            (2)


定义numeric limits基本类型:

numeric_limits  //Numeric limits type (class template )

float_round_style  //Enum type for float rounding style (type )
float_denorm_style //Enum type for float denormalization style (type )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: