Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::lerp

      From cppreference.com
      <cpp‎ |numeric
       
       
       
      Defined in header<cmath>
      (1)
      constexprfloat       lerp(float a,float b,float t)noexcept;

      constexprdouble      lerp(double a,double b,double t)noexcept;
      constexprlongdouble lerp(longdouble a,longdouble b,

                                 longdouble t)noexcept;
      (since C++20)
      (until C++23)
      constexpr/* floating-point-type */

          lerp(/* floating-point-type */ a,
               /* floating-point-type */ b,

               /* floating-point-type */ t)noexcept;
      (since C++23)
      Defined in header<cmath>
      template<class Arithmetic1,class Arithmetic2,class Arithmetic3>

      constexpr/* common-floating-point-type */

          lerp( Arithmetic1 a, Arithmetic2 b, Arithmetic3 t)noexcept;
      (A)(since C++20)
      1) Computes thelinear interpolation betweena andb, if the parametert is inside[01) (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)
      A) Additional overloads are provided for all other combinations of arithmetic types.

      Contents

      [edit]Parameters

      a, b, t - floating-point or integer values

      [edit]Return value

      \(a + t(b − a)\)a + t(b − a)

      Whenstd::isfinite(a)&&std::isfinite(b) istrue, the following properties are guaranteed:

      • Ift==0, the result is equal toa.
      • Ift==1, the result is equal tob.
      • Ift>=0&& t<=1, the result is finite.
      • Ifstd::isfinite(t)&& a== b, the result is equal toa.
      • Ifstd::isfinite(t)||(b- a!=0&&std::isinf(t)), the result is notNaN.

      LetCMP(x, y) be1 ifx> y,-1 ifx< y, and0 otherwise. For anyt1 andt2, the product of

      • CMP(std::lerp(a, b, t2), std::lerp(a, b, t1)),
      • CMP(t2, t1), and
      • CMP(b, a)

      is non-negative. (That is,std::lerp is monotonic.)

      [edit]Notes

      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:

      • Ifnum1,num2 ornum3 has typelongdouble, thenstd::lerp(num1, num2, num3) has the same effect asstd::lerp(static_cast<longdouble>(num1),
                 static_cast<longdouble>(num2),
                 static_cast<longdouble>(num3))
        .
      • Otherwise, ifnum1,num2 and/ornum3 has typedouble or an integer type, thenstd::lerp(num1, num2, num3) has the same effect asstd::lerp(static_cast<double>(num1),
                 static_cast<double>(num2),
                 static_cast<double>(num3))
        .
      • Otherwise, ifnum1,num2 ornum3 has typefloat, thenstd::lerp(num1, num2, num3) has the same effect asstd::lerp(static_cast<float>(num1),
                 static_cast<float>(num2),
                 static_cast<float>(num3))
        .
      (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),
               static_cast</*common-floating-point-type*/>(num2),
               static_cast</*common-floating-point-type*/>(num3))
      , where/*common-floating-point-type*/ is the floating-point type with the greatestfloating-point conversion rank and greatestfloating-point conversion subrank among the types ofnum1,num2 andnum3, arguments of integer type are considered to have the same floating-point conversion rank asdouble.

      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 macroValueStdFeature
      __cpp_lib_interpolate201902L(C++20)std::lerp,std::midpoint

      [edit]Example

      Run this code
      #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

      [edit]See also

      (C++20)
      midpoint between two numbers or pointers
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/lerp&oldid=160721"

      [8]ページ先頭

      ©2009-2025 Movatter.jp