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

Percentage Accurate: 45.1% → 81.6%
Time: 3.9s
Alternatives: 7
Speedup: 35.6×

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)))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(p, r, q)
use fmin_fmax_functions
    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: 45.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \end{array} \]
(FPCore (p r q)
 :precision binary64
 (*
  (/ 1.0 2.0)
  (+ (+ (fabs p) (fabs r)) (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0)))))))
double code(double p, double r, double q) {
	return (1.0 / 2.0) * ((fabs(p) + fabs(r)) + sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(p, r, q)
use fmin_fmax_functions
    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: 81.6% accurate, 9.3× speedup?

\[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} t_0 := \left|r\right| + \left|p\right|\\ \mathbf{if}\;q\_m \leq 1.15 \cdot 10^{+119}:\\ \;\;\;\;\left(t\_0 + \left(r - p\right)\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(q\_m \cdot 2 + t\_0\right) \cdot 0.5\\ \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) (fabs p))))
   (if (<= q_m 1.15e+119)
     (* (+ t_0 (- r p)) 0.5)
     (* (+ (* q_m 2.0) t_0) 0.5))))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
	double t_0 = fabs(r) + fabs(p);
	double tmp;
	if (q_m <= 1.15e+119) {
		tmp = (t_0 + (r - p)) * 0.5;
	} else {
		tmp = ((q_m * 2.0) + t_0) * 0.5;
	}
	return tmp;
}
q_m =     private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(p, r, q_m)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs(r) + abs(p)
    if (q_m <= 1.15d+119) then
        tmp = (t_0 + (r - p)) * 0.5d0
    else
        tmp = ((q_m * 2.0d0) + t_0) * 0.5d0
    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 t_0 = Math.abs(r) + Math.abs(p);
	double tmp;
	if (q_m <= 1.15e+119) {
		tmp = (t_0 + (r - p)) * 0.5;
	} else {
		tmp = ((q_m * 2.0) + t_0) * 0.5;
	}
	return tmp;
}
q_m = math.fabs(q)
[p, r, q_m] = sort([p, r, q_m])
def code(p, r, q_m):
	t_0 = math.fabs(r) + math.fabs(p)
	tmp = 0
	if q_m <= 1.15e+119:
		tmp = (t_0 + (r - p)) * 0.5
	else:
		tmp = ((q_m * 2.0) + t_0) * 0.5
	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) + abs(p))
	tmp = 0.0
	if (q_m <= 1.15e+119)
		tmp = Float64(Float64(t_0 + Float64(r - p)) * 0.5);
	else
		tmp = Float64(Float64(Float64(q_m * 2.0) + t_0) * 0.5);
	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)
	t_0 = abs(r) + abs(p);
	tmp = 0.0;
	if (q_m <= 1.15e+119)
		tmp = (t_0 + (r - p)) * 0.5;
	else
		tmp = ((q_m * 2.0) + t_0) * 0.5;
	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_] := Block[{t$95$0 = N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[q$95$m, 1.15e+119], N[(N[(t$95$0 + N[(r - p), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(q$95$m * 2.0), $MachinePrecision] + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision]]]
\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| + \left|p\right|\\
\mathbf{if}\;q\_m \leq 1.15 \cdot 10^{+119}:\\
\;\;\;\;\left(t\_0 + \left(r - p\right)\right) \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\left(q\_m \cdot 2 + t\_0\right) \cdot 0.5\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if q < 1.15e119

    1. Initial program 50.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 \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r \cdot \left(1 + -1 \cdot \frac{p}{r}\right)}\right) \]
    4. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \mathsf{fma}\left(\frac{p}{r}, -1, 1\right) \cdot r\right) \]
      6. lower-/.f6433.6

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \mathsf{fma}\left(\frac{p}{r}, -1, 1\right) \cdot r\right) \]
    5. Applied rewrites33.6%

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

      \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r + \color{blue}{-1 \cdot p}\right)\right) \]
    7. Step-by-step derivation
      1. fp-cancel-sign-sub-invN/A

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|-1\right| \cdot {p}^{\left(\frac{2}{2}\right)}\right)\right) \]
      6. sqrt-pow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|-1\right| \cdot \sqrt{{p}^{2}}\right)\right) \]
      7. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|-1\right| \cdot \sqrt{p \cdot p}\right)\right) \]
      8. rem-sqrt-square-revN/A

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

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

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|p\right|\right)\right) \]
      12. rem-sqrt-square-revN/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \sqrt{p \cdot p}\right)\right) \]
      13. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \sqrt{{p}^{2}}\right)\right) \]
      14. sqrt-pow1N/A

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - {p}^{1}\right)\right) \]
      16. unpow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - p\right)\right) \]
      17. lower--.f6439.0

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - p\right)\right) \]
    8. Applied rewrites39.0%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(\left|r\right| + \color{blue}{\left|p\right|}\right) + \left(r - p\right)\right) \cdot \frac{1}{2} \]
      12. metadata-eval39.0

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\right)\right) \cdot \color{blue}{0.5} \]
    10. Applied rewrites39.0%

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

    if 1.15e119 < q

    1. Initial program 18.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 q around inf

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\left(\left(\left|p\right| + \left|r\right|\right) + q \cdot 2\right) \cdot 0.5} \]
    7. Applied rewrites75.0%

      \[\leadsto \color{blue}{\left(q \cdot 2 + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 63.2% 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}\;r \leq -1.1 \cdot 10^{-307}:\\ \;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\ \mathbf{elif}\;r \leq 3.5 \cdot 10^{+71}:\\ \;\;\;\;\left(\mathsf{fma}\left(q\_m, 2, r\right) + p\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right)\\ \end{array} \end{array} \]
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
 :precision binary64
 (if (<= r -1.1e-307)
   (* (+ (- p) (+ (fabs r) (fabs p))) 0.5)
   (if (<= r 3.5e+71)
     (* (+ (fma q_m 2.0 r) p) 0.5)
     (* 0.5 (+ (+ (fabs p) (fabs r)) r)))))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
	double tmp;
	if (r <= -1.1e-307) {
		tmp = (-p + (fabs(r) + fabs(p))) * 0.5;
	} else if (r <= 3.5e+71) {
		tmp = (fma(q_m, 2.0, r) + p) * 0.5;
	} else {
		tmp = 0.5 * ((fabs(p) + fabs(r)) + r);
	}
	return tmp;
}
q_m = abs(q)
p, r, q_m = sort([p, r, q_m])
function code(p, r, q_m)
	tmp = 0.0
	if (r <= -1.1e-307)
		tmp = Float64(Float64(Float64(-p) + Float64(abs(r) + abs(p))) * 0.5);
	elseif (r <= 3.5e+71)
		tmp = Float64(Float64(fma(q_m, 2.0, r) + p) * 0.5);
	else
		tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) + r));
	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[r, -1.1e-307], N[(N[((-p) + N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[r, 3.5e+71], N[(N[(N[(q$95$m * 2.0 + r), $MachinePrecision] + p), $MachinePrecision] * 0.5), $MachinePrecision], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + r), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
\mathbf{if}\;r \leq -1.1 \cdot 10^{-307}:\\
\;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\

\mathbf{elif}\;r \leq 3.5 \cdot 10^{+71}:\\
\;\;\;\;\left(\mathsf{fma}\left(q\_m, 2, r\right) + p\right) \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if r < -1.1e-307

    1. Initial program 49.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 p around -inf

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(\mathsf{neg}\left(p\right)\right)\right) \]
      2. lower-neg.f6425.1

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

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

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

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

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

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

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

    if -1.1e-307 < r < 3.4999999999999999e71

    1. Initial program 54.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 p around 0

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

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

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

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

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

      \[\leadsto \left(\left(r + 2 \cdot q\right) + p\right) \cdot \frac{1}{2} \]
    7. Step-by-step derivation
      1. +-commutativeN/A

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

        \[\leadsto \left(\left(q \cdot 2 + r\right) + p\right) \cdot \frac{1}{2} \]
      3. lower-fma.f6431.6

        \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
    8. Applied rewrites31.6%

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

    if 3.4999999999999999e71 < r

    1. Initial program 19.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 \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r}\right) \]
    4. Step-by-step derivation
      1. Applied rewrites78.4%

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

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

          \[\leadsto \color{blue}{0.5} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
      3. Applied rewrites78.4%

        \[\leadsto \color{blue}{0.5} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
    5. Recombined 3 regimes into one program.
    6. Add Preprocessing

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

      1. Initial program 50.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 \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r \cdot \left(1 + -1 \cdot \frac{p}{r}\right)}\right) \]
      4. Step-by-step derivation
        1. *-commutativeN/A

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \mathsf{fma}\left(\frac{p}{r}, -1, 1\right) \cdot r\right) \]
        6. lower-/.f6433.6

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \mathsf{fma}\left(\frac{p}{r}, -1, 1\right) \cdot r\right) \]
      5. Applied rewrites33.6%

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r + \color{blue}{-1 \cdot p}\right)\right) \]
      7. Step-by-step derivation
        1. fp-cancel-sign-sub-invN/A

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|-1\right| \cdot {p}^{\left(\frac{2}{2}\right)}\right)\right) \]
        6. sqrt-pow1N/A

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|-1\right| \cdot \sqrt{{p}^{2}}\right)\right) \]
        7. unpow2N/A

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|-1\right| \cdot \sqrt{p \cdot p}\right)\right) \]
        8. rem-sqrt-square-revN/A

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

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

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

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \left|p\right|\right)\right) \]
        12. rem-sqrt-square-revN/A

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \sqrt{p \cdot p}\right)\right) \]
        13. unpow2N/A

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - \sqrt{{p}^{2}}\right)\right) \]
        14. sqrt-pow1N/A

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

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - {p}^{1}\right)\right) \]
        16. unpow1N/A

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - p\right)\right) \]
        17. lower--.f6439.0

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - p\right)\right) \]
      8. Applied rewrites39.0%

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto \left(\left(\left|r\right| + \color{blue}{\left|p\right|}\right) + \left(r - p\right)\right) \cdot \frac{1}{2} \]
        12. metadata-eval39.0

          \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\right)\right) \cdot \color{blue}{0.5} \]
      10. Applied rewrites39.0%

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

      if 1.15e119 < q

      1. Initial program 18.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 p around 0

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

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

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

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

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

        \[\leadsto \left(\left(r + 2 \cdot q\right) + p\right) \cdot \frac{1}{2} \]
      7. Step-by-step derivation
        1. +-commutativeN/A

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

          \[\leadsto \left(\left(q \cdot 2 + r\right) + p\right) \cdot \frac{1}{2} \]
        3. lower-fma.f6472.6

          \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
      8. Applied rewrites72.6%

        \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 4: 57.1% 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 1.6 \cdot 10^{+99}:\\ \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\mathsf{fma}\left(q\_m, 2, r\right) + p\right) \cdot 0.5\\ \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+99)
       (* 0.5 (+ (+ (fabs p) (fabs r)) r))
       (* (+ (fma q_m 2.0 r) p) 0.5)))
    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+99) {
    		tmp = 0.5 * ((fabs(p) + fabs(r)) + r);
    	} else {
    		tmp = (fma(q_m, 2.0, r) + p) * 0.5;
    	}
    	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+99)
    		tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) + r));
    	else
    		tmp = Float64(Float64(fma(q_m, 2.0, r) + p) * 0.5);
    	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.6e+99], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + r), $MachinePrecision]), $MachinePrecision], N[(N[(N[(q$95$m * 2.0 + r), $MachinePrecision] + p), $MachinePrecision] * 0.5), $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.6 \cdot 10^{+99}:\\
    \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;\left(\mathsf{fma}\left(q\_m, 2, r\right) + p\right) \cdot 0.5\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if q < 1.6e99

      1. Initial program 50.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 \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r}\right) \]
      4. Step-by-step derivation
        1. Applied rewrites27.5%

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

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

            \[\leadsto \color{blue}{0.5} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
        3. Applied rewrites27.5%

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

        if 1.6e99 < 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 p around 0

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

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

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

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

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

          \[\leadsto \left(\left(r + 2 \cdot q\right) + p\right) \cdot \frac{1}{2} \]
        7. Step-by-step derivation
          1. +-commutativeN/A

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

            \[\leadsto \left(\left(q \cdot 2 + r\right) + p\right) \cdot \frac{1}{2} \]
          3. lower-fma.f6469.1

            \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
        8. Applied rewrites69.1%

          \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
      5. Recombined 2 regimes into one program.
      6. Add Preprocessing

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

        1. Initial program 50.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 \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r}\right) \]
        4. Step-by-step derivation
          1. Applied rewrites27.5%

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

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

              \[\leadsto \color{blue}{0.5} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
          3. Applied rewrites27.5%

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

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

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

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

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

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

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

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

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

              \[\leadsto 0.5 \cdot \left(\left|p\right| + \left(\color{blue}{\left|r\right|} + r\right)\right) \]
          5. Applied rewrites27.9%

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

          if 1.6e99 < 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 p around 0

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

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

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

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

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

            \[\leadsto \left(\left(r + 2 \cdot q\right) + p\right) \cdot \frac{1}{2} \]
          7. Step-by-step derivation
            1. +-commutativeN/A

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

              \[\leadsto \left(\left(q \cdot 2 + r\right) + p\right) \cdot \frac{1}{2} \]
            3. lower-fma.f6469.1

              \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
          8. Applied rewrites69.1%

            \[\leadsto \left(\mathsf{fma}\left(q, 2, r\right) + p\right) \cdot 0.5 \]
        5. Recombined 2 regimes into one program.
        6. Add Preprocessing

        Alternative 6: 54.3% accurate, 35.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}\;r \leq 1.55 \cdot 10^{+32}:\\ \;\;\;\;q\_m\\ \mathbf{else}:\\ \;\;\;\;r\\ \end{array} \end{array} \]
        q_m = (fabs.f64 q)
        NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
        (FPCore (p r q_m) :precision binary64 (if (<= r 1.55e+32) q_m r))
        q_m = fabs(q);
        assert(p < r && r < q_m);
        double code(double p, double r, double q_m) {
        	double tmp;
        	if (r <= 1.55e+32) {
        		tmp = q_m;
        	} else {
        		tmp = r;
        	}
        	return tmp;
        }
        
        q_m =     private
        NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(p, r, q_m)
        use fmin_fmax_functions
            real(8), intent (in) :: p
            real(8), intent (in) :: r
            real(8), intent (in) :: q_m
            real(8) :: tmp
            if (r <= 1.55d+32) then
                tmp = q_m
            else
                tmp = r
            end if
            code = tmp
        end function
        
        q_m = Math.abs(q);
        assert p < r && r < q_m;
        public static double code(double p, double r, double q_m) {
        	double tmp;
        	if (r <= 1.55e+32) {
        		tmp = q_m;
        	} else {
        		tmp = r;
        	}
        	return tmp;
        }
        
        q_m = math.fabs(q)
        [p, r, q_m] = sort([p, r, q_m])
        def code(p, r, q_m):
        	tmp = 0
        	if r <= 1.55e+32:
        		tmp = q_m
        	else:
        		tmp = r
        	return tmp
        
        q_m = abs(q)
        p, r, q_m = sort([p, r, q_m])
        function code(p, r, q_m)
        	tmp = 0.0
        	if (r <= 1.55e+32)
        		tmp = q_m;
        	else
        		tmp = r;
        	end
        	return tmp
        end
        
        q_m = abs(q);
        p, r, q_m = num2cell(sort([p, r, q_m])){:}
        function tmp_2 = code(p, r, q_m)
        	tmp = 0.0;
        	if (r <= 1.55e+32)
        		tmp = q_m;
        	else
        		tmp = r;
        	end
        	tmp_2 = tmp;
        end
        
        q_m = N[Abs[q], $MachinePrecision]
        NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
        code[p_, r_, q$95$m_] := If[LessEqual[r, 1.55e+32], q$95$m, r]
        
        \begin{array}{l}
        q_m = \left|q\right|
        \\
        [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
        \\
        \begin{array}{l}
        \mathbf{if}\;r \leq 1.55 \cdot 10^{+32}:\\
        \;\;\;\;q\_m\\
        
        \mathbf{else}:\\
        \;\;\;\;r\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if r < 1.54999999999999997e32

          1. Initial program 52.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}{q} \]
          4. Step-by-step derivation
            1. Applied rewrites20.7%

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

            if 1.54999999999999997e32 < r

            1. Initial program 20.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} + \frac{1}{2} \cdot \frac{\left|p\right| + \left(\left|r\right| + -1 \cdot p\right)}{r}\right)} \]
            4. Step-by-step derivation
              1. metadata-evalN/A

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

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

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

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

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

              \[\leadsto r \]
            7. Step-by-step derivation
              1. Applied rewrites71.4%

                \[\leadsto r \]
            8. Recombined 2 regimes into one program.
            9. Add Preprocessing

            Alternative 7: 35.6% accurate, 250.0× 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 =     private
            NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
            module fmin_fmax_functions
                implicit none
                private
                public fmax
                public fmin
            
                interface fmax
                    module procedure fmax88
                    module procedure fmax44
                    module procedure fmax84
                    module procedure fmax48
                end interface
                interface fmin
                    module procedure fmin88
                    module procedure fmin44
                    module procedure fmin84
                    module procedure fmin48
                end interface
            contains
                real(8) function fmax88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(4) function fmax44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(8) function fmax84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmax48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                end function
                real(8) function fmin88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(4) function fmin44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(8) function fmin84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmin48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                end function
            end module
            
            real(8) function code(p, r, q_m)
            use fmin_fmax_functions
                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 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 45.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}{q} \]
            4. Step-by-step derivation
              1. Applied rewrites18.4%

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

              Reproduce

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