Skip to content

Commit 6da8552

Browse files
authored
Merge 2019-02 LWG Motion 13
P0811R3 Well-behaved interpolation for numbers and pointers Fixes #2704.
2 parents 9be92b8 + b5862d4 commit 6da8552

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

source/algorithms.tex

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8805,6 +8805,12 @@
88058805
// \ref{numeric.ops.lcm}, least common multiple
88068806
template<class M, class N>
88078807
constexpr common_type_t<M,N> lcm(M m, N n);
8808+
8809+
// \ref{numeric.ops.midpoint}, midpoint
8810+
template<class T>
8811+
constexpr T midpoint(T a, T b) noexcept;
8812+
template<class T>
8813+
constexpr T* midpoint(T* a, T* b);
88088814
}
88098815
\end{codeblock}
88108816

@@ -9780,6 +9786,59 @@
97809786
Nothing.
97819787
\end{itemdescr}
97829788

9789+
\rSec2[numeric.ops.midpoint]{Midpoint}
9790+
9791+
\indexlibrary{\idxcode{midpoint}}%
9792+
\begin{itemdecl}
9793+
template<class T>
9794+
constexpr T midpoint(T a, T b) noexcept;
9795+
\end{itemdecl}
9796+
\begin{itemdescr}
9797+
\pnum
9798+
\constraints
9799+
\tcode{T} is an arithmetic type other than \tcode{bool}.
9800+
9801+
\pnum
9802+
\returns
9803+
Half the sum of \tcode{a} and \tcode{b}.
9804+
If \tcode{T} is an integer type and the sum is odd,
9805+
the result is rounded towards \tcode{a}.
9806+
9807+
\pnum
9808+
\remarks
9809+
No overflow occurs.
9810+
If \tcode{T} is a floating-point type, at most one inexact operation occurs.
9811+
\end{itemdescr}
9812+
9813+
\indexlibrary{\idxcode{midpoint}}%
9814+
\begin{itemdecl}
9815+
template<class T>
9816+
constexpr T* midpoint(T* a, T* b);
9817+
\end{itemdecl}
9818+
\begin{itemdescr}
9819+
\pnum
9820+
\constraints
9821+
\tcode{T} is a complete object type.
9822+
9823+
\pnum
9824+
\expects
9825+
\tcode{a} and \tcode{b} point to, respectively,
9826+
elements $\tcode{x}[i]$ and $\tcode{x}[j]$ of the same array object \tcode{x}.
9827+
\begin{note}
9828+
An object that is not an array element is considered to belong
9829+
to a single-element array for this purpose; see \ref{expr.unary.op}.
9830+
A pointer past the last element of an array \tcode{x} of $n$ elements
9831+
is considered to be equivalent to a pointer
9832+
to a hypothetical element $\tcode{x}[n]$ for this purpose;
9833+
see \ref{basic.compound}.
9834+
\end{note}
9835+
9836+
\pnum
9837+
\returns
9838+
A pointer to $\tcode{x}[i+\frac{j-i}{2}]$,
9839+
where the result of the division is truncated towards zero.
9840+
\end{itemdescr}
9841+
97839842
\rSec1[alg.c.library]{C library algorithms}
97849843

97859844
\pnum

source/numerics.tex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9119,6 +9119,11 @@
91199119
float fmaf(float x, float y, float z);
91209120
long double fmal(long double x, long double y, long double z);
91219121

9122+
// \ref{c.math.lerp}, linear interpolation
9123+
constexpr float lerp(float a, float b, float t);
9124+
constexpr double lerp(double a, double b, double t);
9125+
constexpr long double lerp(long double a, long double b, long double t);
9126+
91229127
// \ref{c.math.fpclass}, classification / comparison functions
91239128
int fpclassify(float x);
91249129
int fpclassify(double x);
@@ -9375,6 +9380,39 @@
93759380
\returns $\sqrt{x^2+y^2+z^2}$.
93769381
\end{itemdescr}
93779382

9383+
\rSec2[c.math.lerp]{Linear interpolation}
9384+
9385+
\indexlibrary{\idxcode{lerp}}%
9386+
\begin{itemdecl}
9387+
constexpr float lerp(float a, float b, float t);
9388+
constexpr double lerp(double a, double b, double t);
9389+
constexpr long double lerp(long double a, long double b, long double t);
9390+
\end{itemdecl}
9391+
\begin{itemdescr}
9392+
\pnum
9393+
\returns
9394+
$a+t(b-a)$.
9395+
9396+
\pnum
9397+
\remarks
9398+
Let \tcode{r} be the value returned.
9399+
If \tcode{isfinite(a) \&\& isfinite(b)}, then:
9400+
\begin{itemize}
9401+
\item If \tcode{t == 0}, then \tcode{r == a}.
9402+
\item If \tcode{t == 1}, then \tcode{r == b}.
9403+
\item If \tcode{t >= 0 \&\& t <= 1}, then \tcode{isfinite(r)}.
9404+
\item If \tcode{isfinite(t) \&\& a == b}, then \tcode{r == a}.
9405+
\item If \tcode{isfinite(t) || !isnan(t) \&\& b-a != 0}, then \tcode{!isnan(r)}.
9406+
\end{itemize}
9407+
Let \tcode{\placeholder{CMP}(x,y)} be \tcode{1} if \tcode{x > y},
9408+
\tcode{-1} if \tcode{x < y}, and \tcode{0} otherwise.
9409+
For any \tcode{t1} and \tcode{t2}, the product of
9410+
\tcode{\placeholder{CMP}(lerp(a, b, t2), lerp(a, b, t1))},
9411+
\tcode{\placeholder{CMP}(t2, t1)}, and
9412+
\tcode{\placeholder{CMP}(b, a)}
9413+
is non-negative.
9414+
\end{itemdescr}
9415+
93789416
\rSec2[c.math.fpclass]{Classification / comparison functions}
93799417

93809418
\pnum

source/support.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@
610610
\tcode{<utility>} \\ \rowsep
611611
\defnlibxname{cpp_lib_integral_constant_callable} & \tcode{201304L} &
612612
\tcode{<type_traits>} \\ \rowsep
613+
\defnlibxname{cpp_lib_interpolate} & \tcode{201902L} &
614+
\tcode{<cmath>} \tcode{<numeric>} \\ \rowsep
613615
\defnlibxname{cpp_lib_invoke} & \tcode{201411L} &
614616
\tcode{<functional>} \\ \rowsep
615617
\defnlibxname{cpp_lib_is_aggregate} & \tcode{201703L} &

0 commit comments

Comments
 (0)