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

Percentage Accurate: 45.0% → 80.6%
Time: 3.8s
Alternatives: 7
Speedup: 10.0×

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 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 45.0% 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: 80.6% accurate, 0.5× speedup?

\[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} t_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)\\ \mathbf{if}\;t\_0 \leq 2 \cdot 10^{+152}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;\left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\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
 (let* ((t_0
         (*
          (/ 1.0 2.0)
          (+
           (+ (fabs p) (fabs r))
           (sqrt (+ (pow (- p r) 2.0) (* 4.0 (pow q 2.0))))))))
   (if (<= t_0 2e+152) t_0 (* (+ (+ (fabs r) (fabs p)) (- r p)) 0.5))))
assert(p < r && r < q);
double code(double p, double r, double q) {
	double t_0 = (1.0 / 2.0) * ((fabs(p) + fabs(r)) + sqrt((pow((p - r), 2.0) + (4.0 * pow(q, 2.0)))));
	double tmp;
	if (t_0 <= 2e+152) {
		tmp = t_0;
	} else {
		tmp = ((fabs(r) + fabs(p)) + (r - p)) * 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) :: t_0
    real(8) :: tmp
    t_0 = (1.0d0 / 2.0d0) * ((abs(p) + abs(r)) + sqrt((((p - r) ** 2.0d0) + (4.0d0 * (q ** 2.0d0)))))
    if (t_0 <= 2d+152) then
        tmp = t_0
    else
        tmp = ((abs(r) + abs(p)) + (r - p)) * 0.5d0
    end if
    code = tmp
end function
assert p < r && r < q;
public static double code(double p, double r, double q) {
	double t_0 = (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)))));
	double tmp;
	if (t_0 <= 2e+152) {
		tmp = t_0;
	} else {
		tmp = ((Math.abs(r) + Math.abs(p)) + (r - p)) * 0.5;
	}
	return tmp;
}
[p, r, q] = sort([p, r, q])
def code(p, r, q):
	t_0 = (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)))))
	tmp = 0
	if t_0 <= 2e+152:
		tmp = t_0
	else:
		tmp = ((math.fabs(r) + math.fabs(p)) + (r - p)) * 0.5
	return tmp
p, r, q = sort([p, r, q])
function code(p, r, q)
	t_0 = 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))))))
	tmp = 0.0
	if (t_0 <= 2e+152)
		tmp = t_0;
	else
		tmp = Float64(Float64(Float64(abs(r) + abs(p)) + Float64(r - p)) * 0.5);
	end
	return tmp
end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp_2 = code(p, r, q)
	t_0 = (1.0 / 2.0) * ((abs(p) + abs(r)) + sqrt((((p - r) ^ 2.0) + (4.0 * (q ^ 2.0)))));
	tmp = 0.0;
	if (t_0 <= 2e+152)
		tmp = t_0;
	else
		tmp = ((abs(r) + abs(p)) + (r - p)) * 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_] := Block[{t$95$0 = 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]}, If[LessEqual[t$95$0, 2e+152], t$95$0, N[(N[(N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] + N[(r - p), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
t_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)\\
\mathbf{if}\;t\_0 \leq 2 \cdot 10^{+152}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (/.f64 #s(literal 1 binary64) #s(literal 2 binary64)) (+.f64 (+.f64 (fabs.f64 p) (fabs.f64 r)) (sqrt.f64 (+.f64 (pow.f64 (-.f64 p r) #s(literal 2 binary64)) (*.f64 #s(literal 4 binary64) (pow.f64 q #s(literal 2 binary64))))))) < 2.0000000000000001e152

    1. Initial program 97.0%

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

    if 2.0000000000000001e152 < (*.f64 (/.f64 #s(literal 1 binary64) #s(literal 2 binary64)) (+.f64 (+.f64 (fabs.f64 p) (fabs.f64 r)) (sqrt.f64 (+.f64 (pow.f64 (-.f64 p r) #s(literal 2 binary64)) (*.f64 #s(literal 4 binary64) (pow.f64 q #s(literal 2 binary64)))))))

    1. Initial program 7.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 76.0% accurate, 9.3× 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}\;q \leq 2 \cdot 10^{+120}:\\ \;\;\;\;\left(t\_0 + \left(r - p\right)\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(q \cdot 2 + t\_0\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
 (let* ((t_0 (+ (fabs r) (fabs p))))
   (if (<= q 2e+120) (* (+ t_0 (- r p)) 0.5) (* (+ (* q 2.0) t_0) 0.5))))
assert(p < r && r < q);
double code(double p, double r, double q) {
	double t_0 = fabs(r) + fabs(p);
	double tmp;
	if (q <= 2e+120) {
		tmp = (t_0 + (r - p)) * 0.5;
	} else {
		tmp = ((q * 2.0) + t_0) * 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) :: t_0
    real(8) :: tmp
    t_0 = abs(r) + abs(p)
    if (q <= 2d+120) then
        tmp = (t_0 + (r - p)) * 0.5d0
    else
        tmp = ((q * 2.0d0) + t_0) * 0.5d0
    end if
    code = tmp
end function
assert p < r && r < q;
public static double code(double p, double r, double q) {
	double t_0 = Math.abs(r) + Math.abs(p);
	double tmp;
	if (q <= 2e+120) {
		tmp = (t_0 + (r - p)) * 0.5;
	} else {
		tmp = ((q * 2.0) + t_0) * 0.5;
	}
	return tmp;
}
[p, r, q] = sort([p, r, q])
def code(p, r, q):
	t_0 = math.fabs(r) + math.fabs(p)
	tmp = 0
	if q <= 2e+120:
		tmp = (t_0 + (r - p)) * 0.5
	else:
		tmp = ((q * 2.0) + t_0) * 0.5
	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 (q <= 2e+120)
		tmp = Float64(Float64(t_0 + Float64(r - p)) * 0.5);
	else
		tmp = Float64(Float64(Float64(q * 2.0) + t_0) * 0.5);
	end
	return tmp
end
p, r, q = num2cell(sort([p, r, q])){:}
function tmp_2 = code(p, r, q)
	t_0 = abs(r) + abs(p);
	tmp = 0.0;
	if (q <= 2e+120)
		tmp = (t_0 + (r - p)) * 0.5;
	else
		tmp = ((q * 2.0) + t_0) * 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_] := Block[{t$95$0 = N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[q, 2e+120], N[(N[(t$95$0 + N[(r - p), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(q * 2.0), $MachinePrecision] + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
t_0 := \left|r\right| + \left|p\right|\\
\mathbf{if}\;q \leq 2 \cdot 10^{+120}:\\
\;\;\;\;\left(t\_0 + \left(r - p\right)\right) \cdot 0.5\\

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


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

    1. Initial program 49.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2e120 < q

    1. Initial program 17.1%

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

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

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

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

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

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

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

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

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

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

Alternative 3: 75.8% accurate, 10.0× speedup?

\[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;q \leq 5.2 \cdot 10^{+127}:\\ \;\;\;\;\left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\right)\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(r, 0.5, q\right)\\ \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 5.2e+127) (* (+ (+ (fabs r) (fabs p)) (- r p)) 0.5) (fma r 0.5 q)))
assert(p < r && r < q);
double code(double p, double r, double q) {
	double tmp;
	if (q <= 5.2e+127) {
		tmp = ((fabs(r) + fabs(p)) + (r - p)) * 0.5;
	} else {
		tmp = fma(r, 0.5, q);
	}
	return tmp;
}
p, r, q = sort([p, r, q])
function code(p, r, q)
	tmp = 0.0
	if (q <= 5.2e+127)
		tmp = Float64(Float64(Float64(abs(r) + abs(p)) + Float64(r - p)) * 0.5);
	else
		tmp = fma(r, 0.5, q);
	end
	return tmp
end
NOTE: p, r, and q should be sorted in increasing order before calling this function.
code[p_, r_, q_] := If[LessEqual[q, 5.2e+127], N[(N[(N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $MachinePrecision]), $MachinePrecision] + N[(r - p), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(r * 0.5 + q), $MachinePrecision]]
\begin{array}{l}
[p, r, q] = \mathsf{sort}([p, r, q])\\
\\
\begin{array}{l}
\mathbf{if}\;q \leq 5.2 \cdot 10^{+127}:\\
\;\;\;\;\left(\left(\left|r\right| + \left|p\right|\right) + \left(r - p\right)\right) \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(r, 0.5, q\right)\\


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

    1. Initial program 49.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 5.2000000000000004e127 < q

    1. Initial program 15.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(r, \frac{1}{2}, q\right) \]
    10. Step-by-step derivation
      1. Applied rewrites77.4%

        \[\leadsto \mathsf{fma}\left(r, 0.5, q\right) \]
    11. Recombined 2 regimes into one program.
    12. Add Preprocessing

    Alternative 4: 44.5% accurate, 12.5× speedup?

    \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;r \leq 0.00039:\\ \;\;\;\;\mathsf{fma}\left(r, 0.5, q\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + r\right) + r\right)\\ \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 (<= r 0.00039) (fma r 0.5 q) (* 0.5 (+ (+ (fabs p) r) r))))
    assert(p < r && r < q);
    double code(double p, double r, double q) {
    	double tmp;
    	if (r <= 0.00039) {
    		tmp = fma(r, 0.5, q);
    	} else {
    		tmp = 0.5 * ((fabs(p) + r) + r);
    	}
    	return tmp;
    }
    
    p, r, q = sort([p, r, q])
    function code(p, r, q)
    	tmp = 0.0
    	if (r <= 0.00039)
    		tmp = fma(r, 0.5, q);
    	else
    		tmp = Float64(0.5 * Float64(Float64(abs(p) + r) + r));
    	end
    	return tmp
    end
    
    NOTE: p, r, and q should be sorted in increasing order before calling this function.
    code[p_, r_, q_] := If[LessEqual[r, 0.00039], N[(r * 0.5 + q), $MachinePrecision], N[(0.5 * N[(N[(N[Abs[p], $MachinePrecision] + r), $MachinePrecision] + r), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    [p, r, q] = \mathsf{sort}([p, r, q])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;r \leq 0.00039:\\
    \;\;\;\;\mathsf{fma}\left(r, 0.5, q\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;0.5 \cdot \left(\left(\left|p\right| + r\right) + r\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if r < 3.89999999999999993e-4

      1. Initial program 53.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(r, \frac{1}{2}, q\right) \]
      10. Step-by-step derivation
        1. Applied rewrites23.8%

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

        if 3.89999999999999993e-4 < r

        1. Initial program 34.9%

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

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

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

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

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

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

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

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

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

              \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \sqrt{\color{blue}{e^{\log r \cdot 2}}}\right) + r\right) \]
            5. exp-sqrt-revN/A

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

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

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

              \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + e^{\frac{\color{blue}{\log r \cdot 2}}{2}}\right) + r\right) \]
            9. lower-log.f6464.4

              \[\leadsto 0.5 \cdot \left(\left(\left|p\right| + e^{\frac{\color{blue}{\log r} \cdot 2}{2}}\right) + r\right) \]
          5. Applied rewrites64.4%

            \[\leadsto 0.5 \cdot \left(\left(\left|p\right| + \color{blue}{e^{\frac{\log r \cdot 2}{2}}}\right) + r\right) \]
          6. Taylor expanded in r around 0

            \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \color{blue}{r}\right) + r\right) \]
          7. Step-by-step derivation
            1. exp-sqrt69.8

              \[\leadsto 0.5 \cdot \left(\left(\left|p\right| + r\right) + r\right) \]
            2. pow-to-exp69.8

              \[\leadsto 0.5 \cdot \left(\left(\left|p\right| + r\right) + r\right) \]
            3. pow269.8

              \[\leadsto 0.5 \cdot \left(\left(\left|p\right| + r\right) + r\right) \]
            4. rem-sqrt-square-rev69.8

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

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

        Alternative 5: 44.1% accurate, 19.2× speedup?

        \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;r \leq 0.00039:\\ \;\;\;\;\mathsf{fma}\left(r, 0.5, q\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 (<= r 0.00039) (fma r 0.5 q) r))
        assert(p < r && r < q);
        double code(double p, double r, double q) {
        	double tmp;
        	if (r <= 0.00039) {
        		tmp = fma(r, 0.5, q);
        	} else {
        		tmp = r;
        	}
        	return tmp;
        }
        
        p, r, q = sort([p, r, q])
        function code(p, r, q)
        	tmp = 0.0
        	if (r <= 0.00039)
        		tmp = fma(r, 0.5, q);
        	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_] := If[LessEqual[r, 0.00039], N[(r * 0.5 + q), $MachinePrecision], r]
        
        \begin{array}{l}
        [p, r, q] = \mathsf{sort}([p, r, q])\\
        \\
        \begin{array}{l}
        \mathbf{if}\;r \leq 0.00039:\\
        \;\;\;\;\mathsf{fma}\left(r, 0.5, q\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;r\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if r < 3.89999999999999993e-4

          1. Initial program 53.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \mathsf{fma}\left(r, \frac{1}{2}, q\right) \]
          10. Step-by-step derivation
            1. Applied rewrites23.8%

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

            if 3.89999999999999993e-4 < r

            1. Initial program 34.9%

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

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

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

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

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

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

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

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

            Alternative 6: 43.5% accurate, 35.6× speedup?

            \[\begin{array}{l} [p, r, q] = \mathsf{sort}([p, r, q])\\ \\ \begin{array}{l} \mathbf{if}\;r \leq 0.00039:\\ \;\;\;\;q\\ \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 (<= r 0.00039) q r))
            assert(p < r && r < q);
            double code(double p, double r, double q) {
            	double tmp;
            	if (r <= 0.00039) {
            		tmp = 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 (r <= 0.00039d0) then
                    tmp = 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 (r <= 0.00039) {
            		tmp = q;
            	} else {
            		tmp = r;
            	}
            	return tmp;
            }
            
            [p, r, q] = sort([p, r, q])
            def code(p, r, q):
            	tmp = 0
            	if r <= 0.00039:
            		tmp = q
            	else:
            		tmp = r
            	return tmp
            
            p, r, q = sort([p, r, q])
            function code(p, r, q)
            	tmp = 0.0
            	if (r <= 0.00039)
            		tmp = 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 (r <= 0.00039)
            		tmp = 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[r, 0.00039], q, r]
            
            \begin{array}{l}
            [p, r, q] = \mathsf{sort}([p, r, q])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;r \leq 0.00039:\\
            \;\;\;\;q\\
            
            \mathbf{else}:\\
            \;\;\;\;r\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if r < 3.89999999999999993e-4

              1. Initial program 53.3%

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

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

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

                if 3.89999999999999993e-4 < r

                1. Initial program 34.9%

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

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

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

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

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

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

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

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

                Alternative 7: 17.8% accurate, 250.0× speedup?

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

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

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

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

                  Reproduce

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