std:: lerp
|
Definiert in Header
<cmath>
|
||
| (1) | ||
|
constexpr
float
lerp
(
float
a,
float
b,
float
t
)
noexcept
;
constexpr
double
lerp
(
double
a,
double
b,
double
t
)
noexcept
;
|
(seit C++20)
(bis C++23) |
|
|
constexpr
/* floating-point-type */
lerp
(
/* floating-point-type */
a,
|
(seit C++23) | |
|
Definiert in Header
<cmath>
|
||
|
template
<
class
Arithmetic1,
class
Arithmetic2,
class
Arithmetic3
>
constexpr
/* common-floating-point-type */
|
(A) | (seit C++20) |
[
0
,
1
)
liegt (ansonsten die
lineare Extrapolation
), d.h. das Ergebnis von
a+t(b−a)
unter Berücksichtigung von Ungenauigkeiten bei Gleitkommaberechnungen.
Die Bibliothek bietet Überladungen für alle cv-unqualifizierten Gleitkommatypen als Typ der Parameter
a
,
b
und
t
.
(seit C++23)
Inhaltsverzeichnis |
Parameter
| a, b, t | - | Gleitkomma- oder Ganzzahlwerte |
Rückgabewert
a + t(b − a)
Wenn std:: isfinite ( a ) && std:: isfinite ( b ) true ist, sind die folgenden Eigenschaften garantiert:
- Wenn t == 0 , ist das Ergebnis gleich a .
- Wenn t == 1 , ist das Ergebnis gleich b .
- Wenn t >= 0 && t <= 1 , ist das Ergebnis endlich.
- Wenn std:: isfinite ( t ) && a == b , ist das Ergebnis gleich a .
- Wenn std:: isfinite ( t ) || ( b - a ! = 0 && std:: isinf ( t ) ) , ist das Ergebnis nicht NaN .
Sei CMP ( x, y ) gleich 1 falls x > y , - 1 falls x < y , und 0 sonst. Für beliebige t1 und t2 ist das Produkt von
- CMP ( std :: lerp ( a, b, t2 ) , std :: lerp ( a, b, t1 ) ) ,
- CMP ( t2, t1 ) , und
- CMP ( b, a )
ist nicht-negativ. (Das heißt,
std::lerp
ist monoton.)
Hinweise
Die zusätzlichen Überladungen müssen nicht exakt wie (A) bereitgestellt werden. Sie müssen lediglich sicherstellen, dass für ihr erstes Argument num1 , zweites Argument num2 und drittes Argument num3 :
|
(bis C++23) |
|
Wenn
num1
,
num2
und
num3
arithmetische Typen haben, dann hat
std
::
lerp
(
num1, num2, num3
)
denselben Effekt wie
std
::
lerp
(
static_cast
<
/*common-floating-point-type*/
>
(
num1
)
,
Wenn kein solcher Gleitkommatyp mit dem höchsten Rang und Unterrang existiert, dann führt die Überladungsauflösung nicht zu einem verwendbaren Kandidaten aus den bereitgestellten Überladungen. |
(seit C++23) |
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_interpolate
|
201902L
|
(C++20) |
std::lerp
,
std::midpoint
|
Beispiel
#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'; }
Mögliche Ausgabe:
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
Siehe auch
|
(C++20)
|
Mittelpunkt zwischen zwei Zahlen oder Zeigern
(Funktions-Template) |