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

Percentage Accurate: 45.6% → 99.8%
Time: 3.5s
Alternatives: 7
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 7 alternatives:

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

Initial Program: 45.6% 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: 99.8% accurate, 1.9× speedup?

\[0.5 \cdot \left(\mathsf{hypot}\left(q + q, p - r\right) + \left(\left|r\right| + \left|p\right|\right)\right) \]
(FPCore (p r q)
  :precision binary64
  :pre TRUE
  (* 0.5 (+ (hypot (+ q q) (- p r)) (+ (fabs r) (fabs p)))))
double code(double p, double r, double q) {
	return 0.5 * (hypot((q + q), (p - r)) + (fabs(r) + fabs(p)));
}
public static double code(double p, double r, double q) {
	return 0.5 * (Math.hypot((q + q), (p - r)) + (Math.abs(r) + Math.abs(p)));
}
def code(p, r, q):
	return 0.5 * (math.hypot((q + q), (p - r)) + (math.fabs(r) + math.fabs(p)))
function code(p, r, q)
	return Float64(0.5 * Float64(hypot(Float64(q + q), Float64(p - r)) + Float64(abs(r) + abs(p))))
end
function tmp = code(p, r, q)
	tmp = 0.5 * (hypot((q + q), (p - r)) + (abs(r) + abs(p)));
end
code[p_, r_, q_] := N[(0.5 * N[(N[Sqrt[N[(q + q), $MachinePrecision] ^ 2 + N[(p - r), $MachinePrecision] ^ 2], $MachinePrecision] + N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $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 =
	(5e-1) * ((sqrt((((q + q) ^ (2)) + ((p - r) ^ (2))))) + ((abs(r)) + (abs(p))))
END code
0.5 \cdot \left(\mathsf{hypot}\left(q + q, p - r\right) + \left(\left|r\right| + \left|p\right|\right)\right)
Derivation
  1. Initial program 45.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. Applied rewrites45.6%

    \[\leadsto 0.5 \cdot \left(\sqrt{\mathsf{fma}\left(q \cdot q, 4, \left(p - r\right) \cdot \left(p - r\right)\right)} + \left(\left|r\right| + \left|p\right|\right)\right) \]
  3. Step-by-step derivation
    1. Applied rewrites99.8%

      \[\leadsto 0.5 \cdot \left(\mathsf{hypot}\left(q + q, p - r\right) + \left(\left|r\right| + \left|p\right|\right)\right) \]
    2. Add Preprocessing

    Alternative 2: 82.0% accurate, 1.2× speedup?

    \[\begin{array}{l} t_0 := \left|\mathsf{max}\left(p, r\right)\right| + \left|\mathsf{min}\left(p, r\right)\right|\\ \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{+283}:\\ \;\;\;\;0.5 \cdot \left(\left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right) + t\_0\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 (fmax p r)) (fabs (fmin p r)))))
      (if (<= (pow (fabs q) 2.0) 5e+283)
        (* 0.5 (+ (- (fmax p r) (fmin p r)) t_0))
        (fma t_0 0.5 (fabs q)))))
    double code(double p, double r, double q) {
    	double t_0 = fabs(fmax(p, r)) + fabs(fmin(p, r));
    	double tmp;
    	if (pow(fabs(q), 2.0) <= 5e+283) {
    		tmp = 0.5 * ((fmax(p, r) - fmin(p, r)) + t_0);
    	} else {
    		tmp = fma(t_0, 0.5, fabs(q));
    	}
    	return tmp;
    }
    
    function code(p, r, q)
    	t_0 = Float64(abs(fmax(p, r)) + abs(fmin(p, r)))
    	tmp = 0.0
    	if ((abs(q) ^ 2.0) <= 5e+283)
    		tmp = Float64(0.5 * Float64(Float64(fmax(p, r) - fmin(p, r)) + t_0));
    	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[Max[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Power[N[Abs[q], $MachinePrecision], 2.0], $MachinePrecision], 5e+283], N[(0.5 * N[(N[(N[Max[p, r], $MachinePrecision] - N[Min[p, r], $MachinePrecision]), $MachinePrecision] + t$95$0), $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)) <= (50000000000000003960719125422883827062840959584985546704194967116721787948758551386272267278602882264876081416647209031203419106557526049419390978660438178426771560410745940876447333535260291112887354734608898565252528592034690824272687386622186778733613155375371021108230826846322688)) THEN ((5e-1) * ((tmp_5 - tmp_6) + t_0)) ELSE ((t_0 * (5e-1)) + (abs(q))) ENDIF IN
    	tmp_4
    END code
    \begin{array}{l}
    t_0 := \left|\mathsf{max}\left(p, r\right)\right| + \left|\mathsf{min}\left(p, r\right)\right|\\
    \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{+283}:\\
    \;\;\;\;0.5 \cdot \left(\left(\mathsf{max}\left(p, r\right) - \mathsf{min}\left(p, r\right)\right) + t\_0\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.0000000000000004e283

      1. Initial program 45.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 \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. Step-by-step derivation
        1. Applied rewrites30.6%

          \[\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) \]
        2. Applied rewrites35.1%

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

        if 5.0000000000000004e283 < (pow.f64 q #s(literal 2 binary64))

        1. Initial program 45.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 q \cdot \left(1 + \frac{1}{2} \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
        3. Step-by-step derivation
          1. Applied rewrites26.8%

            \[\leadsto q \cdot \left(1 + 0.5 \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
          2. Step-by-step derivation
            1. Applied rewrites26.8%

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

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

          Alternative 3: 65.2% accurate, 1.8× speedup?

          \[\begin{array}{l} t_0 := \left|\mathsf{max}\left(p, r\right)\right| + \left|\mathsf{min}\left(p, r\right)\right|\\ \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -2.8276982787306963 \cdot 10^{+112}:\\ \;\;\;\;0.5 \cdot \left(\left(-\mathsf{min}\left(p, r\right)\right) + t\_0\right)\\ \mathbf{elif}\;\mathsf{min}\left(p, r\right) \leq 2.5006003538741998 \cdot 10^{-282}:\\ \;\;\;\;\mathsf{fma}\left(t\_0, 0.5, \left|q\right|\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\mathsf{max}\left(p, r\right) + t\_0\right)\\ \end{array} \]
          (FPCore (p r q)
            :precision binary64
            :pre TRUE
            (let* ((t_0 (+ (fabs (fmax p r)) (fabs (fmin p r)))))
            (if (<= (fmin p r) -2.8276982787306963e+112)
              (* 0.5 (+ (- (fmin p r)) t_0))
              (if (<= (fmin p r) 2.5006003538741998e-282)
                (fma t_0 0.5 (fabs q))
                (* 0.5 (+ (fmax p r) t_0))))))
          double code(double p, double r, double q) {
          	double t_0 = fabs(fmax(p, r)) + fabs(fmin(p, r));
          	double tmp;
          	if (fmin(p, r) <= -2.8276982787306963e+112) {
          		tmp = 0.5 * (-fmin(p, r) + t_0);
          	} else if (fmin(p, r) <= 2.5006003538741998e-282) {
          		tmp = fma(t_0, 0.5, fabs(q));
          	} else {
          		tmp = 0.5 * (fmax(p, r) + t_0);
          	}
          	return tmp;
          }
          
          function code(p, r, q)
          	t_0 = Float64(abs(fmax(p, r)) + abs(fmin(p, r)))
          	tmp = 0.0
          	if (fmin(p, r) <= -2.8276982787306963e+112)
          		tmp = Float64(0.5 * Float64(Float64(-fmin(p, r)) + t_0));
          	elseif (fmin(p, r) <= 2.5006003538741998e-282)
          		tmp = fma(t_0, 0.5, abs(q));
          	else
          		tmp = Float64(0.5 * Float64(fmax(p, r) + t_0));
          	end
          	return tmp
          end
          
          code[p_, r_, q_] := Block[{t$95$0 = N[(N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Min[p, r], $MachinePrecision], -2.8276982787306963e+112], N[(0.5 * N[((-N[Min[p, r], $MachinePrecision]) + t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Min[p, r], $MachinePrecision], 2.5006003538741998e-282], N[(t$95$0 * 0.5 + N[Abs[q], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Max[p, r], $MachinePrecision] + t$95$0), $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_7 = IF (p < r) THEN p ELSE r ENDIF IN
          		LET tmp_8 = IF (p > r) THEN p ELSE r ENDIF IN
          		LET tmp_6 = IF (tmp_7 <= (25006003538741997925184834042125713126024134723319139516339178110215015875748990859096657398310063804142853372647214064532583806618098531015864881474630384764087557398051208418086942526374381224503412131864295566923986478969012819231430475836401765747914203140271043532209315119866015266938872901547281132324505672345135617506323202214624047305665008730133649303453391502528929761367453982854101751092846423560422306075043556201568648588367202819692724235115754971581918812504973490679061529893797976304450107196089485614953817801159052671274212914344692988541350145927877292192782364534029865725434344032374444629454485689712922625642764798761010117921337118275667242561777214859830564819276332855224609375e-988)) THEN ((t_0 * (5e-1)) + (abs(q))) ELSE ((5e-1) * (tmp_8 + t_0)) ENDIF IN
          		LET tmp_3 = IF (tmp_4 <= (-28276982787306962957484987270071390985553079490738673020521826223577307217393897804264473207543409075810289582080)) THEN ((5e-1) * ((- tmp_5) + t_0)) ELSE tmp_6 ENDIF IN
          	tmp_3
          END code
          \begin{array}{l}
          t_0 := \left|\mathsf{max}\left(p, r\right)\right| + \left|\mathsf{min}\left(p, r\right)\right|\\
          \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -2.8276982787306963 \cdot 10^{+112}:\\
          \;\;\;\;0.5 \cdot \left(\left(-\mathsf{min}\left(p, r\right)\right) + t\_0\right)\\
          
          \mathbf{elif}\;\mathsf{min}\left(p, r\right) \leq 2.5006003538741998 \cdot 10^{-282}:\\
          \;\;\;\;\mathsf{fma}\left(t\_0, 0.5, \left|q\right|\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;0.5 \cdot \left(\mathsf{max}\left(p, r\right) + t\_0\right)\\
          
          
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if p < -2.8276982787306963e112

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

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

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

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

              if -2.8276982787306963e112 < p < 2.5006003538741998e-282

              1. Initial program 45.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 q \cdot \left(1 + \frac{1}{2} \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
              3. Step-by-step derivation
                1. Applied rewrites26.8%

                  \[\leadsto q \cdot \left(1 + 0.5 \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
                2. Step-by-step derivation
                  1. Applied rewrites26.8%

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

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

                  if 2.5006003538741998e-282 < p

                  1. Initial program 45.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 \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. Step-by-step derivation
                    1. Applied rewrites30.6%

                      \[\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) \]
                    2. Applied rewrites35.1%

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

                      \[\leadsto 0.5 \cdot \left(r + \left(\left|r\right| + \left|p\right|\right)\right) \]
                    4. Step-by-step derivation
                      1. Applied rewrites24.8%

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

                    Alternative 4: 60.4% accurate, 1.4× speedup?

                    \[\begin{array}{l} t_0 := \left|\mathsf{max}\left(p, r\right)\right| + \left|\mathsf{min}\left(p, r\right)\right|\\ \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{-50}:\\ \;\;\;\;0.5 \cdot \left(\mathsf{max}\left(p, r\right) + t\_0\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 (fmax p r)) (fabs (fmin p r)))))
                      (if (<= (pow (fabs q) 2.0) 5e-50)
                        (* 0.5 (+ (fmax p r) t_0))
                        (fma t_0 0.5 (fabs q)))))
                    double code(double p, double r, double q) {
                    	double t_0 = fabs(fmax(p, r)) + fabs(fmin(p, r));
                    	double tmp;
                    	if (pow(fabs(q), 2.0) <= 5e-50) {
                    		tmp = 0.5 * (fmax(p, r) + t_0);
                    	} else {
                    		tmp = fma(t_0, 0.5, fabs(q));
                    	}
                    	return tmp;
                    }
                    
                    function code(p, r, q)
                    	t_0 = Float64(abs(fmax(p, r)) + abs(fmin(p, r)))
                    	tmp = 0.0
                    	if ((abs(q) ^ 2.0) <= 5e-50)
                    		tmp = Float64(0.5 * Float64(fmax(p, r) + t_0));
                    	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[Max[p, r], $MachinePrecision]], $MachinePrecision] + N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Power[N[Abs[q], $MachinePrecision], 2.0], $MachinePrecision], 5e-50], N[(0.5 * N[(N[Max[p, r], $MachinePrecision] + t$95$0), $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)) <= (49999999999999996819973280629192612577499607123901762114707048811068332385806453764841381484801688708203161712406645496013682210267869976405563647858798503875732421875e-216)) THEN ((5e-1) * (tmp_4 + t_0)) ELSE ((t_0 * (5e-1)) + (abs(q))) ENDIF IN
                    	tmp_3
                    END code
                    \begin{array}{l}
                    t_0 := \left|\mathsf{max}\left(p, r\right)\right| + \left|\mathsf{min}\left(p, r\right)\right|\\
                    \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 5 \cdot 10^{-50}:\\
                    \;\;\;\;0.5 \cdot \left(\mathsf{max}\left(p, r\right) + t\_0\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)) < 4.9999999999999997e-50

                      1. Initial program 45.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 \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. Step-by-step derivation
                        1. Applied rewrites30.6%

                          \[\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) \]
                        2. Applied rewrites35.1%

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

                          \[\leadsto 0.5 \cdot \left(r + \left(\left|r\right| + \left|p\right|\right)\right) \]
                        4. Step-by-step derivation
                          1. Applied rewrites24.8%

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

                          if 4.9999999999999997e-50 < (pow.f64 q #s(literal 2 binary64))

                          1. Initial program 45.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 q \cdot \left(1 + \frac{1}{2} \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
                          3. Step-by-step derivation
                            1. Applied rewrites26.8%

                              \[\leadsto q \cdot \left(1 + 0.5 \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
                            2. Step-by-step derivation
                              1. Applied rewrites26.8%

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

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

                            Alternative 5: 46.6% accurate, 5.1× speedup?

                            \[\mathsf{fma}\left(\left|r\right| + \left|p\right|, 0.5, \left|q\right|\right) \]
                            (FPCore (p r q)
                              :precision binary64
                              :pre TRUE
                              (fma (+ (fabs r) (fabs p)) 0.5 (fabs q)))
                            double code(double p, double r, double q) {
                            	return fma((fabs(r) + fabs(p)), 0.5, fabs(q));
                            }
                            
                            function code(p, r, q)
                            	return fma(Float64(abs(r) + abs(p)), 0.5, abs(q))
                            end
                            
                            code[p_, r_, q_] := N[(N[(N[Abs[r], $MachinePrecision] + N[Abs[p], $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(r)) + (abs(p))) * (5e-1)) + (abs(q))
                            END code
                            \mathsf{fma}\left(\left|r\right| + \left|p\right|, 0.5, \left|q\right|\right)
                            
                            Derivation
                            1. Initial program 45.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 q \cdot \left(1 + \frac{1}{2} \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
                            3. Step-by-step derivation
                              1. Applied rewrites26.8%

                                \[\leadsto q \cdot \left(1 + 0.5 \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
                              2. Step-by-step derivation
                                1. Applied rewrites26.8%

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

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

                                Alternative 6: 19.2% accurate, 6.9× speedup?

                                \[0.5 \cdot \left(\left|p\right| + \left|r\right|\right) \]
                                (FPCore (p r q)
                                  :precision binary64
                                  :pre TRUE
                                  (* 0.5 (+ (fabs p) (fabs r))))
                                double code(double p, double r, double q) {
                                	return 0.5 * (fabs(p) + fabs(r));
                                }
                                
                                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 = 0.5d0 * (abs(p) + abs(r))
                                end function
                                
                                public static double code(double p, double r, double q) {
                                	return 0.5 * (Math.abs(p) + Math.abs(r));
                                }
                                
                                def code(p, r, q):
                                	return 0.5 * (math.fabs(p) + math.fabs(r))
                                
                                function code(p, r, q)
                                	return Float64(0.5 * Float64(abs(p) + abs(r)))
                                end
                                
                                function tmp = code(p, r, q)
                                	tmp = 0.5 * (abs(p) + abs(r));
                                end
                                
                                code[p_, r_, q_] := N[(0.5 * N[(N[Abs[p], $MachinePrecision] + N[Abs[r], $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 =
                                	(5e-1) * ((abs(p)) + (abs(r)))
                                END code
                                0.5 \cdot \left(\left|p\right| + \left|r\right|\right)
                                
                                Derivation
                                1. Initial program 45.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 q \cdot \left(1 + \frac{1}{2} \cdot \frac{\left|p\right| + \left|r\right|}{q}\right) \]
                                3. Step-by-step derivation
                                  1. Applied rewrites26.8%

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

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

                                      \[\leadsto 0.5 \cdot \left(\left|p\right| + \left|r\right|\right) \]
                                    2. Add Preprocessing

                                    Alternative 7: 14.2% 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 45.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 -1 \cdot q \]
                                    3. Step-by-step derivation
                                      1. Applied rewrites19.2%

                                        \[\leadsto -1 \cdot q \]
                                      2. Applied rewrites19.2%

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

                                      Reproduce

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