C++ 直接从<cmath>中寻找库函数——数学标准库函数大全

如题,直接从<cmath>扒出以下三段内容,其中第一段是筛选全部using ::开头的语句:

第一部分:

using ::acos;
using ::asin;
using ::atan;
using ::atan2;
using ::ceil;
using ::cos;
using ::cosh;
using ::exp;
using ::fabs;
using ::floor;
using ::fmod;
using ::frexp;
using ::ldexp;
using ::log;
using ::log10;
using ::modf;
using ::pow;
using ::sin;
using ::sinh;
using ::sqrt;
using ::tan;
using ::tanh;

using ::acosh;
using ::acoshf;
using ::acoshl;
using ::asinh;
using ::asinhf;
using ::asinhl;
using ::atanh;
using ::atanhf;
using ::atanhl;
using ::cbrt;
using ::cbrtf;
using ::cbrtl;
using ::copysign;
using ::copysignf;
using ::copysignl;
using ::erf;
using ::erff;
using ::erfl;
using ::erfc;
using ::erfcf;
using ::erfcl;
using ::exp2;
using ::exp2f;
using ::exp2l;
using ::expm1;
using ::expm1f;
using ::expm1l;
using ::fdim;
using ::fdimf;
using ::fdiml;
using ::fma;
using ::fmaf;
using ::fmal;
using ::fmax;
using ::fmaxf;
using ::fmaxl;
using ::fmin;
using ::fminf;
using ::fminl;
using ::hypot;
using ::hypotf;
using ::hypotl;
using ::ilogb;
using ::ilogbf;
using ::ilogbl;
using ::lgamma;
using ::lgammaf;
using ::lgammal;
using ::llrint;
using ::llrintf;
using ::llrintl;
using ::llround;
using ::llroundf;
using ::llroundl;
using ::log1p;
using ::log1pf;
using ::log1pl;
using ::log2;
using ::log2f;
using ::log2l;
using ::logb;
using ::logbf;
using ::logbl;
using ::lrint;
using ::lrintf;
using ::lrintl;
using ::lround;
using ::lroundf;
using ::lroundl;
using ::nan;
using ::nanf;
using ::nanl;
using ::nearbyint;
using ::nearbyintf;
using ::nearbyintl;
using ::nextafter;
using ::nextafterf;
using ::nextafterl;
using ::nexttoward;
using ::nexttowardf;
using ::nexttowardl;
using ::remainder;
using ::remainderf;
using ::remainderl;
using ::remquo;
using ::remquof;
using ::remquol;
using ::rint;
using ::rintf;
using ::rintl;
using ::round;
using ::roundf;
using ::roundl;
using ::scalbln;
using ::scalblnf;
using ::scalblnl;
using ::scalbn;
using ::scalbnf;
using ::scalbnl;
using ::tgamma;
using ::tgammaf;
using ::tgammal;
using ::trunc;
using ::truncf;
using ::truncl;

第二部分:

// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs  //取消旧版的宏标识符,除abs、div外与上面重复
#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

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

#ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
  inline _GLIBCXX_CONSTEXPR double
  abs(double __x)   //取消老版本后重新定义
  { return __builtin_fabs(__x); }
#endif

#ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
  inline _GLIBCXX_CONSTEXPR float
  abs(float __x)
  { return __builtin_fabsf(__x); }

  inline _GLIBCXX_CONSTEXPR long double
  abs(long double __x)
  { return __builtin_fabsl(__x); }
#endif

此处略。。。

_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

扫描二维码关注公众号,回复: 13128282 查看本文章

第三部分:

#undef fpclassify  //取消旧版的宏标识符
#undef isfinite
#undef isinf
#undef isnan
#undef isnormal
#undef signbit
#undef isgreater
#undef isgreaterequal
#undef isless
#undef islessequal
#undef islessgreater
#undef isunordered

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

#if __cplusplus >= 201103L
  constexpr int
  fpclassify(float __x)    //取消老版本后重新定义
  { return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
                FP_SUBNORMAL, FP_ZERO, __x); }

此处略。。。

_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

还有一段代码中的函数与第一部分全部重复,略。

就这样子总共抓取到141个数学库函数,估计这才是《数学标准库函数大全》完整版 ^_^

当然标准库还有像 __builtin_fabs(x)、 __builtin_fabsf(x)、 __builtin_fabsl(x) 超多的这种内建函数其实也能被调用,只是比较难记函数名称:

    float x = -1.23;
    cout<< __builtin_fabs(x) << endl;  // 输出: 1.23

《数学标准库函数大全》完整版

序号 函数 功能 用法说明
1 abs 绝对值 // y = |x|
2 acos 反余弦 // y = arccos(x)
3 acosh 反双曲余弦 // y=arcosh(x)=ln[x+sqrt(x^2-1)]
// 双曲余弦 y=cosh(x)=[e^x + e^(-x)]/2 的反函数
4 acoshf 函数带下划线都同上
仅形参不同以下略过
 
5 acoshl  (略过)  
6 asin 反正弦 // y = arcsin(x)
7 asinh 反双曲正弦 // y=arsinh(x)=ln[x+sqrt(x^2+1)]
// 双曲正弦 y=sinh(x)=[e^x - e^(-x)]/2 的反函数
8 asinhf  (略过)  
9 asinhl  (略过)  
10 atan 反正切 // y = arctan(x)
11 atan2 坐标法反正切 // f(x,y)=atan2(y,x) 返回坐标轴上点(x,y)到(0,0)的连线与x轴的夹角,允许x=y=0(f = 0 )
// 类似于 atan(y/x) ,但 atan(y/x) 可以x=0,y<>0 即atan(inf)=π/2,但不允许x=y=0
// 所有反三角函数返回值可以转角度数 = 弧度 * 180 /
M_PI (<--#define _USE_MATH_DEFINES)
12 atanh 反双曲正切 // y=artanh(x)=(1/2)*ln[(1+x)/(1-x)]
// 双曲正切 y=tanh(x)=[e^x - e^(-x)]/[e^x + e^(-x)] 的反函数
13 atanhf  (略过)  
14 atanhl  (略过)  
15 cbrt  立方根 // y = ³√x
16 cbrtf  (略过)  
17 cbrtl  (略过)  
18 ceil 右向取整 // ceil(x) 取不小于x的最小整数
19 copysign 复制正负号 // copysign(a, b)  =  |a*b| / b
// 复制a的绝对值和b的正负,a和b值不变
20 copysignf  (略过)  
21 copysignl  (略过)  
22 cos 余弦 // y=cos(x)
23 cosh 双曲余弦 // y=cosh(x)=[e^x + e^(-x)] / 2
24 div 整除函数 div_t qr = div(10, 3);   // 要记住结构体及成员变量的名称
cout << qr.
quot <<endl;   // 商 = 3
cout << qr.
rem  <<endl;   // 余数 = 1
25 erf 误差函数
// 高斯误差函数, erf(x) = 1-erfc(x) 余差函数的补函数
// erf 函数返回一个介于 -1.0 到 1.0 之间的值

// M_2_SQRTPI  (<--#define _USE_MATH_DEFINES)
26 erff (略过)  
27 erfl (略过)  
28 erfc 余差函数 // 补余高斯误差函数, erfc(x) = 1-erf(x) 误差函数的补函数
// erfc 函数返回一个介于 0.0 到 2.0 之间的值
29 erfcf  (略过)  
30 erfcl  (略过)  
31 exp 指数函数 e为底 // y =  e^x
32 exp2 指数函数 2为底 // y =  2^x
33 exp2f  (略过)  
34 exp2l  (略过)  
35 expm1 指数函数 e为底, 指数-1 // y =  e^(x-1)
36 expm1f  (略过)  
37 expm1l  (略过)  
38 fabs 浮点数绝对值 // y = |a|,类似于 abs()
39 fdim  求正差 // f(x,y)=fdim(x,y) = x>y ? x-y : 0;   // 求两数的差,若是负数置为0
40 fdimf  (略过)  
41 fdiml  (略过)  
42 floor 左向取整 // floor(x) 取不大于x的最大整数
43 fma 积和函数 // fma(a,b,c) = a * b + c
44 fmaf  (略过)  
45 fmal  (略过)  
46 fmax 最大值 // f(x,y)=fmax(x,y) = x>y?x:y
47 fmaxf  (略过)  
48 fmaxl  (略过)  
49 fmin 最小值 // f(x,y)=fmin(x,y) = x<y?x:y
50 fminf  (略过)  
51 fminl  (略过)  
52 fmod  求浮点数的余数 // f(x,y)=fmod(x,y)  // 求x除以y的余数,相当于整数的 x%y 
53 fpclassify  判断数字类型 switch(fpclassify(x)) {
        case FP_INFINITE:  return "Inf";
        case FP_NAN:         return "NaN";
        case FP_NORMAL: return "normal";
        case FP_SUBNORMAL: return "subnormal";
        case FP_ZERO:      return "zero";
        default:                    return "unknown";
    }
54 frexp 求浮点数的尾数 double frexp( double x, int *expptr );
int n; double y, num = -6133.23;   //  转成一个基底数为2的科学记数法
y = frexp(num, &n);  // 指数部分返回一个指针,用&取址读取
cout << num << " = " << y << " * 2 ^ " << n <<endl;
55 hypot 求斜边长 // f(x,y)=hypot(x,y) = sqrt(x²+y²)   // 或有参数inf,函数值也返回inf
56 hypotf  (略过)  
57 hypotl  (略过)  
58 ilogb 特殊的对数函数 类似于 logb()
59 ilogbf  (略过)  
60 ilogbl  (略过)  
61 isfinite 判断INF和NaN // isfinite( INFINITY ) = true , isfinite( NAN ) = true
// INFINITY为无穷常量,cout输出inf, 如 1/0、 pow(2,1024)
// NAN为非数常量,cout输出nan,如sqrt(-1)、pow(-1,1.1)
62 isgreater 判断大小 > // f(x,y)=isgreater(x,y) = x>y?true:false
63 isgreaterequal 判断大小 ≥ // f(x,y)=isgreaterequal(x,y) = x>=y?true:false
64 isinf 判断INF // isinf( INFINITY ) = ture   // 参见isfinite
65 isless 判断大小 < // f(x,y)=isless(x,y) = x<y?true:false
66 islessequal 判断大小 ≤ // f(x,y)=islessequal(x,y) = x<=y?true:false
67 islessgreater 判断大小 ≠ // f(x,y)=islessgreater(x,y) = x<>y?true:false
68 isnan 判断NaN // isnan( NAN ) = ture   // 参见isfinite
69 isnormal 判断是否正常 // y=isnormal(x) = fpclassify(x)==FP_NORMAL ? true : false  // 详见fpclassify
70 isunordered 判断是否有序 // isunordered(NAN, 1)  // 两个参数只要有一个为NAN就返回true
71 ldexp 倍幂函数 // y=ldexp(x, n) = x * 2^n  // 与 x=frexp(y,n) 相反
72 lgamma log伽玛函数 // y=ln[tgamma(x)]  // 伽玛函数值的自然对数,参见tgamma
73 lgammaf  (略过)  
74 lgammal  (略过)  
75 llrint 取整函数 返回long long int类型,类似rint(x)
76 llrintf  (略过)  
77 llrintl  (略过)  
78 llround 取整函数 返回long long int类型,类似round(x)
79 llroundf  (略过)  
80 llroundl  (略过)  
81 log 对数函数 e为底 // y=ln(x)
82 log10 对数函数 10为底 // y=log10(x)
83 log1p 对数函数 e为底, 真数+1 // y=ln(x+1)
84 log1pf  (略过)  
85 log1pl  (略过)  
86 log2 对数函数 2为底 // y=log2(x)
87 log2f  (略过)  
88 log2l  (略过)  
89 logb 特殊的对数函数 // y=trunc(log2(|x|)
// 以2为底、参数的绝对值为真数的对数的整数部分
90 logbf  (略过)  
91 logbl  (略过)  
92 lrint  取整函数 返回long int类型,类似rint(x)
93 lrintf  (略过)  
94 lrintl  (略过)  
95 lround 取整函数 返回long int类型,类似round(x)
96 lroundf  (略过)  
97 lroundl  (略过)  
98 modf 返回小数部分 y = modf(num, &iX);  //整数部分返回一个指针,用&取址读取
cout << num << " => " << iX << " & " << y <<endl;// -3.23 => -3 & -0.23
99 nan 返回字串的NaN值 double nan (const char* arg);
// nan("") = nan
100 nanf  (略过)  
101 nanl  (略过)  
102 nearbyint  取整函数 // 使用当前的舍入方法,将给定值舍入为附近的整数值 
// 由 fesetround(mode) 指定当前的舍入方法,头文件 <cfenv>
// mode = 0|1024|2048|3072 相当于 round() ceil() floor() trunc()
103 nearbyintf (略过)  
104 nearbyintl  (略过)  
105 nextafter 返回在y方向上x之后的下一个可表示值 // 类似nexttoward,有点复杂和啰嗦不用也罢,两者的区别直接抄原文:
// The similar function, nexttoward has the same behavior,
// but it takes
a long double as second argument.
// first representable value greater than zero: nextafter(0.0,1.0)=4.940656e-324
// first representable value less than zero: nextafter(0.0,-1.0)=-4.940656e-324
106 nextafterf (略过)  
107 nextafterl  (略过)  
108 nexttoward 返回在y方向上x之后的下一个可表示值
参见 nextafter
// 类似nextafter,有点复杂和啰嗦不用也罢,两者的区别直接抄原文:
// This function behaves as nextafter, but with a potentially more precise y.
// first representable value greater than zero: nexttoward(0.0,1.0
L)=4.940656e-324
// first representable value less than zero: nexttoward(0.0,-1.0
L)=-4.940656e-324
109 nexttowardf  (略过)  
110 nexttowardl  (略过)  
111 pow 幂指函数 // pow(x,y) = x ^ y
112 remainder  浮点余数 // 函数remainder(x, y) 返回x/y的浮点余数(四舍五入到最接近的值)
// remainder(7.5/2.1) = -0.9 remainder(-17.5/2) = 0.5 remainder(-17.5/0) = -nan
113 remainderf  (略过)  
114 remainderl  (略过)  
115 remquo  浮点余数及商 // 函数remquo(x, y, int* p)函数计算x/y的浮点余数,商存储到指针p
// int q; double ret = remquo(5.5, 2.2, &q);  => 商q=2,余数ret=1.1
// ret = remquo(5.7, 2.2, &q);  => 商q=3,余数ret=-0.9
// ret = remquo(-5.5, 0, &q);   => 商q=0,余数ret=-nan
116 remquof  (略过)  
117 remquol  (略过)  
118 rint 取整函数 double rint (double x);  //或者 float \ long double
double rint (T x);   // T为整型 int \ long int \ ...
如果返回的值与x的值不同,则此函数可能会引发一个FE_INEXACT不精确异常
用法请参见nearbyint(),nearbyint是不引发此类异常的等效函数
119 rintf  (略过)  
120 rintl  (略过)  
121 round 取整函数 // round(x) 取最接近x的整数,即四舍五入取值
// round(-3.56) = floor(-3.56+0.5) = -4
122 roundf  (略过)  
123 roundl  (略过)  
124 scalbln 倍幂函数 // 类似scalbn(x, n),参数n为长整型
125 scalblnf  (略过)  
126 scalblnl  (略过)  
127 scalbn  倍幂函数 // 基数FLT_RADIX(默认为2)的科学记数法,参见frexp() ldexp()
// y=scalbln(x, n) = x * pow(2, n)  // scalbln(1.0001,10) = 1024.1024
128 scalbnf  (略过)  
129 scalbnl  (略过)  
130 signbit 判断是否负数 // y=signbit(x) = x<0 ? 1 : 0
131 sin 正弦 // y=sin(x)
132 sinh 双曲正弦 // y=sinh(x) = [e^x - e^(-x)] / 2
133 sqrt 算术平方根 // y=√x
134 tan 正切 // y=tan(x)
135 tanh 双曲正切 // y=tanh(x) = [e^x - e^(-x)] / [e^x + e^(-x)]
136 tgamma 伽马函数

// Gamma函数亦称欧拉第二积分,是阶乘函数在实数与复数上扩展的一类函数
// <cmath> 仅提供实数域上的公式,它的另一种写法:

// 递归性质: tgamma(x+1) = x * tgamma(x),x为正整数时就是阶乘函数
137 tgammaf  (略过)  
138 tgammal  (略过)  
139 trunc 零向取整 // trunc(x) 取靠向0且最靠近x的整数,即不管正负舍弃小数部分即可
140 truncf  (略过)  
141 truncl  (略过)  

发现2个很有用的判断函数isnan()、isinf(),先测试一下:

#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
	double a = -2;
	double b = 1.1;
	double c = 1024;
	
	cout << pow(a,b) << endl;
	cout << isnan(pow(a,b))<<endl;
	cout << pow(-a,b) << endl;
	cout << isnan(pow(-a,b))<<endl<<endl;
	
	cout << pow(a,c) << endl;
	cout << isinf(pow(a,c))<<endl;
	cout << pow(a,c-1) << endl;
	cout << isinf(pow(a,c-1))<<endl;
		
	return 0;
}

/*
nan
1
2.14355
0

inf
1
-8.98847e+307
0

--------------------------------
Process exited after 0.8773 seconds with return value 0
请按任意键继续. . .
*/

先到此结束吧,这么多函数还有很多都是生面孔,待以后慢慢梳理......

猜你喜欢

转载自blog.csdn.net/boysoft2002/article/details/114175726