# ?

Average Error: 18.2 → 3.7
Time: 23.1s
Precision: binary64
Cost: 7748

# ?

$\left(\left(\left(-1000 \leq a1 \land a1 \leq 1000\right) \land \left(-1000 \leq s1 \land s1 \leq 1000\right)\right) \land \left(-1000 \leq a2 \land a2 \leq 1000\right)\right) \land \left(-1000 \leq s2 \land s2 \leq 1000\right)$
$a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$
$\begin{array}{l} t_0 := a1 + \frac{s1 \cdot \left(a1 - a2\right)}{s2 - s1}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-308}:\\ \;\;\;\;\mathsf{fma}\left(s1, \frac{a2 - a1}{s1 - s2}, a1\right)\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \mathbf{else}:\\ \;\;\;\;a1 - \frac{s1}{\frac{s2 - s1}{a2 - a1}}\\ \end{array}$
(FPCore (a1 s1 a2 s2)
:precision binary64
(- a1 (/ (* s1 (- a2 a1)) (- s2 s1))))
(FPCore (a1 s1 a2 s2)
:precision binary64
(let* ((t_0 (+ a1 (/ (* s1 (- a1 a2)) (- s2 s1)))))
(if (<= t_0 -2e-308)
(fma s1 (/ (- a2 a1) (- s1 s2)) a1)
(if (<= t_0 0.0)
(+ a2 (* s2 (/ (- a2 a1) s1)))
(- a1 (/ s1 (/ (- s2 s1) (- a2 a1))))))))
double code(double a1, double s1, double a2, double s2) {
return a1 - ((s1 * (a2 - a1)) / (s2 - s1));
}

double code(double a1, double s1, double a2, double s2) {
double t_0 = a1 + ((s1 * (a1 - a2)) / (s2 - s1));
double tmp;
if (t_0 <= -2e-308) {
tmp = fma(s1, ((a2 - a1) / (s1 - s2)), a1);
} else if (t_0 <= 0.0) {
tmp = a2 + (s2 * ((a2 - a1) / s1));
} else {
tmp = a1 - (s1 / ((s2 - s1) / (a2 - a1)));
}
return tmp;
}

function code(a1, s1, a2, s2)
return Float64(a1 - Float64(Float64(s1 * Float64(a2 - a1)) / Float64(s2 - s1)))
end

function code(a1, s1, a2, s2)
t_0 = Float64(a1 + Float64(Float64(s1 * Float64(a1 - a2)) / Float64(s2 - s1)))
tmp = 0.0
if (t_0 <= -2e-308)
tmp = fma(s1, Float64(Float64(a2 - a1) / Float64(s1 - s2)), a1);
elseif (t_0 <= 0.0)
tmp = Float64(a2 + Float64(s2 * Float64(Float64(a2 - a1) / s1)));
else
tmp = Float64(a1 - Float64(s1 / Float64(Float64(s2 - s1) / Float64(a2 - a1))));
end
return tmp
end

code[a1_, s1_, a2_, s2_] := N[(a1 - N[(N[(s1 * N[(a2 - a1), $MachinePrecision]),$MachinePrecision] / N[(s2 - s1), $MachinePrecision]),$MachinePrecision]), $MachinePrecision]  code[a1_, s1_, a2_, s2_] := Block[{t$95$0 = N[(a1 + N[(N[(s1 * N[(a1 - a2),$MachinePrecision]), $MachinePrecision] / N[(s2 - s1),$MachinePrecision]), $MachinePrecision]),$MachinePrecision]}, If[LessEqual[t$95$0, -2e-308], N[(s1 * N[(N[(a2 - a1), $MachinePrecision] / N[(s1 - s2),$MachinePrecision]), $MachinePrecision] + a1),$MachinePrecision], If[LessEqual[t$95$0, 0.0], N[(a2 + N[(s2 * N[(N[(a2 - a1), $MachinePrecision] / s1),$MachinePrecision]), $MachinePrecision]),$MachinePrecision], N[(a1 - N[(s1 / N[(N[(s2 - s1), $MachinePrecision] / N[(a2 - a1),$MachinePrecision]), $MachinePrecision]),$MachinePrecision]), \$MachinePrecision]]]]

a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}

\begin{array}{l}
t_0 := a1 + \frac{s1 \cdot \left(a1 - a2\right)}{s2 - s1}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-308}:\\
\;\;\;\;\mathsf{fma}\left(s1, \frac{a2 - a1}{s1 - s2}, a1\right)\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\

\mathbf{else}:\\
\;\;\;\;a1 - \frac{s1}{\frac{s2 - s1}{a2 - a1}}\\

\end{array}


# Derivation?

1. Split input into 3 regimes
2. ## if (-.f64 a1 (/.f64 (*.f64 s1 (-.f64 a2 a1)) (-.f64 s2 s1))) < -1.9999999999999998e-308

1. Initial program 10.3

$a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$
2. Simplified4.2

$\leadsto \color{blue}{\mathsf{fma}\left(s1, \frac{a2 - a1}{s1 - s2}, a1\right)}$
Proof
[Start]10.3 $a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$ $\color{blue}{a1 + \left(-\frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}\right)}$ $\color{blue}{\left(-\frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}\right) + a1}$ $\color{blue}{\frac{-s1 \cdot \left(a2 - a1\right)}{s2 - s1}} + a1$ $\frac{\color{blue}{s1 \cdot \left(-\left(a2 - a1\right)\right)}}{s2 - s1} + a1$ $\color{blue}{s1 \cdot \frac{-\left(a2 - a1\right)}{s2 - s1}} + a1$ $\color{blue}{\mathsf{fma}\left(s1, \frac{-\left(a2 - a1\right)}{s2 - s1}, a1\right)}$

## if -1.9999999999999998e-308 < (-.f64 a1 (/.f64 (*.f64 s1 (-.f64 a2 a1)) (-.f64 s2 s1))) < 0.0

1. Initial program 60.5

$a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$
2. Simplified60.4

$\leadsto \color{blue}{\mathsf{fma}\left(s1, \frac{a2 - a1}{s1 - s2}, a1\right)}$
Proof
[Start]60.5 $a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$ $\color{blue}{a1 + \left(-\frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}\right)}$ $\color{blue}{\left(-\frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}\right) + a1}$ $\color{blue}{\frac{-s1 \cdot \left(a2 - a1\right)}{s2 - s1}} + a1$ $\frac{\color{blue}{s1 \cdot \left(-\left(a2 - a1\right)\right)}}{s2 - s1} + a1$ $\color{blue}{s1 \cdot \frac{-\left(a2 - a1\right)}{s2 - s1}} + a1$ $\color{blue}{\mathsf{fma}\left(s1, \frac{-\left(a2 - a1\right)}{s2 - s1}, a1\right)}$
3. Taylor expanded in s1 around inf 5.0

$\leadsto \color{blue}{\frac{\left(a2 - a1\right) \cdot s2}{s1} + a2}$
4. Simplified0.2

$\leadsto \color{blue}{a2 + \frac{a2 - a1}{\frac{s1}{s2}}}$
Proof
[Start]5.0 $\frac{\left(a2 - a1\right) \cdot s2}{s1} + a2$ $\color{blue}{a2 + \frac{\left(a2 - a1\right) \cdot s2}{s1}}$ $a2 + \color{blue}{\frac{a2 - a1}{\frac{s1}{s2}}}$
5. Applied egg-rr0.2

$\leadsto a2 + \color{blue}{\frac{a2 - a1}{s1} \cdot s2}$

## if 0.0 < (-.f64 a1 (/.f64 (*.f64 s1 (-.f64 a2 a1)) (-.f64 s2 s1)))

1. Initial program 10.3

$a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$
2. Simplified4.5

$\leadsto \color{blue}{a1 - \frac{s1}{\frac{s2 - s1}{a2 - a1}}}$
Proof
[Start]10.3 $a1 - \frac{s1 \cdot \left(a2 - a1\right)}{s2 - s1}$ $a1 - \color{blue}{\frac{s1}{\frac{s2 - s1}{a2 - a1}}}$
3. Recombined 3 regimes into one program.
4. Final simplification3.7

$\leadsto \begin{array}{l} \mathbf{if}\;a1 + \frac{s1 \cdot \left(a1 - a2\right)}{s2 - s1} \leq -2 \cdot 10^{-308}:\\ \;\;\;\;\mathsf{fma}\left(s1, \frac{a2 - a1}{s1 - s2}, a1\right)\\ \mathbf{elif}\;a1 + \frac{s1 \cdot \left(a1 - a2\right)}{s2 - s1} \leq 0:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \mathbf{else}:\\ \;\;\;\;a1 - \frac{s1}{\frac{s2 - s1}{a2 - a1}}\\ \end{array}$

# Alternatives

Alternative 1
Error3.7
Cost2248
$\begin{array}{l} t_0 := a1 + \frac{s1 \cdot \left(a1 - a2\right)}{s2 - s1}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-308}:\\ \;\;\;\;a1 + \frac{s1}{s2 - s1} \cdot \left(a1 - a2\right)\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \mathbf{else}:\\ \;\;\;\;a1 - \frac{s1}{\frac{s2 - s1}{a2 - a1}}\\ \end{array}$
Alternative 2
Error27.4
Cost1240
$\begin{array}{l} t_0 := a2 - a1 \cdot \frac{s2}{s1}\\ \mathbf{if}\;a1 \leq -1.02 \cdot 10^{-16}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq -1.56 \cdot 10^{-85}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;a1 \leq -1.55 \cdot 10^{-128}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 9.5 \cdot 10^{-203}:\\ \;\;\;\;a2 \cdot \frac{s1}{s1 - s2}\\ \mathbf{elif}\;a1 \leq 9 \cdot 10^{-154}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 7.4 \cdot 10^{-41}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;a1\\ \end{array}$
Alternative 3
Error27.2
Cost1240
$\begin{array}{l} t_0 := a2 - a1 \cdot \frac{s2}{s1}\\ t_1 := \frac{s1}{s1 - s2}\\ t_2 := a1 \cdot \left(1 - t_1\right)\\ \mathbf{if}\;a1 \leq -1.86 \cdot 10^{-16}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a1 \leq -1.65 \cdot 10^{-85}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;a1 \leq -1.95 \cdot 10^{-129}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 1.6 \cdot 10^{-203}:\\ \;\;\;\;a2 \cdot t_1\\ \mathbf{elif}\;a1 \leq 1.85 \cdot 10^{-153}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a1 \leq 3.2 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;a1\\ \end{array}$
Alternative 4
Error27.1
Cost1240
$\begin{array}{l} t_0 := \frac{s1}{s1 - s2}\\ t_1 := a1 \cdot \left(1 - t_0\right)\\ \mathbf{if}\;a1 \leq -9.6 \cdot 10^{-17}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a1 \leq -1.06 \cdot 10^{-85}:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \mathbf{elif}\;a1 \leq -5.6 \cdot 10^{-127}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 9.5 \cdot 10^{-203}:\\ \;\;\;\;a2 \cdot t_0\\ \mathbf{elif}\;a1 \leq 7.5 \cdot 10^{-154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a1 \leq 2.6 \cdot 10^{-40}:\\ \;\;\;\;a2 - a1 \cdot \frac{s2}{s1}\\ \mathbf{else}:\\ \;\;\;\;a1\\ \end{array}$
Alternative 5
Error28.5
Cost1040
$\begin{array}{l} \mathbf{if}\;a1 \leq -2.9 \cdot 10^{-130}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 8.6 \cdot 10^{-203}:\\ \;\;\;\;a2 \cdot \frac{s1}{s1 - s2}\\ \mathbf{elif}\;a1 \leq 8.2 \cdot 10^{-154}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 6.8 \cdot 10^{-108}:\\ \;\;\;\;-\frac{s1}{\frac{s2 - s1}{a2}}\\ \mathbf{elif}\;a1 \leq 9.5 \cdot 10^{-89}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 2.9 \cdot 10^{-43}:\\ \;\;\;\;a2\\ \mathbf{else}:\\ \;\;\;\;a1\\ \end{array}$
Alternative 6
Error28.6
Cost976
$\begin{array}{l} t_0 := a2 \cdot \frac{s1}{s1 - s2}\\ \mathbf{if}\;a1 \leq -9.6 \cdot 10^{-130}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 9.5 \cdot 10^{-203}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;a1 \leq 3 \cdot 10^{-153}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 3.6 \cdot 10^{-107}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;a1 \leq 6.2 \cdot 10^{-92}:\\ \;\;\;\;a1\\ \mathbf{elif}\;a1 \leq 2.9 \cdot 10^{-43}:\\ \;\;\;\;a2\\ \mathbf{else}:\\ \;\;\;\;a1\\ \end{array}$
Alternative 7
Error9.9
Cost968
$\begin{array}{l} \mathbf{if}\;s1 \leq -7.2 \cdot 10^{-51}:\\ \;\;\;\;a2 - a1 \cdot \frac{s2}{s1}\\ \mathbf{elif}\;s1 \leq 4.8 \cdot 10^{-116}:\\ \;\;\;\;a1 + \frac{s1}{s2 - s1} \cdot \left(a1 - a2\right)\\ \mathbf{else}:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \end{array}$
Alternative 8
Error9.9
Cost968
$\begin{array}{l} \mathbf{if}\;s1 \leq -7.2 \cdot 10^{-51}:\\ \;\;\;\;a2 - a1 \cdot \frac{s2}{s1}\\ \mathbf{elif}\;s1 \leq 6 \cdot 10^{-115}:\\ \;\;\;\;a1 + s1 \cdot \frac{a1 - a2}{s2 - s1}\\ \mathbf{else}:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \end{array}$
Alternative 9
Error11.4
Cost840
$\begin{array}{l} \mathbf{if}\;s1 \leq -1.65 \cdot 10^{-84}:\\ \;\;\;\;a2 - a1 \cdot \frac{s2}{s1}\\ \mathbf{elif}\;s1 \leq 5.8 \cdot 10^{-117}:\\ \;\;\;\;a1 - s1 \cdot \frac{a2}{s2 - s1}\\ \mathbf{else}:\\ \;\;\;\;a2 + s2 \cdot \frac{a2 - a1}{s1}\\ \end{array}$
Alternative 10
Error25.3
Cost328
$\begin{array}{l} \mathbf{if}\;s1 \leq -1.1 \cdot 10^{-157}:\\ \;\;\;\;a2\\ \mathbf{elif}\;s1 \leq 2.7 \cdot 10^{-138}:\\ \;\;\;\;a1\\ \mathbf{else}:\\ \;\;\;\;a2\\ \end{array}$
Alternative 11
Error38.3
Cost64
$a1$

# Reproduce?

herbie shell --seed 1
(FPCore (a1 s1 a2 s2)
:name "a1 - s1 * (a2 - a1)/(s2 - s1)"
:precision binary64
:pre (and (and (and (and (<= -1000.0 a1) (<= a1 1000.0)) (and (<= -1000.0 s1) (<= s1 1000.0))) (and (<= -1000.0 a2) (<= a2 1000.0))) (and (<= -1000.0 s2) (<= s2 1000.0)))
(- a1 (/ (* s1 (- a2 a1)) (- s2 s1))))