# ?

Average Error: 6.0 → 3.1
Time: 22.6s
Precision: binary64
Cost: 26692

# ?

$\left(\left(-1 \leq p_2 \land p_2 \leq 1\right) \land \left(-1 \leq p_1 \land p_1 \leq 1\right)\right) \land \left(-1 \leq p_3 \land p_3 \leq 1\right)$
$\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\left(2 \cdot p_2 - p_1\right) - p_3}$
$\begin{array}{l} t_0 := p_2 \cdot 2 - \left(p_1 + p_3\right)\\ \mathbf{if}\;p_2 \leq -4 \cdot 10^{-310}:\\ \;\;\;\;\frac{p_2}{t_0} + \left(\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{t_0} - \frac{p_1}{t_0}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{p_3 + \mathsf{fma}\left(-2, p_2, p_1\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}\\ \end{array}$
(FPCore (p_2 p_1 p_3)
:precision binary64
(/
(+ (- p_2 p_1) (sqrt (- (* p_1 p_2) (* p_3 p_2))))
(- (- (* 2.0 p_2) p_1) p_3)))
(FPCore (p_2 p_1 p_3)
:precision binary64
(let* ((t_0 (- (* p_2 2.0) (+ p_1 p_3))))
(if (<= p_2 -4e-310)
(+ (/ p_2 t_0) (- (/ (sqrt (* p_2 (- p_1 p_3))) t_0) (/ p_1 t_0)))
(/
1.0
(/
(+ p_3 (fma -2.0 p_2 p_1))
(- p_1 (fma (sqrt p_2) (sqrt (- p_1 p_3)) p_2)))))))
double code(double p_2, double p_1, double p_3) {
return ((p_2 - p_1) + sqrt(((p_1 * p_2) - (p_3 * p_2)))) / (((2.0 * p_2) - p_1) - p_3);
}

double code(double p_2, double p_1, double p_3) {
double t_0 = (p_2 * 2.0) - (p_1 + p_3);
double tmp;
if (p_2 <= -4e-310) {
tmp = (p_2 / t_0) + ((sqrt((p_2 * (p_1 - p_3))) / t_0) - (p_1 / t_0));
} else {
tmp = 1.0 / ((p_3 + fma(-2.0, p_2, p_1)) / (p_1 - fma(sqrt(p_2), sqrt((p_1 - p_3)), p_2)));
}
return tmp;
}

function code(p_2, p_1, p_3)
return Float64(Float64(Float64(p_2 - p_1) + sqrt(Float64(Float64(p_1 * p_2) - Float64(p_3 * p_2)))) / Float64(Float64(Float64(2.0 * p_2) - p_1) - p_3))
end

function code(p_2, p_1, p_3)
t_0 = Float64(Float64(p_2 * 2.0) - Float64(p_1 + p_3))
tmp = 0.0
if (p_2 <= -4e-310)
tmp = Float64(Float64(p_2 / t_0) + Float64(Float64(sqrt(Float64(p_2 * Float64(p_1 - p_3))) / t_0) - Float64(p_1 / t_0)));
else
tmp = Float64(1.0 / Float64(Float64(p_3 + fma(-2.0, p_2, p_1)) / Float64(p_1 - fma(sqrt(p_2), sqrt(Float64(p_1 - p_3)), p_2))));
end
return tmp
end

code[p$95$2_, p$95$1_, p$95$3_] := N[(N[(N[(p$95$2 - p$95$1), $MachinePrecision] + N[Sqrt[N[(N[(p$95$1 * p$95$2),$MachinePrecision] - N[(p$95$3 * p$95$2), $MachinePrecision]),$MachinePrecision]], $MachinePrecision]),$MachinePrecision] / N[(N[(N[(2.0 * p$95$2), $MachinePrecision] - p$95$1),$MachinePrecision] - p$95$3), $MachinePrecision]),$MachinePrecision]

code[p$95$2_, p$95$1_, p$95$3_] := Block[{t$95$0 = N[(N[(p$95$2 * 2.0), $MachinePrecision] - N[(p$95$1 + p$95$3),$MachinePrecision]), $MachinePrecision]}, If[LessEqual[p$95$2, -4e-310], N[(N[(p$95$2 / t$95$0),$MachinePrecision] + N[(N[(N[Sqrt[N[(p$95$2 * N[(p$95$1 - p$95$3), $MachinePrecision]),$MachinePrecision]], $MachinePrecision] / t$95$0),$MachinePrecision] - N[(p$95$1 / t$95$0), $MachinePrecision]),$MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(p$95$3 + N[(-2.0 * p$95$2 + p$95$1),$MachinePrecision]), $MachinePrecision] / N[(p$95$1 - N[(N[Sqrt[p$95$2],$MachinePrecision] * N[Sqrt[N[(p$95$1 - p$95$3), $MachinePrecision]],$MachinePrecision] + p$95$2), $MachinePrecision]),$MachinePrecision]), $MachinePrecision]),$MachinePrecision]]]

\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\left(2 \cdot p_2 - p_1\right) - p_3}

\begin{array}{l}
t_0 := p_2 \cdot 2 - \left(p_1 + p_3\right)\\
\mathbf{if}\;p_2 \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\frac{p_2}{t_0} + \left(\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{t_0} - \frac{p_1}{t_0}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{p_3 + \mathsf{fma}\left(-2, p_2, p_1\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}\\

\end{array}


# Derivation?

1. Split input into 2 regimes
2. ## if p_2 < -3.999999999999988e-310

1. Initial program 6.0

$\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\left(2 \cdot p_2 - p_1\right) - p_3}$
2. Applied egg-rr6.0

$\leadsto \color{blue}{\frac{p_2}{p_2 \cdot 2 - \left(p_1 + p_3\right)} - \frac{p_1 - \sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{p_2 \cdot 2 - \left(p_1 + p_3\right)}}$
3. Applied egg-rr6.0

$\leadsto \frac{p_2}{p_2 \cdot 2 - \left(p_1 + p_3\right)} - \color{blue}{\left(\frac{p_1}{p_2 \cdot 2 - \left(p_1 + p_3\right)} - \frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{p_2 \cdot 2 - \left(p_1 + p_3\right)}\right)}$

## if -3.999999999999988e-310 < p_2

1. Initial program 5.9

$\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\left(2 \cdot p_2 - p_1\right) - p_3}$
2. Simplified5.9

$\leadsto \color{blue}{\frac{p_1 - \left(p_2 + \sqrt{p_2 \cdot \left(p_1 - p_3\right)}\right)}{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}}$
Proof
[Start]5.9 $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\left(2 \cdot p_2 - p_1\right) - p_3}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{\left(2 \cdot p_2 - p_1\right) + \left(-p_3\right)}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{2 \cdot p_2 - \left(p_1 - \left(-p_3\right)\right)}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{2 \cdot p_2 + \left(-\left(p_1 - \left(-p_3\right)\right)\right)}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{\left(-\left(p_1 - \left(-p_3\right)\right)\right) + 2 \cdot p_2}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{\left(0 - \left(p_1 - \left(-p_3\right)\right)\right)} + 2 \cdot p_2}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{0 - \left(\left(p_1 - \left(-p_3\right)\right) - 2 \cdot p_2\right)}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{0 - \color{blue}{\left(p_1 - \left(\left(-p_3\right) + 2 \cdot p_2\right)\right)}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{-\left(p_1 - \left(\left(-p_3\right) + 2 \cdot p_2\right)\right)}}$ $\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{\color{blue}{-1 \cdot \left(p_1 - \left(\left(-p_3\right) + 2 \cdot p_2\right)\right)}}$ $\color{blue}{\frac{\frac{\left(p_2 - p_1\right) + \sqrt{p_1 \cdot p_2 - p_3 \cdot p_2}}{-1}}{p_1 - \left(\left(-p_3\right) + 2 \cdot p_2\right)}}$
3. Applied egg-rr7.9

$\leadsto \frac{p_1 - \color{blue}{\frac{p_2 \cdot \left(p_1 - p_3\right) - p_2 \cdot p_2}{\sqrt{p_2 \cdot \left(p_1 - p_3\right)} - p_2}}}{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}$
4. Applied egg-rr0.2

$\leadsto \color{blue}{{\left(\frac{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}\right)}^{-1}}$
5. Simplified0.2

$\leadsto \color{blue}{\frac{1}{\frac{p_3 + \mathsf{fma}\left(-2, p_2, p_1\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}}$
Proof
[Start]0.2 ${\left(\frac{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}\right)}^{-1}$ $\color{blue}{\frac{1}{\frac{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}}$ $\frac{1}{\frac{\color{blue}{\mathsf{fma}\left(p_2, -2, p_3\right) + p_1}}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}$ $\frac{1}{\frac{\color{blue}{\left(p_2 \cdot -2 + p_3\right)} + p_1}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}$ $\frac{1}{\frac{\left(\color{blue}{-2 \cdot p_2} + p_3\right) + p_1}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}$ $\frac{1}{\frac{\color{blue}{\left(p_3 + -2 \cdot p_2\right)} + p_1}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}$ $\frac{1}{\frac{\color{blue}{p_3 + \left(-2 \cdot p_2 + p_1\right)}}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}$ $\frac{1}{\frac{p_3 + \color{blue}{\mathsf{fma}\left(-2, p_2, p_1\right)}}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}$
3. Recombined 2 regimes into one program.
4. Final simplification3.1

$\leadsto \begin{array}{l} \mathbf{if}\;p_2 \leq -4 \cdot 10^{-310}:\\ \;\;\;\;\frac{p_2}{p_2 \cdot 2 - \left(p_1 + p_3\right)} + \left(\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{p_2 \cdot 2 - \left(p_1 + p_3\right)} - \frac{p_1}{p_2 \cdot 2 - \left(p_1 + p_3\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{p_3 + \mathsf{fma}\left(-2, p_2, p_1\right)}{p_1 - \mathsf{fma}\left(\sqrt{p_2}, \sqrt{p_1 - p_3}, p_2\right)}}\\ \end{array}$

# Alternatives

Alternative 1
Error3.0
Cost20292
$\begin{array}{l} t_0 := p_2 \cdot 2 - \left(p_1 + p_3\right)\\ \mathbf{if}\;p_2 \leq -4 \cdot 10^{-310}:\\ \;\;\;\;\frac{p_2}{t_0} + \left(\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{t_0} - \frac{p_1}{t_0}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{p_1 - \left(p_2 + \sqrt{p_2} \cdot \sqrt{p_1 - p_3}\right)}{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}\\ \end{array}$
Alternative 2
Error3.0
Cost14020
$\begin{array}{l} t_0 := p_2 \cdot 2 - \left(p_1 + p_3\right)\\ \mathbf{if}\;p_2 \leq -4 \cdot 10^{-310}:\\ \;\;\;\;\frac{p_2}{t_0} + \left(\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{t_0} - \frac{p_1}{t_0}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{p_2} \cdot \sqrt{p_1 - p_3} + \left(p_2 - p_1\right)}{\left(p_2 \cdot 2 - p_1\right) - p_3}\\ \end{array}$
Alternative 3
Error5.9
Cost8512
$\begin{array}{l} t_0 := p_2 \cdot 2 - \left(p_1 + p_3\right)\\ \frac{p_2}{t_0} + \left(\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)}}{t_0} - \frac{p_1}{t_0}\right) \end{array}$
Alternative 4
Error5.9
Cost8000
$\begin{array}{l} t_0 := p_2 \cdot 2 - \left(p_1 + p_3\right)\\ \frac{p_2}{t_0} + \frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)} - p_1}{t_0} \end{array}$
Alternative 5
Error10.9
Cost7689
$\begin{array}{l} t_0 := \left(p_2 \cdot 2 - p_1\right) - p_3\\ \mathbf{if}\;p_1 \leq -4.2 \cdot 10^{-39} \lor \neg \left(p_1 \leq 1.2 \cdot 10^{-113}\right):\\ \;\;\;\;\frac{\left(p_2 + \sqrt{p_2 \cdot p_1}\right) - p_1}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(p_2 - p_1\right) + \sqrt{p_2 \cdot \left(-p_3\right)}}{t_0}\\ \end{array}$
Alternative 6
Error19.0
Cost7625
$\begin{array}{l} \mathbf{if}\;p_3 \leq -1 \cdot 10^{-174} \lor \neg \left(p_3 \leq 4.2 \cdot 10^{-204}\right):\\ \;\;\;\;\frac{p_1 - p_2}{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(p_2 + \sqrt{p_2 \cdot p_1}\right) - p_1}{\left(p_2 \cdot 2 - p_1\right) - p_3}\\ \end{array}$
Alternative 7
Error6.0
Cost7616
$\frac{\left(p_2 - p_1\right) + \sqrt{p_2 \cdot p_1 - p_2 \cdot p_3}}{\left(p_2 \cdot 2 - p_1\right) - p_3}$
Alternative 8
Error6.0
Cost7488
$\frac{\sqrt{p_2 \cdot \left(p_1 - p_3\right)} + \left(p_2 - p_1\right)}{\left(p_2 \cdot 2 - p_1\right) - p_3}$
Alternative 9
Error19.7
Cost6976
$\frac{p_1 - p_2}{p_1 + \mathsf{fma}\left(p_2, -2, p_3\right)}$
Alternative 10
Error23.8
Cost1224
$\begin{array}{l} \mathbf{if}\;p_2 \leq -7.5 \cdot 10^{-91}:\\ \;\;\;\;\frac{p_2}{p_2 \cdot 2 - p_3}\\ \mathbf{elif}\;p_2 \leq 7 \cdot 10^{-93}:\\ \;\;\;\;\frac{p_2}{p_2 \cdot 2 - \left(p_1 + p_3\right)} + \frac{p_1}{p_1 + p_3}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array}$
Alternative 11
Error26.0
Cost584
$\begin{array}{l} \mathbf{if}\;p_2 \leq -6.6 \cdot 10^{-146}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;p_2 \leq 10^{-110}:\\ \;\;\;\;\frac{p_1}{p_1 + p_3}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array}$
Alternative 12
Error30.4
Cost328
$\begin{array}{l} \mathbf{if}\;p_2 \leq -1.06 \cdot 10^{-157}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;p_2 \leq 2 \cdot 10^{-149}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array}$
Alternative 13
Error40.1
Cost64
$0.5$

# Reproduce?

herbie shell --seed 1
(FPCore (p_2 p_1 p_3)
:name "(p_2-p_1 + sqrt(p_1*p_2 - p_3*p_2)) / (2*p_2 - p_1 - p_3)"
:precision binary64
:pre (and (and (and (<= -1.0 p_2) (<= p_2 1.0)) (and (<= -1.0 p_1) (<= p_1 1.0))) (and (<= -1.0 p_3) (<= p_3 1.0)))
(/ (+ (- p_2 p_1) (sqrt (- (* p_1 p_2) (* p_3 p_2)))) (- (- (* 2.0 p_2) p_1) p_3)))