expm1, expm1f, expm1l
来自cppreference.com
| 在标头 <math.h> 定义
|
||
| |
(1) | (C99 起) |
| |
(2) | (C99 起) |
| |
(3) | (C99 起) |
| 在标头 <tgmath.h> 定义
|
||
| |
(4) | (C99 起) |
1-3) 计算 e(欧拉数,
2.7182818)的给定 arg 次幂减 1.0。若 arg 接近零,则此函数比表达式 exp(arg)-1.0 更精确。4) 泛型宏:若
arg 拥有 long double 类型,则调用 expm1l。否则,若 arg 拥有整数类型或 double 类型,则调用 expm1。否则调用 expm1f。参数
| arg | - | 浮点数 |
返回值
若不出现错误则返回 earg
-1。
若出现上溢所致的值域错误,则返回 HUGE_VAL、+HUGE_VALF 或 +HUGE_VALL。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点数算术(IEC 60559),则
- 若参数为 ±0,则返回不修改的参数
- 若参数为 -∞,则返回 -1
- 若参数为 +∞,则返回 +∞
- 若参数为 NaN,则返回 NaN
注意
函数 expm1 和 log1p 对于金融计算有用:例如在计算小的日利率时:(1+x)n
-1 能表示为 expm1(n * log1p(x))。这些函数亦简化书写精确的反双曲函数。
对于 IEEE 兼容的 double 类型,若 709.8 < arg 则保证上溢。
注意
运行此代码
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
printf("expm1(1) = %f\n", expm1(1));
printf("Interest earned in 2 days on $100, compounded daily at 1%%\n"
" on a 30/360 calendar = %f\n",
100*expm1(2*log1p(0.01/360)));
printf("exp(1e-16)-1 = %g, but expm1(1e-16) = %g\n",
exp(1e-16)-1, expm1(1e-16));
// 特殊值
printf("expm1(-0) = %f\n", expm1(-0.0));
printf("expm1(-Inf) = %f\n", expm1(-INFINITY));
// 错误处理
errno = 0; feclearexcept(FE_ALL_EXCEPT);
printf("expm1(710) = %f\n", expm1(710));
if(errno == ERANGE)
perror(" errno == ERANGE");
if(fetestexcept(FE_OVERFLOW))
puts(" FE_OVERFLOW raised");
}
可能的输出:
expm1(1) = 1.718282
Interest earned in 2 days on $100, compounded daily at 1%
on a 30/360 calendar = 0.005556
exp(1e-16)-1 = 0, but expm1(1e-16) = 1e-16
expm1(-0) = -0.000000
expm1(-Inf) = -1.000000
expm1(710) = inf
errno == ERANGE: Result too large
FE_OVERFLOW raised
引用
- C23 标准(ISO/IEC 9899:2024):
- 7.12.6.3 The expm1 functions (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.3.3 The expm1 functions (第 TBD 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.6.3 The expm1 functions (第 177 页)
- 7.25 Type-generic math <tgmath.h> (第 272-273 页)
- F.10.3.3 The expm1 functions (第 379 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.6.3 The expm1 functions (第 243 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.3.3 The expm1 functions (第 521 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.6.3 The expm1 functions (第 223-224 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.3.3 The expm1 functions (第 458 页)
参阅
(C99)(C99) |
计算 e 的给定次幂(ex) (函数) |
(C99)(C99)(C99) |
计算 2 的给定次幂(2x) (函数) |
(C99)(C99)(C99) |
计算给定数加 1 的自然对数(底为 e)(ln(1+x)) (函数) |
expm1 的 C++ 文档
| |