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

Percentage Accurate: 45.3% → 82.4%
Time: 3.7s
Alternatives: 8
Speedup: 11.4×

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}

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: 45.3% 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.4% accurate, 9.3× speedup?

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

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

real(8) function code(p, r, q_m)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs(r) + abs(p)
    if (q_m <= 6.2d+125) then
        tmp = (t_0 + (-p + r)) * 0.5d0
    else
        tmp = ((q_m + q_m) + t_0) * 0.5d0
    end if
    code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
	double t_0 = Math.abs(r) + Math.abs(p);
	double tmp;
	if (q_m <= 6.2e+125) {
		tmp = (t_0 + (-p + r)) * 0.5;
	} else {
		tmp = ((q_m + q_m) + t_0) * 0.5;
	}
	return tmp;
}
q_m = math.fabs(q)
[p, r, q_m] = sort([p, r, q_m])
def code(p, r, q_m):
	t_0 = math.fabs(r) + math.fabs(p)
	tmp = 0
	if q_m <= 6.2e+125:
		tmp = (t_0 + (-p + r)) * 0.5
	else:
		tmp = ((q_m + q_m) + t_0) * 0.5
	return tmp
q_m = abs(q)
p, r, q_m = sort([p, r, q_m])
function code(p, r, q_m)
	t_0 = Float64(abs(r) + abs(p))
	tmp = 0.0
	if (q_m <= 6.2e+125)
		tmp = Float64(Float64(t_0 + Float64(Float64(-p) + r)) * 0.5);
	else
		tmp = Float64(Float64(Float64(q_m + q_m) + t_0) * 0.5);
	end
	return tmp
end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
	t_0 = abs(r) + abs(p);
	tmp = 0.0;
	if (q_m <= 6.2e+125)
		tmp = (t_0 + (-p + r)) * 0.5;
	else
		tmp = ((q_m + q_m) + t_0) * 0.5;
	end
	tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := Block[{t$95$0 = N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[q$95$m, 6.2e+125], N[(N[(t$95$0 + N[((-p) + r), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(q$95$m + q$95$m), $MachinePrecision] + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
t_0 := \left|r\right| + \left|p\right|\\
\mathbf{if}\;q\_m \leq 6.2 \cdot 10^{+125}:\\
\;\;\;\;\left(t\_0 + \left(\left(-p\right) + r\right)\right) \cdot 0.5\\

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


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

    1. Initial program 57.8%

      \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
    2. 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) \]
    3. 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-/.f6474.0

        \[\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) \]
    4. Applied rewrites74.0%

      \[\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) \]
    5. 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) \]
    6. Step-by-step derivation
      1. 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) \]
      2. +-commutativeN/A

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(-1 \cdot p + r\right)\right) \]
      4. lower-fma.f6484.9

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \mathsf{fma}\left(-1, p, r\right)\right) \cdot \color{blue}{0.5} \]
    9. Applied rewrites84.9%

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

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

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

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \left(\left(-p\right) + r\right)\right) \cdot 0.5 \]
    11. Applied rewrites84.9%

      \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \left(\left(-p\right) + r\right)\right) \cdot 0.5 \]

    if 6.2e125 < q

    1. Initial program 15.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. 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) \]
    3. 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-*.f6476.4

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 65.0% 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} t_0 := \left|r\right| + \left|p\right|\\ \mathbf{if}\;r \leq -3.4 \cdot 10^{-202}:\\ \;\;\;\;\left(t\_0 + \left(-p\right)\right) \cdot 0.5\\ \mathbf{elif}\;r \leq 2.25 \cdot 10^{+15}:\\ \;\;\;\;\left(\left(q\_m + q\_m\right) + t\_0\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(r + t\_0\right) \cdot 0.5\\ \end{array} \end{array} \]
q_m = (fabs.f64 q)
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
(FPCore (p r q_m)
 :precision binary64
 (let* ((t_0 (+ (fabs r) (fabs p))))
   (if (<= r -3.4e-202)
     (* (+ t_0 (- p)) 0.5)
     (if (<= r 2.25e+15) (* (+ (+ q_m q_m) t_0) 0.5) (* (+ r t_0) 0.5)))))
q_m = fabs(q);
assert(p < r && r < q_m);
double code(double p, double r, double q_m) {
	double t_0 = fabs(r) + fabs(p);
	double tmp;
	if (r <= -3.4e-202) {
		tmp = (t_0 + -p) * 0.5;
	} else if (r <= 2.25e+15) {
		tmp = ((q_m + q_m) + t_0) * 0.5;
	} else {
		tmp = (r + t_0) * 0.5;
	}
	return tmp;
}
q_m =     private
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(p, r, q_m)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs(r) + abs(p)
    if (r <= (-3.4d-202)) then
        tmp = (t_0 + -p) * 0.5d0
    else if (r <= 2.25d+15) then
        tmp = ((q_m + q_m) + t_0) * 0.5d0
    else
        tmp = (r + t_0) * 0.5d0
    end if
    code = tmp
end function
q_m = Math.abs(q);
assert p < r && r < q_m;
public static double code(double p, double r, double q_m) {
	double t_0 = Math.abs(r) + Math.abs(p);
	double tmp;
	if (r <= -3.4e-202) {
		tmp = (t_0 + -p) * 0.5;
	} else if (r <= 2.25e+15) {
		tmp = ((q_m + q_m) + t_0) * 0.5;
	} else {
		tmp = (r + t_0) * 0.5;
	}
	return tmp;
}
q_m = math.fabs(q)
[p, r, q_m] = sort([p, r, q_m])
def code(p, r, q_m):
	t_0 = math.fabs(r) + math.fabs(p)
	tmp = 0
	if r <= -3.4e-202:
		tmp = (t_0 + -p) * 0.5
	elif r <= 2.25e+15:
		tmp = ((q_m + q_m) + t_0) * 0.5
	else:
		tmp = (r + t_0) * 0.5
	return tmp
q_m = abs(q)
p, r, q_m = sort([p, r, q_m])
function code(p, r, q_m)
	t_0 = Float64(abs(r) + abs(p))
	tmp = 0.0
	if (r <= -3.4e-202)
		tmp = Float64(Float64(t_0 + Float64(-p)) * 0.5);
	elseif (r <= 2.25e+15)
		tmp = Float64(Float64(Float64(q_m + q_m) + t_0) * 0.5);
	else
		tmp = Float64(Float64(r + t_0) * 0.5);
	end
	return tmp
end
q_m = abs(q);
p, r, q_m = num2cell(sort([p, r, q_m])){:}
function tmp_2 = code(p, r, q_m)
	t_0 = abs(r) + abs(p);
	tmp = 0.0;
	if (r <= -3.4e-202)
		tmp = (t_0 + -p) * 0.5;
	elseif (r <= 2.25e+15)
		tmp = ((q_m + q_m) + t_0) * 0.5;
	else
		tmp = (r + t_0) * 0.5;
	end
	tmp_2 = tmp;
end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := Block[{t$95$0 = N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[r, -3.4e-202], N[(N[(t$95$0 + (-p)), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[r, 2.25e+15], N[(N[(N[(q$95$m + q$95$m), $MachinePrecision] + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(r + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision]]]]
\begin{array}{l}
q_m = \left|q\right|
\\
[p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
\\
\begin{array}{l}
t_0 := \left|r\right| + \left|p\right|\\
\mathbf{if}\;r \leq -3.4 \cdot 10^{-202}:\\
\;\;\;\;\left(t\_0 + \left(-p\right)\right) \cdot 0.5\\

\mathbf{elif}\;r \leq 2.25 \cdot 10^{+15}:\\
\;\;\;\;\left(\left(q\_m + q\_m\right) + t\_0\right) \cdot 0.5\\

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


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

    1. Initial program 42.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. 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) \]
    3. 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-/.f6462.0

        \[\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) \]
    4. Applied rewrites62.0%

      \[\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) \]
    5. 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) \]
    6. Step-by-step derivation
      1. 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) \]
      2. +-commutativeN/A

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

        \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(-1 \cdot p + r\right)\right) \]
      4. lower-fma.f6473.9

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \mathsf{fma}\left(-1, p, r\right)\right) \cdot \color{blue}{0.5} \]
    9. Applied rewrites73.9%

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

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

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

        \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \left(-p\right)\right) \cdot 0.5 \]
    12. Applied rewrites72.6%

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

    if -3.40000000000000012e-202 < r < 2.25e15

    1. Initial program 59.9%

      \[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
    2. 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) \]
    3. 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-*.f6456.3

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.25e15 < r

    1. Initial program 32.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. 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) \]
    3. 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-/.f6480.3

        \[\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) \]
    4. Applied rewrites80.3%

      \[\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) \]
    5. Taylor expanded in p around 0

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

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

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

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

      1. Initial program 42.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. 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) \]
      3. 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-/.f6462.0

          \[\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) \]
      4. Applied rewrites62.0%

        \[\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) \]
      5. 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) \]
      6. Step-by-step derivation
        1. 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) \]
        2. +-commutativeN/A

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

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(-1 \cdot p + r\right)\right) \]
        4. lower-fma.f6473.9

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

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \mathsf{fma}\left(-1, p, r\right)\right) \cdot \color{blue}{0.5} \]
      9. Applied rewrites73.9%

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

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

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

          \[\leadsto \left(\left(\left|r\right| + \left|p\right|\right) + \left(-p\right)\right) \cdot 0.5 \]
      12. Applied rewrites72.6%

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

      if -3.2000000000000001e-202 < r < 2.25e15

      1. Initial program 59.9%

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

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

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

        if 2.25e15 < r

        1. Initial program 32.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. 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) \]
        3. 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-/.f6480.3

            \[\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) \]
        4. Applied rewrites80.3%

          \[\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) \]
        5. Taylor expanded in p around 0

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

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

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

        Alternative 4: 54.7% accurate, 11.4× speedup?

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

          1. Initial program 54.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. Taylor expanded in q around inf

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

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

            if 2.25e15 < r

            1. Initial program 32.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. 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) \]
            3. 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-/.f6480.3

                \[\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) \]
            4. Applied rewrites80.3%

              \[\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) \]
            5. Taylor expanded in p around 0

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

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

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

            Alternative 5: 39.7% accurate, 15.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}\;p \leq -6.3 \cdot 10^{+193}:\\ \;\;\;\;-0.5 \cdot p\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(r + p, 0.5, q\_m\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 (<= p -6.3e+193) (* -0.5 p) (fma (+ r p) 0.5 q_m)))
            q_m = fabs(q);
            assert(p < r && r < q_m);
            double code(double p, double r, double q_m) {
            	double tmp;
            	if (p <= -6.3e+193) {
            		tmp = -0.5 * p;
            	} else {
            		tmp = fma((r + p), 0.5, 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 (p <= -6.3e+193)
            		tmp = Float64(-0.5 * p);
            	else
            		tmp = fma(Float64(r + p), 0.5, q_m);
            	end
            	return tmp
            end
            
            q_m = N[Abs[q], $MachinePrecision]
            NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
            code[p_, r_, q$95$m_] := If[LessEqual[p, -6.3e+193], N[(-0.5 * p), $MachinePrecision], N[(N[(r + p), $MachinePrecision] * 0.5 + q$95$m), $MachinePrecision]]
            
            \begin{array}{l}
            q_m = \left|q\right|
            \\
            [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;p \leq -6.3 \cdot 10^{+193}:\\
            \;\;\;\;-0.5 \cdot p\\
            
            \mathbf{else}:\\
            \;\;\;\;\mathsf{fma}\left(r + p, 0.5, q\_m\right)\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if p < -6.29999999999999999e193

              1. Initial program 7.9%

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

                \[\leadsto \color{blue}{\frac{-1}{2} \cdot p} \]
              3. Step-by-step derivation
                1. lower-*.f6417.4

                  \[\leadsto -0.5 \cdot \color{blue}{p} \]
              4. Applied rewrites17.4%

                \[\leadsto \color{blue}{-0.5 \cdot p} \]

              if -6.29999999999999999e193 < p

              1. Initial program 53.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. Taylor expanded in q around inf

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

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

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

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

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

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

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

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

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

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

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

                  \[\leadsto \mathsf{fma}\left(r + p, \frac{1}{2}, q\right) \]
                7. metadata-eval44.4

                  \[\leadsto \mathsf{fma}\left(r + p, 0.5, q\right) \]
              7. Applied rewrites44.4%

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

            Alternative 6: 35.5% accurate, 20.8× speedup?

            \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ \begin{array}{l} \mathbf{if}\;r \leq 2.9 \cdot 10^{+204}:\\ \;\;\;\;q\_m\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot 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.9e+204) q_m (* 0.5 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.9e+204) {
            		tmp = q_m;
            	} else {
            		tmp = 0.5 * 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.9d+204) then
                    tmp = q_m
                else
                    tmp = 0.5d0 * 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.9e+204) {
            		tmp = q_m;
            	} else {
            		tmp = 0.5 * 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.9e+204:
            		tmp = q_m
            	else:
            		tmp = 0.5 * 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.9e+204)
            		tmp = q_m;
            	else
            		tmp = Float64(0.5 * 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.9e+204)
            		tmp = q_m;
            	else
            		tmp = 0.5 * 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.9e+204], q$95$m, N[(0.5 * r), $MachinePrecision]]
            
            \begin{array}{l}
            q_m = \left|q\right|
            \\
            [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;r \leq 2.9 \cdot 10^{+204}:\\
            \;\;\;\;q\_m\\
            
            \mathbf{else}:\\
            \;\;\;\;0.5 \cdot r\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if r < 2.90000000000000004e204

              1. Initial program 52.8%

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

                \[\leadsto \color{blue}{q} \]
              3. Step-by-step derivation
                1. Applied rewrites39.2%

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

                if 2.90000000000000004e204 < r

                1. Initial program 8.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. Taylor expanded in r around inf

                  \[\leadsto \color{blue}{\frac{1}{2} \cdot r} \]
                3. Step-by-step derivation
                  1. metadata-evalN/A

                    \[\leadsto \frac{1}{2} \cdot r \]
                  2. lower-*.f64N/A

                    \[\leadsto \frac{1}{2} \cdot \color{blue}{r} \]
                  3. metadata-eval17.6

                    \[\leadsto 0.5 \cdot r \]
                4. Applied rewrites17.6%

                  \[\leadsto \color{blue}{0.5 \cdot r} \]
              4. Recombined 2 regimes into one program.
              5. Add Preprocessing

              Alternative 7: 35.5% accurate, 20.8× speedup?

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

                1. Initial program 7.9%

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

                  \[\leadsto \color{blue}{\frac{-1}{2} \cdot p} \]
                3. Step-by-step derivation
                  1. lower-*.f6417.4

                    \[\leadsto -0.5 \cdot \color{blue}{p} \]
                4. Applied rewrites17.4%

                  \[\leadsto \color{blue}{-0.5 \cdot p} \]

                if -2.34999999999999986e194 < p

                1. Initial program 53.0%

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

                  \[\leadsto \color{blue}{q} \]
                3. Step-by-step derivation
                  1. Applied rewrites39.2%

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

                Alternative 8: 34.4% 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.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. Taylor expanded in q around inf

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

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

                  Reproduce

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