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

Percentage Accurate: 43.7% → 86.6%
Time: 39.6s
Alternatives: 10
Speedup: 5.1×

Specification

?
\[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (*
 (/ 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)))));
}
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]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	((1) / (2)) * (((abs(p)) + (abs(r))) + (sqrt((((p - r) ^ (2)) + ((4) * (q ^ (2)))))))
END code
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)

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 10 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: 43.7% accurate, 1.0× speedup?

\[\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right) \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (*
 (/ 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)))));
}
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]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	((1) / (2)) * (((abs(p)) + (abs(r))) + (sqrt((((p - r) ^ (2)) + ((4) * (q ^ (2)))))))
END code
\frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \sqrt{{\left(p - r\right)}^{2} + 4 \cdot {q}^{2}}\right)

Alternative 1: 86.6% accurate, 1.1× speedup?

\[\begin{array}{l} t_0 := \left|\mathsf{min}\left(p, r\right)\right|\\ t_1 := \left|\mathsf{max}\left(p, r\right)\right|\\ t_2 := -1 \cdot \mathsf{max}\left(p, r\right)\\ \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -58411768126780620:\\ \;\;\;\;\mathsf{fma}\left(-0.5, t\_2, 0.5 \cdot \left(t\_0 + -1 \cdot \left(\mathsf{min}\left(p, r\right) \cdot \left(1 + -1 \cdot \frac{t\_1}{\mathsf{min}\left(p, r\right)}\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-0.5, t\_2, 0.5 \cdot \left(t\_0 + \left(t\_1 + 2 \cdot \left|q\right|\right)\right)\right)\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (fabs (fmin p r)))
       (t_1 (fabs (fmax p r)))
       (t_2 (* -1.0 (fmax p r))))
  (if (<= (fmin p r) -58411768126780620.0)
    (fma
     -0.5
     t_2
     (*
      0.5
      (+
       t_0
       (* -1.0 (* (fmin p r) (+ 1.0 (* -1.0 (/ t_1 (fmin p r)))))))))
    (fma -0.5 t_2 (* 0.5 (+ t_0 (+ t_1 (* 2.0 (fabs q)))))))))
double code(double p, double r, double q) {
	double t_0 = fabs(fmin(p, r));
	double t_1 = fabs(fmax(p, r));
	double t_2 = -1.0 * fmax(p, r);
	double tmp;
	if (fmin(p, r) <= -58411768126780620.0) {
		tmp = fma(-0.5, t_2, (0.5 * (t_0 + (-1.0 * (fmin(p, r) * (1.0 + (-1.0 * (t_1 / fmin(p, r)))))))));
	} else {
		tmp = fma(-0.5, t_2, (0.5 * (t_0 + (t_1 + (2.0 * fabs(q))))));
	}
	return tmp;
}
function code(p, r, q)
	t_0 = abs(fmin(p, r))
	t_1 = abs(fmax(p, r))
	t_2 = Float64(-1.0 * fmax(p, r))
	tmp = 0.0
	if (fmin(p, r) <= -58411768126780620.0)
		tmp = fma(-0.5, t_2, Float64(0.5 * Float64(t_0 + Float64(-1.0 * Float64(fmin(p, r) * Float64(1.0 + Float64(-1.0 * Float64(t_1 / fmin(p, r)))))))));
	else
		tmp = fma(-0.5, t_2, Float64(0.5 * Float64(t_0 + Float64(t_1 + Float64(2.0 * abs(q))))));
	end
	return tmp
end
code[p_, r_, q_] := Block[{t$95$0 = N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(-1.0 * N[Max[p, r], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Min[p, r], $MachinePrecision], -58411768126780620.0], N[(-0.5 * t$95$2 + N[(0.5 * N[(t$95$0 + N[(-1.0 * N[(N[Min[p, r], $MachinePrecision] * N[(1.0 + N[(-1.0 * N[(t$95$1 / N[Min[p, r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-0.5 * t$95$2 + N[(0.5 * N[(t$95$0 + N[(t$95$1 + N[(2.0 * N[Abs[q], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (p < r) THEN p ELSE r ENDIF IN
	LET t_0 = (abs(tmp)) IN
		LET tmp_1 = IF (p > r) THEN p ELSE r ENDIF IN
		LET t_1 = (abs(tmp_1)) IN
			LET tmp_2 = IF (p > r) THEN p ELSE r ENDIF IN
			LET t_2 = ((-1) * tmp_2) IN
				LET tmp_6 = IF (p < r) THEN p ELSE r ENDIF IN
				LET tmp_7 = IF (p < r) THEN p ELSE r ENDIF IN
				LET tmp_8 = IF (p < r) THEN p ELSE r ENDIF IN
				LET tmp_5 = IF (tmp_6 <= (-58411768126780624)) THEN (((-5e-1) * t_2) + ((5e-1) * (t_0 + ((-1) * (tmp_7 * ((1) + ((-1) * (t_1 / tmp_8)))))))) ELSE (((-5e-1) * t_2) + ((5e-1) * (t_0 + (t_1 + ((2) * (abs(q))))))) ENDIF IN
	tmp_5
END code
\begin{array}{l}
t_0 := \left|\mathsf{min}\left(p, r\right)\right|\\
t_1 := \left|\mathsf{max}\left(p, r\right)\right|\\
t_2 := -1 \cdot \mathsf{max}\left(p, r\right)\\
\mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -58411768126780620:\\
\;\;\;\;\mathsf{fma}\left(-0.5, t\_2, 0.5 \cdot \left(t\_0 + -1 \cdot \left(\mathsf{min}\left(p, r\right) \cdot \left(1 + -1 \cdot \frac{t\_1}{\mathsf{min}\left(p, r\right)}\right)\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-0.5, t\_2, 0.5 \cdot \left(t\_0 + \left(t\_1 + 2 \cdot \left|q\right|\right)\right)\right)\\


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

    1. Initial program 43.7%

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

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

      \[\leadsto \mathsf{fma}\left(-0.5, \frac{p \cdot r}{\sqrt{\mathsf{fma}\left(4, {q}^{2}, {p}^{2}\right)}}, 0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + \sqrt{\mathsf{fma}\left(4, {q}^{2}, {p}^{2}\right)}\right)\right)\right) \]
    4. Applied rewrites36.0%

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

      \[\leadsto \mathsf{fma}\left(-0.5, -1 \cdot r, 0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + \sqrt{\mathsf{fma}\left(q, q \cdot 4, p \cdot p\right)}\right)\right)\right) \]
    6. Applied rewrites43.8%

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

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

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

    if -58411768126780624 < p

    1. Initial program 43.7%

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

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

      \[\leadsto \mathsf{fma}\left(-0.5, \frac{p \cdot r}{\sqrt{\mathsf{fma}\left(4, {q}^{2}, {p}^{2}\right)}}, 0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + \sqrt{\mathsf{fma}\left(4, {q}^{2}, {p}^{2}\right)}\right)\right)\right) \]
    4. Applied rewrites36.0%

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

      \[\leadsto \mathsf{fma}\left(-0.5, -1 \cdot r, 0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + \sqrt{\mathsf{fma}\left(q, q \cdot 4, p \cdot p\right)}\right)\right)\right) \]
    6. Applied rewrites43.8%

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

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

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

Alternative 2: 86.6% accurate, 1.5× speedup?

\[\begin{array}{l} t_0 := \left|\mathsf{min}\left(p, r\right)\right|\\ t_1 := \left|\mathsf{max}\left(p, r\right)\right|\\ \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -58411768126780620:\\ \;\;\;\;0.5 \cdot \left(\left(t\_0 + t\_1\right) + \left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-0.5, -1 \cdot \mathsf{max}\left(p, r\right), 0.5 \cdot \left(t\_0 + \left(t\_1 + 2 \cdot \left|q\right|\right)\right)\right)\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (fabs (fmin p r))) (t_1 (fabs (fmax p r))))
  (if (<= (fmin p r) -58411768126780620.0)
    (* 0.5 (+ (+ t_0 t_1) (- (fmax p r) (fmin p r))))
    (fma
     -0.5
     (* -1.0 (fmax p r))
     (* 0.5 (+ t_0 (+ t_1 (* 2.0 (fabs q)))))))))
double code(double p, double r, double q) {
	double t_0 = fabs(fmin(p, r));
	double t_1 = fabs(fmax(p, r));
	double tmp;
	if (fmin(p, r) <= -58411768126780620.0) {
		tmp = 0.5 * ((t_0 + t_1) + (fmax(p, r) - fmin(p, r)));
	} else {
		tmp = fma(-0.5, (-1.0 * fmax(p, r)), (0.5 * (t_0 + (t_1 + (2.0 * fabs(q))))));
	}
	return tmp;
}
function code(p, r, q)
	t_0 = abs(fmin(p, r))
	t_1 = abs(fmax(p, r))
	tmp = 0.0
	if (fmin(p, r) <= -58411768126780620.0)
		tmp = Float64(0.5 * Float64(Float64(t_0 + t_1) + Float64(fmax(p, r) - fmin(p, r))));
	else
		tmp = fma(-0.5, Float64(-1.0 * fmax(p, r)), Float64(0.5 * Float64(t_0 + Float64(t_1 + Float64(2.0 * abs(q))))));
	end
	return tmp
end
code[p_, r_, q_] := Block[{t$95$0 = N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[Min[p, r], $MachinePrecision], -58411768126780620.0], N[(0.5 * N[(N[(t$95$0 + t$95$1), $MachinePrecision] + N[(N[Max[p, r], $MachinePrecision] - N[Min[p, r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-0.5 * N[(-1.0 * N[Max[p, r], $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[(t$95$0 + N[(t$95$1 + N[(2.0 * N[Abs[q], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (p < r) THEN p ELSE r ENDIF IN
	LET t_0 = (abs(tmp)) IN
		LET tmp_1 = IF (p > r) THEN p ELSE r ENDIF IN
		LET t_1 = (abs(tmp_1)) IN
			LET tmp_5 = IF (p < r) THEN p ELSE r ENDIF IN
			LET tmp_6 = IF (p > r) THEN p ELSE r ENDIF IN
			LET tmp_7 = IF (p < r) THEN p ELSE r ENDIF IN
			LET tmp_8 = IF (p > r) THEN p ELSE r ENDIF IN
			LET tmp_4 = IF (tmp_5 <= (-58411768126780624)) THEN ((5e-1) * ((t_0 + t_1) + (tmp_6 - tmp_7))) ELSE (((-5e-1) * ((-1) * tmp_8)) + ((5e-1) * (t_0 + (t_1 + ((2) * (abs(q))))))) ENDIF IN
	tmp_4
END code
\begin{array}{l}
t_0 := \left|\mathsf{min}\left(p, r\right)\right|\\
t_1 := \left|\mathsf{max}\left(p, r\right)\right|\\
\mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -58411768126780620:\\
\;\;\;\;0.5 \cdot \left(\left(t\_0 + t\_1\right) + \left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-0.5, -1 \cdot \mathsf{max}\left(p, r\right), 0.5 \cdot \left(t\_0 + \left(t\_1 + 2 \cdot \left|q\right|\right)\right)\right)\\


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

    1. Initial program 43.7%

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

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

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

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

    if -58411768126780624 < p

    1. Initial program 43.7%

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

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

      \[\leadsto \mathsf{fma}\left(-0.5, \frac{p \cdot r}{\sqrt{\mathsf{fma}\left(4, {q}^{2}, {p}^{2}\right)}}, 0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + \sqrt{\mathsf{fma}\left(4, {q}^{2}, {p}^{2}\right)}\right)\right)\right) \]
    4. Applied rewrites36.0%

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

      \[\leadsto \mathsf{fma}\left(-0.5, -1 \cdot r, 0.5 \cdot \left(\left|p\right| + \left(\left|r\right| + \sqrt{\mathsf{fma}\left(q, q \cdot 4, p \cdot p\right)}\right)\right)\right) \]
    6. Applied rewrites43.8%

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

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

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

Alternative 3: 82.4% accurate, 1.2× speedup?

\[\begin{array}{l} t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\ \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 10^{+263}:\\ \;\;\;\;0.5 \cdot \left(t\_0 + \left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t\_0, 0.5, \left|q\right|\right)\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (+ (fabs (fmin p r)) (fabs (fmax p r)))))
  (if (<= (pow (fabs q) 2.0) 1e+263)
    (* 0.5 (+ t_0 (- (fmax p r) (fmin p r))))
    (fma t_0 0.5 (fabs q)))))
double code(double p, double r, double q) {
	double t_0 = fabs(fmin(p, r)) + fabs(fmax(p, r));
	double tmp;
	if (pow(fabs(q), 2.0) <= 1e+263) {
		tmp = 0.5 * (t_0 + (fmax(p, r) - fmin(p, r)));
	} else {
		tmp = fma(t_0, 0.5, fabs(q));
	}
	return tmp;
}
function code(p, r, q)
	t_0 = Float64(abs(fmin(p, r)) + abs(fmax(p, r)))
	tmp = 0.0
	if ((abs(q) ^ 2.0) <= 1e+263)
		tmp = Float64(0.5 * Float64(t_0 + Float64(fmax(p, r) - fmin(p, r))));
	else
		tmp = fma(t_0, 0.5, abs(q));
	end
	return tmp
end
code[p_, r_, q_] := Block[{t$95$0 = N[(N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Power[N[Abs[q], $MachinePrecision], 2.0], $MachinePrecision], 1e+263], N[(0.5 * N[(t$95$0 + N[(N[Max[p, r], $MachinePrecision] - N[Min[p, r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * 0.5 + N[Abs[q], $MachinePrecision]), $MachinePrecision]]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (p < r) THEN p ELSE r ENDIF IN
	LET tmp_1 = IF (p > r) THEN p ELSE r ENDIF IN
	LET t_0 = ((abs(tmp)) + (abs(tmp_1))) IN
		LET tmp_5 = IF (p > r) THEN p ELSE r ENDIF IN
		LET tmp_6 = IF (p < r) THEN p ELSE r ENDIF IN
		LET tmp_4 = IF (((abs(q)) ^ (2)) <= (100000000000000001617283929500958347809617271215324681096755776296054153530035788436133522496440536428819053303318396315116321724674929173953241540025456475844343490985646025955809392324929988807089135627070664687603614947110183136436054375358690154446666302750720)) THEN ((5e-1) * (t_0 + (tmp_5 - tmp_6))) ELSE ((t_0 * (5e-1)) + (abs(q))) ENDIF IN
	tmp_4
END code
\begin{array}{l}
t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\
\mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 10^{+263}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right)\right)\\

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


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 q #s(literal 2 binary64)) < 1e263

    1. Initial program 43.7%

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

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

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

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

    if 1e263 < (pow.f64 q #s(literal 2 binary64))

    1. Initial program 43.7%

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

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

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

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

      \[\leadsto q + 0.5 \cdot \left(\left|p\right| + \left|r\right|\right) \]
    6. Applied rewrites29.2%

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

Alternative 4: 82.4% accurate, 1.2× speedup?

\[\begin{array}{l} t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\ \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 10^{+263}:\\ \;\;\;\;0.5 \cdot \left(t\_0 + \left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left|q\right| \cdot \left(1 + 0.5 \cdot \frac{t\_0}{\left|q\right|}\right)\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (+ (fabs (fmin p r)) (fabs (fmax p r)))))
  (if (<= (pow (fabs q) 2.0) 1e+263)
    (* 0.5 (+ t_0 (- (fmax p r) (fmin p r))))
    (* (fabs q) (+ 1.0 (* 0.5 (/ t_0 (fabs q))))))))
double code(double p, double r, double q) {
	double t_0 = fabs(fmin(p, r)) + fabs(fmax(p, r));
	double tmp;
	if (pow(fabs(q), 2.0) <= 1e+263) {
		tmp = 0.5 * (t_0 + (fmax(p, r) - fmin(p, r)));
	} else {
		tmp = fabs(q) * (1.0 + (0.5 * (t_0 / fabs(q))));
	}
	return tmp;
}
real(8) function code(p, r, q)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs(fmin(p, r)) + abs(fmax(p, r))
    if ((abs(q) ** 2.0d0) <= 1d+263) then
        tmp = 0.5d0 * (t_0 + (fmax(p, r) - fmin(p, r)))
    else
        tmp = abs(q) * (1.0d0 + (0.5d0 * (t_0 / abs(q))))
    end if
    code = tmp
end function
public static double code(double p, double r, double q) {
	double t_0 = Math.abs(fmin(p, r)) + Math.abs(fmax(p, r));
	double tmp;
	if (Math.pow(Math.abs(q), 2.0) <= 1e+263) {
		tmp = 0.5 * (t_0 + (fmax(p, r) - fmin(p, r)));
	} else {
		tmp = Math.abs(q) * (1.0 + (0.5 * (t_0 / Math.abs(q))));
	}
	return tmp;
}
def code(p, r, q):
	t_0 = math.fabs(fmin(p, r)) + math.fabs(fmax(p, r))
	tmp = 0
	if math.pow(math.fabs(q), 2.0) <= 1e+263:
		tmp = 0.5 * (t_0 + (fmax(p, r) - fmin(p, r)))
	else:
		tmp = math.fabs(q) * (1.0 + (0.5 * (t_0 / math.fabs(q))))
	return tmp
function code(p, r, q)
	t_0 = Float64(abs(fmin(p, r)) + abs(fmax(p, r)))
	tmp = 0.0
	if ((abs(q) ^ 2.0) <= 1e+263)
		tmp = Float64(0.5 * Float64(t_0 + Float64(fmax(p, r) - fmin(p, r))));
	else
		tmp = Float64(abs(q) * Float64(1.0 + Float64(0.5 * Float64(t_0 / abs(q)))));
	end
	return tmp
end
function tmp_2 = code(p, r, q)
	t_0 = abs(min(p, r)) + abs(max(p, r));
	tmp = 0.0;
	if ((abs(q) ^ 2.0) <= 1e+263)
		tmp = 0.5 * (t_0 + (max(p, r) - min(p, r)));
	else
		tmp = abs(q) * (1.0 + (0.5 * (t_0 / abs(q))));
	end
	tmp_2 = tmp;
end
code[p_, r_, q_] := Block[{t$95$0 = N[(N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Power[N[Abs[q], $MachinePrecision], 2.0], $MachinePrecision], 1e+263], N[(0.5 * N[(t$95$0 + N[(N[Max[p, r], $MachinePrecision] - N[Min[p, r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Abs[q], $MachinePrecision] * N[(1.0 + N[(0.5 * N[(t$95$0 / N[Abs[q], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (p < r) THEN p ELSE r ENDIF IN
	LET tmp_1 = IF (p > r) THEN p ELSE r ENDIF IN
	LET t_0 = ((abs(tmp)) + (abs(tmp_1))) IN
		LET tmp_5 = IF (p > r) THEN p ELSE r ENDIF IN
		LET tmp_6 = IF (p < r) THEN p ELSE r ENDIF IN
		LET tmp_4 = IF (((abs(q)) ^ (2)) <= (100000000000000001617283929500958347809617271215324681096755776296054153530035788436133522496440536428819053303318396315116321724674929173953241540025456475844343490985646025955809392324929988807089135627070664687603614947110183136436054375358690154446666302750720)) THEN ((5e-1) * (t_0 + (tmp_5 - tmp_6))) ELSE ((abs(q)) * ((1) + ((5e-1) * (t_0 / (abs(q)))))) ENDIF IN
	tmp_4
END code
\begin{array}{l}
t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\
\mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 10^{+263}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right)\right)\\

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


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 q #s(literal 2 binary64)) < 1e263

    1. Initial program 43.7%

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

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

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

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

    if 1e263 < (pow.f64 q #s(literal 2 binary64))

    1. Initial program 43.7%

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

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

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

Alternative 5: 59.5% accurate, 2.1× speedup?

\[\begin{array}{l} t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\ \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -58411768126780620:\\ \;\;\;\;0.5 \cdot \left(t\_0 + \left(-\mathsf{min}\left(p, r\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t\_0, 0.5, \left|q\right|\right)\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (+ (fabs (fmin p r)) (fabs (fmax p r)))))
  (if (<= (fmin p r) -58411768126780620.0)
    (* 0.5 (+ t_0 (- (fmin p r))))
    (fma t_0 0.5 (fabs q)))))
double code(double p, double r, double q) {
	double t_0 = fabs(fmin(p, r)) + fabs(fmax(p, r));
	double tmp;
	if (fmin(p, r) <= -58411768126780620.0) {
		tmp = 0.5 * (t_0 + -fmin(p, r));
	} else {
		tmp = fma(t_0, 0.5, fabs(q));
	}
	return tmp;
}
function code(p, r, q)
	t_0 = Float64(abs(fmin(p, r)) + abs(fmax(p, r)))
	tmp = 0.0
	if (fmin(p, r) <= -58411768126780620.0)
		tmp = Float64(0.5 * Float64(t_0 + Float64(-fmin(p, r))));
	else
		tmp = fma(t_0, 0.5, abs(q));
	end
	return tmp
end
code[p_, r_, q_] := Block[{t$95$0 = N[(N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Min[p, r], $MachinePrecision], -58411768126780620.0], N[(0.5 * N[(t$95$0 + (-N[Min[p, r], $MachinePrecision])), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * 0.5 + N[Abs[q], $MachinePrecision]), $MachinePrecision]]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (p < r) THEN p ELSE r ENDIF IN
	LET tmp_1 = IF (p > r) THEN p ELSE r ENDIF IN
	LET t_0 = ((abs(tmp)) + (abs(tmp_1))) IN
		LET tmp_4 = IF (p < r) THEN p ELSE r ENDIF IN
		LET tmp_5 = IF (p < r) THEN p ELSE r ENDIF IN
		LET tmp_3 = IF (tmp_4 <= (-58411768126780624)) THEN ((5e-1) * (t_0 + (- tmp_5))) ELSE ((t_0 * (5e-1)) + (abs(q))) ENDIF IN
	tmp_3
END code
\begin{array}{l}
t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\
\mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -58411768126780620:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \left(-\mathsf{min}\left(p, r\right)\right)\right)\\

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


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

    1. Initial program 43.7%

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

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

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

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

    if -58411768126780624 < p

    1. Initial program 43.7%

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

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

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

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

      \[\leadsto q + 0.5 \cdot \left(\left|p\right| + \left|r\right|\right) \]
    6. Applied rewrites29.2%

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

Alternative 6: 57.4% accurate, 1.4× speedup?

\[\begin{array}{l} t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\ \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{-170}:\\ \;\;\;\;0.5 \cdot \left(t\_0 + \mathsf{max}\left(p, r\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t\_0, 0.5, \left|q\right|\right)\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (let* ((t_0 (+ (fabs (fmin p r)) (fabs (fmax p r)))))
  (if (<= (pow (fabs q) 2.0) 5e-170)
    (* 0.5 (+ t_0 (fmax p r)))
    (fma t_0 0.5 (fabs q)))))
double code(double p, double r, double q) {
	double t_0 = fabs(fmin(p, r)) + fabs(fmax(p, r));
	double tmp;
	if (pow(fabs(q), 2.0) <= 5e-170) {
		tmp = 0.5 * (t_0 + fmax(p, r));
	} else {
		tmp = fma(t_0, 0.5, fabs(q));
	}
	return tmp;
}
function code(p, r, q)
	t_0 = Float64(abs(fmin(p, r)) + abs(fmax(p, r)))
	tmp = 0.0
	if ((abs(q) ^ 2.0) <= 5e-170)
		tmp = Float64(0.5 * Float64(t_0 + fmax(p, r)));
	else
		tmp = fma(t_0, 0.5, abs(q));
	end
	return tmp
end
code[p_, r_, q_] := Block[{t$95$0 = N[(N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Power[N[Abs[q], $MachinePrecision], 2.0], $MachinePrecision], 5e-170], N[(0.5 * N[(t$95$0 + N[Max[p, r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * 0.5 + N[Abs[q], $MachinePrecision]), $MachinePrecision]]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (p < r) THEN p ELSE r ENDIF IN
	LET tmp_1 = IF (p > r) THEN p ELSE r ENDIF IN
	LET t_0 = ((abs(tmp)) + (abs(tmp_1))) IN
		LET tmp_4 = IF (p > r) THEN p ELSE r ENDIF IN
		LET tmp_3 = IF (((abs(q)) ^ (2)) <= (50000000000000001005897896399759447471663533251681488230631673082178993512233877041951504141337718672782395574383719360739346127312822404793014987451483155735953233572139998721523190708243228387841467383029606869434144008536067884865524710326518014000764983693195045464120416181063784414611472713361231600365858598841095313775215944350820782849125325027755638627155853348802458171985828261675066213273201619349261903835213161073625087738037109375e-615)) THEN ((5e-1) * (t_0 + tmp_4)) ELSE ((t_0 * (5e-1)) + (abs(q))) ENDIF IN
	tmp_3
END code
\begin{array}{l}
t_0 := \left|\mathsf{min}\left(p, r\right)\right| + \left|\mathsf{max}\left(p, r\right)\right|\\
\mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{-170}:\\
\;\;\;\;0.5 \cdot \left(t\_0 + \mathsf{max}\left(p, r\right)\right)\\

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


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 q #s(literal 2 binary64)) < 5.0000000000000001e-170

    1. Initial program 43.7%

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

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

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

      \[\leadsto 0.5 \cdot \left(\left(\left|p\right| + \left|r\right|\right) + \left(r - p\right)\right) \]
    5. Taylor expanded in p around 0

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

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

    if 5.0000000000000001e-170 < (pow.f64 q #s(literal 2 binary64))

    1. Initial program 43.7%

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

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

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

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

      \[\leadsto q + 0.5 \cdot \left(\left|p\right| + \left|r\right|\right) \]
    6. Applied rewrites29.2%

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

Alternative 7: 44.7% accurate, 5.1× speedup?

\[\mathsf{fma}\left(\left|p\right| + \left|r\right|, 0.5, \left|q\right|\right) \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (fma (+ (fabs p) (fabs r)) 0.5 (fabs q)))
double code(double p, double r, double q) {
	return fma((fabs(p) + fabs(r)), 0.5, fabs(q));
}
function code(p, r, q)
	return fma(Float64(abs(p) + abs(r)), 0.5, abs(q))
end
code[p_, r_, q_] := N[(N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision] * 0.5 + N[Abs[q], $MachinePrecision]), $MachinePrecision]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	(((abs(p)) + (abs(r))) * (5e-1)) + (abs(q))
END code
\mathsf{fma}\left(\left|p\right| + \left|r\right|, 0.5, \left|q\right|\right)
Derivation
  1. Initial program 43.7%

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

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

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

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

    \[\leadsto q + 0.5 \cdot \left(\left|p\right| + \left|r\right|\right) \]
  6. Applied rewrites29.2%

    \[\leadsto \mathsf{fma}\left(\left|p\right| + \left|r\right|, 0.5, q\right) \]
  7. Add Preprocessing

Alternative 8: 38.6% accurate, 1.9× speedup?

\[\begin{array}{l} \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{-191}:\\ \;\;\;\;0.5 \cdot \left(\left|p\right| + \left|r\right|\right)\\ \mathbf{else}:\\ \;\;\;\;\left|q\right| \cdot 1\\ \end{array} \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (if (<= (pow (fabs q) 2.0) 5e-191)
  (* 0.5 (+ (fabs p) (fabs r)))
  (* (fabs q) 1.0)))
double code(double p, double r, double q) {
	double tmp;
	if (pow(fabs(q), 2.0) <= 5e-191) {
		tmp = 0.5 * (fabs(p) + fabs(r));
	} else {
		tmp = fabs(q) * 1.0;
	}
	return tmp;
}
real(8) function code(p, r, q)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q
    real(8) :: tmp
    if ((abs(q) ** 2.0d0) <= 5d-191) then
        tmp = 0.5d0 * (abs(p) + abs(r))
    else
        tmp = abs(q) * 1.0d0
    end if
    code = tmp
end function
public static double code(double p, double r, double q) {
	double tmp;
	if (Math.pow(Math.abs(q), 2.0) <= 5e-191) {
		tmp = 0.5 * (Math.abs(p) + Math.abs(r));
	} else {
		tmp = Math.abs(q) * 1.0;
	}
	return tmp;
}
def code(p, r, q):
	tmp = 0
	if math.pow(math.fabs(q), 2.0) <= 5e-191:
		tmp = 0.5 * (math.fabs(p) + math.fabs(r))
	else:
		tmp = math.fabs(q) * 1.0
	return tmp
function code(p, r, q)
	tmp = 0.0
	if ((abs(q) ^ 2.0) <= 5e-191)
		tmp = Float64(0.5 * Float64(abs(p) + abs(r)));
	else
		tmp = Float64(abs(q) * 1.0);
	end
	return tmp
end
function tmp_2 = code(p, r, q)
	tmp = 0.0;
	if ((abs(q) ^ 2.0) <= 5e-191)
		tmp = 0.5 * (abs(p) + abs(r));
	else
		tmp = abs(q) * 1.0;
	end
	tmp_2 = tmp;
end
code[p_, r_, q_] := If[LessEqual[N[Power[N[Abs[q], $MachinePrecision], 2.0], $MachinePrecision], 5e-191], N[(0.5 * N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Abs[q], $MachinePrecision] * 1.0), $MachinePrecision]]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	LET tmp = IF (((abs(q)) ^ (2)) <= (50000000000000000942551789279165077655128973232649790772620887129032407459291021743169337641389297212198941154372058224760853099565741627214967626395102181370270746629944267464794990958050845661040964938525483075519980913353731727320837054697313531092421556456303078458350442613230095608530729597341794983149860033481636690818522354026582444713528564815853745850819418461955121683473357921370785834258646975253512312133358029888087576404083053470501162539318951161249060533009469509124755859375e-684)) THEN ((5e-1) * ((abs(p)) + (abs(r)))) ELSE ((abs(q)) * (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{-191}:\\
\;\;\;\;0.5 \cdot \left(\left|p\right| + \left|r\right|\right)\\

\mathbf{else}:\\
\;\;\;\;\left|q\right| \cdot 1\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 q #s(literal 2 binary64)) < 5.0000000000000001e-191

    1. Initial program 43.7%

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

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

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

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

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

    if 5.0000000000000001e-191 < (pow.f64 q #s(literal 2 binary64))

    1. Initial program 43.7%

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

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

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

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

      \[\leadsto q \cdot \left({p}^{2} \cdot \mathsf{fma}\left(0.5, \frac{\left|r\right|}{{p}^{2} \cdot q}, \mathsf{fma}\left(0.5, \frac{\left|\frac{1}{p}\right|}{q}, \frac{1}{{p}^{2}}\right)\right)\right) \]
    6. Taylor expanded in q around inf

      \[\leadsto q \cdot 1 \]
    7. Applied rewrites18.3%

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

Alternative 9: 34.6% accurate, 11.8× speedup?

\[\left|q\right| \cdot 1 \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (* (fabs q) 1.0))
double code(double p, double r, double q) {
	return fabs(q) * 1.0;
}
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 = abs(q) * 1.0d0
end function
public static double code(double p, double r, double q) {
	return Math.abs(q) * 1.0;
}
def code(p, r, q):
	return math.fabs(q) * 1.0
function code(p, r, q)
	return Float64(abs(q) * 1.0)
end
function tmp = code(p, r, q)
	tmp = abs(q) * 1.0;
end
code[p_, r_, q_] := N[(N[Abs[q], $MachinePrecision] * 1.0), $MachinePrecision]
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	(abs(q)) * (1)
END code
\left|q\right| \cdot 1
Derivation
  1. Initial program 43.7%

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

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

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

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

    \[\leadsto q \cdot \left({p}^{2} \cdot \mathsf{fma}\left(0.5, \frac{\left|r\right|}{{p}^{2} \cdot q}, \mathsf{fma}\left(0.5, \frac{\left|\frac{1}{p}\right|}{q}, \frac{1}{{p}^{2}}\right)\right)\right) \]
  6. Taylor expanded in q around inf

    \[\leadsto q \cdot 1 \]
  7. Applied rewrites18.3%

    \[\leadsto q \cdot 1 \]
  8. Add Preprocessing

Alternative 10: 17.5% accurate, 29.6× speedup?

\[-q \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (- q))
double code(double p, double r, double q) {
	return -q;
}
real(8) function code(p, r, q)
use fmin_fmax_functions
    real(8), intent (in) :: p
    real(8), intent (in) :: r
    real(8), intent (in) :: q
    code = -q
end function
public static double code(double p, double r, double q) {
	return -q;
}
def code(p, r, q):
	return -q
function code(p, r, q)
	return Float64(-q)
end
function tmp = code(p, r, q)
	tmp = -q;
end
code[p_, r_, q_] := (-q)
f(p, r, q):
	p in [-inf, +inf],
	r in [-inf, +inf],
	q in [-inf, +inf]
code: THEORY
BEGIN
f(p, r, q: real): real =
	- q
END code
-q
Derivation
  1. Initial program 43.7%

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

    \[\leadsto -1 \cdot q \]
  3. Applied rewrites17.5%

    \[\leadsto -1 \cdot q \]
  4. Applied rewrites17.5%

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

Reproduce

?
herbie shell --seed 2026089 +o generate:egglog
(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)))))))