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)
の結果を浮動小数点計算の誤差を考慮して算出する。
ライブラリは、パラメータ
a
、
b
、
t
の型として、すべてのcv修飾されていない浮動小数点型に対するオーバーロードを提供する。
(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 ) が 1 となるのは x > y の場合、 - 1 となるのは x < y の場合、それ以外は 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 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'; }
出力例:
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)
|
2つの数値またはポインタの中間点
(関数テンプレート) |