exp, expf, expl
来自cppreference.com
| 在标头 <math.h> 定义
|
||
| |
(1) | (C99 起) |
| |
(2) | |
| |
(3) | (C99 起) |
| 在标头 <tgmath.h> 定义
|
||
| |
(4) | (C99 起) |
4) 泛型宏:若
arg 拥有 long double 类型,则调用 expl。否则,若 arg 拥有整数类型或 double 类型,则调用 exp。否则调用 expf。若 arg 为复数或虚数,则宏调用对应的复数函数(cexpf、cexp、cexpl)。参数
| arg | - | 浮点数 |
返回值
若不出现错误,则返回 arg 的底 e 指数(earg
)。
若出现上溢所致的值域错误,则返回 +HUGE_VAL、+HUGE_VALF 或 +HUGE_VALL。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点数算术(IEC 60559),则
- 若参数为 ±0,则返回 1
- 若参数为 -∞,则返回 +0
- 若参数为 +∞,则返回 +∞
- 若参数为 NaN,则返回 NaN
注意
对于 IEEE 兼容的 double 类型,若 709.8 < arg 则保证上溢,而若 arg < -708.4 则保证下溢。
示例
运行此代码
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
printf("exp(1) = %f\n", exp(1));
printf("FV of $100, continuously compounded at 3%% for 1 year = %f\n",
100*exp(0.03));
// 特殊值
printf("exp(-0) = %f\n", exp(-0.0));
printf("exp(-Inf) = %f\n", exp(-INFINITY));
// 错误处理
errno = 0; feclearexcept(FE_ALL_EXCEPT);
printf("exp(710) = %f\n", exp(710));
if (errno == ERANGE)
perror(" errno == ERANGE");
if (fetestexcept(FE_OVERFLOW))
puts(" FE_OVERFLOW raised");
}
可能的输出:
exp(1) = 2.718282
FV of $100, continuously compounded at 3% for 1 year = 103.045453
exp(-0) = 1.000000
exp(-Inf) = 0.000000
exp(710) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised
引用
- C23 标准(ISO/IEC 9899:2024):
- 7.12.6.1 The exp functions (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.3.1 The exp functions (第 TBD 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.6.1 The exp functions (第 175 页)
- 7.25 Type-generic math <tgmath.h> (第 272-273 页)
- F.10.3.1 The exp functions (第 379 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.6.1 The exp functions (第 242 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.3.1 The exp functions (第 520 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.6.1 The exp functions (第 223 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.3.1 The exp functions (第 458 页)
- C89/C90 标准(ISO/IEC 9899:1990):
- 4.5.4.1 The exp function
参阅
(C99)(C99)(C99) |
计算 2 的给定次幂(2x) (函数) |
(C99)(C99)(C99) |
计算 e 的给定次幂减一(ex-1) (函数) |
(C99)(C99) |
计算自然对数(底为 e)(ln(x)) (函数) |
(C99)(C99)(C99) |
计算复数的 e 底指数 (函数) |
exp 的 C++ 文档
| |