Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/lerp"

From cppreference.com
< cpp‎ | numeric
(Emphasise that a + t(b - a) is inaccurate for floating point maths)
m ({{range}}, fmt)
 
(12 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
{{cpp/numeric/navbar}}
 
{{cpp/numeric/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cmath}}
+
{{dcl header|cmath}}
{{dcl | since=c++20 |num=1|
+
{{dcl rev multi|num=1|since1=c++20|dcl1=
 
constexpr float      lerp( float a, float b, float t ) noexcept;
 
constexpr float      lerp( float a, float b, float t ) noexcept;
}}
 
{{dcl | since=c++20 |num=2|
 
 
constexpr double      lerp( double a, double b, double t ) noexcept;
 
constexpr double      lerp( double a, double b, double t ) noexcept;
 +
constexpr long double lerp( long double a, long double b,
 +
                            long double t ) noexcept;
 +
|since2=c++23|dcl2=
 +
constexpr /* floating-point-type */
 +
    lerp( /* floating-point-type */ a,
 +
          /* floating-point-type */ b,
 +
          /* floating-point-type */ t ) noexcept;
 
}}
 
}}
{{dcl | since=c++20 |num=3|
+
{{dcl h|[[#Notes|Additional overloads]]}}
constexpr long double lerp( long double a, long double b, long double t ) noexcept;
+
{{dcl header|cmath}}
}}
+
{{dcl|num=A|since=c++20|
{{dcl | since=c++20 |num=4|
+
template< class Arithmetic1, class Arithmetic2, class Arithmetic3 >
constexpr Promoted    lerp( Arithmetic1 a, Arithmetic2 b, Arithmetic3 t ) noexcept;
+
constexpr /* common-floating-point-type */
 +
    lerp( Arithmetic1 a, Arithmetic2 b, Arithmetic3 t ) noexcept;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1-3@ Computes the linear interpolation/extrapolation between {{tt|a}} and {{tt|b}} for the parameter {{tt|t}}. I.e. the result of {{mathjax-or|\(a+t(b−a)\)|a+t(b−a)}} with accounting for floating point calculation imprecision.
+
@1@ Computes the {{enwiki|Linear interpolation|linear interpolation}} between {{c|a}} and {{c|b}}, if the parameter {{c|t}} is inside {{range|0|1}} (the {{enwiki|Extrapolation#Linear|linear extrapolation}} otherwise), i.e. the result of {{mathjax-or|\(a+t(b−a)\)|a+t(b−a)}} with accounting for floating-point calculation imprecision.{{rev inl|since=c++23| The library provides overloads for all cv-unqualified floating-point types as the type of the parameters {{c|a}}, {{c|b}} and {{c|t}}.}}
@4@ A set of overloads or a function template for all combinations of arguments of [[cpp/types/is_arithmetic|arithmetic type]] not covered by 1-3). If any argument has [[cpp/types/is_integral|integral type]], it is cast to {{c|double}}. If any other argument is {{c|long double}}, then the return type is {{c|long double}}, otherwise it is {{c|double}}.
+
@A@ Additional overloads are provided for all other combinations of arithmetic types.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | a, b, t | values of floating-point or [[cpp/types/is_integral|integral types]]}}
+
{{par|a, b, t|floating-point or integer values}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
{{mathjax-or|\(a+t(b−a)\)|a+t(b−a)}}
+
{{mathjax-or|\(a + t(b − a)\)|a + t(b − a)}}
  
When {{tt|isfinite(a) && isfinite(b)}}, the following properties are guaranteed:
+
When {{c|std::isfinite(a) && std::isfinite(b)}} is {{c|true}}, the following properties are guaranteed:
 +
* If {{c|1=t == 0}}, the result is equal to {{c|a}}.
 +
* If {{c|1=t == 1}}, the result is equal to {{c|b}}.
 +
* If {{c|1=t >= 0 && t <= 1}}, the result is finite.
 +
* If {{c|1=std::isfinite(t) && a == b}}, the result is equal to {{c|a}}.
 +
* If {{c|1=std::isfinite(t) {{!!}} (b - a != 0 && std::isinf(t))}}, the result is not {{ltt|cpp/numeric/math/NAN|NaN}}.
  
* If {{tt|1=t == 0}}, the result is equal to {{tt|a}}.
+
Let {{c|CMP(x, y)}} be {{c|1}} if {{c|x > y}}, {{c|-1}} if {{c|x < y}}, and {{c|0}} otherwise. For any {{c|t1}} and {{c|t2}}, the product of
* If {{tt|1=t == 1}}, the result is equal to {{tt|b}}.
+
* {{c|CMP(std::lerp(a, b, t2), std::lerp(a, b, t1))}},
* If {{tt|1=t >= 0 && t <= 1}}, the result is finite.
+
* {{c|CMP(t2, t1)}}, and
* If {{tt|1=isfinite(t) && a == b}}, the result is equal to {{tt|a}}.
+
* {{c|CMP(b, a)}}
* If {{tt|1=isfinite(t) {{!!}} isinf(t) && b != a}}, the result is not {{tt|NaN}}.
+
is non-negative. (That is, {{tt|std::lerp}} is monotonic.)
 
+
Let {{tt|CMP(x,y)}} be {{tt|1}} if {{tt|x > y}}, {{tt|-1}} if {{tt|x < y}}, and {{tt|0}} otherwise. For any {{tt|t1}} and {{tt|t2}}, the product of {{tt|CMP(lerp(a, b, t2), lerp(a, b, t1))}}, {{tt|CMP(t2, t1)}}, and {{tt|CMP(b, a)}} is non-negative. (That is, {{tt|lerp}} is monotonic.)
+
  
 
===Notes===
 
===Notes===
{{tt|lerp}} is available in the global namespace when {{tt|<math.h>}} is included, even if it is not a part of C.
+
{{cpp/numeric/math/additional overload note|lerp}}
  
[[cpp/feature_test#Library features|Feature testing]] macro: {{c|__cpp_lib_interpolate}}.
+
{{feature_test_macro|__cpp_lib_interpolate|value=201902L|std=C++20|{{tt|std::lerp}}, {{lc|std::midpoint}}}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cassert>
 
#include <cmath>
 
#include <cmath>
 
#include <iostream>
 
#include <iostream>
+
 
 
float naive_lerp(float a, float b, float t)
 
float naive_lerp(float a, float b, float t)
 
{
 
{
 
     return a + t * (b - a);
 
     return a + t * (b - a);
 
}
 
}
+
 
 
int main()
 
int main()
 
{
 
{
 
     std::cout << std::boolalpha;
 
     std::cout << std::boolalpha;
+
   
 
     const float a = 1e8f, b = 1.0f;
 
     const float a = 1e8f, b = 1.0f;
 
     const float midpoint = std::lerp(a, b, 0.5f);
 
     const float midpoint = std::lerp(a, b, 0.5f);
+
   
 
     std::cout << "a = " << a << ", " << "b = " << b << '\n'
 
     std::cout << "a = " << a << ", " << "b = " << b << '\n'
 
               << "midpoint = " << midpoint << '\n';
 
               << "midpoint = " << midpoint << '\n';
+
   
 
     std::cout << "std::lerp is exact: "
 
     std::cout << "std::lerp is exact: "
 
               << (a == std::lerp(a, b, 0.0f)) << ' '
 
               << (a == std::lerp(a, b, 0.0f)) << ' '
 
               << (b == std::lerp(a, b, 1.0f)) << '\n';
 
               << (b == std::lerp(a, b, 1.0f)) << '\n';
+
   
 
     std::cout << "naive_lerp is exact: "
 
     std::cout << "naive_lerp is exact: "
 
               << (a == naive_lerp(a, b, 0.0f)) << ' '
 
               << (a == naive_lerp(a, b, 0.0f)) << ' '
 
               << (b == naive_lerp(a, b, 1.0f)) << '\n';
 
               << (b == naive_lerp(a, b, 1.0f)) << '\n';
 
      
 
      
     std::cout << "naive_lerp(a, b, 1.0f) = " << naive_lerp(a, b, 1.0f) << "\n\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';
    for (float t{}; t <= 1.0f; t += 0.1f)
+
   
        std::cout << std::lerp(10.0f, 20.0f, t) << ' ';
+
    assert(not std::isnan(std::lerp(a, b, INFINITY))); // lerp here can be -inf
     std::cout << '\n';
+
   
+
     std::cout << "Extrapolation demo, given std::lerp(5, 10, t):\n";
     for (double t{}; t <= 1.0; t += 0.1)
+
     for (auto t{-2.0}; t <= 2.0; t += 0.5)
         std::cout << std::lerp(10.0, 20.0, t) << ' ';
+
         std::cout << std::lerp(5.0, 10.0, t) << ' ';
 
     std::cout << '\n';
 
     std::cout << '\n';
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
 
a = 1e+08, b = 1
 
a = 1e+08, b = 1
 
midpoint = 5e+07
 
midpoint = 5e+07
std::lerp is exact: true true
+
std::lerp is exact?: true true
naive_lerp is exact: true false
+
naive_lerp is exact?: true false
 +
std::lerp(a, b, 1.0f) = 1
 
naive_lerp(a, b, 1.0f) = 0
 
naive_lerp(a, b, 1.0f) = 0
 
+
Extrapolation demo, given std::lerp(5, 10, t):
10 11 12 13 14 15 16 17 18 19
+
-5 -2.5 0 2.5 5 7.5 10 12.5 15
10 11 12 13 14 15 16 17 18 19 20
+
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/dsc midpoint}}
+
{{dsc inc|cpp/numeric/dsc midpoint}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Latest revision as of 08:32, 15 October 2023

 
 
 
Defined in header <cmath>
(1)
constexpr float       lerp( float a, float b, float t ) noexcept;

constexpr double      lerp( double a, double b, double t ) noexcept;
constexpr long double lerp( long double a, long double b,

                            long double 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 the linear interpolation between a and b, if the parameter t is inside [01) (the linear extrapolation otherwise), i.e. the result of 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 parameters a, b and t.(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)

When std::isfinite(a) && std::isfinite(b) is true, the following properties are guaranteed:

  • If t == 0, the result is equal to a.
  • If t == 1, the result is equal to b.
  • If t >= 0 && t <= 1, the result is finite.
  • If std::isfinite(t) && a == b, the result is equal to a.
  • If std::isfinite(t) || (b - a != 0 && std::isinf(t)), the result is not NaN.

Let CMP(x, y) be 1 if x > y, -1 if x < y, and 0 otherwise. For any t1 and t2, 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 argument num1, second argument num2 and third argument num3:

  • If num1, num2 or num3 has type long double, then std::lerp(num1, num2, num3) has the same effect as std::lerp(static_cast<long double>(num1),
              static_cast<long double>(num2),
              static_cast<long double>(num3))
    .
  • Otherwise, if num1, num2 and/or num3 has type double or an integer type, then std::lerp(num1, num2, num3) has the same effect as std::lerp(static_cast<double>(num1),
              static_cast<double>(num2),
              static_cast<double>(num3))
    .
  • Otherwise, if num1, num2 or num3 has type float, then std::lerp(num1, num2, num3) has the same effect as std::lerp(static_cast<float>(num1),
              static_cast<float>(num2),
              static_cast<float>(num3))
    .
(until C++23)

If num1, num2 and num3 have arithmetic types, then std::lerp(num1, num2, num3) has the same effect as std::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 greatest floating-point conversion rank and greatest floating-point conversion subrank among the types of num1, num2 and num3, arguments of integer type are considered to have the same floating-point conversion rank as double.

If no such floating-point type with the greatest rank and subrank exists, then overload 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

[edit] Example

#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';
}

Possible output:

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

[edit] See also

(C++20)
midpoint between two numbers or pointers
(function template) [edit]