GSL 系列 1 — 数学函数

本文介绍了GSL库在C++中用于数学计算的一些关键函数,包括数学常量、无穷和非数的概念,以及初等函数、小整数幂、正负测试、奇偶数测试、最大最小函数和浮点数的近似比较。通过宏定义和内联函数,开发者可以方便地进行数值计算和比较操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件

gsl_math.h

数学常量

宏定义示例:

#ifndef M_E
#define M_E        2.71828182845904523536028747135      /* e */
#endif

无穷和非数

GSL_POSINF: 正无穷
GSL_NEGINF: 负无穷
GSL_NAN:非数

宏定义:

// gsl_nan.h
#ifdef INFINITY
# define GSL_POSINF INFINITY
# define GSL_NEGINF (-INFINITY)
#elif defined(HUGE_VAL)
# define GSL_POSINF HUGE_VAL
# define GSL_NEGINF (-HUGE_VAL)
#else
# define GSL_POSINF (gsl_posinf())
# define GSL_NEGINF (gsl_neginf())
#endif

#ifdef NAN
# define GSL_NAN NAN
#elif defined(INFINITY)
# define GSL_NAN (INFINITY/INFINITY)
#else
# define GSL_NAN (gsl_nan())
#endif

//corecrt_math.h
#ifndef _HUGE_ENUF
    #define _HUGE_ENUF  1e+300  // _HUGE_ENUF*_HUGE_ENUF must overflow
#endif

#define INFINITY   ((float)(_HUGE_ENUF * _HUGE_ENUF))
#define HUGE_VAL   ((double)INFINITY)

#define NAN        ((float)(INFINITY * 0.0F))

//gsl_sys.h
double gsl_nan (void);
double gsl_posinf (void);
double gsl_neginf (void);

判断函数:

//gsl_sys.h
int gsl_isnan (const double x); //非数返回 1
int gsl_isinf (const double x); //正无穷返回 1,负无穷返回 -1,其他返回 0
int gsl_finite (const double x); // 实数返回 1,无穷和非数返回 0

初等函数

// gsl_sys.h
double gsl_log1p(const double x); // log(1+x); x 小的时候较为精确
double gsl_expm1(const double x); // exp(x)-1; x 小的时候较为精确
double gsl_hypot(const double x, const double y); // sqrt(x^2 + y^2); 避免溢出
double gsl_hypot3(const double x, const double y, const double z); // sqrt(x^2+y^2+z^2); 避免溢出
double gsl_acosh(const double x); // arccosh(x) 
double gsl_asinh(const double x); // arcsinh(x)
double gsl_atanh(const double x); // arctanh(x)
double gsl_ldexp(const double x, const int e); // x*2^e;
double gsl_frexp(const double x, int * e); // x/2^e; x为0,f,e 都设为 0

小整数幂

// gsl_pow_int.h
INLINE_DECL double gsl_pow_2(const double x);
INLINE_DECL double gsl_pow_3(const double x);
INLINE_DECL double gsl_pow_4(const double x);
INLINE_DECL double gsl_pow_5(const double x);
INLINE_DECL double gsl_pow_6(const double x);
INLINE_DECL double gsl_pow_7(const double x);
INLINE_DECL double gsl_pow_8(const double x);
INLINE_DECL double gsl_pow_9(const double x);
/* HAVE_INLINE被定义时,以上函数使用内联 */

double gsl_pow_int(double x, int n);
double gsl_pow_uint(double x, unsigned int n);
/* 该函数还有另一个版本,会计算数值误差,gsl_sf_pow_int_e()

正负测试

GSL_SIGN(X): 大于等于 0 为 1,小于 0 为 -1

宏定义:

// gsl_math.h
#define GSL_SIGN(x)    ((x) >= 0.0 ? 1 : -1)

奇偶数测试

GSL_IS_ODD(n): 奇数返回 1,偶数返回 0
GSL_IS_EVEN(n): 偶数返回 1,奇数返回 0

宏定义:

// gsl_math.h
#define GSL_IS_ODD(n)  ((n) & 1)
#define GSL_IS_EVEN(n) (!(GSL_IS_ODD(n)))

最大最小函数

GSL_MAX(a,b): 返回 a, b 的最大值
GSL_MIN(a,b): 返回 a, b 的最小值

宏定义:

// gsl_minmax.h
#define GSL_MAX(a,b) ((a) > (b) ? (a) : (b))
#define GSL_MIN(a,b) ((a) < (b) ? (a) : (b))
// 以上两个宏,还有函数版本 gsl_max, gsl_min
double gsl_max (double a, double b);
double gsl_min (double a, double b);

内联函数版本(HAVE_INLINE 定义的前提下):

// gsl_minmax.h
INLINE_FUN int GSL_MAX_INT (int a, int b);
INLINE_FUN int GSL_MIN_INT (int a, int b);
INLINE_FUN double GSL_MAX_DBL (double a, double b);
INLINE_FUN double GSL_MIN_DBL (double a, double b);
INLINE_FUN long double GSL_MAX_LDBL (long double a, long double b);
INLINE_FUN long double GSL_MIN_LDBL (long double a, long double b);

// 内联版本实现示例
INLINE_FUN int
GSL_MAX_INT (int a, int b)
{
  return GSL_MAX (a, b);
}
//无内联版本
#define GSL_MAX_INT(a,b)   GSL_MAX(a,b)

浮点数的近似比较

int gsl_fcmp (const double x1, const double x2, const double epsilon);
/* 
判断 x, y 是否近似等于一个相对精度 epsilon, 
根据 frexp() 计算出一个间隔, 判断 x, y 是否在间隔之内,
在,即返回0,认为近似相等,否则,x < y 返回 -1,x > y 返回 + 1。
注意,比较的是相对精度,而不是两者间隔接近 0 的程度。
基于 fcmp 实现。
*/

参考

https://www.gnu.org/software/gsl/doc/html/math.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值