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

第三部分:

#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

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

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

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

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

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

发现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
请按任意键继续. . .
*/

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

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值