非常教程

C参考手册

数值 | Numerics

expm1f

在头文件<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)减去1.0。 如果arg接近零,则此函数比表达式std :: exp(arg)-1.0更准确。

4)类型 - 通用宏:如果arg的类型是long double,则调用expm1l。 否则,如果arg具有整数类型或类型double,则调用expm1。 否则,调用expm1f。

参数

arg

-

floating point value

返回值

如果没有错误发生 earg

则返回-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的类型,如果709.8 <arg,则保证溢出。

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <errno.h>
#include <fenv.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

参考

  • 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函数(p:223-224)
    • 7.22类型通用数学<tgmath.h>(p:335-337)
    • F.9.3.3 expm1函数(p:458)

扩展内容

Expexpfexpl(C99)(C99)

计算e给定的倍率(ex)(函数)

exp2exp2fexp2l(C99)(C99)(C99)

计算2提高到给定的倍率(2x)(功能)

log1plog1pflog1pl(C99)(C99)(C99)

计算1加上给定数(ln(1 + x))(函数)的自然(基e)对数,

| expm1的C ++文档 |

C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。