std::expm1, std::expm1f, std::expm1l
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float expm1 ( float num ); double expm1 ( double num ); |
(直到 C++23) | |
/*floating-point-type*/ expm1 ( /*floating-point-type*/ num ); |
(自 C++23 起) (constexpr 自 C++26 起) |
|
float expm1f( float num ); |
(2) | (自 C++11 起) (constexpr 自 C++26 起) |
long double expm1l( long double num ); |
(3) | (自 C++11 起) (constexpr 自 C++26 起) |
SIMD 重载 (自 C++26 起) |
||
定义于头文件 <simd> |
||
template< /*math-floating-point*/ V > constexpr /*deduced-simd-t*/<V> |
(S) | (自 C++26 起) |
附加重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double expm1 ( Integer num ); |
(A) | (constexpr 自 C++26 起) |
1-3) 计算 e (欧拉数, 2.7182818...) 的给定次幂 num,再减去 1.0。如果 num 接近于零,此函数比表达式 std::exp(num) - 1.0 更精确。 库为所有 cv-无限定浮点类型提供
std::expm1
的重载作为参数类型。(自 C++23 起)
S) SIMD 重载对 v_num 执行逐元素
std::expm1 。
|
(自 C++26 起) |
A) 为所有整数类型提供附加重载,它们被视为 double 类型。
|
(自 C++11 起) |
目录 |
[编辑] 参数
num | - | 浮点或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 enum
-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 < num,则保证溢出。
不需要完全按照 (A) 提供附加重载。它们只需要足以确保对于整数类型的参数 num,std::expm1(num) 具有与 std::expm1(static_cast<double>(num)) 相同的效果。
[编辑] 示例
运行此代码
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iostream> // #pragma STDC FENV_ACCESS ON int main() { std::cout << "expm1(1) = " << std::expm1(1) << '\n' << "Interest earned in 2 days 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'; // special values std::cout << "expm1(-0) = " << std::expm1(-0.0) << '\n' << "expm1(-Inf) = " << std::expm1(-INFINITY) << '\n'; // error handling 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 $100, compounded daily at 1% on a 30/360 calendar = 0.00555563 exp(1e-16)-1 = 0, but 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 的给定次幂 (e^x) (函数) |
(C++11)(C++11)(C++11) |
返回 2 的给定次幂 (2^x) (函数) |
(C++11)(C++11)(C++11) |
1 加上给定数的自然对数(以 e 为底)(ln(1+x)) (函数) |
C 文档 关于 expm1
|