Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
Defined in header <cmath> | ||
(1) | ||
constexprfloat lerp(float a,float b,float t)noexcept; constexprdouble lerp(double a,double b,double t)noexcept; | (since C++20) (until C++23) | |
constexpr/* floating-point-type */ lerp(/* floating-point-type */ a, | (since C++23) | |
Defined in header <cmath> | ||
template<class Arithmetic1,class Arithmetic2,class Arithmetic3> constexpr/* common-floating-point-type */ | (A) | (since C++20) |
[
0,
1)
(thelinear 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 parametersa,b andt.(since C++23)Contents |
a, b, t | - | floating-point or integer values |
\(a + t(b − a)\)a + t(b − a)
Whenstd::isfinite(a)&&std::isfinite(b) istrue, the following properties are guaranteed:
LetCMP(x, y) be1 ifx> y,-1 ifx< y, and0 otherwise. For anyt1 andt2, the product of
is non-negative. (That is,std::lerp
is monotonic.)
The additional overloads are not required to be provided exactly as(A). They only need to be sufficient to ensure that for their first argumentnum1, second argumentnum2 and third argumentnum3:
| (until C++23) |
Ifnum1,num2 andnum3 have arithmetic types, thenstd::lerp(num1, num2, num3) has the same effect asstd::lerp(static_cast</*common-floating-point-type*/>(num1), If no such floating-point type with the greatest rank and subrank exists, thenoverload resolution does not result in a usable candidate from the overloads provided. | (since C++23) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__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; constfloat a= 1e8f, b=1.0f;constfloat 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(notstd::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 = 1midpoint = 5e+07std::lerp is exact?: true truenaive_lerp is exact?: true falsestd::lerp(a, b, 1.0f) = 1naive_lerp(a, b, 1.0f) = 0Extrapolation demo, given std::lerp(5, 10, t):-5 -2.5 0 2.5 5 7.5 10 12.5 15
(C++20) | midpoint between two numbers or pointers (function template)[edit] |