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
와
b
사이의
선형 보간
을 계산하고(범위 밖일 경우
선형 외삽
), 즉 부동소수점 계산의 부정확성을 고려한
a+t(b−a)
의 결과를 반환합니다.
라이브러리는 매개변수
a
,
b
,
t
의 타입으로 모든 cv-unqualified 부동소수점 타입에 대한 오버로드를 제공합니다.
(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)
|
두 숫자 또는 포인터 간의 중간점
(함수 템플릿) |