Difference between revisions of "cpp/numeric/lerp"
From cppreference.com
m (→Example: += f(t)) |
(Equivalent) |
||
Line 34: | Line 34: | ||
* If {{tt|1=t >= 0 && t <= 1}}, the result is finite. | * If {{tt|1=t >= 0 && t <= 1}}, the result is finite. | ||
* If {{tt|1=isfinite(t) && a == b}}, the result is equal to {{tt|a}}. | * If {{tt|1=isfinite(t) && a == b}}, the result is equal to {{tt|a}}. | ||
− | * If {{tt|1=isfinite(t) {{!!}} | + | * If {{tt|1=isfinite(t) {{!!}} isinf(t) && b-a != 0}}, the result is not {{tt|NaN}}. |
Let {{tt|CMP(x,y)}} be {{tt|1}} if {{tt|x > y}}, {{tt|-1}} if {{tt|x < y}}, and {{tt|0}} otherwise. For any {{tt|t1}} and {{tt|t2}}, the product of {{tt|CMP(lerp(a, b, t2), lerp(a, b, t1))}}, {{tt|CMP(t2, t1)}}, and {{tt|CMP(b, a)}} is non-negative. (That is, {{tt|lerp}} is monotonic.) | Let {{tt|CMP(x,y)}} be {{tt|1}} if {{tt|x > y}}, {{tt|-1}} if {{tt|x < y}}, and {{tt|0}} otherwise. For any {{tt|t1}} and {{tt|t2}}, the product of {{tt|CMP(lerp(a, b, t2), lerp(a, b, t1))}}, {{tt|CMP(t2, t1)}}, and {{tt|CMP(b, a)}} is non-negative. (That is, {{tt|lerp}} is monotonic.) |
Revision as of 20:48, 31 December 2021
Defined in header <cmath>
|
||
constexpr float lerp( float a, float b, float t ) noexcept; |
(1) | (since C++20) |
constexpr double lerp( double a, double b, double t ) noexcept; |
(2) | (since C++20) |
constexpr long double lerp( long double a, long double b, long double t ) noexcept; |
(3) | (since C++20) |
constexpr Promoted lerp( Arithmetic1 a, Arithmetic2 b, Arithmetic3 t ) noexcept; |
(4) | (since C++20) |
1-3) Computes a+t(b−a), i.e. the linear interpolation between
a
and b
for the parameter t
(or extrapolation, when t
is outside the range [0,1]
).4) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). If any argument has integral type, it is cast to double. If any other argument is long double, then the return type is long double, otherwise it is double.
Contents |
Parameters
a, b, t | - | values of floating-point or integral types |
Return value
a+t(b−a)
When isfinite(a) && isfinite(b)
, 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
isfinite(t) && a == b
, the result is equal toa
. - If
isfinite(t) || isinf(t) && b-a != 0
, 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(lerp(a, b, t2), lerp(a, b, t1))
, CMP(t2, t1)
, and CMP(b, a)
is non-negative. (That is, lerp
is monotonic.)
Notes
lerp
is available in the global namespace when <math.h>
is included, even if it is not a part of C.
Feature testing macro: __cpp_lib_interpolate.
Example
Run this code
#include <iostream> #include <cmath> int main() { float a=10.0f, b=20.0f; std::cout << "a=" << a << ", " << "b=" << b << '\n' << "mid point=" << std::lerp(a,b,0.5f) << '\n' << std::boolalpha << (a == std::lerp(a,b,0.0f)) << ' ' << std::boolalpha << (b == std::lerp(a,b,1.0f)) << '\n'; for (float t{}; t <= 1.0f; t += 0.1f) std::cout << std::lerp(10.f, 20.f, t) << ' '; std::cout << '\n'; for (double t{}; t <= 1.0; t += 0.1) std::cout << std::lerp(10., 20., t) << ' '; std::cout << '\n'; }
Output:
a=10, b=20 mid point=15 true true 10 11 12 13 14 15 16 17 18 19 10 11 12 13 14 15 16 17 18 19 20
See also
(C++20) |
midpoint between two numbers or pointers (function template) |