Namespaces
Variants

std:: expm1, std:: expm1f, std:: expm1l

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float expm1 ( float num ) ;

double expm1 ( double num ) ;

long double expm1 ( long double num ) ;
(C++23 前)
/*floating-point-type*/
expm1 ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float expm1f ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double expm1l ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

expm1 ( const V & v_num ) ;
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double expm1 ( Integer num ) ;
(A) (C++26 起为 constexpr)
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 运算。
(定义参见 math-floating-point deduced-simd-t
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

如果未发生错误,将返回 e num
-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'
              << "以1%日利率按30/360日历计算,100美元在2天内获得的复利利息\n"
              << "    = "
              << 100 * std::expm1(2 * std::log1p(0.01 / 360)) << '\n'
              << "exp(1e-16)-1 = " << std::exp(1e-16) - 1
              << ", 但 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 异常\n";
}

可能的输出:

expm1(1) = 1.71828
以1%日利率按30/360日历计算,100美元在2天内获得的复利利息
    = 0.00555563
exp(1e-16)-1 = 0, 但 expm1(1e-16) = 1e-16
expm1(-0) = -0
expm1(-Inf) = -1
expm1(710) = inf
    errno == ERANGE: 结果过大
    触发 FE_OVERFLOW 异常

参见

(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