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

Percentage Accurate: 24.0% → 69.5%
Time: 11.8s
Alternatives: 6
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 6 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.0% 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: 69.5% accurate, 1.6× speedup?

\[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} t_0 := \left|r\right| - r\\ \mathbf{if}\;q\_m \leq 3 \cdot 10^{-29}:\\ \;\;\;\;0.5 \cdot \left(\left(p + \mathsf{fma}\left(\frac{q\_m \cdot q\_m}{r}, -2, \left|p\right|\right)\right) + t\_0\right)\\ \mathbf{elif}\;q\_m \leq 2.2 \cdot 10^{+34}:\\ \;\;\;\;{2}^{-1} \cdot \left(\mathsf{fma}\left(-2 \cdot q\_m, \frac{q\_m}{r}, \left|p\right| + p\right) + t\_0\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
 (let* ((t_0 (- (fabs r) r)))
   (if (<= q_m 3e-29)
     (* 0.5 (+ (+ p (fma (/ (* q_m q_m) r) -2.0 (fabs p))) t_0))
     (if (<= q_m 2.2e+34)
       (* (pow 2.0 -1.0) (+ (fma (* -2.0 q_m) (/ q_m r) (+ (fabs p) p)) t_0))
       (- q_m)))))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
	double t_0 = fabs(r) - r;
	double tmp;
	if (q_m <= 3e-29) {
		tmp = 0.5 * ((p + fma(((q_m * q_m) / r), -2.0, fabs(p))) + t_0);
	} else if (q_m <= 2.2e+34) {
		tmp = pow(2.0, -1.0) * (fma((-2.0 * q_m), (q_m / r), (fabs(p) + p)) + t_0);
	} 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)
	t_0 = Float64(abs(r) - r)
	tmp = 0.0
	if (q_m <= 3e-29)
		tmp = Float64(0.5 * Float64(Float64(p + fma(Float64(Float64(q_m * q_m) / r), -2.0, abs(p))) + t_0));
	elseif (q_m <= 2.2e+34)
		tmp = Float64((2.0 ^ -1.0) * Float64(fma(Float64(-2.0 * q_m), Float64(q_m / r), Float64(abs(p) + p)) + t_0));
	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_] := Block[{t$95$0 = N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]}, If[LessEqual[q$95$m, 3e-29], N[(0.5 * N[(N[(p + N[(N[(N[(q$95$m * q$95$m), $MachinePrecision] / r), $MachinePrecision] * -2.0 + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[q$95$m, 2.2e+34], N[(N[Power[2.0, -1.0], $MachinePrecision] * N[(N[(N[(-2.0 * q$95$m), $MachinePrecision] * N[(q$95$m / r), $MachinePrecision] + N[(N[Abs[p], $MachinePrecision] + p), $MachinePrecision]), $MachinePrecision] + t$95$0), $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}
t_0 := \left|r\right| - r\\
\mathbf{if}\;q\_m \leq 3 \cdot 10^{-29}:\\
\;\;\;\;0.5 \cdot \left(\left(p + \mathsf{fma}\left(\frac{q\_m \cdot q\_m}{r}, -2, \left|p\right|\right)\right) + t\_0\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if q < 3.0000000000000003e-29

    1. Initial program 24.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 r around inf

      \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(r \cdot \left(\left(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right)} \]
    5. Applied rewrites14.1%

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

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

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

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

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

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

            \[\leadsto \color{blue}{0.5} \cdot \left(\left(p + \mathsf{fma}\left(\frac{q \cdot q}{r}, -2, \left|p\right|\right)\right) + \left(\left|r\right| - r\right)\right) \]
        3. Applied rewrites28.8%

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

        if 3.0000000000000003e-29 < q < 2.2000000000000002e34

        1. Initial program 23.9%

          \[\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right)} \]
        5. Applied rewrites3.9%

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

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

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

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

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

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

              if 2.2000000000000002e34 < q

              1. Initial program 25.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.f6464.5

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

                \[\leadsto \color{blue}{-q} \]
            3. Recombined 3 regimes into one program.
            4. Final simplification35.1%

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

            Alternative 2: 66.3% accurate, 2.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}\;q\_m \leq 1.12 \cdot 10^{+18}:\\ \;\;\;\;{2}^{-1} \cdot \left(\left(\left|p\right| + p\right) + \left(\left|r\right| - r\right)\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 (<= q_m 1.12e+18)
               (* (pow 2.0 -1.0) (+ (+ (fabs p) p) (- (fabs r) 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 (q_m <= 1.12e+18) {
            		tmp = pow(2.0, -1.0) * ((fabs(p) + p) + (fabs(r) - 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 <= 1.12d+18) then
                    tmp = (2.0d0 ** (-1.0d0)) * ((abs(p) + p) + (abs(r) - 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 (q_m <= 1.12e+18) {
            		tmp = Math.pow(2.0, -1.0) * ((Math.abs(p) + p) + (Math.abs(r) - 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 q_m <= 1.12e+18:
            		tmp = math.pow(2.0, -1.0) * ((math.fabs(p) + p) + (math.fabs(r) - 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 <= 1.12e+18)
            		tmp = Float64((2.0 ^ -1.0) * Float64(Float64(abs(p) + p) + Float64(abs(r) - 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 <= 1.12e+18)
            		tmp = (2.0 ^ -1.0) * ((abs(p) + p) + (abs(r) - 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[q$95$m, 1.12e+18], N[(N[Power[2.0, -1.0], $MachinePrecision] * N[(N[(N[Abs[p], $MachinePrecision] + p), $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]), $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 \leq 1.12 \cdot 10^{+18}:\\
            \;\;\;\;{2}^{-1} \cdot \left(\left(\left|p\right| + p\right) + \left(\left|r\right| - r\right)\right)\\
            
            \mathbf{else}:\\
            \;\;\;\;-q\_m\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if q < 1.12e18

              1. Initial program 23.6%

                \[\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right)} \]
              5. Applied rewrites13.5%

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

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

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

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

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

                    \[\leadsto \frac{1}{2} \cdot \left(\left(p + \left|p\right|\right) + \left(\left|r\right| - r\right)\right) \]
                  3. Step-by-step derivation
                    1. Applied rewrites25.9%

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

                    if 1.12e18 < q

                    1. Initial program 26.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.f6462.8

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

                      \[\leadsto \color{blue}{-q} \]
                  4. Recombined 2 regimes into one program.
                  5. Final simplification34.2%

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

                  Alternative 3: 68.8% accurate, 5.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}\;q\_m \leq 1.75 \cdot 10^{+29}:\\ \;\;\;\;0.5 \cdot \left(\left(p + \mathsf{fma}\left(\frac{q\_m \cdot q\_m}{r}, -2, \left|p\right|\right)\right) + \left(\left|r\right| - r\right)\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 (<= q_m 1.75e+29)
                     (* 0.5 (+ (+ p (fma (/ (* q_m q_m) r) -2.0 (fabs p))) (- (fabs r) 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 (q_m <= 1.75e+29) {
                  		tmp = 0.5 * ((p + fma(((q_m * q_m) / r), -2.0, fabs(p))) + (fabs(r) - 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 <= 1.75e+29)
                  		tmp = Float64(0.5 * Float64(Float64(p + fma(Float64(Float64(q_m * q_m) / r), -2.0, abs(p))) + Float64(abs(r) - 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[q$95$m, 1.75e+29], N[(0.5 * N[(N[(p + N[(N[(N[(q$95$m * q$95$m), $MachinePrecision] / r), $MachinePrecision] * -2.0 + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]), $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 \leq 1.75 \cdot 10^{+29}:\\
                  \;\;\;\;0.5 \cdot \left(\left(p + \mathsf{fma}\left(\frac{q\_m \cdot q\_m}{r}, -2, \left|p\right|\right)\right) + \left(\left|r\right| - r\right)\right)\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;-q\_m\\
                  
                  
                  \end{array}
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if q < 1.74999999999999989e29

                    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 r around inf

                      \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(r \cdot \left(\left(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\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(-2 \cdot \frac{{q}^{2}}{{r}^{2}} + \left(\frac{\left|p\right|}{r} + \frac{\left|r\right|}{r}\right)\right) - \left(1 + -1 \cdot \frac{p}{r}\right)\right) \cdot r\right)} \]
                    5. Applied rewrites13.3%

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

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

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

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

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

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

                            \[\leadsto \color{blue}{0.5} \cdot \left(\left(p + \mathsf{fma}\left(\frac{q \cdot q}{r}, -2, \left|p\right|\right)\right) + \left(\left|r\right| - r\right)\right) \]
                        3. Applied rewrites27.7%

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

                        if 1.74999999999999989e29 < q

                        1. Initial program 26.2%

                          \[\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.f6464.0

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

                          \[\leadsto \color{blue}{-q} \]
                      4. Recombined 2 regimes into one program.
                      5. Add Preprocessing

                      Alternative 4: 43.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}\;q\_m \leq 1.75 \cdot 10^{-93}:\\ \;\;\;\;-0.5 \cdot \left(r - \left(\left(p + \left|r\right|\right) + \left|p\right|\right)\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 (<= q_m 1.75e-93) (* -0.5 (- r (+ (+ p (fabs r)) (fabs p)))) (- 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 <= 1.75e-93) {
                      		tmp = -0.5 * (r - ((p + fabs(r)) + fabs(p)));
                      	} 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 <= 1.75d-93) then
                              tmp = (-0.5d0) * (r - ((p + abs(r)) + abs(p)))
                          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 <= 1.75e-93) {
                      		tmp = -0.5 * (r - ((p + Math.abs(r)) + Math.abs(p)));
                      	} 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 <= 1.75e-93:
                      		tmp = -0.5 * (r - ((p + math.fabs(r)) + math.fabs(p)))
                      	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 <= 1.75e-93)
                      		tmp = Float64(-0.5 * Float64(r - Float64(Float64(p + abs(r)) + abs(p))));
                      	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 <= 1.75e-93)
                      		tmp = -0.5 * (r - ((p + abs(r)) + abs(p)));
                      	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, 1.75e-93], N[(-0.5 * N[(r - N[(N[(p + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $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 \leq 1.75 \cdot 10^{-93}:\\
                      \;\;\;\;-0.5 \cdot \left(r - \left(\left(p + \left|r\right|\right) + \left|p\right|\right)\right)\\
                      
                      \mathbf{else}:\\
                      \;\;\;\;-q\_m\\
                      
                      
                      \end{array}
                      \end{array}
                      
                      Derivation
                      1. Split input into 2 regimes
                      2. if q < 1.75e-93

                        1. Initial program 23.4%

                          \[\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 rewrites11.9%

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

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

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

                          if 1.75e-93 < q

                          1. Initial program 26.4%

                            \[\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.f6452.6

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

                            \[\leadsto \color{blue}{-q} \]
                        8. Recombined 2 regimes into one program.
                        9. Add Preprocessing

                        Alternative 5: 40.4% 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 3.5 \cdot 10^{-166}:\\ \;\;\;\;\left(\left(p + \left|r\right|\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 3.5e-166) (* (+ (+ p (fabs r)) (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 <= 3.5e-166) {
                        		tmp = ((p + fabs(r)) + 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 <= 3.5d-166) then
                                tmp = ((p + abs(r)) + 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 <= 3.5e-166) {
                        		tmp = ((p + Math.abs(r)) + 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 <= 3.5e-166:
                        		tmp = ((p + math.fabs(r)) + 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 <= 3.5e-166)
                        		tmp = Float64(Float64(Float64(p + abs(r)) + 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 <= 3.5e-166)
                        		tmp = ((p + abs(r)) + 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, 3.5e-166], N[(N[(N[(p + N[Abs[r], $MachinePrecision]), $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 3.5 \cdot 10^{-166}:\\
                        \;\;\;\;\left(\left(p + \left|r\right|\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 < 3.4999999999999999e-166

                          1. Initial program 23.2%

                            \[\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 rewrites11.4%

                            \[\leadsto \color{blue}{\left(\frac{\left(\left|r\right| + p\right) + \left|p\right|}{r} \cdot 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 rewrites8.2%

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

                            if 3.4999999999999999e-166 < q

                            1. Initial program 26.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.f6448.0

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

                              \[\leadsto \color{blue}{-q} \]
                          8. Recombined 2 regimes into one program.
                          9. Add Preprocessing

                          Alternative 6: 36.2% 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 24.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.5

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

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

                          Reproduce

                          ?
                          herbie shell --seed 2024332 
                          (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)))))))