名稱空間
變體
操作

std::lerp

來自 cppreference.com
< cpp‎ | numeric
定義於標頭檔案 <cmath>
(1)
constexpr float       lerp( float a, float b, float t ) noexcept;

constexpr double      lerp( double a, double b, double t ) noexcept;
constexpr long double lerp( long double a, long double b,

                            long double t ) noexcept;
(C++20 起)
(直至 C++23)
constexpr /* floating-point-type */

    lerp( /* floating-point-type */ a,
          /* floating-point-type */ b,

          /* floating-point-type */ t ) noexcept;
(C++23 起)
定義於標頭檔案 <cmath>
template< class Arithmetic1, class Arithmetic2, class Arithmetic3 >

constexpr /* common-floating-point-type */

    lerp( Arithmetic1 a, Arithmetic2 b, Arithmetic3 t ) noexcept;
(A) (C++20 起)
1) 計算 ab 之間的線性插值,如果引數 t[01) 內(否則為線性外推),即 a+t(b−a) 的結果,並考慮浮點計算不精確性。 庫為所有 cv 非限定浮點型別提供了過載,作為引數 abt 的型別。(C++23 起)
A) 為所有其他算術型別組合提供了附加過載。

目錄

[編輯] 引數

a, b, t - 浮點數或整數值

[編輯] 返回值

a + t(b − a)

std::isfinite(a) && std::isfinite(b)true 時,保證以下屬性

  • 如果 t == 0,結果等於 a
  • 如果 t == 1,結果等於 b
  • 如果 t >= 0 && t <= 1,結果是有限的。
  • 如果 std::isfinite(t) && a == b,結果等於 a
  • 如果 std::isfinite(t) || (b - a != 0 && std::isinf(t)),結果不是 NaN

CMP(x, y)1 (如果 x > y)、-1 (如果 x < y),否則為 0。對於任意 t1t2,以下各項的乘積

  • CMP(std::lerp(a, b, t2), std::lerp(a, b, t1)),
  • CMP(t2, t1),以及
  • CMP(b, a)

為非負。(即,std::lerp 是單調的。)

[編輯] 注意

附加過載不必完全按照 (A) 提供。它們只需足以確保對於它們的第一個引數 num1、第二個引數 num2 和第三個引數 num3

  • 如果 num1num2num3 的型別是 long double,則 std::lerp(num1, num2, num3) 的效果與 std::lerp(static_cast<long double>(num1),
              static_cast<long double>(num2),
              static_cast<long double>(num3))
    相同。
  • 否則,如果 num1num2 和/或 num3 的型別是 double 或整數型別,則 std::lerp(num1, num2, num3) 的效果與 std::lerp(static_cast<double>(num1),
              static_cast<double>(num2),
              static_cast<double>(num3))
    相同。
  • 否則,如果 num1num2num3 的型別是 float,則 std::lerp(num1, num2, num3) 的效果與 std::lerp(static_cast<float>(num1),
              static_cast<float>(num2),
              static_cast<float>(num3))
    相同。
(直至 C++23)

如果 num1num2num3 具有算術型別,則 std::lerp(num1, num2, num3) 的效果與 std::lerp(static_cast</*common-floating-point-type*/>(num1),
          static_cast</*common-floating-point-type*/>(num2),
          static_cast</*common-floating-point-type*/>(num3))
相同,其中 /*common-floating-point-type*/ 是具有最高浮點轉換等級和最高浮點轉換次等級的浮點型別,該浮點型別是在 num1num2num3 的型別中,整數型別引數被視為與 double 具有相同的浮點轉換等級。

如果不存在具有最高等級和次等級的浮點型別,則過載決議不會從提供的過載中產生可用的候選函式。

(C++23 起)
特性測試 標準 特性
__cpp_lib_interpolate 201902L (C++20) std::lerp, std::midpoint

[編輯] 示例

#include <cassert>
#include <cmath>
#include <iostream>
 
float naive_lerp(float a, float b, float t)
{
    return a + t * (b - a);
}
 
int main()
{
    std::cout << std::boolalpha;
 
    const float a = 1e8f, b = 1.0f;
    const float midpoint = std::lerp(a, b, 0.5f);
 
    std::cout << "a = " << a << ", " << "b = " << b << '\n'
              << "midpoint = " << midpoint << '\n';
 
    std::cout << "std::lerp is exact: "
              << (a == std::lerp(a, b, 0.0f)) << ' '
              << (b == std::lerp(a, b, 1.0f)) << '\n';
 
    std::cout << "naive_lerp is exact: "
              << (a == naive_lerp(a, b, 0.0f)) << ' '
              << (b == naive_lerp(a, b, 1.0f)) << '\n';
 
    std::cout << "std::lerp(a, b, 1.0f) = " << std::lerp(a, b, 1.0f) << '\n'
              << "naive_lerp(a, b, 1.0f) = " << naive_lerp(a, b, 1.0f) << '\n';
 
    assert(not std::isnan(std::lerp(a, b, INFINITY))); // lerp here can be -inf
 
    std::cout << "Extrapolation demo, given std::lerp(5, 10, t):\n";
    for (auto t{-2.0}; t <= 2.0; t += 0.5)
        std::cout << std::lerp(5.0, 10.0, t) << ' ';
    std::cout << '\n';
}

可能的輸出

a = 1e+08, b = 1
midpoint = 5e+07
std::lerp is exact?: true true
naive_lerp is exact?: true false
std::lerp(a, b, 1.0f) = 1
naive_lerp(a, b, 1.0f) = 0
Extrapolation demo, given std::lerp(5, 10, t):
-5 -2.5 0 2.5 5 7.5 10 12.5 15

[編輯] 參閱

(C++20)
兩個數或指標之間的中點
(函式模板) [編輯]