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

Percentage Accurate: 24.6% → 57.5%
Time: 4.6s
Alternatives: 5
Speedup: 28.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 5 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: 24.6% 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: 57.5% accurate, 3.2× speedup?

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

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

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

\mathbf{else}:\\
\;\;\;\;q\_m \cdot \left(0.5 \cdot \frac{p}{q\_m} - 1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if q < 1.85e-65

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto r \cdot \left(\frac{1}{2} \cdot \frac{\left|r\right|}{r} - \frac{1}{2}\right) \]
      7. lower-fabs.f64N/A

        \[\leadsto r \cdot \left(\frac{1}{2} \cdot \frac{\left|r\right|}{r} - \frac{1}{2}\right) \]
      8. metadata-eval31.3

        \[\leadsto r \cdot \left(0.5 \cdot \frac{\left|r\right|}{r} - 0.5\right) \]
    9. Applied rewrites31.3%

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

    if 1.85e-65 < q

    1. Initial program 24.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. Step-by-step derivation
      1. lift--.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \color{blue}{\sqrt{p}}, \left|r\right| - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
      10. lower--.f645.4

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

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

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

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\color{blue}{\mathsf{fma}\left(4 \cdot q, q, {\left(p - r\right)}^{2}\right)}}\right) \]
      18. lower-*.f645.4

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{{\left(p - r\right)}^{2}}\right)}\right) \]
      20. unpow2N/A

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{\left(p - r\right)} \cdot \left(p - r\right)\right)}\right) \]
      22. sub-negate-revN/A

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(p - r\right)}\right)}\right) \]
      24. sub-negate-revN/A

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(r - p\right)\right)\right)}\right)}\right) \]
    3. Applied rewrites5.4%

      \[\leadsto \frac{1}{2} \cdot \color{blue}{\mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)} \]
    4. Applied rewrites20.4%

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

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

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

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

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

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

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

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

        \[\leadsto q \cdot \left(\frac{1}{2} \cdot \frac{p + \left|r\right|}{q} - 1\right) \]
      8. lower-fabs.f6434.5

        \[\leadsto q \cdot \left(0.5 \cdot \frac{p + \left|r\right|}{q} - 1\right) \]
    7. Applied rewrites34.5%

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

      \[\leadsto q \cdot \left(\frac{1}{2} \cdot \frac{p}{q} - 1\right) \]
    9. Step-by-step derivation
      1. lower-/.f6436.0

        \[\leadsto q \cdot \left(0.5 \cdot \frac{p}{q} - 1\right) \]
    10. Applied rewrites36.0%

      \[\leadsto q \cdot \left(0.5 \cdot \frac{p}{q} - 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 49.0% accurate, 3.4× speedup?

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

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

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

\mathbf{else}:\\
\;\;\;\;q\_m \cdot \left(0.5 \cdot \frac{p}{q\_m} - 1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if q < 7.19999999999999986e-87

    1. Initial program 24.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. Step-by-step derivation
      1. lift--.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \color{blue}{\sqrt{p}}, \left|r\right| - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
      10. lower--.f645.4

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

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

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

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\color{blue}{\mathsf{fma}\left(4 \cdot q, q, {\left(p - r\right)}^{2}\right)}}\right) \]
      18. lower-*.f645.4

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{{\left(p - r\right)}^{2}}\right)}\right) \]
      20. unpow2N/A

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{\left(p - r\right)} \cdot \left(p - r\right)\right)}\right) \]
      22. sub-negate-revN/A

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(p - r\right)}\right)}\right) \]
      24. sub-negate-revN/A

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(r - p\right)\right)\right)}\right)}\right) \]
    3. Applied rewrites5.4%

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

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

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

        \[\leadsto \frac{1}{2} \cdot \left(\sqrt{p} \cdot \color{blue}{\sqrt{p}} + \left(\left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)\right) \]
      4. rem-square-sqrtN/A

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

        \[\leadsto \frac{1}{2} \cdot \left(p + \color{blue}{\left(\left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)}\right) \]
      6. associate-+r-N/A

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\sqrt{r \cdot r}} + \left(p - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)\right) \]
      11. sqrt-prodN/A

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

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

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{r}, \color{blue}{\sqrt{r}}, p - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right) \]
      15. lower--.f6413.2

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

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

        \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{r}, \sqrt{r}, p - \sqrt{\color{blue}{\left(r - p\right) \cdot \left(r - p\right) + \left(4 \cdot q\right) \cdot q}}\right) \]
    5. Applied rewrites13.2%

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

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

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

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

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

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

        \[\leadsto \left(\sqrt{r} \cdot \color{blue}{\sqrt{r}} + \left(p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}\right)\right) \cdot \frac{1}{2} \]
      7. rem-square-sqrtN/A

        \[\leadsto \left(\color{blue}{r} + \left(p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}\right)\right) \cdot \frac{1}{2} \]
      8. sum-to-multN/A

        \[\leadsto \color{blue}{\left(\left(1 + \frac{p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}}{r}\right) \cdot r\right)} \cdot \frac{1}{2} \]
      9. associate-*l*N/A

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

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

        \[\leadsto \color{blue}{\left(1 + \frac{p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}}{r}\right) \cdot \left(\frac{1}{2} \cdot r\right)} \]
    7. Applied rewrites18.2%

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

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

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

      if 7.19999999999999986e-87 < q

      1. Initial program 24.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. Step-by-step derivation
        1. lift--.f64N/A

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

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

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

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

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \color{blue}{\sqrt{p}}, \left|r\right| - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
        10. lower--.f645.4

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

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

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

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\color{blue}{\mathsf{fma}\left(4 \cdot q, q, {\left(p - r\right)}^{2}\right)}}\right) \]
        18. lower-*.f645.4

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{{\left(p - r\right)}^{2}}\right)}\right) \]
        20. unpow2N/A

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{\left(p - r\right)} \cdot \left(p - r\right)\right)}\right) \]
        22. sub-negate-revN/A

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(p - r\right)}\right)}\right) \]
        24. sub-negate-revN/A

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(r - p\right)\right)\right)}\right)}\right) \]
      3. Applied rewrites5.4%

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)} \]
      4. Applied rewrites20.4%

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

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

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

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

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

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

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

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

          \[\leadsto q \cdot \left(\frac{1}{2} \cdot \frac{p + \left|r\right|}{q} - 1\right) \]
        8. lower-fabs.f6434.5

          \[\leadsto q \cdot \left(0.5 \cdot \frac{p + \left|r\right|}{q} - 1\right) \]
      7. Applied rewrites34.5%

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

        \[\leadsto q \cdot \left(\frac{1}{2} \cdot \frac{p}{q} - 1\right) \]
      9. Step-by-step derivation
        1. lower-/.f6436.0

          \[\leadsto q \cdot \left(0.5 \cdot \frac{p}{q} - 1\right) \]
      10. Applied rewrites36.0%

        \[\leadsto q \cdot \left(0.5 \cdot \frac{p}{q} - 1\right) \]
    10. Recombined 2 regimes into one program.
    11. Add Preprocessing

    Alternative 3: 48.9% accurate, 4.2× speedup?

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

      1. Initial program 24.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. Step-by-step derivation
        1. lift--.f64N/A

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

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

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

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

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \color{blue}{\sqrt{p}}, \left|r\right| - \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
        10. lower--.f645.4

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

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

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

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\color{blue}{\mathsf{fma}\left(4 \cdot q, q, {\left(p - r\right)}^{2}\right)}}\right) \]
        18. lower-*.f645.4

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{{\left(p - r\right)}^{2}}\right)}\right) \]
        20. unpow2N/A

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \color{blue}{\left(p - r\right)} \cdot \left(p - r\right)\right)}\right) \]
        22. sub-negate-revN/A

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(p - r\right)}\right)}\right) \]
        24. sub-negate-revN/A

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{p}, \sqrt{p}, \left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(\mathsf{neg}\left(\left(r - p\right)\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\left(r - p\right)\right)\right)}\right)}\right) \]
      3. Applied rewrites5.4%

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

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

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

          \[\leadsto \frac{1}{2} \cdot \left(\sqrt{p} \cdot \color{blue}{\sqrt{p}} + \left(\left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)\right) \]
        4. rem-square-sqrtN/A

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

          \[\leadsto \frac{1}{2} \cdot \left(p + \color{blue}{\left(\left|r\right| - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)}\right) \]
        6. associate-+r-N/A

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

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

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

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

          \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\sqrt{r \cdot r}} + \left(p - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right)\right) \]
        11. sqrt-prodN/A

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

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

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{r}, \color{blue}{\sqrt{r}}, p - \sqrt{\mathsf{fma}\left(4 \cdot q, q, \left(r - p\right) \cdot \left(r - p\right)\right)}\right) \]
        15. lower--.f6413.2

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

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

          \[\leadsto \frac{1}{2} \cdot \mathsf{fma}\left(\sqrt{r}, \sqrt{r}, p - \sqrt{\color{blue}{\left(r - p\right) \cdot \left(r - p\right) + \left(4 \cdot q\right) \cdot q}}\right) \]
      5. Applied rewrites13.2%

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

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

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

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

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

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

          \[\leadsto \left(\sqrt{r} \cdot \color{blue}{\sqrt{r}} + \left(p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}\right)\right) \cdot \frac{1}{2} \]
        7. rem-square-sqrtN/A

          \[\leadsto \left(\color{blue}{r} + \left(p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}\right)\right) \cdot \frac{1}{2} \]
        8. sum-to-multN/A

          \[\leadsto \color{blue}{\left(\left(1 + \frac{p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}}{r}\right) \cdot r\right)} \cdot \frac{1}{2} \]
        9. associate-*l*N/A

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

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

          \[\leadsto \color{blue}{\left(1 + \frac{p - \sqrt{\mathsf{fma}\left(p - r, p - r, \left(q \cdot 4\right) \cdot q\right)}}{r}\right) \cdot \left(\frac{1}{2} \cdot r\right)} \]
      7. Applied rewrites18.2%

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

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

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

        if 7.19999999999999986e-87 < q

        1. Initial program 24.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}{-1 \cdot q} \]
        3. Step-by-step derivation
          1. lower-*.f6435.8

            \[\leadsto -1 \cdot \color{blue}{q} \]
        4. Applied rewrites35.8%

          \[\leadsto \color{blue}{-1 \cdot q} \]
        5. Step-by-step derivation
          1. lift-*.f64N/A

            \[\leadsto -1 \cdot \color{blue}{q} \]
          2. mul-1-negN/A

            \[\leadsto \mathsf{neg}\left(q\right) \]
          3. lower-neg.f6435.8

            \[\leadsto -q \]
        6. Applied rewrites35.8%

          \[\leadsto -q \]
      10. Recombined 2 regimes into one program.
      11. Add Preprocessing

      Alternative 4: 41.5% accurate, 5.1× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto r \cdot \frac{p}{\color{blue}{r}} \]
        8. Step-by-step derivation
          1. lower-/.f6414.5

            \[\leadsto r \cdot \frac{p}{r} \]
        9. Applied rewrites14.5%

          \[\leadsto r \cdot \frac{p}{\color{blue}{r}} \]

        if 1.1000000000000001e-67 < q

        1. Initial program 24.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}{-1 \cdot q} \]
        3. Step-by-step derivation
          1. lower-*.f6435.8

            \[\leadsto -1 \cdot \color{blue}{q} \]
        4. Applied rewrites35.8%

          \[\leadsto \color{blue}{-1 \cdot q} \]
        5. Step-by-step derivation
          1. lift-*.f64N/A

            \[\leadsto -1 \cdot \color{blue}{q} \]
          2. mul-1-negN/A

            \[\leadsto \mathsf{neg}\left(q\right) \]
          3. lower-neg.f6435.8

            \[\leadsto -q \]
        6. Applied rewrites35.8%

          \[\leadsto -q \]
      3. Recombined 2 regimes into one program.
      4. Add Preprocessing

      Alternative 5: 35.8% accurate, 28.9× speedup?

      \[\begin{array}{l} q_m = \left|q\right| \\ [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\ \\ -q\_m \end{array} \]
      q_m = (fabs.f64 q)
      NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
      (FPCore (p r q_m) :precision binary64 (- q_m))
      q_m = fabs(q);
      assert(p < r && r < q_m);
      double code(double p, double r, double q_m) {
      	return -q_m;
      }
      
      q_m =     private
      NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      real(8) function code(p, r, q_m)
      use fmin_fmax_functions
          real(8), intent (in) :: p
          real(8), intent (in) :: r
          real(8), intent (in) :: q_m
          code = -q_m
      end function
      
      q_m = Math.abs(q);
      assert p < r && r < q_m;
      public static double code(double p, double r, double q_m) {
      	return -q_m;
      }
      
      q_m = math.fabs(q)
      [p, r, q_m] = sort([p, r, q_m])
      def code(p, r, q_m):
      	return -q_m
      
      q_m = abs(q)
      p, r, q_m = sort([p, r, q_m])
      function code(p, r, q_m)
      	return Float64(-q_m)
      end
      
      q_m = abs(q);
      p, r, q_m = num2cell(sort([p, r, q_m])){:}
      function tmp = code(p, r, q_m)
      	tmp = -q_m;
      end
      
      q_m = N[Abs[q], $MachinePrecision]
      NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
      code[p_, r_, q$95$m_] := (-q$95$m)
      
      \begin{array}{l}
      q_m = \left|q\right|
      \\
      [p, r, q_m] = \mathsf{sort}([p, r, q_m])\\
      \\
      -q\_m
      \end{array}
      
      Derivation
      1. Initial program 24.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}{-1 \cdot q} \]
      3. Step-by-step derivation
        1. lower-*.f6435.8

          \[\leadsto -1 \cdot \color{blue}{q} \]
      4. Applied rewrites35.8%

        \[\leadsto \color{blue}{-1 \cdot q} \]
      5. Step-by-step derivation
        1. lift-*.f64N/A

          \[\leadsto -1 \cdot \color{blue}{q} \]
        2. mul-1-negN/A

          \[\leadsto \mathsf{neg}\left(q\right) \]
        3. lower-neg.f6435.8

          \[\leadsto -q \]
      6. Applied rewrites35.8%

        \[\leadsto -q \]
      7. Add Preprocessing

      Reproduce

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