该头文件引入 C 标准库头文件 <math.h>,并将相关标识符加入 std 命名空间。
目录
- [1 说明](#1 说明)
- [2 常量与类型](#2 常量与类型)
- [3 函数](#3 函数)
- [4 绝对值](#4 绝对值)
- [5 三维斜边](#5 三维斜边)
- [6 分类/比较函数](#6 分类/比较函数)
- [7 数学特殊函数](#7 数学特殊函数)
-
- [7.1 连带拉盖尔多项式](#7.1 连带拉盖尔多项式)
- [7.2 连带勒让德函数](#7.2 连带勒让德函数)
- [7.3 β函数](#7.3 β函数)
- [7.4 第一类完全椭圆积分](#7.4 第一类完全椭圆积分)
- [7.5 第二类完全椭圆积分](#7.5 第二类完全椭圆积分)
- [7.6 第三类完全椭圆积分](#7.6 第三类完全椭圆积分)
- [7.7 第一类修正圆柱贝塞尔函数](#7.7 第一类修正圆柱贝塞尔函数)
- [7.8 第一类圆柱贝塞尔函数](#7.8 第一类圆柱贝塞尔函数)
- [7.9 第二类修正圆柱贝塞尔函数(不规则修正贝塞尔)](#7.9 第二类修正圆柱贝塞尔函数(不规则修正贝塞尔))
- [7.10 圆柱诺伊曼函数(第二类圆柱贝塞尔函数)](#7.10 圆柱诺伊曼函数(第二类圆柱贝塞尔函数))
- [7.11 第一类不完全椭圆积分](#7.11 第一类不完全椭圆积分)
- [7.12 第二类不完全椭圆积分](#7.12 第二类不完全椭圆积分)
- [7.13 第三类不完全椭圆积分](#7.13 第三类不完全椭圆积分)
- [7.14 指数积分](#7.14 指数积分)
- [7.15 埃尔米特多项式](#7.15 埃尔米特多项式)
- [7.16 拉盖尔多项式](#7.16 拉盖尔多项式)
- [7.17 勒让德多项式](#7.17 勒让德多项式)
- [7.18 黎曼ζ函数](#7.18 黎曼ζ函数)
- [7.19 第一类球贝塞尔函数](#7.19 第一类球贝塞尔函数)
- [7.20 球连带勒让德函数](#7.20 球连带勒让德函数)
- [7.21 第二类球贝塞尔函数(球诺伊曼函数)](#7.21 第二类球贝塞尔函数(球诺伊曼函数))
1 说明
语法:
cpp#include <cmath>
包含该头文件后,C 标准库头文件中采用外部链接声明的标识符,都会在 std 命名空间中进行声明。
2 常量与类型
cpp
namespace std{
using float_t = 见下文;
using double_t = 见下文;
}
#define HUGE_VAL //表示浮点正无穷(double)
#define HUGE_VALF //表示浮点正无穷(float)
#define HUGE_VALL //表示浮点正无穷(long double)
#define INFINITY //无穷大常量
#define NAN //非数值(Not a Number)
#define FP_INFINITE //浮点值类别:无穷
#define FP_NAN //浮点值类别:NaN
#define FP_NORMAL //浮点值类别:规格化正常数
#define FP_SUBNORMAL //浮点值类别:次正规数
#define FP_ZERO //浮点值类别:零
#define FP_FAST_FMA //标识fma运算硬件加速(double)
#define FP_FAST_FMAF //标识fma运算硬件加速(float)
#define FP_FAST_FMAL //标识fma运算硬件加速(long double)
#define FP_ILOGB0 //ilogb(0)返回值
#define FP_ILOGBNAN //ilogb(NAN)返回值
#define MATH_ERRNO //数学错误处理方式:设置errno
#define MATH_ERREXCEPT //数学错误处理方式:抛出浮点异常
#define math_errhandling //数学错误处理方式:是上面二者的按或组合
3 函数
cpp
/*
- 后缀 f = float 版本;
- 后缀 l = long double 版本;
- 无后缀 = double 版本;
*/
/*
**三角函数**
- 入参单位:弧度,不是角度
*/
float acos(float x); //反余弦,返回[0,π]
double acos(double x);
long double acos(long double x);
float acosf(float x);
long double acosl(long double x);
float asin(float x); //反正弦,返回[-π/2,π/2]
double asin(double x);
long double asin(long double x);
float asinf(float x);
long double asinl(long double x);
float atan(float x); //反正切,返回[-π/2,π/2]
double atan(double x);
long double atan(long double x);
float atanf(float x);
long double atanl(long double x);
float atan2(float y, float x); //根据坐标(y,x)求反正切,返回[-π,π],可判断象限,比 atan 更适合坐标转换
double atan2(double y, double x);
long double atan2(long double y, long double x);
float atan2f(float y, float x);
long double atan2l(long double y, long double x);
float cos(float x); //余弦
double cos(double x);
long double cos(long double x);
float cosf(float x);
long double cosl(long double x);
float sin(float x); //正弦
double sin(double x);
long double sin(long double x);
float sinf(float x);
long double sinl(long double x);
float tan(float x); //正切
double tan(double x);
long double tan(long double x);
float tanf(float x);
long double tanl(long double x);
float acosh(float x); //反双曲余弦
double acosh(double x);
long double acosh(long double x);
float acoshf(float x);
long double acoshl(long double x);
float asinh(float x); //反双曲正弦
double asinh(double x);
long double asinh(long double x);
float asinhf(float x);
long double asinhl(long double x);
float atanh(float x); //反双曲正切
double atanh(double x);
long double atanh(long double x);
float atanhf(float x);
long double atanhl(long double x);
float cosh(float x); //双曲余弦
double cosh(double x);
long double cosh(long double x);
float coshf(float x);
long double coshl(long double x);
float sinh(float x); //双曲正弦
double sinh(double x);
long double sinh(long double x);
float sinhf(float x);
long double sinhl(long double x);
float tanh(float x); //双曲正切
double tanh(double x);
long double tanh(long double x);
float tanhf(float x);
long double tanhl(long double x);
/*
**指数&幂相关**
*/
float exp(float x); //自然指数e^x
double exp(double x);
long double exp(long double x);
float expf(float x);
long double expl(long double x);
float exp2(float x); //2的x次方2^x
double exp2(double x);
long double exp2(long double x);
float exp2f(float x);
long double exp2l(long double x);
float expm1(float x); //e^x-1; x很小时精度优于exp(x)-1
double expm1(double x);
long double expm1(long double x);
float expm1f(float x);
long double expm1l(long double x);
float frexp(float value, int* exp); //拆分浮点数为尾数*2^exp,返回尾数(范围[0.5,1])
double frexp(double value, int* exp);
long double frexp(long double value, int* exp);
float frexpf(float value, int* exp);
long double frexpl(long double value, int* exp);
int ilogb(float x); //取浮点数的指数部分(整数),等价于取logb后截断
int ilogb(double x);
int ilogb(long double x);
int ilogbf(float x);
int ilogbl(long double x);
float ldexp(float x, int exp); //x*2^exp,frexp的逆运算
double ldexp(double x, int exp);
long double ldexp(long double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
float log(float x); //自然对数lnx
double log(double x);
long double log(long double x);
float logf(float x);
long double logl(long double x);
float log10(float x); //以10为底对数log10x
double log10(double x);
long double log10(long double x);
float log10f(float x);
long double log10l(long double x);
float log1p(float x); //ln(1+x);x接近0时精度更高
double log1p(double x);
long double log1p(long double x);
float log1pf(float x);
long double log1pl(long double x);
float log2(float x); //以2为底对数log2x
double log2(double x);
long double log2(long double x);
float log2f(float x);
long double log2l(long double x);
float logb(float x); //获取浮点数的指数(浮点返回)
double logb(double x);
long double logb(long double x);
float logbf(float x);
long double logbl(long double x);
float modf(float value, float* iptr); //拆分浮点数,返回小数部分,整数部分存入iptr
double modf(double value, double* iptr);
long double modf(long double value, long double* iptr);
float modff(float value, float* iptr);
long double modfl(long double value, long double* iptr);
float scalbn(float x, int n); //x*2^n,n是int
double scalbn(double x, int n);
long double scalbn(long double x, int n);
float scalbnf(float x, int n);
long double scalbnl(long double x, int n);
float scalbln(float x, long int n); //x*2^n,n是long int
double scalbln(double x, long int n);
long double scalbln(long double x, long int n);
float scalblnf(float x, long int n);
long double scalblnl(long double x, long int n);
float cbrt(float x); //立方根3(x)(支持负数)
double cbrt(double x);
long double cbrt(long double x);
float cbrtf(float x);
long double cbrtl(long double x);
4 绝对值
💡提示:C++11 之后推荐直接用 std::abs,它自带浮点重载,优先于 fabs。
cpp
int abs(int j); //求绝对值:整数、浮点重载;整数版本常定义在 <cstdlib>,浮点在 <cmath>
long int abs(long int j);
long long int abs(long long int j);
float abs(float j);
double abs(double j);
long double abs(long double j);
float fabs(float x); //浮点绝对值:C风格,兼容旧代码
double fabs(double x);
long double fabs(long double x);
float fabsf(float x); //float 绝对值:单精度版本
long double fabsl(long double x); //long double 绝对值:长双精度版本
float hypot(float x, float y); //sqrt(x^2+y^2):计算斜边,避免平方溢出
double hypot(double x, double y);
long double hypot(double x, double y);
float hypotf(float x, float y); //float 版 hypot
long double hypotl(long double x, long double y); //long double 版 hypot
5 三维斜边
cpp
/*
f = float 单精度
l = long double 长双精度
*/
float hypot(float x, float y, float z); //三维斜边:sqrt(x^2+y^2+z^2),防止平方溢出
double hypot(double x, double y, double z);
long double hypot(long double x, long double y, long double z);
float pow(float x, float y); //求x的y次幂
double pow(double x, double y);
long double pow(long double x, long double y);
float powf(float x, float y);
long double powl(long double x, long double y);
float sqrt(float x); //平方根
double sqrt(double x);
long double sqrt(long double x);
float sqrtf(float x);
long double sqrtl(long double x);
float erf(float x); //高斯误差函数
double erf(double x);
long double erf(long double x);
float erff(float x);
long double erfl(long double x);
float erfc(float x); //互补高斯误差函数:1-高斯误差函数
double erfc(double x);
long double erfc(long double x);
float erfcf(float x);
long double erfcl(long double x);
float lgamma(float x); //伽马函数的自然对数
double lgamma(double x);
long double lgamma(long double x);
float lgammaf(float x);
long double lgammal(long double x);
float tgamma(float x); //伽马函数
double tgamma(double x);
long double tgamma(long double x);
float tgammaf(float x);
long double tgammal(long double x);
float ceil(float x); //向上取整(向正无穷)
double ceil(double x);
long double ceil(long double x);
float ceilf(float x);
long double ceill(long double x);
float floor(float x); //向下取整(向负无穷)
double floor(double x);
long double floor(long double x);
float floorf(float x);
long double floorl(long double x);
float nearbyint(float x); //按当前浮点舍入模式取整,不抛出异常
double nearbyint(double x);
long double nearbyint(long double x);
float nearbyintf(float x);
long double nearbyintl(long double x);
float rint(float x); //按当前浮点舍入模式取整,可能触发浮点异常
double rint(double x);
long double rint(long double x);
float rintf(float x);
long double rintl(long double x);
long int lrint(float x); //按当前舍入模式取整,返回 long
long int lrint(double x);
long int lrint(long double x);
long int lrintf(float x);
long int lrintl(long double x);
long long int llrint(float x); //按当前舍入模式取整,返回 long long
long long int llrint(double x);
long long int llrint(long double x);
long long int llrintf(float x);
long long int llrintl(long double x);
float round(float x); //四舍五入(远离0方向:0.5进1)
double round(double x);
long double round(long double x);
float roundf(float x);
long double roundl(long double x);
long int lround(float x); //四舍五入,返回 long
long int lround(double x);
long int lround(long double x);
long int lroundf(float x);
long int lroundl(long double x);
long long int llround(float x); //四舍五入,返回 long long
long long int llround(double x);
long long int llround(long double x);
long long int llroundf(float x);
long long int llroundl(long double x);
float trunc(float x); //截断取整(直接砍掉小数部分,向0取整)
double trunc(double x);
long double trunc(long double x);
float truncf(float x);
long double truncl(long double x);
float fmod(float x, float y); //浮点取模,结果符号和x相同
double fmod(double x, double y);
long double fmod(long double x, long double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);
float remainder(float x, float y); //浮点余数,结果绝对值≤y,IEEE余数定义
double remainder(double x, double y);
long double remainder(long double x, long double y);
float remainderf(float x, float y);
long double remainderl(long double x, long double y);
float remquo(float x, float y, int* quo); //同时算出余数+商的低几位(存入quo)
double remquo(double x, double y, int* quo);
long double remquo(long double x, long double y, int* quo);
float remquof(float x, float y, int* quo);
long double remquol(long double x, long double y, int* quo);
float copysign(float x, float y); //返回一个值,大小等于x,符号和y一致
double copysign(double x, double y);
long double copysign(long double x, long double y);
float copysignf(float x, float y);
long double copysignl(long double x, long double y);
double nan(const char* tagp); //生成NaN(非数),tagp为实现相关标记字符串
float nanf(const char* tagp);
long double nanl(const char* tagp);
float nextafter(float x, float y); //获取x朝y方向的下一个可表示浮点值
double nextafter(double x, double y);
long double nextafter(long double x, long double y);
float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);
float nexttoward(float x, long double y); //类似nextafter,目标y固定为long double精度
double nexttoward(double x, long double y);
long double nexttoward(long double x, long double y);
float nexttowardf(float x, long double y);
long double nexttowardl(long double x, long double y);
float fdim(float x, float y); //正差:max(x-y,0); x<y返回0
double fdim(double x, double y);
long double fdim(long double x, long double y);
float fdimf(float x, float y);
long double fdiml(long double x, long double y);
float fmax(float x, float y); //取两个浮点最大值
double fmax(double x, double y);
long double fmax(long double x, long double y);
float fmaxf(float x, float y);
long double fmaxl(long double x, long double y);
float fmin(float x, float y); //取两个浮点最小值
double fmin(double x, double y);
long double fmin(long double x, long double y);
float fminf(float x, float y);
long double fminl(long double x, long double y);
float fma(float x, float y, float z); //乘加:x*y+z,硬件 fused multiply-add,精度更高
double fma(double x, double y, double z);
long double fma(long double x, long double y, long double z);
float fmaf(float x, float y, float z);
long double fmal(long double x, long double y, long double z);
6 分类/比较函数
💡要点:普通 > < 遇到 NaN 会产生浮点异常;这一组宏函数是安全浮点比较,不会抛异常。
cpp
int fpclassify(float x); //对浮点数分类,返回值:FP_INFINITE/FP_NAN/FP_NORMAL/FP_SUBNORMAL/FP_ZERO
int fpclassify(double x);
int fpclassify(long double x);
int isfinite(float x); //判断是否为有限值(不是无穷、不是NaN)
int isfinite(double x);
int isfinite(long double x);
int isinf(float x); //判断是否为无穷(±inf)
int isinf(double x);
int isinf(long double x);
int isnan(float x); //判断是否为NaN(非数)
int isnan(double x);
int isnan(long double x);
int isnormal(float x); //判断是否是正规浮点数(非零、非次正规、非无穷、非NaN)
int isnormal(double x);
int isnormal(long double x);
int signbit(float x); //检测符号位;负数返回非0,正数/0返回0
int signbit(double x);
int signbit(long double x);
int isgreater(float x, float y); //x>y,规避NaN带来的异常
int isgreater(double x, double y);
int isgreater(long double x, long double y);
int isgreaterequal(float x, float y); //x≥y
int isgreaterequal(double x, double y);
int isgreaterequal(long double x, long double y);
int isless(float x, float y); //x<y
int isless(double x, double y);
int isless(long double x, long double y);
int islessequal(float x, float y); //x≤y
int islessequal(double x, double y);
int islessequal(long double x, long double y);
int islessgreater(float x, float y); //x!=y,等价于不相等
int islessgreater(double x, double y);
int islessgreater(long double x, long double y);
int isunordered(float x, float y); //x、y任意一个是NaN,则二者不可比较,返回真
int isunordered(double x, double y);
int isunordered(long double x, long double y);
7 数学特殊函数
7.1 连带拉盖尔多项式
💡备注:属于 C++17 新增特殊数学函数,多用于量子力学(氢原子径向方程)。
cpp
/*
n:多项式阶数,无符号整数
m:连带阶数,无符号整数
x:求值点
*/
double assoc_laguerre(unsigned n, unsigned m, double x);
float assoc_laguerref(unsigned n, unsigned m, float x);
long double assoc_laguerrel(unsigned n, unsigned m, long double x);
7.2 连带勒让德函数
💡备注:C++17 新增特殊函数,常用于球谐函数、电磁学、量子力学、球坐标下偏微分方程。
cpp
/*
l:阶数,无符号整数
m:次阶,无符号整数
x:求值点,定义域一般[-1,1]
*/
double assoc_legendre(unsigned l, unsigned m, double x);
float assoc_legendref(unsigned l, unsigned m, float x);
long double assoc_legendrel(unsigned l, unsigned m, long double x);
7.3 β函数
💡备注:C++17 新增特殊函数,常用于概率论、积分计算。
cpp
/*
定义域:x>0, y>0
*/
double beta(double x, double y);
float betaf(float x, float y);
long double betal(long double x, long double y);
7.4 第一类完全椭圆积分
💡备注:C++17 新增特殊函数,常用于力学、摆运动、电磁、几何计算。
cpp
/*
参数k为模数,范围|k|≤1
*/
double comp_ellint_1(double k);
float comp_ellint_1f(float k);
long double comp_ellint_1l(long double k);
7.5 第二类完全椭圆积分
💡备注:C++17 新增特殊函数,典型用途:椭圆周长计算、刚体力学。
cpp
/*
参数k为模数,范围|k|≤1
*/
double comp_ellint_2(double k);
float comp_ellint_2f(float k);
long double comp_ellint_2l(long double k);
7.6 第三类完全椭圆积分
💡备注:C++17 新增特殊函数,用于磁场、引力场、轨道力学等复杂场计算。
cpp
/*
k:模数,|k|≤1
v:特征参数
*/
double comp_ellint_3(double k, double nu);
float comp_ellint_3f(float k, float nu);
long double comp_ellint_3l(long double k, long double nu);
7.7 第一类修正圆柱贝塞尔函数
💡备注:C++17 新增特殊函数,常用于热传导、电磁扩散、柱坐标系稳态问题。
cpp
/*
v:阶数
x:求值点,一般x≥0
*/
double cyl_bessel_i(double nu, double x);
float cyl_bessel_if(float nu, float x);
long double cyl_bessel_il(long double nu, long double x);
7.8 第一类圆柱贝塞尔函数
💡备注:C++17 新增特殊函数,应用:声波、电磁波、振动、圆膜、光学衍射。
cpp
/*
v:阶数
x:求值点
*/
double cyl_bessel_j(double nu, double x);
float cyl_bessel_jf(float nu, float x);
long double cyl_bessel_jl(long double nu, long double x);
7.9 第二类修正圆柱贝塞尔函数(不规则修正贝塞尔)
💡备注:C++17 新增特殊函数,常用于辐射、势场、衰减型柱对称问题。
cpp
/*
v:阶数
x:求值点,x>0
*/
double cyl_bessel_k(double nu, double x);
float cyl_bessel_kf(float nu, float x);
long double cyl_bessel_kl(long double nu, long double x);
7.10 圆柱诺伊曼函数(第二类圆柱贝塞尔函数)
💡备注:C++17 新增特殊函数,多用于散射、外域波动、声学/电磁辐射边界问题。
cpp
/*
v:阶数
x:求值点,x>0
*/
double cyl_neumann(double nu, double x);
float cyl_neumannf(float nu, float x);
long double cyl_neumannl(long double nu, long double x);
7.11 第一类不完全椭圆积分
💡备注:C++17 新增特殊函数,当振幅角等于 π/2 时退化为第一类完全椭圆积分 comp_ellint_1,常用于单摆、轨道、非线性力学。
cpp
/*
k:模数,|k|≤1
φ:振幅角
*/
double ellint_1(double k, double phi);
float ellint_1f(float k, float phi);
long double ellint_1l(long double k, long double phi);
7.12 第二类不完全椭圆积分
💡备注:C++17 新增特殊数学函数。
cpp
/*
k:模数
phi:振幅角
*/
double ellint_2(double k, double phi);
float ellint_2f(float k, float phi);
long double ellint_2l(long double k, long double phi);
7.13 第三类不完全椭圆积分
💡备注:C++17 新增特殊数学函数。
cpp
/*
k:模数
nu:特征参数
phi:振幅角
*/
double ellint_3(double k, double nu, double phi);
float ellint_3f(float k, float nu, float phi);
long double ellint_3l(long double k, long double nu, long double phi);
7.14 指数积分
💡备注:C++17 新增特殊数学函数。
cpp
/*
x:输入自变量
*/
double expint(double x);
float expintf(float x);
long double expintl(long double x);
7.15 埃尔米特多项式
💡备注:C++17 新增特殊数学函数。
cpp
/*
n:多项式阶数(非负无符号整数)
x:自变量
*/
double hermite(unsigned n, double x);
float hermitef(unsigned n, float x);
long double hermitel(unsigned n, long double x);
7.16 拉盖尔多项式
💡备注:C++17 新增特殊数学函数。
cpp
/*
n:多项式阶数(非负无符号整数)
x:自变量
*/
double laguerre(unsigned n, double x);
float laguerref(unsigned n, float x);
long double laguerrel(unsigned n, long double x);
7.17 勒让德多项式
💡备注:C++17 新增特殊数学函数。
cpp
/*
l:多项式阶数(非负无符号整数)
x:自变量,定义域一般为[-1,1]
*/
double legendre(unsigned l, double x);
float legendref(unsigned l, float x);
long double legendrel(unsigned l, long double x);
7.18 黎曼ζ函数
💡备注:C++17 新增特殊数学函数。
cpp
/*
x:输入自变量
*/
double riemann_zeta(double x);
float riemann_zetaf(float x);
long double riemann_zetal(long double x);
7.19 第一类球贝塞尔函数
💡备注:C++17 新增特殊数学函数。
cpp
/*
n:阶数(非负无符号整数)
x:自变量
*/
double sph_bessel(unsigned n, double x);
float sph_besself(unsigned n, float x);
long double sph_bessell(unsigned n, long double x);
7.20 球连带勒让德函数
💡备注:C++17 新增特殊数学函数。
cpp
/*
l:角量子数(非负无符号整数)
m:磁量子数,满足 0≤m≤l
theta:极角(弧度)
*/
double sph_legendre(unsigned l, unsigned m, double theta);
float sph_legendref(unsigned l, unsigned m, float theta);
long double sph_legendrel(unsigned l, unsigned m, long double theta);
7.21 第二类球贝塞尔函数(球诺伊曼函数)
💡备注:C++17 新增特殊数学函数。
cpp
/*
n:阶数(非负无符号整数)
x:自变量
*/
double sph_neumann(unsigned n, double x);
float sph_neumannf(unsigned n, float x);
long double sph_neumannl(unsigned n, long double x);