std:: lerp
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
constexpr
float
lerp
(
float
a,
float
b,
float
t
)
noexcept
;
constexpr
double
lerp
(
double
a,
double
b,
double
t
)
noexcept
;
|
(C++20 起)
(C++23 前) |
|
|
constexpr
/* floating-point-type */
lerp
(
/* floating-point-type */
a,
|
(C++23 起) | |
|
定义于头文件
<cmath>
|
||
|
template
<
class
Arithmetic1,
class
Arithmetic2,
class
Arithmetic3
>
constexpr
/* common-floating-point-type */
|
(A) | (C++20 起) |
[
0
,
1
)
区间内时),否则进行
线性外推
。即计算
a+t(b−a)
并考虑浮点计算精度误差。
该库为所有无 cv 限定的浮点类型提供了参数
a
、
b
和
t
的重载。
(C++23 起)
目录 |
参数
| 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 ) 为:当 x > y 时等于 1 ,当 x < y 时等于 - 1 ,其余情况等于 0 。对于任意 t1 和 t2 ,其乘积
- CMP ( std :: lerp ( a, b, t2 ) , std :: lerp ( a, b, t1 ) ) ,
- CMP ( t2, t1 ) ,以及
- CMP ( b, a )
是非负的。(即
std::lerp
是单调的。)
注释
额外的重载不需要完全按照 (A) 提供。只需确保对于它们的第一个参数 num1 、第二个参数 num2 和第三个参数 num3 满足以下条件:
|
(C++23 前) |
|
如果
num1
、
num2
和
num3
具有算术类型,则
std
::
lerp
(
num1, num2, num3
)
的效果等同于
std
::
lerp
(
static_cast
<
/*common-floating-point-type*/
>
(
num1
)
,
如果不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中得到可用的候选函数。 |
(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 可能返回 -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 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)
|
两个数值或指针之间的中点
(函数模板) |