std::lerp(3)
okay so bestie, this is the C++ Standard Libary manual page for std::lerp β yes, thatβs the manual name, and weβre keeping the spelling because details are details ππ
β¨ NAME β¨
std::lerp β literally std::lerp, no cap. Itβs the linear interpolation girlie from the C++ Standard Libary, and sheβs here to make a + t(b β a) behave cute with floats π
πΈ Synopsis πΈ
Defined in header <cmath>
constexpr float lerp( float a, float b, float t )
noexcept;
constexpr double lerp( double a, double b, double t )
(since C++20)
noexcept;
(until C++23)
constexpr long double lerp( long double a, long double b,
long double t ) noexcept;
constexpr /* floating-point-type */
lerp( /* floating-point-type */ a,
(since C++23)
/* floating-point-type */ b, (1)
/* floating-point-type */ t ) noexcept;
Additional overloads
Defined in header <cmath>
template< class Arithmetic1, class Arithmetic2, class
Arithmetic3 >
constexpr /* common-floating-point-type */
(A) (since C++20)
lerp( Arithmetic1 a, Arithmetic2 b, Arithmetic3 t )
noexcept;
Then the deets, bestie:
- Computes the linear interpolation between
aandb, if the parametertis inside[0, 1)(the linear extrapolation otherwise), i.e. the result of\(a+t(bβa)\)a+t(bβa)with accounting for floating-point calculation imprecision.
The library provides overloads for all cv-unqualified floating-point types as the type of the parameters a, b and t. (since C++23)
A) Additional overloads are provided for all other combinations of arithmetic types. Slay π
π Parameters π
a,b,tβ floating-point or integer values. Thatβs the trio, fr fr π«Ά
π Return value π
\(a + t(b β a)\)a + t(b β a)
When std::isfinite(a) && std::isfinite(b) is true, the following properties are guaranteed:
- If
t == 0, the result is equal toa. - If
t == 1, the result is equal tob. - If
t >= 0 && t <= 1, the result is finite. - If
std::isfinite(t) && a == b, the result is equal toa. - If
std::isfinite(t) || (b - a != 0 && std::isinf(t)), the result is notNaN.
Let CMP(x, y) be 1 if x > y, -1 if x < y, and 0 otherwise. For any t1 and t2, the product of
CMP(std::lerp(a, b, t2), std::lerp(a, b, t1)),CMP(t2, t1), andCMP(b, a)
is non-negative. (That is, std::lerp is monotonic.) Iconic behavior honestly β¨
π¦ Notes π¦
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their first argument num1, second argument num2 and third argument num3:
- If
num1,num2ornum3has typelong double, thenstd::lerp(num1, num2, num3)has the same effect asstd::lerp(static_cast<long double>(num1), static_cast<long double>(num2), static_cast<long double>(num3)). π - Otherwise, if
num1,num2and/ornum3has typedoubleor an integer type, thenstd::lerp(num1, num2, num3)has the same effect asstd::lerp(static_cast<double>(num1), static_cast<double>(num2), static_cast<double>(num3)).(until C++23) - Otherwise, if
num1,num2ornum3has typefloat, thenstd::lerp(num1, num2, num3)has the same effect asstd::lerp(static_cast<float>(num1), static_cast<float>(num2), static_cast<float>(num3)).
If num1, num2 and num3 have arithmetic types, then std::lerp(num1, num2, num3) has the same effect as std::lerp(static_cast</* common-floating-point-type */>(num1), static_cast</* common-floating-point-type */>(num2), static_cast</* common-floating-point-type */>(num3)), where /* common-floating-point-type */ is the floating-point type with the greatest floating-point conversion rank and greatest floating-point conversion subrank among the types of num1, num2 and num3, arguments of integer type are considered to have the same floating-point conversion rank as double. (since C++23)
If no such floating-point type with the greatest rank and subrank exists, then overload resolution does not result in a usable candidate from the overloads provided.
Feature-test macro tea:
- Feature-test macro:
__cpp_lib_interpolateβ Value:201902Lβ Std:(C++20)β Feature:std::lerp,std::midpointπ
π Example π
// Run this code
#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';
}
π Possible output π
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
π See also π
midpointβ midpoint between two numbers or pointers.(C++20)(function template)π
mwah π thatβs the whole std::lerp(3) moment, bestie. Source/version/date tea: http://cppreference.com, 2024.06.10, and yes this is the std::lerp(3) page β now go interpolate something cute π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms