std::lerp
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| 在标头 <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) 如果形参
t 在 [0, 1) 之内,则计算 a 与 b 间参的线性内插(否则是线性外插),即其结果为 a+t(b−a)(计入浮点数运算的不精确性)。标准库提供所有以无 cv 限定的浮点数类型作为参数 a,b 和 t 的类型的重载。(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,那么结果有限。 - 如果
isfinite(t) && a == b,那么结果等于a。 - 如果
isfinite(t) || (b - a != 0 && std::isinf(t)),那么结果非 NaN。
令 CMP(x, y) 在 x > y 时是 1,在 x < y 时是 -1,否则是 0。对于任意 t1 与 t2,以下三者的积非负(即 std::lerp 单调):
CMP(std::lerp(a, b, t2), std::lerp(a, b, t1))CMP(t2, t1)CMP(b, a)
注解
额外重载不需要以 (A) 的形式提供。它们只需要能够对它们的第一个实参 num1,第二个实参 num2 和第三个实参 num3 满足以下要求:
|
(C++23 前) |
|
如果 如果不存在等级和子等级最高的浮点数类型,那么在重载决议时不会从提供的重载中产生可用的候选。 |
(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 << '\n';
std::cout << "std::lerp 是否精确?:"
<< (a == std::lerp(a, b, 0.0f)) << ' '
<< (b == std::lerp(a, b, 1.0f)) << '\n';
std::cout << "naive_lerp 是否精确?:"
<< (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 可以是 -inf
std::cout << "演示外插,给定 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
中点 = 5e+07
std::lerp 是否精确?:true true
naive_lerp 是否精确?:true false
std::lerp(a, b, 1.0f) = 1
naive_lerp(a, b, 1.0f) = 0
演示外插,给定 std::lerp(5, 10, t):
-5 -2.5 0 2.5 5 7.5 10 12.5 15
参阅
(C++20) |
两个数值或指针间的中点 (函数模板) |