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

Percentage Accurate: 23.9% → 57.8%
Time: 9.6s
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: 23.9% 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, 3.6× 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.26 \cdot 10^{-37}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{-0.125 \cdot p}{q\_m}, \frac{p}{q\_m}, \mathsf{fma}\left(\frac{\left|r\right| + \left|p\right|}{q\_m}, 0.5, -1\right)\right) \cdot 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.26e-37)
   (* (fma (/ (+ (fabs p) (- (fabs r) r)) p) -0.5 -0.5) (- p))
   (*
    (fma
     (/ (* -0.125 p) q_m)
     (/ p q_m)
     (fma (/ (+ (fabs r) (fabs p)) q_m) 0.5 -1.0))
    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.26e-37) {
		tmp = fma(((fabs(p) + (fabs(r) - r)) / p), -0.5, -0.5) * -p;
	} else {
		tmp = fma(((-0.125 * p) / q_m), (p / q_m), fma(((fabs(r) + fabs(p)) / q_m), 0.5, -1.0)) * 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.26e-37)
		tmp = Float64(fma(Float64(Float64(abs(p) + Float64(abs(r) - r)) / p), -0.5, -0.5) * Float64(-p));
	else
		tmp = Float64(fma(Float64(Float64(-0.125 * p) / q_m), Float64(p / q_m), fma(Float64(Float64(abs(r) + abs(p)) / q_m), 0.5, -1.0)) * 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.26e-37], 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], N[(N[(N[(N[(-0.125 * p), $MachinePrecision] / q$95$m), $MachinePrecision] * N[(p / q$95$m), $MachinePrecision] + N[(N[(N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] / q$95$m), $MachinePrecision] * 0.5 + -1.0), $MachinePrecision]), $MachinePrecision] * q$95$m), $MachinePrecision]]
\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.26 \cdot 10^{-37}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{-0.125 \cdot p}{q\_m}, \frac{p}{q\_m}, \mathsf{fma}\left(\frac{\left|r\right| + \left|p\right|}{q\_m}, 0.5, -1\right)\right) \cdot q\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if q < 1.25999999999999992e-37

    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.0

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

      \[\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. mul-1-negN/A

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

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

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

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

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

        \[\leadsto \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)} \cdot \left(-1 \cdot p\right) \]
      7. *-commutativeN/A

        \[\leadsto \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) \cdot \left(-1 \cdot p\right) \]
      8. metadata-evalN/A

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p}}, \frac{-1}{2}, \frac{-1}{2}\right) \cdot \left(-1 \cdot p\right) \]
      11. associate--l+N/A

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, \frac{-1}{2}, \frac{-1}{2}\right) \cdot \color{blue}{\left(\mathsf{neg}\left(p\right)\right)} \]
      17. lower-neg.f6418.4

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

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

    if 1.25999999999999992e-37 < q

    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 0

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) - \sqrt{\color{blue}{\mathsf{fma}\left({q}^{2}, 4, {p}^{2}\right)}}\right) \cdot \frac{1}{2} \]
      11. unpow2N/A

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) - \sqrt{\mathsf{fma}\left(\color{blue}{q \cdot q}, 4, {p}^{2}\right)}\right) \cdot \frac{1}{2} \]
      13. unpow2N/A

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) - \sqrt{\mathsf{fma}\left(q \cdot q, 4, \color{blue}{p \cdot p}\right)}\right) \cdot \frac{1}{2} \]
      14. lower-*.f6418.7

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) - \sqrt{\mathsf{fma}\left(q \cdot q, 4, \color{blue}{p \cdot p}\right)}\right) \cdot 0.5 \]
    5. Applied rewrites18.7%

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

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) - q \cdot 2\right) \cdot 0.5 \]
      2. Taylor expanded in q around inf

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

          \[\leadsto \mathsf{fma}\left(\frac{-0.125 \cdot p}{q}, \frac{p}{q}, \mathsf{fma}\left(\frac{\left|r\right| + \left|p\right|}{q}, 0.5, -1\right)\right) \cdot \color{blue}{q} \]
      4. Recombined 2 regimes into one program.
      5. Add Preprocessing

      Alternative 2: 45.7% accurate, 1.8× 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 5 \cdot 10^{-83}:\\ \;\;\;\;\frac{\left(\left(-q\_m\right) \cdot q\_m\right) \cdot \left(r + p\right)}{r \cdot 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) 5e-83) (/ (* (* (- q_m) q_m) (+ r p)) (* 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 (pow(q_m, 2.0) <= 5e-83) {
      		tmp = ((-q_m * q_m) * (r + p)) / (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 ** 2.0d0) <= 5d-83) then
              tmp = ((-q_m * q_m) * (r + p)) / (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 (Math.pow(q_m, 2.0) <= 5e-83) {
      		tmp = ((-q_m * q_m) * (r + p)) / (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 math.pow(q_m, 2.0) <= 5e-83:
      		tmp = ((-q_m * q_m) * (r + p)) / (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 ^ 2.0) <= 5e-83)
      		tmp = Float64(Float64(Float64(Float64(-q_m) * q_m) * Float64(r + p)) / Float64(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 ^ 2.0) <= 5e-83)
      		tmp = ((-q_m * q_m) * (r + p)) / (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[N[Power[q$95$m, 2.0], $MachinePrecision], 5e-83], N[(N[(N[((-q$95$m) * q$95$m), $MachinePrecision] * N[(r + p), $MachinePrecision]), $MachinePrecision] / N[(r * r), $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 5 \cdot 10^{-83}:\\
      \;\;\;\;\frac{\left(\left(-q\_m\right) \cdot q\_m\right) \cdot \left(r + p\right)}{r \cdot r}\\
      
      \mathbf{else}:\\
      \;\;\;\;-q\_m\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if (pow.f64 q #s(literal 2 binary64)) < 5e-83

        1. Initial program 20.0%

          \[\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{p \cdot {q}^{2}}{{r}^{3}} + \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)\right) - \frac{1}{2}\right)} \]
        4. Applied rewrites16.0%

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

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

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

          if 5e-83 < (pow.f64 q #s(literal 2 binary64))

          1. Initial program 22.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 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.f6431.2

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

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

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

        Alternative 3: 40.8% 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}^{2} \leq 5 \cdot 10^{-195}:\\ \;\;\;\;\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 (<= (pow q_m 2.0) 5e-195) (* (+ (+ 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 (pow(q_m, 2.0) <= 5e-195) {
        		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 ** 2.0d0) <= 5d-195) 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 (Math.pow(q_m, 2.0) <= 5e-195) {
        		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 math.pow(q_m, 2.0) <= 5e-195:
        		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 ^ 2.0) <= 5e-195)
        		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 ^ 2.0) <= 5e-195)
        		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[N[Power[q$95$m, 2.0], $MachinePrecision], 5e-195], 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}^{2} \leq 5 \cdot 10^{-195}:\\
        \;\;\;\;\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 (pow.f64 q #s(literal 2 binary64)) < 5.00000000000000009e-195

          1. Initial program 18.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 \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 rewrites15.0%

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

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

            if 5.00000000000000009e-195 < (pow.f64 q #s(literal 2 binary64))

            1. Initial program 22.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 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.f6427.9

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

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

          Alternative 4: 57.9% accurate, 6.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 \leq 1.1 \cdot 10^{-37}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\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.1e-37)
             (* (fma (/ (+ (fabs p) (- (fabs r) r)) p) -0.5 -0.5) (- 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.1e-37) {
          		tmp = fma(((fabs(p) + (fabs(r) - r)) / p), -0.5, -0.5) * -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.1e-37)
          		tmp = Float64(fma(Float64(Float64(abs(p) + Float64(abs(r) - r)) / p), -0.5, -0.5) * Float64(-p));
          	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.1e-37], 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], (-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.1 \cdot 10^{-37}:\\
          \;\;\;\;\mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right) \cdot \left(-p\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;-q\_m\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if q < 1.10000000000000001e-37

            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.0

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

              \[\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. mul-1-negN/A

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

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

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

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

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

                \[\leadsto \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)} \cdot \left(-1 \cdot p\right) \]
              7. *-commutativeN/A

                \[\leadsto \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) \cdot \left(-1 \cdot p\right) \]
              8. metadata-evalN/A

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

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

                \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\left(\left|p\right| + \left|r\right|\right) - r}{p}}, \frac{-1}{2}, \frac{-1}{2}\right) \cdot \left(-1 \cdot p\right) \]
              11. associate--l+N/A

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, \frac{-1}{2}, \frac{-1}{2}\right) \cdot \color{blue}{\left(\mathsf{neg}\left(p\right)\right)} \]
              17. lower-neg.f6418.4

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

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

            if 1.10000000000000001e-37 < q

            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 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.f6456.5

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

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

          Alternative 5: 43.0% accurate, 8.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 \leq 7.3 \cdot 10^{-118}:\\ \;\;\;\;\mathsf{fma}\left(-0.5, r, \left(\left(p + \left|r\right|\right) + \left|p\right|\right) \cdot 0.5\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 7.3e-118)
             (fma -0.5 r (* (+ (+ 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 <= 7.3e-118) {
          		tmp = fma(-0.5, r, (((p + fabs(r)) + 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 <= 7.3e-118)
          		tmp = fma(-0.5, r, Float64(Float64(Float64(p + abs(r)) + abs(p)) * 0.5));
          	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, 7.3e-118], N[(-0.5 * r + N[(N[(N[(p + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] * 0.5), $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 7.3 \cdot 10^{-118}:\\
          \;\;\;\;\mathsf{fma}\left(-0.5, r, \left(\left(p + \left|r\right|\right) + \left|p\right|\right) \cdot 0.5\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;-q\_m\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if q < 7.2999999999999999e-118

            1. Initial program 22.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 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 rewrites9.3%

              \[\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 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 rewrites9.3%

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

              if 7.2999999999999999e-118 < q

              1. Initial program 20.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.f6451.4

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

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

            Alternative 6: 46.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.6 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{q\_m}\\ \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.6e-162) (/ (* (- q_m) 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 (q_m <= 1.6e-162) {
            		tmp = (-q_m * q_m) / q_m;
            	} 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.6d-162) then
                    tmp = (-q_m * q_m) / q_m
                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.6e-162) {
            		tmp = (-q_m * q_m) / q_m;
            	} 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.6e-162:
            		tmp = (-q_m * 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 <= 1.6e-162)
            		tmp = Float64(Float64(Float64(-q_m) * q_m) / q_m);
            	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.6e-162)
            		tmp = (-q_m * q_m) / q_m;
            	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.6e-162], N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / q$95$m), $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.6 \cdot 10^{-162}:\\
            \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{q\_m}\\
            
            \mathbf{else}:\\
            \;\;\;\;-q\_m\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if q < 1.59999999999999988e-162

              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 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.f643.2

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

                \[\leadsto \color{blue}{-q} \]
              6. Step-by-step derivation
                1. Applied rewrites4.0%

                  \[\leadsto \frac{0 - {q}^{3}}{\color{blue}{0 + \mathsf{fma}\left(q, q, 0 \cdot q\right)}} \]
                2. Step-by-step derivation
                  1. Applied rewrites13.2%

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

                  if 1.59999999999999988e-162 < q

                  1. Initial program 19.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.f6446.1

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

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

                Alternative 7: 36.1% 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 21.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 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.f6421.5

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

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

                Reproduce

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