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

Percentage Accurate: 45.2% → 67.9%
Time: 3.2s
Alternatives: 8
Speedup: 2.9×

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.2% 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: 67.9% accurate, 0.9× speedup?

\[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;p \leq -1.25 \cdot 10^{+124}:\\ \;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\ \mathbf{elif}\;p \leq -1.05 \cdot 10^{-264}:\\ \;\;\;\;\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;r\\ \end{array} \end{array} \]
NOTE: p, r, and q should be sorted in increasing order before calling this function.
(FPCore (p r q)
 :precision binary64
 (if (<= p -1.25e+124)
   (* (+ (- p) (+ (fabs r) (fabs p))) 0.5)
   (if (<= p -1.05e-264)
     (*
      (/ 1.0 2.0)
      (+
       (+ (fabs p) (fabs r))
       (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0))))))
     r)))
assert(p < r && r < q);
double code(double p, double r, double q) {
	double tmp;
	if (p <= -1.25e+124) {
		tmp = (-p + (fabs(r) + fabs(p))) * 0.5;
	} else if (p <= -1.05e-264) {
		tmp = (1.0 / 2.0) * ((fabs(p) + fabs(r)) + sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
	} else {
		tmp = r;
	}
	return tmp;
}
NOTE: p, r, and q 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)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q
    real(8) :: tmp
    if (p <= (-1.25d+124)) then
        tmp = (-p + (abs(r) + abs(p))) * 0.5d0
    else if (p <= (-1.05d-264)) then
        tmp = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) + sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
    else
        tmp = r
    end if
    code = tmp
end function
assert p < r && r < q;
public static double code(double p, double r, double q) {
	double tmp;
	if (p <= -1.25e+124) {
		tmp = (-p + (Math.abs(r) + Math.abs(p))) * 0.5;
	} else if (p <= -1.05e-264) {
		tmp = (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)))));
	} else {
		tmp = r;
	}
	return tmp;
}
[p, r, q] = sort([p, r, q])
def code(p, r, q):
	tmp = 0
	if p <= -1.25e+124:
		tmp = (-p + (math.fabs(r) + math.fabs(p))) * 0.5
	elif p <= -1.05e-264:
		tmp = (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)))))
	else:
		tmp = r
	return tmp
p, r, q = sort([p, r, q])
function code(p, r, q)
	tmp = 0.0
	if (p <= -1.25e+124)
		tmp = Float64(Float64(Float64(-p) + Float64(abs(r) + abs(p))) * 0.5);
	elseif (p <= -1.05e-264)
		tmp = 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))))));
	else
		tmp = r;
	end
	return tmp
end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp_2 = code(p, r, q)
	tmp = 0.0;
	if (p <= -1.25e+124)
		tmp = (-p + (abs(r) + abs(p))) * 0.5;
	elseif (p <= -1.05e-264)
		tmp = (1.0 / 2.0) * ((abs(p) + abs(r)) + sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0)))));
	else
		tmp = r;
	end
	tmp_2 = tmp;
end
NOTE: p, r, and q should be sorted in increasing order before calling this function.
code[p_, r_, q_] := If[LessEqual[p, -1.25e+124], N[(N[((-p) + N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[p, -1.05e-264], 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], r]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq -1.25 \cdot 10^{+124}:\\
\;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\

\mathbf{elif}\;p \leq -1.05 \cdot 10^{-264}:\\
\;\;\;\;\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)\\

\mathbf{else}:\\
\;\;\;\;r\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < -1.2499999999999999e124

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

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

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

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

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

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

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

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

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

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

    if -1.2499999999999999e124 < p < -1.0500000000000001e-264

    1. Initial program 62.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) \]

    if -1.0500000000000001e-264 < p

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{r} \]
      5. Step-by-step derivation
        1. Applied rewrites65.7%

          \[\leadsto \color{blue}{r} \]
      6. Recombined 3 regimes into one program.
      7. Add Preprocessing

      Alternative 2: 61.5% accurate, 1.8× speedup?

      \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} t_0 := \left|r\right| + \left|p\right|\\ \mathbf{if}\;p \leq -3.8 \cdot 10^{+153}:\\ \;\;\;\;\left(\left(-p\right) + t\_0\right) \cdot 0.5\\ \mathbf{elif}\;p \leq -1.2 \cdot 10^{-92}:\\ \;\;\;\;\left(t\_0 + \sqrt{\mathsf{fma}\left(q \cdot q, 4, p \cdot p\right)}\right) \cdot 0.5\\ \mathbf{elif}\;p \leq -6 \cdot 10^{-278}:\\ \;\;\;\;\left(\left(r + p\right) + \left(q + q\right)\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;r\\ \end{array} \end{array} \]
      NOTE: p, r, and q should be sorted in increasing order before calling this function.
      (FPCore (p r q)
       :precision binary64
       (let* ((t_0 (+ (fabs r) (fabs p))))
         (if (<= p -3.8e+153)
           (* (+ (- p) t_0) 0.5)
           (if (<= p -1.2e-92)
             (* (+ t_0 (sqrt (fma (* q q) 4.0 (* p p)))) 0.5)
             (if (<= p -6e-278) (* (+ (+ r p) (+ q q)) 0.5) r)))))
      assert(p < r && r < q);
      double code(double p, double r, double q) {
      	double t_0 = fabs(r) + fabs(p);
      	double tmp;
      	if (p <= -3.8e+153) {
      		tmp = (-p + t_0) * 0.5;
      	} else if (p <= -1.2e-92) {
      		tmp = (t_0 + sqrt(fma((q * q), 4.0, (p * p)))) * 0.5;
      	} else if (p <= -6e-278) {
      		tmp = ((r + p) + (q + q)) * 0.5;
      	} else {
      		tmp = r;
      	}
      	return tmp;
      }
      
      p, r, q = sort([p, r, q])
      function code(p, r, q)
      	t_0 = Float64(abs(r) + abs(p))
      	tmp = 0.0
      	if (p <= -3.8e+153)
      		tmp = Float64(Float64(Float64(-p) + t_0) * 0.5);
      	elseif (p <= -1.2e-92)
      		tmp = Float64(Float64(t_0 + sqrt(fma(Float64(q * q), 4.0, Float64(p * p)))) * 0.5);
      	elseif (p <= -6e-278)
      		tmp = Float64(Float64(Float64(r + p) + Float64(q + q)) * 0.5);
      	else
      		tmp = r;
      	end
      	return tmp
      end
      
      NOTE: p, r, and q should be sorted in increasing order before calling this function.
      code[p_, r_, q_] := Block[{t$95$0 = N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p, -3.8e+153], N[(N[((-p) + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[p, -1.2e-92], N[(N[(t$95$0 + N[Sqrt[N[(N[(q * q), $MachinePrecision] * 4.0 + N[(p * p), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[p, -6e-278], N[(N[(N[(r + p), $MachinePrecision] + N[(q + q), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], r]]]]
      
      \begin{array}{l}
      [p, r, q] = \mathsf{sort}([p, r, q])\\
      \\
      \begin{array}{l}
      t_0 := \left|r\right| + \left|p\right|\\
      \mathbf{if}\;p \leq -3.8 \cdot 10^{+153}:\\
      \;\;\;\;\left(\left(-p\right) + t\_0\right) \cdot 0.5\\
      
      \mathbf{elif}\;p \leq -1.2 \cdot 10^{-92}:\\
      \;\;\;\;\left(t\_0 + \sqrt{\mathsf{fma}\left(q \cdot q, 4, p \cdot p\right)}\right) \cdot 0.5\\
      
      \mathbf{elif}\;p \leq -6 \cdot 10^{-278}:\\
      \;\;\;\;\left(\left(r + p\right) + \left(q + q\right)\right) \cdot 0.5\\
      
      \mathbf{else}:\\
      \;\;\;\;r\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 4 regimes
      2. if p < -3.79999999999999966e153

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

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

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

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

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

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

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

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

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

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

        if -3.79999999999999966e153 < p < -1.2000000000000001e-92

        1. Initial program 64.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 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. 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) \]
          2. lower-+.f6430.0

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

          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \color{blue}{\left(q + q\right)}\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) + \left(q + q\right)\right) \]
          2. metadata-eval30.0

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

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

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

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

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

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

        if -1.2000000000000001e-92 < p < -6e-278

        1. Initial program 58.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 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. 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) \]
          2. lower-+.f6434.6

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

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

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

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

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

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

        if -6e-278 < p

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

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

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

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

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

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

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

            \[\leadsto \color{blue}{r} \]
          5. Step-by-step derivation
            1. Applied rewrites66.2%

              \[\leadsto \color{blue}{r} \]
          6. Recombined 4 regimes into one program.
          7. Add Preprocessing

          Alternative 3: 58.1% accurate, 2.6× speedup?

          \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;p \leq -8.8 \cdot 10^{-14}:\\ \;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\ \mathbf{elif}\;p \leq -6 \cdot 10^{-278}:\\ \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(q + q\right)\right)\\ \mathbf{else}:\\ \;\;\;\;r\\ \end{array} \end{array} \]
          NOTE: p, r, and q should be sorted in increasing order before calling this function.
          (FPCore (p r q)
           :precision binary64
           (if (<= p -8.8e-14)
             (* (+ (- p) (+ (fabs r) (fabs p))) 0.5)
             (if (<= p -6e-278) (* 0.5 (+ (+ (fabs p) (fabs r)) (+ q q))) r)))
          assert(p < r && r < q);
          double code(double p, double r, double q) {
          	double tmp;
          	if (p <= -8.8e-14) {
          		tmp = (-p + (fabs(r) + fabs(p))) * 0.5;
          	} else if (p <= -6e-278) {
          		tmp = 0.5 * ((fabs(p) + fabs(r)) + (q + q));
          	} else {
          		tmp = r;
          	}
          	return tmp;
          }
          
          NOTE: p, r, and q 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)
          use fmin_fmax_functions
              real(8), intent (in) :: p
              real(8), intent (in) :: r
              real(8), intent (in) :: q
              real(8) :: tmp
              if (p <= (-8.8d-14)) then
                  tmp = (-p + (abs(r) + abs(p))) * 0.5d0
              else if (p <= (-6d-278)) then
                  tmp = 0.5d0 * ((abs(p) + abs(r)) + (q + q))
              else
                  tmp = r
              end if
              code = tmp
          end function
          
          assert p < r && r < q;
          public static double code(double p, double r, double q) {
          	double tmp;
          	if (p <= -8.8e-14) {
          		tmp = (-p + (Math.abs(r) + Math.abs(p))) * 0.5;
          	} else if (p <= -6e-278) {
          		tmp = 0.5 * ((Math.abs(p) + Math.abs(r)) + (q + q));
          	} else {
          		tmp = r;
          	}
          	return tmp;
          }
          
          [p, r, q] = sort([p, r, q])
          def code(p, r, q):
          	tmp = 0
          	if p <= -8.8e-14:
          		tmp = (-p + (math.fabs(r) + math.fabs(p))) * 0.5
          	elif p <= -6e-278:
          		tmp = 0.5 * ((math.fabs(p) + math.fabs(r)) + (q + q))
          	else:
          		tmp = r
          	return tmp
          
          p, r, q = sort([p, r, q])
          function code(p, r, q)
          	tmp = 0.0
          	if (p <= -8.8e-14)
          		tmp = Float64(Float64(Float64(-p) + Float64(abs(r) + abs(p))) * 0.5);
          	elseif (p <= -6e-278)
          		tmp = Float64(0.5 * Float64(Float64(abs(p) + abs(r)) + Float64(q + q)));
          	else
          		tmp = r;
          	end
          	return tmp
          end
          
          p, r, q = num2cell(sort([p, r, q])){:}
          function tmp_2 = code(p, r, q)
          	tmp = 0.0;
          	if (p <= -8.8e-14)
          		tmp = (-p + (abs(r) + abs(p))) * 0.5;
          	elseif (p <= -6e-278)
          		tmp = 0.5 * ((abs(p) + abs(r)) + (q + q));
          	else
          		tmp = r;
          	end
          	tmp_2 = tmp;
          end
          
          NOTE: p, r, and q should be sorted in increasing order before calling this function.
          code[p_, r_, q_] := If[LessEqual[p, -8.8e-14], N[(N[((-p) + N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[p, -6e-278], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] + N[(q + q), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], r]]
          
          \begin{array}{l}
          [p, r, q] = \mathsf{sort}([p, r, q])\\
          \\
          \begin{array}{l}
          \mathbf{if}\;p \leq -8.8 \cdot 10^{-14}:\\
          \;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\
          
          \mathbf{elif}\;p \leq -6 \cdot 10^{-278}:\\
          \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(q + q\right)\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;r\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if p < -8.8000000000000004e-14

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

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

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

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

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

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

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

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

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

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

            if -8.8000000000000004e-14 < p < -6e-278

            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. 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) \]
              2. lower-+.f6434.3

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

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

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

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

            if -6e-278 < p

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

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

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

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

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

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

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

                \[\leadsto \color{blue}{r} \]
              5. Step-by-step derivation
                1. Applied rewrites66.2%

                  \[\leadsto \color{blue}{r} \]
              6. Recombined 3 regimes into one program.
              7. Add Preprocessing

              Alternative 4: 57.5% accurate, 2.9× speedup?

              \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;p \leq -9.5 \cdot 10^{-30}:\\ \;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\ \mathbf{elif}\;p \leq -6 \cdot 10^{-278}:\\ \;\;\;\;\left(\left(r + p\right) + \left(q + q\right)\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;r\\ \end{array} \end{array} \]
              NOTE: p, r, and q should be sorted in increasing order before calling this function.
              (FPCore (p r q)
               :precision binary64
               (if (<= p -9.5e-30)
                 (* (+ (- p) (+ (fabs r) (fabs p))) 0.5)
                 (if (<= p -6e-278) (* (+ (+ r p) (+ q q)) 0.5) r)))
              assert(p < r && r < q);
              double code(double p, double r, double q) {
              	double tmp;
              	if (p <= -9.5e-30) {
              		tmp = (-p + (fabs(r) + fabs(p))) * 0.5;
              	} else if (p <= -6e-278) {
              		tmp = ((r + p) + (q + q)) * 0.5;
              	} else {
              		tmp = r;
              	}
              	return tmp;
              }
              
              NOTE: p, r, and q 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)
              use fmin_fmax_functions
                  real(8), intent (in) :: p
                  real(8), intent (in) :: r
                  real(8), intent (in) :: q
                  real(8) :: tmp
                  if (p <= (-9.5d-30)) then
                      tmp = (-p + (abs(r) + abs(p))) * 0.5d0
                  else if (p <= (-6d-278)) then
                      tmp = ((r + p) + (q + q)) * 0.5d0
                  else
                      tmp = r
                  end if
                  code = tmp
              end function
              
              assert p < r && r < q;
              public static double code(double p, double r, double q) {
              	double tmp;
              	if (p <= -9.5e-30) {
              		tmp = (-p + (Math.abs(r) + Math.abs(p))) * 0.5;
              	} else if (p <= -6e-278) {
              		tmp = ((r + p) + (q + q)) * 0.5;
              	} else {
              		tmp = r;
              	}
              	return tmp;
              }
              
              [p, r, q] = sort([p, r, q])
              def code(p, r, q):
              	tmp = 0
              	if p <= -9.5e-30:
              		tmp = (-p + (math.fabs(r) + math.fabs(p))) * 0.5
              	elif p <= -6e-278:
              		tmp = ((r + p) + (q + q)) * 0.5
              	else:
              		tmp = r
              	return tmp
              
              p, r, q = sort([p, r, q])
              function code(p, r, q)
              	tmp = 0.0
              	if (p <= -9.5e-30)
              		tmp = Float64(Float64(Float64(-p) + Float64(abs(r) + abs(p))) * 0.5);
              	elseif (p <= -6e-278)
              		tmp = Float64(Float64(Float64(r + p) + Float64(q + q)) * 0.5);
              	else
              		tmp = r;
              	end
              	return tmp
              end
              
              p, r, q = num2cell(sort([p, r, q])){:}
              function tmp_2 = code(p, r, q)
              	tmp = 0.0;
              	if (p <= -9.5e-30)
              		tmp = (-p + (abs(r) + abs(p))) * 0.5;
              	elseif (p <= -6e-278)
              		tmp = ((r + p) + (q + q)) * 0.5;
              	else
              		tmp = r;
              	end
              	tmp_2 = tmp;
              end
              
              NOTE: p, r, and q should be sorted in increasing order before calling this function.
              code[p_, r_, q_] := If[LessEqual[p, -9.5e-30], N[(N[((-p) + N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[p, -6e-278], N[(N[(N[(r + p), $MachinePrecision] + N[(q + q), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], r]]
              
              \begin{array}{l}
              [p, r, q] = \mathsf{sort}([p, r, q])\\
              \\
              \begin{array}{l}
              \mathbf{if}\;p \leq -9.5 \cdot 10^{-30}:\\
              \;\;\;\;\left(\left(-p\right) + \left(\left|r\right| + \left|p\right|\right)\right) \cdot 0.5\\
              
              \mathbf{elif}\;p \leq -6 \cdot 10^{-278}:\\
              \;\;\;\;\left(\left(r + p\right) + \left(q + q\right)\right) \cdot 0.5\\
              
              \mathbf{else}:\\
              \;\;\;\;r\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 3 regimes
              2. if p < -9.49999999999999939e-30

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

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

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

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

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

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

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

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

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

                if -9.49999999999999939e-30 < p < -6e-278

                1. Initial program 59.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. 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. 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) \]
                  2. lower-+.f6434.6

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

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

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

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

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

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

                if -6e-278 < p

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

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

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

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

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

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

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

                    \[\leadsto \color{blue}{r} \]
                  5. Step-by-step derivation
                    1. Applied rewrites66.2%

                      \[\leadsto \color{blue}{r} \]
                  6. Recombined 3 regimes into one program.
                  7. Add Preprocessing

                  Alternative 5: 44.5% accurate, 3.6× speedup?

                  \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;q \leq 4.5 \cdot 10^{-41}:\\ \;\;\;\;r\\ \mathbf{else}:\\ \;\;\;\;\left(\left(r + p\right) + \left(q + q\right)\right) \cdot 0.5\\ \end{array} \end{array} \]
                  NOTE: p, r, and q should be sorted in increasing order before calling this function.
                  (FPCore (p r q)
                   :precision binary64
                   (if (<= q 4.5e-41) r (* (+ (+ r p) (+ q q)) 0.5)))
                  assert(p < r && r < q);
                  double code(double p, double r, double q) {
                  	double tmp;
                  	if (q <= 4.5e-41) {
                  		tmp = r;
                  	} else {
                  		tmp = ((r + p) + (q + q)) * 0.5;
                  	}
                  	return tmp;
                  }
                  
                  NOTE: p, r, and q 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)
                  use fmin_fmax_functions
                      real(8), intent (in) :: p
                      real(8), intent (in) :: r
                      real(8), intent (in) :: q
                      real(8) :: tmp
                      if (q <= 4.5d-41) then
                          tmp = r
                      else
                          tmp = ((r + p) + (q + q)) * 0.5d0
                      end if
                      code = tmp
                  end function
                  
                  assert p < r && r < q;
                  public static double code(double p, double r, double q) {
                  	double tmp;
                  	if (q <= 4.5e-41) {
                  		tmp = r;
                  	} else {
                  		tmp = ((r + p) + (q + q)) * 0.5;
                  	}
                  	return tmp;
                  }
                  
                  [p, r, q] = sort([p, r, q])
                  def code(p, r, q):
                  	tmp = 0
                  	if q <= 4.5e-41:
                  		tmp = r
                  	else:
                  		tmp = ((r + p) + (q + q)) * 0.5
                  	return tmp
                  
                  p, r, q = sort([p, r, q])
                  function code(p, r, q)
                  	tmp = 0.0
                  	if (q <= 4.5e-41)
                  		tmp = r;
                  	else
                  		tmp = Float64(Float64(Float64(r + p) + Float64(q + q)) * 0.5);
                  	end
                  	return tmp
                  end
                  
                  p, r, q = num2cell(sort([p, r, q])){:}
                  function tmp_2 = code(p, r, q)
                  	tmp = 0.0;
                  	if (q <= 4.5e-41)
                  		tmp = r;
                  	else
                  		tmp = ((r + p) + (q + q)) * 0.5;
                  	end
                  	tmp_2 = tmp;
                  end
                  
                  NOTE: p, r, and q should be sorted in increasing order before calling this function.
                  code[p_, r_, q_] := If[LessEqual[q, 4.5e-41], r, N[(N[(N[(r + p), $MachinePrecision] + N[(q + q), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]]
                  
                  \begin{array}{l}
                  [p, r, q] = \mathsf{sort}([p, r, q])\\
                  \\
                  \begin{array}{l}
                  \mathbf{if}\;q \leq 4.5 \cdot 10^{-41}:\\
                  \;\;\;\;r\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\left(\left(r + p\right) + \left(q + q\right)\right) \cdot 0.5\\
                  
                  
                  \end{array}
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if q < 4.5e-41

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

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

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

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

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

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

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

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

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

                        if 4.5e-41 < q

                        1. Initial program 36.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. 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. 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) \]
                          2. lower-+.f6461.9

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

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

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

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

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

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

                      Alternative 6: 43.9% accurate, 11.9× speedup?

                      \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;q \leq 4.5 \cdot 10^{-41}:\\ \;\;\;\;r\\ \mathbf{else}:\\ \;\;\;\;q\\ \end{array} \end{array} \]
                      NOTE: p, r, and q should be sorted in increasing order before calling this function.
                      (FPCore (p r q) :precision binary64 (if (<= q 4.5e-41) r q))
                      assert(p < r && r < q);
                      double code(double p, double r, double q) {
                      	double tmp;
                      	if (q <= 4.5e-41) {
                      		tmp = r;
                      	} else {
                      		tmp = q;
                      	}
                      	return tmp;
                      }
                      
                      NOTE: p, r, and q 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)
                      use fmin_fmax_functions
                          real(8), intent (in) :: p
                          real(8), intent (in) :: r
                          real(8), intent (in) :: q
                          real(8) :: tmp
                          if (q <= 4.5d-41) then
                              tmp = r
                          else
                              tmp = q
                          end if
                          code = tmp
                      end function
                      
                      assert p < r && r < q;
                      public static double code(double p, double r, double q) {
                      	double tmp;
                      	if (q <= 4.5e-41) {
                      		tmp = r;
                      	} else {
                      		tmp = q;
                      	}
                      	return tmp;
                      }
                      
                      [p, r, q] = sort([p, r, q])
                      def code(p, r, q):
                      	tmp = 0
                      	if q <= 4.5e-41:
                      		tmp = r
                      	else:
                      		tmp = q
                      	return tmp
                      
                      p, r, q = sort([p, r, q])
                      function code(p, r, q)
                      	tmp = 0.0
                      	if (q <= 4.5e-41)
                      		tmp = r;
                      	else
                      		tmp = q;
                      	end
                      	return tmp
                      end
                      
                      p, r, q = num2cell(sort([p, r, q])){:}
                      function tmp_2 = code(p, r, q)
                      	tmp = 0.0;
                      	if (q <= 4.5e-41)
                      		tmp = r;
                      	else
                      		tmp = q;
                      	end
                      	tmp_2 = tmp;
                      end
                      
                      NOTE: p, r, and q should be sorted in increasing order before calling this function.
                      code[p_, r_, q_] := If[LessEqual[q, 4.5e-41], r, q]
                      
                      \begin{array}{l}
                      [p, r, q] = \mathsf{sort}([p, r, q])\\
                      \\
                      \begin{array}{l}
                      \mathbf{if}\;q \leq 4.5 \cdot 10^{-41}:\\
                      \;\;\;\;r\\
                      
                      \mathbf{else}:\\
                      \;\;\;\;q\\
                      
                      
                      \end{array}
                      \end{array}
                      
                      Derivation
                      1. Split input into 2 regimes
                      2. if q < 4.5e-41

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

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

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

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

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

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

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

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

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

                            if 4.5e-41 < q

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

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

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

                            Alternative 7: 35.4% accurate, 56.9× speedup?

                            \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ r \end{array} \]
                            NOTE: p, r, and q should be sorted in increasing order before calling this function.
                            (FPCore (p r q) :precision binary64 r)
                            assert(p < r && r < q);
                            double code(double p, double r, double q) {
                            	return r;
                            }
                            
                            NOTE: p, r, and q 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)
                            use fmin_fmax_functions
                                real(8), intent (in) :: p
                                real(8), intent (in) :: r
                                real(8), intent (in) :: q
                                code = r
                            end function
                            
                            assert p < r && r < q;
                            public static double code(double p, double r, double q) {
                            	return r;
                            }
                            
                            [p, r, q] = sort([p, r, q])
                            def code(p, r, q):
                            	return r
                            
                            p, r, q = sort([p, r, q])
                            function code(p, r, q)
                            	return r
                            end
                            
                            p, r, q = num2cell(sort([p, r, q])){:}
                            function tmp = code(p, r, q)
                            	tmp = r;
                            end
                            
                            NOTE: p, r, and q should be sorted in increasing order before calling this function.
                            code[p_, r_, q_] := r
                            
                            \begin{array}{l}
                            [p, r, q] = \mathsf{sort}([p, r, q])\\
                            \\
                            r
                            \end{array}
                            
                            Derivation
                            1. Initial program 45.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. Taylor expanded in p around inf

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

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

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

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

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

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

                                \[\leadsto \color{blue}{r} \]
                              5. Step-by-step derivation
                                1. Applied rewrites35.4%

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

                                Alternative 8: 1.9% accurate, 56.9× speedup?

                                \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ p \end{array} \]
                                NOTE: p, r, and q should be sorted in increasing order before calling this function.
                                (FPCore (p r q) :precision binary64 p)
                                assert(p < r && r < q);
                                double code(double p, double r, double q) {
                                	return p;
                                }
                                
                                NOTE: p, r, and q 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)
                                use fmin_fmax_functions
                                    real(8), intent (in) :: p
                                    real(8), intent (in) :: r
                                    real(8), intent (in) :: q
                                    code = p
                                end function
                                
                                assert p < r && r < q;
                                public static double code(double p, double r, double q) {
                                	return p;
                                }
                                
                                [p, r, q] = sort([p, r, q])
                                def code(p, r, q):
                                	return p
                                
                                p, r, q = sort([p, r, q])
                                function code(p, r, q)
                                	return p
                                end
                                
                                p, r, q = num2cell(sort([p, r, q])){:}
                                function tmp = code(p, r, q)
                                	tmp = p;
                                end
                                
                                NOTE: p, r, and q should be sorted in increasing order before calling this function.
                                code[p_, r_, q_] := p
                                
                                \begin{array}{l}
                                [p, r, q] = \mathsf{sort}([p, r, q])\\
                                \\
                                p
                                \end{array}
                                
                                Derivation
                                1. Initial program 45.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. Taylor expanded in p around inf

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

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

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

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

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

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

                                    \[\leadsto \color{blue}{p} \]
                                  5. Step-by-step derivation
                                    1. Applied rewrites1.9%

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

                                    Reproduce

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