首页
学习
活动
专区
圈层
工具
发布

exp

作者头像
用户7886150
修改于 2021-02-09 16:44:52
修改于 2021-02-09 16:44:52
1.3K0
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: C++ feclearexcept()

exp

C99

double exp(double x);

float expf(float x);

long double expl(long double x);

C++11

double exp (double x);

float exp (float x);

long double exp (long double x);

double exp (T x); // additional overloads for integral types

1. Compute exponential function        Returns the base-e exponential function of x, which is e raised to the power x: e^x.C99        Header <tgmath.h> provides a type-generic macro version of this function.C++98        This function is overloaded in <complex> and <valarray> (see complex exp and valarray exp).C++11        Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations.        This function is also overloaded in <complex> and <valarray> (see complex exp and valarray exp).1.1 Parametersx        Value of the exponent.1.2 Return Value        Exponential value of x.        If the magnitude of the result is too large to be represented by a value of the return type, the function returns HUGE_VAL (or HUGE_VALF or HUGE_VALL) with the proper sign, and an overflow range error occurs:C90 (C++98)        If an overflow range error occurs, the global variable errno is set to ERANGE.C99 (C+11)        If an overflow range error occurs:        - And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.        - And math_errhandling has MATH_ERREXCEPT set: FE_OVERFLOW is raised.2. Example2.1 example

/*

 ============================================================================

 Name        : exp_example_1.c

 Author      : foreverstrong

 Version     :

 Copyright   : Your copyright notice

 Description : exp, expf, expl in C, Ansi-style

 ============================================================================

 */

/* exp example */

#include <stdio.h>      /* printf */

#include <math.h>       /* exp */

int main()

{

    double param, result;

    param = 5.0;

    result = exp(param);

    printf("The exponential value of %f is %f.\n", param, result);

    return 0;

}

Output:

The exponential value of 5.000000 is 148.413159.

3. exp, expf, expl        Defined in header <math.h>

float expf(float arg); (1) (since C99)

double exp(double arg); (2) 

long double expl(long double arg); (3) (since C99)       

 Defined in header <tgmath.h>

#define exp(arg) (4) (since C99)

        1-3) Computes the e (Euler's number, 2.7182818) raised to the given power arg.        4) Type-generic macro: If arg has type long double, expl is called. Otherwise, if arg has integer type or the type double, exp is called. Otherwise, expf is called. If arg is complex or imaginary, then the macro invokes the corresponding complex function (cexpf, cexp, cexpl).3.1 Parameters        arg - floating point value3.2 Return value        If no errors occur, the base-e exponential of arg (e^arg) is returned.        若不出现错误,则返回 arg 的底 e 指数 (e^arg)        If a range error due to overflow occurs, +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL is returned.        If a range error occurs due to underflow, the correct result (after rounding) is returned.        若出现下溢所致的值域错误,则返回 (舍入后的) 正确结果。3.3 Error handling       Errors are reported as specified in math_errhandling.       If the implementation supports IEEE floating-point arithmetic (IEC 60559),        If the argument is ±0, 1 is returned        If the argument is -∞, +0 is returned        If the argument is +∞, +∞ is returned        If the argument is NaN, NaN is returned3.4 Notes        For IEEE-compatible type double, overflow is guaranteed if 709.8 < arg, and underflow is guaranteed if arg < -708.4.4. Example4.1 example

/*

 ============================================================================

 Name        : exp_example_2.c

 Author      : foreverstrong

 Version     :

 Copyright   : Your copyright notice

 Description : exp, expf, expl in C, Ansi-style

 ============================================================================

 */

#include <stdio.h>

#include <math.h>

#include <float.h>

#include <errno.h>

#include <fenv.h>

#pragma STDC FENV_ACCESS ON

int main(void)

{

    printf("exp(1) = %f\n", exp(1));

    printf("FV of $100, continuously compounded at 3%% for 1 year = %f\n",

            100 * exp(0.03));

    // special values

    printf("exp(-0) = %f\n", exp(-0.0));

    printf("exp(-Inf) = %f\n", exp(-INFINITY));

    // error handling

    errno = 0;

    feclearexcept(FE_ALL_EXCEPT);

    printf("exp(710) = %f\n", exp(710));

    if (errno == ERANGE)

    {

        perror("    errno == ERANGE");

    }

    if (fetestexcept(FE_OVERFLOW))

    {

        puts("    FE_OVERFLOW raised");

    }

    return 0;

}

Compiler messages:

15:50:07 **** Build of configuration Debug for project exp_example ****

make all 

Building file: ../src/exp_example_1.c

Invoking: GCC C Compiler

gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/exp_example_1.d" -MT"src/exp_example_1.o" -o "src/exp_example_1.o" "../src/exp_example_1.c"

Finished building: ../src/exp_example_1.c

Building file: ../src/exp_example_2.c

Invoking: GCC C Compiler

gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/exp_example_2.d" -MT"src/exp_example_2.o" -o "src/exp_example_2.o" "../src/exp_example_2.c"

../src/exp_example_2.c:17:0: warning: ignoring #pragma STDC FENV_ACCESS [-Wunknown-pragmas]

 #pragma STDC FENV_ACCESS ON

 ^

Finished building: ../src/exp_example_2.c

Building target: exp_example

Invoking: GCC C Linker

gcc  -o "exp_example"  ./src/exp_example_1.o ./src/exp_example_2.o   -lm

Finished building target: exp_example

15:50:08 Build Finished (took 320ms)

Possible output:

exp(1) = 2.718282

FV of $100, continuously compounded at 3% for 1 year = 103.045453

exp(-0) = 1.000000

exp(-Inf) = 0.000000

exp(710) = inf

    FE_OVERFLOW raised

    errno == ERANGE: Numerical result out of range

Referenceshttp://zh.cppreference.com/w/c/numeric/math/exphttp://en.cppreference.com/w/c/numeric/math/exphttp://www.cplusplus.com/reference/cmath/exp/

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档