std::expm1, std::expm1f, std::expm1l
定义于头文件 <cmath>
|
||
float expm1 ( float arg ); float expm1f( float arg ); |
(1) | (C++11 起) |
double expm1 ( double arg ); |
(2) | (C++11 起) |
long double expm1 ( long double arg ); long double expm1l( long double arg ); |
(3) | (C++11 起) |
double expm1 ( IntegralType arg ); |
(4) | (C++11 起) |
参数
arg | - | 浮点或整数类型值 |
返回值
若不出现错误则返回 earg
-1 。
若出现上溢所致的值域错误,则返回 +HUGE_VAL
、 +HUGE_VALF
或 +HUGE_VALL
。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- 若参数为 ±0 ,则返回不修改的参数
- 若参数为 -∞ ,则返回 -1
- 若参数为 +∞ ,则返回 +∞
- 若参数为 NaN ,则返回 NaN
注意
函数 std::expm1
和 std::log1p 对于金融计算有用:例如在计算小的日利率时: (1+x)n
-1 能表示为 std::expm1(n * std::log1p(x)) 。这些函数亦简化书写精确的反双曲函数。
对于 IEEE 兼容的 double 类型,若 709.8 < arg 则保证上溢。
示例
运行此代码
#include <iostream> #include <cmath> #include <cerrno> #include <cstring> #include <cfenv> #pragma STDC FENV_ACCESS ON int main() { std::cout << "expm1(1) = " << std::expm1(1) << '\n' << "Interest earned in 2 days on on $100, compounded daily at 1%\n" << " on a 30/360 calendar = " << 100*std::expm1(2*std::log1p(0.01/360)) << '\n' << "exp(1e-16)-1 = " << std::exp(1e-16)-1 << ", but expm1(1e-16) = " << std::expm1(1e-16) << '\n'; // 特殊值 std::cout << "expm1(-0) = " << std::expm1(-0.0) << '\n' << "expm1(-Inf) = " << std::expm1(-INFINITY) << '\n'; // 错误处理 errno = 0; std::feclearexcept(FE_ALL_EXCEPT); std::cout << "expm1(710) = " << std::expm1(710) << '\n'; if (errno == ERANGE) std::cout << " errno == ERANGE: " << std::strerror(errno) << '\n'; if (std::fetestexcept(FE_OVERFLOW)) std::cout << " FE_OVERFLOW raised\n"; }
可能的输出:
expm1(1) = 1.71828 Interest earned in 2 days on on $100, compounded daily at 1% on a 30/360 calendar = 0.00555563 exp(1e-16)-1 = 0 expm1(1e-16) = 1e-16 expm1(-0) = -0 expm1(-Inf) = -1 expm1(710) = inf errno == ERANGE: Result too large FE_OVERFLOW raised
参阅
(C++11)(C++11) |
返回 e 的给定次幂( ex ) (函数) |
(C++11)(C++11)(C++11) |
返回 2 的给定次幂( 2x ) (函数) |
(C++11)(C++11)(C++11) |
1 加上给定数值的自然(以 e 为底)对数( ln(1+x) ) (函数) |