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

Percentage Accurate: 44.7% → 82.3%
Time: 4.6s
Alternatives: 8
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 8 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: 44.7% 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: 82.3% 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 5.4 \cdot 10^{+122}:\\ \;\;\;\;\left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\right)\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(q\_m + q\_m\right)\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 (<= q_m 5.4e+122)
   (* (+ (+ (fabs r) (fabs p)) (- r p)) 0.5)
   (* 0.5 (+ (+ (fabs p) (fabs r)) (+ 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 <= 5.4e+122) {
		tmp = ((fabs(r) + fabs(p)) + (r - p)) * 0.5;
	} else {
		tmp = 0.5 * ((fabs(p) + fabs(r)) + (q_m + q_m));
	}
	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 (q_m <= 5.4d+122) then
        tmp = ((abs(r) + abs(p)) + (r - p)) * 0.5d0
    else
        tmp = 0.5d0 * ((abs(p) + abs(r)) + (q_m + q_m))
    end if
    code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
	double tmp;
	if (q_m <= 5.4e+122) {
		tmp = ((Math.abs(r) + Math.abs(p)) + (r - p)) * 0.5;
	} else {
		tmp = 0.5 * ((Math.abs(p) + Math.abs(r)) + (q_m + q_m));
	}
	return tmp;
}
q_m = math.fabs(q)
[p, r, q_m] = sort([p, r, q_m])
def code(p, r, q_m):
	tmp = 0
	if q_m <= 5.4e+122:
		tmp = ((math.fabs(r) + math.fabs(p)) + (r - p)) * 0.5
	else:
		tmp = 0.5 * ((math.fabs(p) + math.fabs(r)) + (q_m + q_m))
	return tmp
q_m = abs(q)
p, r, q_m = sort([p, r, q_m])
function code(p, r, q_m)
	tmp = 0.0
	if (q_m <= 5.4e+122)
		tmp = Float64(Float64(Float64(abs(r) + abs(p)) + Float64(r - p)) * 0.5);
	else
		tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) + Float64(q_m + q_m)));
	end
	return tmp
end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
	tmp = 0.0;
	if (q_m <= 5.4e+122)
		tmp = ((abs(r) + abs(p)) + (r - p)) * 0.5;
	else
		tmp = 0.5 * ((abs(p) + abs(r)) + (q_m + q_m));
	end
	tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 5.4e+122], N[(N[(N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] + N[(r - p), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[(q$95$m + q$95$m), $MachinePrecision]), $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}\;q\_m \leq 5.4 \cdot 10^{+122}:\\
\;\;\;\;\left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\right)\right) \cdot 0.5\\

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


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

    1. Initial program 49.5%

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

      \[\leadsto \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-/.f6431.4

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

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

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

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

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

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

    if 5.3999999999999997e122 < q

    1. Initial program 18.4%

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

      \[\leadsto \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-*.f6484.8

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

      \[\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. metadata-eval84.8

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

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

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

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

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

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

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

Alternative 2: 65.4% accurate, 8.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}\;r \leq 1.52 \cdot 10^{-274}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\ \mathbf{elif}\;r \leq 2.15 \cdot 10^{+64}:\\ \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(q\_m + q\_m\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + r\right)\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.52e-274)
   (* 0.5 (+ (fabs p) (- p)))
   (if (<= r 2.15e+64)
     (* 0.5 (+ (+ (fabs p) (fabs r)) (+ q_m q_m)))
     (* 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.52e-274) {
		tmp = 0.5 * (fabs(p) + -p);
	} else if (r <= 2.15e+64) {
		tmp = 0.5 * ((fabs(p) + fabs(r)) + (q_m + q_m));
	} else {
		tmp = 0.5 * (fabs(p) + (fabs(r) + 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.52d-274) then
        tmp = 0.5d0 * (abs(p) + -p)
    else if (r <= 2.15d+64) then
        tmp = 0.5d0 * ((abs(p) + abs(r)) + (q_m + q_m))
    else
        tmp = 0.5d0 * (abs(p) + (abs(r) + 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.52e-274) {
		tmp = 0.5 * (Math.abs(p) + -p);
	} else if (r <= 2.15e+64) {
		tmp = 0.5 * ((Math.abs(p) + Math.abs(r)) + (q_m + q_m));
	} else {
		tmp = 0.5 * (Math.abs(p) + (Math.abs(r) + 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.52e-274:
		tmp = 0.5 * (math.fabs(p) + -p)
	elif r <= 2.15e+64:
		tmp = 0.5 * ((math.fabs(p) + math.fabs(r)) + (q_m + q_m))
	else:
		tmp = 0.5 * (math.fabs(p) + (math.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.52e-274)
		tmp = Float64(0.5 * Float64(abs(p) + Float64(-p)));
	elseif (r <= 2.15e+64)
		tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) + Float64(q_m + q_m)));
	else
		tmp = Float64(0.5 * Float64(abs(p) + Float64(abs(r) + 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.52e-274)
		tmp = 0.5 * (abs(p) + -p);
	elseif (r <= 2.15e+64)
		tmp = 0.5 * ((abs(p) + abs(r)) + (q_m + q_m));
	else
		tmp = 0.5 * (abs(p) + (abs(r) + 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.52e-274], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 2.15e+64], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[(q$95$m + q$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] + r), $MachinePrecision]), $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.52 \cdot 10^{-274}:\\
\;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\

\mathbf{elif}\;r \leq 2.15 \cdot 10^{+64}:\\
\;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(q\_m + q\_m\right)\right)\\

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


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

    1. Initial program 40.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}\right) \]
    4. Step-by-step derivation
      1. Applied rewrites8.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-eval8.4

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

        \[\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.f649.2

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

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

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

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

          \[\leadsto 0.5 \cdot \left(\left|p\right| + \left(-p\right)\right) \]
      8. Applied rewrites20.7%

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

      if 1.51999999999999989e-274 < r < 2.1499999999999999e64

      1. Initial program 67.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 \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-*.f6439.2

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

        \[\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. metadata-eval39.2

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

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

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

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

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

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

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

      if 2.1499999999999999e64 < r

      1. Initial program 34.7%

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

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

          \[\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-eval68.8

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

          \[\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.f6468.8

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

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

      Alternative 3: 64.8% 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.52 \cdot 10^{-274}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\ \mathbf{elif}\;r \leq 2.2 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + 2 \cdot q\_m\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + r\right)\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.52e-274)
         (* 0.5 (+ (fabs p) (- p)))
         (if (<= r 2.2e+41)
           (* 0.5 (+ (fabs p) (* 2.0 q_m)))
           (* 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.52e-274) {
      		tmp = 0.5 * (fabs(p) + -p);
      	} else if (r <= 2.2e+41) {
      		tmp = 0.5 * (fabs(p) + (2.0 * q_m));
      	} else {
      		tmp = 0.5 * (fabs(p) + (fabs(r) + 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.52d-274) then
              tmp = 0.5d0 * (abs(p) + -p)
          else if (r <= 2.2d+41) then
              tmp = 0.5d0 * (abs(p) + (2.0d0 * q_m))
          else
              tmp = 0.5d0 * (abs(p) + (abs(r) + 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.52e-274) {
      		tmp = 0.5 * (Math.abs(p) + -p);
      	} else if (r <= 2.2e+41) {
      		tmp = 0.5 * (Math.abs(p) + (2.0 * q_m));
      	} else {
      		tmp = 0.5 * (Math.abs(p) + (Math.abs(r) + 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.52e-274:
      		tmp = 0.5 * (math.fabs(p) + -p)
      	elif r <= 2.2e+41:
      		tmp = 0.5 * (math.fabs(p) + (2.0 * q_m))
      	else:
      		tmp = 0.5 * (math.fabs(p) + (math.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.52e-274)
      		tmp = Float64(0.5 * Float64(abs(p) + Float64(-p)));
      	elseif (r <= 2.2e+41)
      		tmp = Float64(0.5 * Float64(abs(p) + Float64(2.0 * q_m)));
      	else
      		tmp = Float64(0.5 * Float64(abs(p) + Float64(abs(r) + 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.52e-274)
      		tmp = 0.5 * (abs(p) + -p);
      	elseif (r <= 2.2e+41)
      		tmp = 0.5 * (abs(p) + (2.0 * q_m));
      	else
      		tmp = 0.5 * (abs(p) + (abs(r) + 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.52e-274], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 2.2e+41], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + N[(2.0 * q$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] + r), $MachinePrecision]), $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.52 \cdot 10^{-274}:\\
      \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\
      
      \mathbf{elif}\;r \leq 2.2 \cdot 10^{+41}:\\
      \;\;\;\;0.5 \cdot \left(\left|p\right| + 2 \cdot q\_m\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + r\right)\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if r < 1.51999999999999989e-274

        1. Initial program 40.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}\right) \]
        4. Step-by-step derivation
          1. Applied rewrites8.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-eval8.4

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

            \[\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.f649.2

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

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

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

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

              \[\leadsto 0.5 \cdot \left(\left|p\right| + \left(-p\right)\right) \]
          8. Applied rewrites20.7%

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

          if 1.51999999999999989e-274 < r < 2.1999999999999999e41

          1. Initial program 69.2%

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

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r}\right) \]
          4. Step-by-step derivation
            1. Applied rewrites19.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-eval19.5

                \[\leadsto \color{blue}{0.5} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
            3. Applied rewrites19.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.f6419.5

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

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

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

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

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

            if 2.1999999999999999e41 < r

            1. Initial program 33.6%

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

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

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

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

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

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

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

            Alternative 4: 64.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.52 \cdot 10^{-274}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\ \mathbf{elif}\;r \leq 2.15 \cdot 10^{+64}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + 2 \cdot q\_m\right)\\ \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.52e-274)
               (* 0.5 (+ (fabs p) (- p)))
               (if (<= r 2.15e+64) (* 0.5 (+ (fabs p) (* 2.0 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.52e-274) {
            		tmp = 0.5 * (fabs(p) + -p);
            	} else if (r <= 2.15e+64) {
            		tmp = 0.5 * (fabs(p) + (2.0 * 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.52d-274) then
                    tmp = 0.5d0 * (abs(p) + -p)
                else if (r <= 2.15d+64) then
                    tmp = 0.5d0 * (abs(p) + (2.0d0 * 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.52e-274) {
            		tmp = 0.5 * (Math.abs(p) + -p);
            	} else if (r <= 2.15e+64) {
            		tmp = 0.5 * (Math.abs(p) + (2.0 * 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.52e-274:
            		tmp = 0.5 * (math.fabs(p) + -p)
            	elif r <= 2.15e+64:
            		tmp = 0.5 * (math.fabs(p) + (2.0 * 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.52e-274)
            		tmp = Float64(0.5 * Float64(abs(p) + Float64(-p)));
            	elseif (r <= 2.15e+64)
            		tmp = Float64(0.5 * Float64(abs(p) + Float64(2.0 * 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.52e-274)
            		tmp = 0.5 * (abs(p) + -p);
            	elseif (r <= 2.15e+64)
            		tmp = 0.5 * (abs(p) + (2.0 * 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.52e-274], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 2.15e+64], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + N[(2.0 * q$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 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.52 \cdot 10^{-274}:\\
            \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\
            
            \mathbf{elif}\;r \leq 2.15 \cdot 10^{+64}:\\
            \;\;\;\;0.5 \cdot \left(\left|p\right| + 2 \cdot q\_m\right)\\
            
            \mathbf{else}:\\
            \;\;\;\;r\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 3 regimes
            2. if r < 1.51999999999999989e-274

              1. Initial program 40.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}\right) \]
              4. Step-by-step derivation
                1. Applied rewrites8.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-eval8.4

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

                  \[\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.f649.2

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

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

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

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

                    \[\leadsto 0.5 \cdot \left(\left|p\right| + \left(-p\right)\right) \]
                8. Applied rewrites20.7%

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

                if 1.51999999999999989e-274 < r < 2.1499999999999999e64

                1. Initial program 67.1%

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

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

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

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

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

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

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

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

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

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

                  if 2.1499999999999999e64 < r

                  1. Initial program 34.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 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 rewrites31.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 inf

                    \[\leadsto r \]
                  7. Step-by-step derivation
                    1. Applied rewrites66.6%

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

                  Alternative 5: 64.6% accurate, 9.2× speedup?

                  \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;p \leq -5.8 \cdot 10^{+67}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\ \mathbf{elif}\;p \leq 8.2 \cdot 10^{-282}:\\ \;\;\;\;\left(\mathsf{fma}\left(q\_m, 2, r\right) + p\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(r + \left(r + p\right)\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 (<= p -5.8e+67)
                     (* 0.5 (+ (fabs p) (- p)))
                     (if (<= p 8.2e-282) (* (+ (fma q_m 2.0 r) p) 0.5) (* (+ r (+ 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 (p <= -5.8e+67) {
                  		tmp = 0.5 * (fabs(p) + -p);
                  	} else if (p <= 8.2e-282) {
                  		tmp = (fma(q_m, 2.0, r) + p) * 0.5;
                  	} else {
                  		tmp = (r + (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 (p <= -5.8e+67)
                  		tmp = Float64(0.5 * Float64(abs(p) + Float64(-p)));
                  	elseif (p <= 8.2e-282)
                  		tmp = Float64(Float64(fma(q_m, 2.0, r) + p) * 0.5);
                  	else
                  		tmp = Float64(Float64(r + Float64(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[p, -5.8e+67], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 8.2e-282], N[(N[(N[(q$95$m * 2.0 + r), $MachinePrecision] + p), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(r + N[(r + p), $MachinePrecision]), $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}\;p \leq -5.8 \cdot 10^{+67}:\\
                  \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\
                  
                  \mathbf{elif}\;p \leq 8.2 \cdot 10^{-282}:\\
                  \;\;\;\;\left(\mathsf{fma}\left(q\_m, 2, r\right) + p\right) \cdot 0.5\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\left(r + \left(r + p\right)\right) \cdot 0.5\\
                  
                  
                  \end{array}
                  \end{array}
                  
                  Derivation
                  1. Split input into 3 regimes
                  2. if p < -5.80000000000000047e67

                    1. Initial program 29.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 \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{r}\right) \]
                    4. Step-by-step derivation
                      1. Applied rewrites20.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-eval20.5

                          \[\leadsto \color{blue}{0.5} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
                      3. Applied rewrites20.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.f6420.6

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

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

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

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

                          \[\leadsto 0.5 \cdot \left(\left|p\right| + \left(-p\right)\right) \]
                      8. Applied rewrites78.8%

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

                      if -5.80000000000000047e67 < p < 8.19999999999999954e-282

                      1. Initial program 55.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 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 rewrites38.7%

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

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

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

                      if 8.19999999999999954e-282 < p

                      1. Initial program 44.1%

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

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

                          \[\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. lift-/.f64N/A

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

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

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

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

                      Alternative 6: 62.3% accurate, 13.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}\;r \leq 1.16 \cdot 10^{-273}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\ \mathbf{elif}\;r \leq 2.15 \cdot 10^{+64}:\\ \;\;\;\;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.16e-273) (* 0.5 (+ (fabs p) (- p))) (if (<= r 2.15e+64) 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.16e-273) {
                      		tmp = 0.5 * (fabs(p) + -p);
                      	} else if (r <= 2.15e+64) {
                      		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.16d-273) then
                              tmp = 0.5d0 * (abs(p) + -p)
                          else if (r <= 2.15d+64) 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.16e-273) {
                      		tmp = 0.5 * (Math.abs(p) + -p);
                      	} else if (r <= 2.15e+64) {
                      		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.16e-273:
                      		tmp = 0.5 * (math.fabs(p) + -p)
                      	elif r <= 2.15e+64:
                      		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.16e-273)
                      		tmp = Float64(0.5 * Float64(abs(p) + Float64(-p)));
                      	elseif (r <= 2.15e+64)
                      		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.16e-273)
                      		tmp = 0.5 * (abs(p) + -p);
                      	elseif (r <= 2.15e+64)
                      		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.16e-273], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + (-p)), $MachinePrecision]), $MachinePrecision], If[LessEqual[r, 2.15e+64], 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.16 \cdot 10^{-273}:\\
                      \;\;\;\;0.5 \cdot \left(\left|p\right| + \left(-p\right)\right)\\
                      
                      \mathbf{elif}\;r \leq 2.15 \cdot 10^{+64}:\\
                      \;\;\;\;q\_m\\
                      
                      \mathbf{else}:\\
                      \;\;\;\;r\\
                      
                      
                      \end{array}
                      \end{array}
                      
                      Derivation
                      1. Split input into 3 regimes
                      2. if r < 1.1599999999999999e-273

                        1. Initial program 40.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}\right) \]
                        4. Step-by-step derivation
                          1. Applied rewrites8.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-eval8.4

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

                            \[\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.f649.2

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

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

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

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

                              \[\leadsto 0.5 \cdot \left(\left|p\right| + \left(-p\right)\right) \]
                          8. Applied rewrites20.7%

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

                          if 1.1599999999999999e-273 < r < 2.1499999999999999e64

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

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

                            if 2.1499999999999999e64 < r

                            1. Initial program 34.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 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 rewrites31.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 inf

                              \[\leadsto r \]
                            7. Step-by-step derivation
                              1. Applied rewrites66.6%

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

                            Alternative 7: 54.5% 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 2.15 \cdot 10^{+64}:\\ \;\;\;\;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 2.15e+64) 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 <= 2.15e+64) {
                            		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 <= 2.15d+64) 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 <= 2.15e+64) {
                            		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 <= 2.15e+64:
                            		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 <= 2.15e+64)
                            		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 <= 2.15e+64)
                            		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, 2.15e+64], 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 2.15 \cdot 10^{+64}:\\
                            \;\;\;\;q\_m\\
                            
                            \mathbf{else}:\\
                            \;\;\;\;r\\
                            
                            
                            \end{array}
                            \end{array}
                            
                            Derivation
                            1. Split input into 2 regimes
                            2. if r < 2.1499999999999999e64

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

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

                                if 2.1499999999999999e64 < r

                                1. Initial program 34.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 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 rewrites31.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 inf

                                  \[\leadsto r \]
                                7. Step-by-step derivation
                                  1. Applied rewrites66.6%

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

                                Alternative 8: 36.1% 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.4%

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

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

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

                                  Reproduce

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