名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
expm1
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <cmath>
(1)
float       expm1 ( float num );

double      expm1 ( double num );

long double expm1 ( long double num );
(直至 C++23)
/*浮點型別*/
            expm1 ( /*浮點型別*/ 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< /*數學浮點型別*/ V >

constexpr /*推導 SIMD 型別*/<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-unqualified 浮點型別提供了 std::expm1 的過載作為引數的型別。(C++23 起)
S) SIMD 過載對 v_num 執行逐元素的 std::expm1
(有關它們的定義,請參閱 math-floating-pointdeduced-simd-t。)
(C++26 起)
A) 為所有整數型別提供了額外的過載,它們被視為 double
(C++11 起)

目錄

[edit] 引數

num - 浮點值或整數值

[edit] 返回值

如果未發生錯誤,則返回 enum
-1

如果發生因溢位導致的範圍錯誤,返回 +HUGE_VAL+HUGE_VALF+HUGE_VALL

如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。

[edit] 錯誤處理

錯誤按 math_errhandling 指定的方式報告。

如果實現支援 IEEE 浮點運算 (IEC 60559),

  • 如果引數是 ±0,則原樣返回。
  • 如果引數是 -∞,則返回 -1。
  • 如果引數是 +∞,則返回 +∞。
  • 如果引數為 NaN,則返回 NaN。

[edit] 備註

函式 std::expm1std::log1p 在金融計算中很有用,例如,在計算小日利率時:(1+x)n
-1
可以表示為 std::expm1(n * std::log1p(x))。這些函式還簡化了精確反雙曲函式的編寫。

對於 IEEE 相容的 double 型別,如果 709.8 < num,則保證溢位。

額外的過載不要求完全按照 (A) 提供。它們只需要足以確保對於整數型別的引數 numstd::expm1(num) 具有與 std::expm1(static_cast<double>(num)) 相同的效果。

[edit] 示例

#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

[edit] 參閱

(C++11)(C++11)
返回 e 的給定冪(ex
(函式) [編輯]
(C++11)(C++11)(C++11)
返回 2 的給定冪(2x
(函式) [編輯]
(C++11)(C++11)(C++11)
給定數加 1 的自然對數(以 e 為底)(ln(1+x)
(函式) [編輯]
C 文件 for expm1