C++11标准模板(STL)- 常用数学函数 - 计算e的给定幂 (ex)(std::exp, std::expf, std::expl)
常用数学函数
计算e的给定幂 (ex)
std::exp, 
std::expf, 
std::expl| 定义于头文件  | ||
| float expf( float arg ); | (1) | (C99 起) | 
| double exp( double arg ); | (2) | |
| long double expl( long double arg ); | (3) | (C99 起) | 
| 定义于头文件  | ||
| #define exp( arg ) | (4) | (C99 起) | 
1-3) 计算 e (欧拉数, 2.7182818 )的 arg 次幂。
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 <iostream>
#include <cstdlib>
#include <typeinfo>
#include <cinttypes>
#include <cmath>int main()
{//计算浮点值 arg 的绝对值。const float fNumber = 0.1314;std::cout << "typeid(float).name():   " << typeid(float).name() << std::endl;for (int i = 0; i < 18; i += 3){std::cout << "std::exp(" << fNumber + i << "):   "<< std::exp(fNumber + i) << std::endl;}std::cout << std::endl;for (int i = 0; i < 18; i += 3){std::cout << "std::exp(" << -fNumber - i << "):   "<< std::exp(-fNumber - i) << std::endl;}std::cout << std::endl;const double dNumber = 0.00001314;std::cout << "typeid(double).name():   " << typeid(double).name() << std::endl;for (int i = 0; i < 18; i += 3){std::cout << "std::exp(" << dNumber + i << "):   "<< std::exp(dNumber + i) << std::endl;}std::cout << std::endl;for (int i = 0; i < 18; i += 3){std::cout << "std::exp(" << -dNumber - i << "):   "<< std::exp(-dNumber - i) << std::endl;}std::cout << std::endl;return 0;
}
输出
typeid(float).name():   f
std::exp(0.1314):   1.14042
std::exp(3.1314):   22.906
std::exp(6.1314):   460.08
std::exp(9.1314):   9240.95
std::exp(12.1314):   185609
std::exp(15.1314):   3.72807e+006std::exp(-0.1314):   0.876867
std::exp(-3.1314):   0.0436566
std::exp(-6.1314):   0.00217354
std::exp(-9.1314):   0.000108214
std::exp(-12.1314):   5.38766e-006
std::exp(-15.1314):   2.68236e-007typeid(double).name():   d
std::exp(1.314e-005):   1.00001
std::exp(3.00001):   20.0858
std::exp(6.00001):   403.434
std::exp(9.00001):   8103.19
std::exp(12):   162757
std::exp(15):   3.26906e+006std::exp(-1.314e-005):   0.999987
std::exp(-3.00001):   0.0497864
std::exp(-6.00001):   0.00247872
std::exp(-9.00001):   0.000123408
std::exp(-12):   6.14413e-006
std::exp(-15):   3.05898e-007