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 )
相关文章推荐
- 在 C++ 代码中使用 UE4 插件---Using a plugin in C++ code
- Thrift C++ Server demo 实现
- c语言的隐式(自动)数据类型转换
- c++11,for,for each,std::for_each的应用
- c语言中gets ,getschar 和fgets 的用法及三者之间的差别
- c++ 中的观察者模式
- 【转载】c++之类的基本操作(c++ primer 的读书笔记 ,类对象, 类用户, 类成员的含义)
- VS开发】C中调用C++文件中定义的function函数
- 学习笔记——关于指向常量指针的解引用的发现
- C++直接初始化和复制初始化2
- C++直接初始化和复制初始化1
- C语言之字符串
- 【C语言提高24】二级指针做输入的第一种内存模型:数组指针
- 解决iOS报错libc++abi.dylib handler threw exception
- 【C语言提高23】二级指针做输出的内存模型
- c语言中static用法总结
- VS2008 C++ 调用托管C++dll 当前不会命中断点,没有与此关联的代码
- C++ (Bug)--调 C文件,报错 xxxx.0 not fountt, x86
- C++ Timer定时器
- Vector c++ 的基本操作 矢量 *_* !