Namespaces
Variants

expm1, expm1f, expm1l

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
(C23)
(C99)
expm1
(C99)
(C23)
(C23)

Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
float expm1f ( float arg ) ;
(1) (C99 起)
double expm1 ( double arg ) ;
(2) (C99 起)
long double expm1l ( long double arg ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define expm1( arg )
(4) (C99 起)
1-3) 计算 e (欧拉数, 2.7182818 )的给定次幂 arg 减去 1.0 。若 arg 接近零,此函数比表达式 exp ( arg ) - 1.0 具有更高精度。
4) 类型泛型宏:若 arg 具有类型 long double ,则调用 expm1l 。否则,若 arg 具有整数类型或类型 double ,则调用 expm1 。否则调用 expm1f

目录

参数

arg - 浮点数值

返回值

如果未发生错误,将返回 e arg
-1

如果发生因上溢导致的范围错误,则返回 +HUGE_VAL +HUGE_VALF +HUGE_VALL

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规定。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为 ±0,则不作修改直接返回
  • 若参数为 -∞,则返回 -1
  • 若参数为 +∞,则返回 +∞
  • 若参数为 NaN,则返回 NaN

注释

函数 expm1 log1p 在金融计算中非常实用,例如在计算小额日利率时: (1+x) n
-1
可表示为 expm1 ( n * log1p ( x ) ) 。这些函数还能简化精确反双曲函数的编写。

对于 IEEE 兼容类型 double ,当 709.8 < arg 时保证会发生溢出。

示例

#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("expm1(1) = %f\n", expm1(1));
    printf("Interest earned in 2 days on $100, compounded daily at 1%%\n"
           " on a 30/360 calendar = %f\n",
           100*expm1(2*log1p(0.01/360)));
    printf("exp(1e-16)-1 = %g, but expm1(1e-16) = %g\n",
           exp(1e-16)-1, expm1(1e-16));
    // special values
    printf("expm1(-0) = %f\n", expm1(-0.0));
    printf("expm1(-Inf) = %f\n", expm1(-INFINITY));
    //error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("expm1(710) = %f\n", expm1(710));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

可能的输出:

expm1(1) = 1.718282
Interest earned in 2 days on $100, compounded daily at 1%
 on a 30/360 calendar = 0.005556
exp(1e-16)-1 = 0, but expm1(1e-16) = 1e-16
expm1(-0) = -0.000000
expm1(-Inf) = -1.000000
expm1(710) = inf
    errno == ERANGE: Result too large
    FE_OVERFLOW raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.6.3 expm1 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.3.3 expm1 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.6.3 expm1 函数 (p: 177)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.3.3 expm1 函数 (p: 379)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.6.3 expm1 函数 (p: 243)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.3.3 expm1 函数 (p: 521)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.6.3 expm1函数(页码:223-224)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.3.3 expm1函数(页码:458)

参见

(C99) (C99)
计算 e 的给定次幂 ( e x )
(函数)
(C99) (C99) (C99)
计算 2 的给定次幂 ( 2 x )
(函数)
(C99) (C99) (C99)
计算 1 加给定数值的自然(以 e 为底)对数 ( ln(1+x) )
(函数)
C++ 文档 关于 expm1