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

Percentage Accurate: 24.4% → 57.9%
Time: 4.6s
Alternatives: 3
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 3 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.4% 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.9% accurate, 2.3× 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 3.5 \cdot 10^{-12}:\\ \;\;\;\;\left(-p\right) \cdot \mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -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 3.5e-12)
   (* (- p) (fma (/ (+ (fabs p) (- (fabs r) r)) p) -0.5 -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 <= 3.5e-12) {
		tmp = -p * fma(((fabs(p) + (fabs(r) - r)) / p), -0.5, -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 <= 3.5e-12)
		tmp = Float64(Float64(-p) * fma(Float64(Float64(abs(p) + Float64(abs(r) - r)) / p), -0.5, -0.5));
	else
		tmp = Float64(-q_m);
	end
	return tmp
end
q_m = N[Abs[q], $MachinePrecision]
NOTE: p, r, and q_m should be sorted in increasing order before calling this function.
code[p_, r_, q$95$m_] := If[LessEqual[q$95$m, 3.5e-12], N[((-p) * N[(N[(N[(N[Abs[p], $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] - r), $MachinePrecision]), $MachinePrecision] / p), $MachinePrecision] * -0.5 + -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 3.5 \cdot 10^{-12}:\\
\;\;\;\;\left(-p\right) \cdot \mathsf{fma}\left(\frac{\left|p\right| + \left(\left|r\right| - r\right)}{p}, -0.5, -0.5\right)\\

\mathbf{else}:\\
\;\;\;\;-q\_m\\


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

    1. Initial program 24.4%

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

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

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

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

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

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

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

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

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

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

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

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

    if 3.5e-12 < q

    1. Initial program 24.4%

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

      \[\leadsto \color{blue}{-1 \cdot q} \]
    3. Step-by-step derivation
      1. mul-1-negN/A

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

        \[\leadsto -q \]
    4. Applied rewrites36.3%

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

Alternative 2: 48.8% accurate, 4.7× 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 4.5 \cdot 10^{-42}:\\ \;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{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 4.5e-42) (/ (* (- q_m) q_m) 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 <= 4.5e-42) {
		tmp = (-q_m * q_m) / 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 <= 4.5d-42) then
        tmp = (-q_m * q_m) / 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 <= 4.5e-42) {
		tmp = (-q_m * q_m) / 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 <= 4.5e-42:
		tmp = (-q_m * q_m) / 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 <= 4.5e-42)
		tmp = Float64(Float64(Float64(-q_m) * q_m) / 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 <= 4.5e-42)
		tmp = (-q_m * q_m) / 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, 4.5e-42], N[(N[((-q$95$m) * q$95$m), $MachinePrecision] / r), $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 4.5 \cdot 10^{-42}:\\
\;\;\;\;\frac{\left(-q\_m\right) \cdot q\_m}{r}\\

\mathbf{else}:\\
\;\;\;\;-q\_m\\


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

    1. Initial program 24.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\frac{1}{2}, \left(\left|p\right| + \left|r\right|\right) - r, {q}^{2} \cdot \left(\frac{{q}^{2}}{{r}^{3}} - \frac{1}{r}\right)\right) \]
      9. pow2N/A

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

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

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

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

      \[\leadsto \frac{-1 \cdot \left({q}^{2} \cdot {r}^{2}\right) + {q}^{4}}{{r}^{\color{blue}{3}}} \]
    9. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \frac{-1 \cdot \left({q}^{2} \cdot {r}^{2}\right) + {q}^{4}}{{r}^{3}} \]
      2. associate-*r*N/A

        \[\leadsto \frac{\left(-1 \cdot {q}^{2}\right) \cdot {r}^{2} + {q}^{4}}{{r}^{3}} \]
      3. mul-1-negN/A

        \[\leadsto \frac{\left(\mathsf{neg}\left({q}^{2}\right)\right) \cdot {r}^{2} + {q}^{4}}{{r}^{3}} \]
      4. lower-fma.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(\mathsf{neg}\left({q}^{2}\right), {r}^{2}, {q}^{4}\right)}{{r}^{3}} \]
      5. lower-neg.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(-{q}^{2}, {r}^{2}, {q}^{4}\right)}{{r}^{3}} \]
      6. pow2N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, {r}^{2}, {q}^{4}\right)}{{r}^{3}} \]
      7. lift-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, {r}^{2}, {q}^{4}\right)}{{r}^{3}} \]
      8. pow2N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{{r}^{3}} \]
      9. lift-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{{r}^{3}} \]
      10. lower-pow.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{{r}^{3}} \]
      11. pow3N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{\left(r \cdot r\right) \cdot r} \]
      12. lift-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{\left(r \cdot r\right) \cdot r} \]
      13. lift-*.f649.2

        \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{\left(r \cdot r\right) \cdot r} \]
    10. Applied rewrites9.2%

      \[\leadsto \frac{\mathsf{fma}\left(-q \cdot q, r \cdot r, {q}^{4}\right)}{\left(r \cdot r\right) \cdot \color{blue}{r}} \]
    11. Taylor expanded in r around inf

      \[\leadsto -1 \cdot \frac{{q}^{2}}{r} \]
    12. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \frac{-1 \cdot {q}^{2}}{r} \]
      2. mul-1-negN/A

        \[\leadsto \frac{\mathsf{neg}\left({q}^{2}\right)}{r} \]
      3. lower-/.f64N/A

        \[\leadsto \frac{\mathsf{neg}\left({q}^{2}\right)}{r} \]
      4. pow2N/A

        \[\leadsto \frac{\mathsf{neg}\left(q \cdot q\right)}{r} \]
      5. distribute-lft-neg-inN/A

        \[\leadsto \frac{\left(\mathsf{neg}\left(q\right)\right) \cdot q}{r} \]
      6. lower-*.f64N/A

        \[\leadsto \frac{\left(\mathsf{neg}\left(q\right)\right) \cdot q}{r} \]
      7. lower-neg.f6426.5

        \[\leadsto \frac{\left(-q\right) \cdot q}{r} \]
    13. Applied rewrites26.5%

      \[\leadsto \frac{\left(-q\right) \cdot q}{r} \]

    if 4.5e-42 < q

    1. Initial program 24.4%

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

      \[\leadsto \color{blue}{-1 \cdot q} \]
    3. Step-by-step derivation
      1. mul-1-negN/A

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

        \[\leadsto -q \]
    4. Applied rewrites36.3%

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

Alternative 3: 36.3% 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.4%

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

    \[\leadsto \color{blue}{-1 \cdot q} \]
  3. Step-by-step derivation
    1. mul-1-negN/A

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

      \[\leadsto -q \]
  4. Applied rewrites36.3%

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

Reproduce

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