Guarantees on computing $a+x(b-a)$ in floating point I want to implement the function $f(x,a,b) = a + x(b-a)$ where all the inputs are floating point (doubles, say), such that (a) $f(0,a,b)=a$ exactly; (b) $f(1,a,b)=b$ exactly; (c) $f(x,a,b) \le f(y,a,b)$ whenever $x \le y$; and preferably (d) it is accurate (correct up to rounding) for $0 \le x \le 1$.

Implementing $f(x,a,b)=a+x(b-a)$ directly does not work because for example $f(1.0,-1.0,\operatorname{prev}(1.0)) = 1.0$ (where $\operatorname{prev}(a)$ is the floating point number before $a$). And $f(x,a,b)=b-(1-x)(b-a)$ has the same issue.

Now $$f(x,a,b) = (1-x)(a+x(b-a))+x(b-(1-x)(b-a))$$ has the first two properties.


Does it have property (c)?
How accurate is it?
Is there a more performant way to do this?
Here is a proposed alternative
$$f(x,a,b) = \operatorname{ifelse}(x&lt;0.5, a+x(b-a), b-(1-x)(b-a))$$
which also satisfies (a) and (b), but it is not accurate near $0.5$. It also satisfies (c) except maybe at $\operatorname{prev}(0.5)$ and $0.5$.

For example with double-precision arithmetic we compute $f(0.5,-1.0,\operatorname{prev}(1.0))$ to be -1.11e-16 but it is actually -5.55e-17, so out by a factor of 2.

It's worth noting that the implementation proposed in the question gets this example correct.