1/2(abs(p)+abs(r) - sqrt((p-r)^2 + 4q^2))

Percentage Accurate: 24.1% → 57.8%
Time: 9.2s
Alternatives: 7
Speedup: 83.3×

Specification

?
\[\begin{array}{l} \\ \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \end{array} \]
(FPCore (p r q)
 :precision binary64
 (*
  (/ 1.0 2.0)
  (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
	return (1.0 / 2.0) * ((fabs(p) + fabs(r)) - sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
real(8) function code(p, r, q)
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q
    code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
	return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q):
	return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q)
	return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0))))))
end
function tmp = code(p, r, q)
	tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0)))));
end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 24.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \end{array} \]
(FPCore (p r q)
 :precision binary64
 (*
  (/ 1.0 2.0)
  (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
	return (1.0 / 2.0) * ((fabs(p) + fabs(r)) - sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
real(8) function code(p, r, q)
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q
    code = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) - sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
end function
public static double code(double p, double r, double q) {
	return (1.0 / 2.0) * ((Math.abs(p) + Math.abs(r)) - Math.sqrt((Math.pow((p - r), 2.0) + (4.0 * Math.pow(q, 2.0)))));
}
def code(p, r, q):
	return (1.0 / 2.0) * ((math.fabs(p) + math.fabs(r)) - math.sqrt((math.pow((p - r), 2.0) + (4.0 * math.pow(q, 2.0)))))
function code(p, r, q)
	return Float64(Float64(1.0 / 2.0) * Float64(Float64(abs(p) + abs(r)) - sqrt(Float64((Float64(p - r) ^ 2.0) + Float64(4.0 * (q ^ 2.0))))))
end
function tmp = code(p, r, q)
	tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) - sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0)))));
end
code[p_, r_, q_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(p - r), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[Power[q, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)
\end{array}

Alternative 1: 57.8% accurate, 0.9× speedup?

\[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;{q\_m}^{2} \leq 10^{-108}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\ \mathbf{elif}\;{q\_m}^{2} \leq 2 \cdot 10^{+31}:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right), \frac{r}{q\_m \cdot q\_m}, \frac{-1}{r}\right) \cdot \left(q\_m \cdot q\_m\right)\\ \mathbf{else}:\\ \;\;\;\;-q\_m\\ \end{array} \end{array} \]
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
 :precision binary64
 (if (<= (pow q_m 2.0) 1e-108)
   (* (fma (/ (+ (fabs p) (- (fabs r) r)) p) -0.5 -0.5) (- p))
   (if (<= (pow q_m 2.0) 2e+31)
     (*
      (fma
       (fma (/ (+ (+ (fabs r) p) (fabs p)) r) 0.5 -0.5)
       (/ r (* q_m q_m))
       (/ -1.0 r))
      (* q_m q_m))
     (- q_m))))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
	double tmp;
	if (pow(q_m, 2.0) <= 1e-108) {
		tmp = fma(((fabs(p) + (fabs(r) - r)) / p), -0.5, -0.5) * -p;
	} else if (pow(q_m, 2.0) <= 2e+31) {
		tmp = fma(fma((((fabs(r) + p) + fabs(p)) / r), 0.5, -0.5), (r / (q_m * q_m)), (-1.0 / r)) * (q_m * q_m);
	} else {
		tmp = -q_m;
	}
	return tmp;
}
q_m = abs(q)
p, r, q_m = sort([p, r, q_m])
function code(p, r, q_m)
	tmp = 0.0
	if ((q_m ^ 2.0) <= 1e-108)
		tmp = Float64(fma(Float64(Float64(abs(p) + Float64(abs(r) - r)) / p), -0.5, -0.5) * Float64(-p));
	elseif ((q_m ^ 2.0) <= 2e+31)
		tmp = Float64(fma(fma(Float64(Float64(Float64(abs(r) + p) + abs(p)) / r), 0.5, -0.5), Float64(r / Float64(q_m * q_m)), Float64(-1.0 / r)) * Float64(q_m * q_m));
	else
		tmp = Float64(-q_m);
	end
	return tmp
end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := If[LessEqual[N[Power[q$95$m, 2.0], $MachinePrecision], 1e-108], N[(N[(N[(N[(N[Abs[p], $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]), $MachinePrecision] / p), $MachinePrecision] * -0.5 + -0.5), $MachinePrecision] * (-p)), $MachinePrecision], If[LessEqual[N[Power[q$95$m, 2.0], $MachinePrecision], 2e+31], N[(N[(N[(N[(N[(N[(N[Abs[r], $MachinePrecision] + p), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] / r), $MachinePrecision] * 0.5 + -0.5), $MachinePrecision] * N[(r / N[(q$95$m * q$95$m), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / r), $MachinePrecision]), $MachinePrecision] * N[(q$95$m * q$95$m), $MachinePrecision]), $MachinePrecision], (-q$95$m)]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;{q\_m}^{2} \leq 10^{-108}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\

\mathbf{elif}\;{q\_m}^{2} \leq 2 \cdot 10^{+31}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right), \frac{r}{q\_m \cdot q\_m}, \frac{-1}{r}\right) \cdot \left(q\_m \cdot q\_m\right)\\

\mathbf{else}:\\
\;\;\;\;-q\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (pow.f64 q #s(literal 2 binary64)) < 1.00000000000000004e-108

    1. Initial program 22.7%

      \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in q around inf

      \[\leadsto \color{blue}{-1 \cdot q} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
      2. lower-neg.f645.9

        \[\leadsto \color{blue}{-q} \]
    5. Applied rewrites5.9%

      \[\leadsto \color{blue}{-q} \]
    6. Taylor expanded in p around -inf

      \[\leadsto \color{blue}{-1 \cdot \left(p \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right)\right)} \]
    7. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \color{blue}{\left(-1 \cdot p\right) \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right)} \]
      2. mul-1-negN/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(p\right)\right)} \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right) \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(p\right)\right) \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right)} \]
      4. lower-neg.f64N/A

        \[\leadsto \color{blue}{\left(-p\right)} \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right) \]
      5. sub-negN/A

        \[\leadsto \left(-p\right) \cdot \color{blue}{\left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)\right)} \]
      6. *-commutativeN/A

        \[\leadsto \left(-p\right) \cdot \left(\color{blue}{\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} \cdot \frac{-1}{2}} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)\right) \]
      7. metadata-evalN/A

        \[\leadsto \left(-p\right) \cdot \left(\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} \cdot \frac{-1}{2} + \color{blue}{\frac{-1}{2}}\right) \]
      8. lower-fma.f64N/A

        \[\leadsto \left(-p\right) \cdot \color{blue}{\mathsf{fma}\left(\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p}, \frac{-1}{2}, \frac{-1}{2}\right)} \]
    8. Applied rewrites29.4%

      \[\leadsto \color{blue}{\left(-p\right) \cdot \mathsf{fma}\left(\frac{\left(\left|r\right| - r\right) + \left|p\right|}{p}, -0.5, -0.5\right)} \]

    if 1.00000000000000004e-108 < (pow.f64 q #s(literal 2 binary64)) < 1.9999999999999999e31

    1. Initial program 18.8%

      \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in r around inf

      \[\leadsto \color{blue}{r \cdot \left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
    5. Applied rewrites11.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(-q, \frac{q}{r \cdot r}, \mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right)\right) \cdot r} \]
    6. Taylor expanded in q around inf

      \[\leadsto {q}^{2} \cdot \color{blue}{\left(\frac{r \cdot \left(\frac{1}{2} \cdot \frac{p + \left(\left|p\right| + \left|r\right|\right)}{r} - \frac{1}{2}\right)}{{q}^{2}} - \frac{1}{r}\right)} \]
    7. Step-by-step derivation
      1. Applied rewrites20.1%

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\frac{\left(p + \left|r\right|\right) + \left|p\right|}{r}, 0.5, -0.5\right), \frac{r}{q \cdot q}, \frac{-1}{r}\right) \cdot \color{blue}{\left(q \cdot q\right)} \]

      if 1.9999999999999999e31 < (pow.f64 q #s(literal 2 binary64))

      1. Initial program 25.8%

        \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
      2. Add Preprocessing
      3. Taylor expanded in q around inf

        \[\leadsto \color{blue}{-1 \cdot q} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
        2. lower-neg.f6428.2

          \[\leadsto \color{blue}{-q} \]
      5. Applied rewrites28.2%

        \[\leadsto \color{blue}{-q} \]
    8. Recombined 3 regimes into one program.
    9. Final simplification27.6%

      \[\leadsto \begin{array}{l} \mathbf{if}\;{q}^{2} \leq 10^{-108}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\ \mathbf{elif}\;{q}^{2} \leq 2 \cdot 10^{+31}:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right), \frac{r}{q \cdot q}, \frac{-1}{r}\right) \cdot \left(q \cdot q\right)\\ \mathbf{else}:\\ \;\;\;\;-q\\ \end{array} \]
    10. Add Preprocessing

    Alternative 2: 58.1% accurate, 1.1× speedup?

    \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;{q\_m}^{2} \leq 10^{-108}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\ \mathbf{elif}\;{q\_m}^{2} \leq 2 \cdot 10^{+31}:\\ \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\ \mathbf{else}:\\ \;\;\;\;-q\_m\\ \end{array} \end{array} \]
    q_m = (fabs.f64 q)
    NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
    (FPCore (p r q_m)
     :precision binary64
     (if (<= (pow q_m 2.0) 1e-108)
       (* (fma (/ (+ (fabs p) (- (fabs r) r)) p) -0.5 -0.5) (- p))
       (if (<= (pow q_m 2.0) 2e+31) (/ (* (- q_m) q_m) r) (- q_m))))
    q_m = fabs(q);
    assert(p < r && r < q_m);
    double code(double p, double r, double q_m) {
    	double tmp;
    	if (pow(q_m, 2.0) <= 1e-108) {
    		tmp = fma(((fabs(p) + (fabs(r) - r)) / p), -0.5, -0.5) * -p;
    	} else if (pow(q_m, 2.0) <= 2e+31) {
    		tmp = (-q_m * q_m) / r;
    	} else {
    		tmp = -q_m;
    	}
    	return tmp;
    }
    
    q_m = abs(q)
    p, r, q_m = sort([p, r, q_m])
    function code(p, r, q_m)
    	tmp = 0.0
    	if ((q_m ^ 2.0) <= 1e-108)
    		tmp = Float64(fma(Float64(Float64(abs(p) + Float64(abs(r) - r)) / p), -0.5, -0.5) * Float64(-p));
    	elseif ((q_m ^ 2.0) <= 2e+31)
    		tmp = Float64(Float64(Float64(-q_m) * q_m) / r);
    	else
    		tmp = Float64(-q_m);
    	end
    	return tmp
    end
    
    q_m = N[Abs[q], $MachinePrecision]
    NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
    code[p_, r_, q$95$m_] := If[LessEqual[N[Power[q$95$m, 2.0], $MachinePrecision], 1e-108], N[(N[(N[(N[(N[Abs[p], $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]), $MachinePrecision] / p), $MachinePrecision] * -0.5 + -0.5), $MachinePrecision] * (-p)), $MachinePrecision], If[LessEqual[N[Power[q$95$m, 2.0], $MachinePrecision], 2e+31], N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / r), $MachinePrecision], (-q$95$m)]]
    
    \begin{array}{l}
    q_m = \left|q\right|
    \\
    [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;{q\_m}^{2} \leq 10^{-108}:\\
    \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\
    
    \mathbf{elif}\;{q\_m}^{2} \leq 2 \cdot 10^{+31}:\\
    \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\
    
    \mathbf{else}:\\
    \;\;\;\;-q\_m\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if (pow.f64 q #s(literal 2 binary64)) < 1.00000000000000004e-108

      1. Initial program 22.7%

        \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
      2. Add Preprocessing
      3. Taylor expanded in q around inf

        \[\leadsto \color{blue}{-1 \cdot q} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
        2. lower-neg.f645.9

          \[\leadsto \color{blue}{-q} \]
      5. Applied rewrites5.9%

        \[\leadsto \color{blue}{-q} \]
      6. Taylor expanded in p around -inf

        \[\leadsto \color{blue}{-1 \cdot \left(p \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right)\right)} \]
      7. Step-by-step derivation
        1. associate-*r*N/A

          \[\leadsto \color{blue}{\left(-1 \cdot p\right) \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right)} \]
        2. mul-1-negN/A

          \[\leadsto \color{blue}{\left(\mathsf{neg}\left(p\right)\right)} \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right) \]
        3. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(\mathsf{neg}\left(p\right)\right) \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right)} \]
        4. lower-neg.f64N/A

          \[\leadsto \color{blue}{\left(-p\right)} \cdot \left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} - \frac{1}{2}\right) \]
        5. sub-negN/A

          \[\leadsto \left(-p\right) \cdot \color{blue}{\left(\frac{-1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)\right)} \]
        6. *-commutativeN/A

          \[\leadsto \left(-p\right) \cdot \left(\color{blue}{\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} \cdot \frac{-1}{2}} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)\right) \]
        7. metadata-evalN/A

          \[\leadsto \left(-p\right) \cdot \left(\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p} \cdot \frac{-1}{2} + \color{blue}{\frac{-1}{2}}\right) \]
        8. lower-fma.f64N/A

          \[\leadsto \left(-p\right) \cdot \color{blue}{\mathsf{fma}\left(\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p}, \frac{-1}{2}, \frac{-1}{2}\right)} \]
      8. Applied rewrites29.4%

        \[\leadsto \color{blue}{\left(-p\right) \cdot \mathsf{fma}\left(\frac{\left(\left|r\right| - r\right) + \left|p\right|}{p}, -0.5, -0.5\right)} \]

      if 1.00000000000000004e-108 < (pow.f64 q #s(literal 2 binary64)) < 1.9999999999999999e31

      1. Initial program 18.8%

        \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
      2. Add Preprocessing
      3. Taylor expanded in r around inf

        \[\leadsto \color{blue}{r \cdot \left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
        2. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
      5. Applied rewrites11.3%

        \[\leadsto \color{blue}{\mathsf{fma}\left(-q, \frac{q}{r \cdot r}, \mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right)\right) \cdot r} \]
      6. Taylor expanded in r around 0

        \[\leadsto -1 \cdot \color{blue}{\frac{{q}^{2}}{r}} \]
      7. Step-by-step derivation
        1. Applied rewrites19.2%

          \[\leadsto \frac{\left(-q\right) \cdot q}{\color{blue}{r}} \]

        if 1.9999999999999999e31 < (pow.f64 q #s(literal 2 binary64))

        1. Initial program 25.8%

          \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
        2. Add Preprocessing
        3. Taylor expanded in q around inf

          \[\leadsto \color{blue}{-1 \cdot q} \]
        4. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
          2. lower-neg.f6428.2

            \[\leadsto \color{blue}{-q} \]
        5. Applied rewrites28.2%

          \[\leadsto \color{blue}{-q} \]
      8. Recombined 3 regimes into one program.
      9. Final simplification27.5%

        \[\leadsto \begin{array}{l} \mathbf{if}\;{q}^{2} \leq 10^{-108}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\ \mathbf{elif}\;{q}^{2} \leq 2 \cdot 10^{+31}:\\ \;\;\;\;\frac{\left(-q\right) \cdot q}{r}\\ \mathbf{else}:\\ \;\;\;\;-q\\ \end{array} \]
      10. Add Preprocessing

      Alternative 3: 57.8% accurate, 1.1× speedup?

      \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;{q\_m}^{2} \leq 10^{-108}:\\ \;\;\;\;\left(\left|p\right| + p\right) \cdot \frac{1}{2}\\ \mathbf{elif}\;{q\_m}^{2} \leq 2 \cdot 10^{+31}:\\ \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\ \mathbf{else}:\\ \;\;\;\;-q\_m\\ \end{array} \end{array} \]
      q_m = (fabs.f64 q)
      NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
      (FPCore (p r q_m)
       :precision binary64
       (if (<= (pow q_m 2.0) 1e-108)
         (* (+ (fabs p) p) (/ 1.0 2.0))
         (if (<= (pow q_m 2.0) 2e+31) (/ (* (- q_m) q_m) r) (- q_m))))
      q_m = fabs(q);
      assert(p < r && r < q_m);
      double code(double p, double r, double q_m) {
      	double tmp;
      	if (pow(q_m, 2.0) <= 1e-108) {
      		tmp = (fabs(p) + p) * (1.0 / 2.0);
      	} else if (pow(q_m, 2.0) <= 2e+31) {
      		tmp = (-q_m * q_m) / r;
      	} else {
      		tmp = -q_m;
      	}
      	return tmp;
      }
      
      q_m = abs(q)
      NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
      real(8) function code(p, r, q_m)
          real(8), intent (in) :: p
          real(8), intent (in) :: r
          real(8), intent (in) :: q_m
          real(8) :: tmp
          if ((q_m ** 2.0d0) <= 1d-108) then
              tmp = (abs(p) + p) * (1.0d0 / 2.0d0)
          else if ((q_m ** 2.0d0) <= 2d+31) then
              tmp = (-q_m * q_m) / r
          else
              tmp = -q_m
          end if
          code = tmp
      end function
      
      q_m = Math.abs(q);
      assert p < r && r < q_m;
      public static double code(double p, double r, double q_m) {
      	double tmp;
      	if (Math.pow(q_m, 2.0) <= 1e-108) {
      		tmp = (Math.abs(p) + p) * (1.0 / 2.0);
      	} else if (Math.pow(q_m, 2.0) <= 2e+31) {
      		tmp = (-q_m * q_m) / r;
      	} else {
      		tmp = -q_m;
      	}
      	return tmp;
      }
      
      q_m = math.fabs(q)
      [p, r, q_m] = sort([p, r, q_m])
      def code(p, r, q_m):
      	tmp = 0
      	if math.pow(q_m, 2.0) <= 1e-108:
      		tmp = (math.fabs(p) + p) * (1.0 / 2.0)
      	elif math.pow(q_m, 2.0) <= 2e+31:
      		tmp = (-q_m * q_m) / r
      	else:
      		tmp = -q_m
      	return tmp
      
      q_m = abs(q)
      p, r, q_m = sort([p, r, q_m])
      function code(p, r, q_m)
      	tmp = 0.0
      	if ((q_m ^ 2.0) <= 1e-108)
      		tmp = Float64(Float64(abs(p) + p) * Float64(1.0 / 2.0));
      	elseif ((q_m ^ 2.0) <= 2e+31)
      		tmp = Float64(Float64(Float64(-q_m) * q_m) / r);
      	else
      		tmp = Float64(-q_m);
      	end
      	return tmp
      end
      
      q_m = abs(q);
      p, r, q_m = num2cell(sort([p, r, q_m])){:}
      function tmp_2 = code(p, r, q_m)
      	tmp = 0.0;
      	if ((q_m ^ 2.0) <= 1e-108)
      		tmp = (abs(p) + p) * (1.0 / 2.0);
      	elseif ((q_m ^ 2.0) <= 2e+31)
      		tmp = (-q_m * q_m) / r;
      	else
      		tmp = -q_m;
      	end
      	tmp_2 = tmp;
      end
      
      q_m = N[Abs[q], $MachinePrecision]
      NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
      code[p_, r_, q$95$m_] := If[LessEqual[N[Power[q$95$m, 2.0], $MachinePrecision], 1e-108], N[(N[(N[Abs[p], $MachinePrecision] + p), $MachinePrecision] * N[(1.0 / 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Power[q$95$m, 2.0], $MachinePrecision], 2e+31], N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / r), $MachinePrecision], (-q$95$m)]]
      
      \begin{array}{l}
      q_m = \left|q\right|
      \\
      [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
      \\
      \begin{array}{l}
      \mathbf{if}\;{q\_m}^{2} \leq 10^{-108}:\\
      \;\;\;\;\left(\left|p\right| + p\right) \cdot \frac{1}{2}\\
      
      \mathbf{elif}\;{q\_m}^{2} \leq 2 \cdot 10^{+31}:\\
      \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\
      
      \mathbf{else}:\\
      \;\;\;\;-q\_m\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if (pow.f64 q #s(literal 2 binary64)) < 1.00000000000000004e-108

        1. Initial program 22.7%

          \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
        2. Add Preprocessing
        3. Taylor expanded in r around inf

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(r \cdot \left(\left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right)\right)} \]
        4. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\left(\left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right)} \]
          2. lower-*.f64N/A

            \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\left(\left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right)} \]
          3. lower--.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right)} \cdot r\right) \]
          4. +-commutativeN/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\color{blue}{\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right)} - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right) \]
          5. lower-+.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\color{blue}{\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right)} - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right) \]
          6. lower-/.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\color{blue}{\frac{\left|r\right|}{r}} + \frac{\left|p\right|}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right) \]
          7. lower-fabs.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\color{blue}{\left|r\right|}}{r} + \frac{\left|p\right|}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right) \]
          8. lower-/.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\left|r\right|}{r} + \color{blue}{\frac{\left|p\right|}{r}}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right) \]
          9. lower-fabs.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\left|r\right|}{r} + \frac{\color{blue}{\left|p\right|}}{r}\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right) \]
          10. mul-1-negN/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right) - \left(1 + \color{blue}{\left(\mathsf{neg}\left(\frac{p}{r}\right)\right)}\right)\right) \cdot r\right) \]
          11. unsub-negN/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right) - \color{blue}{\left(1 - \frac{p}{r}\right)}\right) \cdot r\right) \]
          12. lower--.f64N/A

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right) - \color{blue}{\left(1 - \frac{p}{r}\right)}\right) \cdot r\right) \]
          13. lower-/.f6416.2

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right) - \left(1 - \color{blue}{\frac{p}{r}}\right)\right) \cdot r\right) \]
        5. Applied rewrites16.2%

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\left(\left(\frac{\left|r\right|}{r} + \frac{\left|p\right|}{r}\right) - \left(1 - \frac{p}{r}\right)\right) \cdot r\right)} \]
        6. Step-by-step derivation
          1. Applied rewrites11.7%

            \[\leadsto \frac{1}{2} \cdot \left(\left(\frac{\left|p\right|}{r} + \left({1}^{1.5} - \left(1 - \frac{p}{r}\right)\right)\right) \cdot r\right) \]
          2. Taylor expanded in p around 0

            \[\leadsto \frac{1}{2} \cdot \left(p + \color{blue}{\left|p\right|}\right) \]
          3. Step-by-step derivation
            1. Applied rewrites29.7%

              \[\leadsto \frac{1}{2} \cdot \left(\left|p\right| + \color{blue}{p}\right) \]

            if 1.00000000000000004e-108 < (pow.f64 q #s(literal 2 binary64)) < 1.9999999999999999e31

            1. Initial program 18.8%

              \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
            2. Add Preprocessing
            3. Taylor expanded in r around inf

              \[\leadsto \color{blue}{r \cdot \left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right)} \]
            4. Step-by-step derivation
              1. *-commutativeN/A

                \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
              2. lower-*.f64N/A

                \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
            5. Applied rewrites11.3%

              \[\leadsto \color{blue}{\mathsf{fma}\left(-q, \frac{q}{r \cdot r}, \mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right)\right) \cdot r} \]
            6. Taylor expanded in r around 0

              \[\leadsto -1 \cdot \color{blue}{\frac{{q}^{2}}{r}} \]
            7. Step-by-step derivation
              1. Applied rewrites19.2%

                \[\leadsto \frac{\left(-q\right) \cdot q}{\color{blue}{r}} \]

              if 1.9999999999999999e31 < (pow.f64 q #s(literal 2 binary64))

              1. Initial program 25.8%

                \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
              2. Add Preprocessing
              3. Taylor expanded in q around inf

                \[\leadsto \color{blue}{-1 \cdot q} \]
              4. Step-by-step derivation
                1. mul-1-negN/A

                  \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
                2. lower-neg.f6428.2

                  \[\leadsto \color{blue}{-q} \]
              5. Applied rewrites28.2%

                \[\leadsto \color{blue}{-q} \]
            8. Recombined 3 regimes into one program.
            9. Final simplification27.6%

              \[\leadsto \begin{array}{l} \mathbf{if}\;{q}^{2} \leq 10^{-108}:\\ \;\;\;\;\left(\left|p\right| + p\right) \cdot \frac{1}{2}\\ \mathbf{elif}\;{q}^{2} \leq 2 \cdot 10^{+31}:\\ \;\;\;\;\frac{\left(-q\right) \cdot q}{r}\\ \mathbf{else}:\\ \;\;\;\;-q\\ \end{array} \]
            10. Add Preprocessing

            Alternative 4: 43.8% accurate, 9.3× speedup?

            \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;r \leq 6.2 \cdot 10^{+31}:\\ \;\;\;\;\left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\_m\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\ \end{array} \end{array} \]
            q_m = (fabs.f64 q)
            NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
            (FPCore (p r q_m)
             :precision binary64
             (if (<= r 6.2e+31)
               (* (- (+ (fabs p) (fabs r)) (* 2.0 q_m)) 0.5)
               (/ (* (- q_m) q_m) r)))
            q_m = fabs(q);
            assert(p < r && r < q_m);
            double code(double p, double r, double q_m) {
            	double tmp;
            	if (r <= 6.2e+31) {
            		tmp = ((fabs(p) + fabs(r)) - (2.0 * q_m)) * 0.5;
            	} else {
            		tmp = (-q_m * q_m) / r;
            	}
            	return tmp;
            }
            
            q_m = abs(q)
            NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
            real(8) function code(p, r, q_m)
                real(8), intent (in) :: p
                real(8), intent (in) :: r
                real(8), intent (in) :: q_m
                real(8) :: tmp
                if (r <= 6.2d+31) then
                    tmp = ((abs(p) + abs(r)) - (2.0d0 * q_m)) * 0.5d0
                else
                    tmp = (-q_m * q_m) / r
                end if
                code = tmp
            end function
            
            q_m = Math.abs(q);
            assert p < r && r < q_m;
            public static double code(double p, double r, double q_m) {
            	double tmp;
            	if (r <= 6.2e+31) {
            		tmp = ((Math.abs(p) + Math.abs(r)) - (2.0 * q_m)) * 0.5;
            	} else {
            		tmp = (-q_m * q_m) / r;
            	}
            	return tmp;
            }
            
            q_m = math.fabs(q)
            [p, r, q_m] = sort([p, r, q_m])
            def code(p, r, q_m):
            	tmp = 0
            	if r <= 6.2e+31:
            		tmp = ((math.fabs(p) + math.fabs(r)) - (2.0 * q_m)) * 0.5
            	else:
            		tmp = (-q_m * q_m) / r
            	return tmp
            
            q_m = abs(q)
            p, r, q_m = sort([p, r, q_m])
            function code(p, r, q_m)
            	tmp = 0.0
            	if (r <= 6.2e+31)
            		tmp = Float64(Float64(Float64(abs(p) + abs(r)) - Float64(2.0 * q_m)) * 0.5);
            	else
            		tmp = Float64(Float64(Float64(-q_m) * q_m) / r);
            	end
            	return tmp
            end
            
            q_m = abs(q);
            p, r, q_m = num2cell(sort([p, r, q_m])){:}
            function tmp_2 = code(p, r, q_m)
            	tmp = 0.0;
            	if (r <= 6.2e+31)
            		tmp = ((abs(p) + abs(r)) - (2.0 * q_m)) * 0.5;
            	else
            		tmp = (-q_m * q_m) / r;
            	end
            	tmp_2 = tmp;
            end
            
            q_m = N[Abs[q], $MachinePrecision]
            NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
            code[p_, r_, q$95$m_] := If[LessEqual[r, 6.2e+31], N[(N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] - N[(2.0 * q$95$m), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / r), $MachinePrecision]]
            
            \begin{array}{l}
            q_m = \left|q\right|
            \\
            [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;r \leq 6.2 \cdot 10^{+31}:\\
            \;\;\;\;\left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\_m\right) \cdot 0.5\\
            
            \mathbf{else}:\\
            \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if r < 6.2000000000000004e31

              1. Initial program 27.3%

                \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
              2. Add Preprocessing
              3. Taylor expanded in q around inf

                \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \color{blue}{2 \cdot q}\right) \]
              4. Step-by-step derivation
                1. lower-*.f6419.3

                  \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \color{blue}{2 \cdot q}\right) \]
              5. Applied rewrites19.3%

                \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \color{blue}{2 \cdot q}\right) \]
              6. Step-by-step derivation
                1. lift-*.f64N/A

                  \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right)} \]
                2. lift-/.f64N/A

                  \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right) \]
                3. *-commutativeN/A

                  \[\leadsto \color{blue}{\left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right) \cdot \frac{1}{2}} \]
                4. lower-*.f64N/A

                  \[\leadsto \color{blue}{\left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right) \cdot \frac{1}{2}} \]
                5. lift-+.f64N/A

                  \[\leadsto \left(\color{blue}{\left(\left|p\right| + \left|r\right|\right)} - 2 \cdot q\right) \cdot \frac{1}{2} \]
                6. +-commutativeN/A

                  \[\leadsto \left(\color{blue}{\left(\left|r\right| + \left|p\right|\right)} - 2 \cdot q\right) \cdot \frac{1}{2} \]
                7. lower-+.f64N/A

                  \[\leadsto \left(\color{blue}{\left(\left|r\right| + \left|p\right|\right)} - 2 \cdot q\right) \cdot \frac{1}{2} \]
                8. metadata-evalN/A

                  \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) - \mathsf{Rewrite=>}\left(lower-*.f64, \left(q \cdot 2\right)\right)\right) \cdot \color{blue}{\frac{1}{2}} \]
              7. Applied rewrites19.3%

                \[\leadsto \color{blue}{\left(\left(\left|r\right| + \left|p\right|\right) - q \cdot 2\right) \cdot 0.5} \]

              if 6.2000000000000004e31 < r

              1. Initial program 11.5%

                \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
              2. Add Preprocessing
              3. Taylor expanded in r around inf

                \[\leadsto \color{blue}{r \cdot \left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right)} \]
              4. Step-by-step derivation
                1. *-commutativeN/A

                  \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
                2. lower-*.f64N/A

                  \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
              5. Applied rewrites23.1%

                \[\leadsto \color{blue}{\mathsf{fma}\left(-q, \frac{q}{r \cdot r}, \mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right)\right) \cdot r} \]
              6. Taylor expanded in r around 0

                \[\leadsto -1 \cdot \color{blue}{\frac{{q}^{2}}{r}} \]
              7. Step-by-step derivation
                1. Applied rewrites43.5%

                  \[\leadsto \frac{\left(-q\right) \cdot q}{\color{blue}{r}} \]
              8. Recombined 2 regimes into one program.
              9. Final simplification24.6%

                \[\leadsto \begin{array}{l} \mathbf{if}\;r \leq 6.2 \cdot 10^{+31}:\\ \;\;\;\;\left(\left(\left|p\right| + \left|r\right|\right) - 2 \cdot q\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-q\right) \cdot q}{r}\\ \end{array} \]
              10. Add Preprocessing

              Alternative 5: 44.0% accurate, 10.0× speedup?

              \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;r \leq 8.2 \cdot 10^{+31}:\\ \;\;\;\;-q\_m\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\ \end{array} \end{array} \]
              q_m = (fabs.f64 q)
              NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
              (FPCore (p r q_m)
               :precision binary64
               (if (<= r 8.2e+31) (- q_m) (/ (* (- q_m) q_m) r)))
              q_m = fabs(q);
              assert(p < r && r < q_m);
              double code(double p, double r, double q_m) {
              	double tmp;
              	if (r <= 8.2e+31) {
              		tmp = -q_m;
              	} else {
              		tmp = (-q_m * q_m) / r;
              	}
              	return tmp;
              }
              
              q_m = abs(q)
              NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
              real(8) function code(p, r, q_m)
                  real(8), intent (in) :: p
                  real(8), intent (in) :: r
                  real(8), intent (in) :: q_m
                  real(8) :: tmp
                  if (r <= 8.2d+31) then
                      tmp = -q_m
                  else
                      tmp = (-q_m * q_m) / r
                  end if
                  code = tmp
              end function
              
              q_m = Math.abs(q);
              assert p < r && r < q_m;
              public static double code(double p, double r, double q_m) {
              	double tmp;
              	if (r <= 8.2e+31) {
              		tmp = -q_m;
              	} else {
              		tmp = (-q_m * q_m) / r;
              	}
              	return tmp;
              }
              
              q_m = math.fabs(q)
              [p, r, q_m] = sort([p, r, q_m])
              def code(p, r, q_m):
              	tmp = 0
              	if r <= 8.2e+31:
              		tmp = -q_m
              	else:
              		tmp = (-q_m * q_m) / r
              	return tmp
              
              q_m = abs(q)
              p, r, q_m = sort([p, r, q_m])
              function code(p, r, q_m)
              	tmp = 0.0
              	if (r <= 8.2e+31)
              		tmp = Float64(-q_m);
              	else
              		tmp = Float64(Float64(Float64(-q_m) * q_m) / r);
              	end
              	return tmp
              end
              
              q_m = abs(q);
              p, r, q_m = num2cell(sort([p, r, q_m])){:}
              function tmp_2 = code(p, r, q_m)
              	tmp = 0.0;
              	if (r <= 8.2e+31)
              		tmp = -q_m;
              	else
              		tmp = (-q_m * q_m) / r;
              	end
              	tmp_2 = tmp;
              end
              
              q_m = N[Abs[q], $MachinePrecision]
              NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
              code[p_, r_, q$95$m_] := If[LessEqual[r, 8.2e+31], (-q$95$m), N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / r), $MachinePrecision]]
              
              \begin{array}{l}
              q_m = \left|q\right|
              \\
              [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
              \\
              \begin{array}{l}
              \mathbf{if}\;r \leq 8.2 \cdot 10^{+31}:\\
              \;\;\;\;-q\_m\\
              
              \mathbf{else}:\\
              \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if r < 8.2000000000000003e31

                1. Initial program 27.3%

                  \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
                2. Add Preprocessing
                3. Taylor expanded in q around inf

                  \[\leadsto \color{blue}{-1 \cdot q} \]
                4. Step-by-step derivation
                  1. mul-1-negN/A

                    \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
                  2. lower-neg.f6419.7

                    \[\leadsto \color{blue}{-q} \]
                5. Applied rewrites19.7%

                  \[\leadsto \color{blue}{-q} \]

                if 8.2000000000000003e31 < r

                1. Initial program 11.5%

                  \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
                2. Add Preprocessing
                3. Taylor expanded in r around inf

                  \[\leadsto \color{blue}{r \cdot \left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right)} \]
                4. Step-by-step derivation
                  1. *-commutativeN/A

                    \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
                  2. lower-*.f64N/A

                    \[\leadsto \color{blue}{\left(\left(-1 \cdot \frac{{q}^{2}}{{r}^{2}} + \frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r}\right) - \frac{1}{2}\right) \cdot r} \]
                5. Applied rewrites23.1%

                  \[\leadsto \color{blue}{\mathsf{fma}\left(-q, \frac{q}{r \cdot r}, \mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right)\right) \cdot r} \]
                6. Taylor expanded in r around 0

                  \[\leadsto -1 \cdot \color{blue}{\frac{{q}^{2}}{r}} \]
                7. Step-by-step derivation
                  1. Applied rewrites43.5%

                    \[\leadsto \frac{\left(-q\right) \cdot q}{\color{blue}{r}} \]
                8. Recombined 2 regimes into one program.
                9. Add Preprocessing

                Alternative 6: 40.6% accurate, 11.4× speedup?

                \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;q\_m \leq 5.5 \cdot 10^{-105}:\\ \;\;\;\;\left(\left(\left|r\right| + p\right) + \left|p\right|\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;-q\_m\\ \end{array} \end{array} \]
                q_m = (fabs.f64 q)
                NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
                (FPCore (p r q_m)
                 :precision binary64
                 (if (<= q_m 5.5e-105) (* (+ (+ (fabs r) p) (fabs p)) 0.5) (- q_m)))
                q_m = fabs(q);
                assert(p < r && r < q_m);
                double code(double p, double r, double q_m) {
                	double tmp;
                	if (q_m <= 5.5e-105) {
                		tmp = ((fabs(r) + p) + fabs(p)) * 0.5;
                	} else {
                		tmp = -q_m;
                	}
                	return tmp;
                }
                
                q_m = abs(q)
                NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
                real(8) function code(p, r, q_m)
                    real(8), intent (in) :: p
                    real(8), intent (in) :: r
                    real(8), intent (in) :: q_m
                    real(8) :: tmp
                    if (q_m <= 5.5d-105) then
                        tmp = ((abs(r) + p) + abs(p)) * 0.5d0
                    else
                        tmp = -q_m
                    end if
                    code = tmp
                end function
                
                q_m = Math.abs(q);
                assert p < r && r < q_m;
                public static double code(double p, double r, double q_m) {
                	double tmp;
                	if (q_m <= 5.5e-105) {
                		tmp = ((Math.abs(r) + p) + Math.abs(p)) * 0.5;
                	} else {
                		tmp = -q_m;
                	}
                	return tmp;
                }
                
                q_m = math.fabs(q)
                [p, r, q_m] = sort([p, r, q_m])
                def code(p, r, q_m):
                	tmp = 0
                	if q_m <= 5.5e-105:
                		tmp = ((math.fabs(r) + p) + math.fabs(p)) * 0.5
                	else:
                		tmp = -q_m
                	return tmp
                
                q_m = abs(q)
                p, r, q_m = sort([p, r, q_m])
                function code(p, r, q_m)
                	tmp = 0.0
                	if (q_m <= 5.5e-105)
                		tmp = Float64(Float64(Float64(abs(r) + p) + abs(p)) * 0.5);
                	else
                		tmp = Float64(-q_m);
                	end
                	return tmp
                end
                
                q_m = abs(q);
                p, r, q_m = num2cell(sort([p, r, q_m])){:}
                function tmp_2 = code(p, r, q_m)
                	tmp = 0.0;
                	if (q_m <= 5.5e-105)
                		tmp = ((abs(r) + p) + abs(p)) * 0.5;
                	else
                		tmp = -q_m;
                	end
                	tmp_2 = tmp;
                end
                
                q_m = N[Abs[q], $MachinePrecision]
                NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
                code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 5.5e-105], N[(N[(N[(N[Abs[r], $MachinePrecision] + p), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], (-q$95$m)]
                
                \begin{array}{l}
                q_m = \left|q\right|
                \\
                [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
                \\
                \begin{array}{l}
                \mathbf{if}\;q\_m \leq 5.5 \cdot 10^{-105}:\\
                \;\;\;\;\left(\left(\left|r\right| + p\right) + \left|p\right|\right) \cdot 0.5\\
                
                \mathbf{else}:\\
                \;\;\;\;-q\_m\\
                
                
                \end{array}
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if q < 5.50000000000000029e-105

                  1. Initial program 24.8%

                    \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
                  2. Add Preprocessing
                  3. Taylor expanded in r around inf

                    \[\leadsto \color{blue}{r \cdot \left(\frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r} - \frac{1}{2}\right)} \]
                  4. Step-by-step derivation
                    1. *-commutativeN/A

                      \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r} - \frac{1}{2}\right) \cdot r} \]
                    2. lower-*.f64N/A

                      \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \frac{\left(\left|p\right| + \left|r\right|\right) - -1 \cdot p}{r} - \frac{1}{2}\right) \cdot r} \]
                  5. Applied rewrites8.4%

                    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r}, 0.5, -0.5\right) \cdot r} \]
                  6. Taylor expanded in r around 0

                    \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(p + \left(\left|p\right| + \left|r\right|\right)\right)} \]
                  7. Step-by-step derivation
                    1. Applied rewrites5.7%

                      \[\leadsto \left(\left(p + \left|r\right|\right) + \left|p\right|\right) \cdot \color{blue}{0.5} \]

                    if 5.50000000000000029e-105 < q

                    1. Initial program 22.1%

                      \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
                    2. Add Preprocessing
                    3. Taylor expanded in q around inf

                      \[\leadsto \color{blue}{-1 \cdot q} \]
                    4. Step-by-step derivation
                      1. mul-1-negN/A

                        \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
                      2. lower-neg.f6444.1

                        \[\leadsto \color{blue}{-q} \]
                    5. Applied rewrites44.1%

                      \[\leadsto \color{blue}{-q} \]
                  8. Recombined 2 regimes into one program.
                  9. Final simplification19.7%

                    \[\leadsto \begin{array}{l} \mathbf{if}\;q \leq 5.5 \cdot 10^{-105}:\\ \;\;\;\;\left(\left(\left|r\right| + p\right) + \left|p\right|\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;-q\\ \end{array} \]
                  10. Add Preprocessing

                  Alternative 7: 35.6% accurate, 83.3× speedup?

                  \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ -q\_m \end{array} \]
                  q_m = (fabs.f64 q)
                  NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
                  (FPCore (p r q_m) :precision binary64 (- q_m))
                  q_m = fabs(q);
                  assert(p < r && r < q_m);
                  double code(double p, double r, double q_m) {
                  	return -q_m;
                  }
                  
                  q_m = abs(q)
                  NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
                  real(8) function code(p, r, q_m)
                      real(8), intent (in) :: p
                      real(8), intent (in) :: r
                      real(8), intent (in) :: q_m
                      code = -q_m
                  end function
                  
                  q_m = Math.abs(q);
                  assert p < r && r < q_m;
                  public static double code(double p, double r, double q_m) {
                  	return -q_m;
                  }
                  
                  q_m = math.fabs(q)
                  [p, r, q_m] = sort([p, r, q_m])
                  def code(p, r, q_m):
                  	return -q_m
                  
                  q_m = abs(q)
                  p, r, q_m = sort([p, r, q_m])
                  function code(p, r, q_m)
                  	return Float64(-q_m)
                  end
                  
                  q_m = abs(q);
                  p, r, q_m = num2cell(sort([p, r, q_m])){:}
                  function tmp = code(p, r, q_m)
                  	tmp = -q_m;
                  end
                  
                  q_m = N[Abs[q], $MachinePrecision]
                  NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
                  code[p_, r_, q$95$m_] := (-q$95$m)
                  
                  \begin{array}{l}
                  q_m = \left|q\right|
                  \\
                  [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
                  \\
                  -q\_m
                  \end{array}
                  
                  Derivation
                  1. Initial program 23.8%

                    \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
                  2. Add Preprocessing
                  3. Taylor expanded in q around inf

                    \[\leadsto \color{blue}{-1 \cdot q} \]
                  4. Step-by-step derivation
                    1. mul-1-negN/A

                      \[\leadsto \color{blue}{\mathsf{neg}\left(q\right)} \]
                    2. lower-neg.f6418.5

                      \[\leadsto \color{blue}{-q} \]
                  5. Applied rewrites18.5%

                    \[\leadsto \color{blue}{-q} \]
                  6. Add Preprocessing

                  Reproduce

                  ?
                  herbie shell --seed 2024295 
                  (FPCore (p r q)
                    :name "1/2(abs(p)+abs(r) - sqrt((p-r)^2 + 4q^2))"
                    :precision binary64
                    (* (/ 1.0 2.0) (- (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))