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

Percentage Accurate: 45.7% → 99.9%
Time: 3.3s
Alternatives: 8
Speedup: 2.2×

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 8 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.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: 99.9% 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.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. Applied rewrites45.7%

    \[\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.9%

      \[\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: 81.3% 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 2 \cdot 10^{+179}:\\ \;\;\;\;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(0.5, t\_0, \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) 2e+179)
        (* 0.5 (+ (- (fmax p r) (fmin p r)) t_0))
        (fma 0.5 t_0 (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) <= 2e+179) {
    		tmp = 0.5 * ((fmax(p, r) - fmin(p, r)) + t_0);
    	} else {
    		tmp = fma(0.5, t_0, 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) <= 2e+179)
    		tmp = Float64(0.5 * Float64(Float64(fmax(p, r) - fmin(p, r)) + t_0));
    	else
    		tmp = fma(0.5, t_0, 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], 2e+179], N[(0.5 * N[(N[(N[Max[p, r], $MachinePrecision] - N[Min[p, r], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision], N[(0.5 * t$95$0 + 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)) <= (199999999999999996091099546963028318915752778493452543828291966300228010772656544918538878468995967298844297195887900676839994006336880488768194581630088140609089562433891216654336)) THEN ((5e-1) * ((tmp_5 - tmp_6) + t_0)) ELSE (((5e-1) * t_0) + (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 2 \cdot 10^{+179}:\\
    \;\;\;\;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(0.5, t\_0, \left|q\right|\right)\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (pow.f64 q #s(literal 2 binary64)) < 2e179

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

          \[\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.6%

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

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

        1. Initial program 45.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. Step-by-step derivation
          1. Applied rewrites26.0%

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

            \[\leadsto q - -0.5 \cdot \left(\left|r\right| + \left|p\right|\right) \]
          3. Applied rewrites28.4%

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

        Alternative 3: 65.7% accurate, 1.8× 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 := t\_1 + t\_0\\ \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -0.15114894474882007:\\ \;\;\;\;0.5 \cdot \left(\left(-\mathsf{min}\left(p, r\right)\right) + t\_2\right)\\ \mathbf{elif}\;\mathsf{min}\left(p, r\right) \leq 1.379450732881067 \cdot 10^{-194}:\\ \;\;\;\;\mathsf{fma}\left(0.5, t\_2, \left|q\right|\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(t\_0 + t\_1\right) + \mathsf{max}\left(p, r\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 (+ t_1 t_0)))
          (if (<= (fmin p r) -0.15114894474882007)
            (* 0.5 (+ (- (fmin p r)) t_2))
            (if (<= (fmin p r) 1.379450732881067e-194)
              (fma 0.5 t_2 (fabs q))
              (* 0.5 (+ (+ t_0 t_1) (fmax p r)))))))
        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 = t_1 + t_0;
        	double tmp;
        	if (fmin(p, r) <= -0.15114894474882007) {
        		tmp = 0.5 * (-fmin(p, r) + t_2);
        	} else if (fmin(p, r) <= 1.379450732881067e-194) {
        		tmp = fma(0.5, t_2, fabs(q));
        	} else {
        		tmp = 0.5 * ((t_0 + t_1) + fmax(p, r));
        	}
        	return tmp;
        }
        
        function code(p, r, q)
        	t_0 = abs(fmin(p, r))
        	t_1 = abs(fmax(p, r))
        	t_2 = Float64(t_1 + t_0)
        	tmp = 0.0
        	if (fmin(p, r) <= -0.15114894474882007)
        		tmp = Float64(0.5 * Float64(Float64(-fmin(p, r)) + t_2));
        	elseif (fmin(p, r) <= 1.379450732881067e-194)
        		tmp = fma(0.5, t_2, abs(q));
        	else
        		tmp = Float64(0.5 * Float64(Float64(t_0 + t_1) + fmax(p, r)));
        	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[(t$95$1 + t$95$0), $MachinePrecision]}, If[LessEqual[N[Min[p, r], $MachinePrecision], -0.15114894474882007], N[(0.5 * N[((-N[Min[p, r], $MachinePrecision]) + t$95$2), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Min[p, r], $MachinePrecision], 1.379450732881067e-194], N[(0.5 * t$95$2 + N[Abs[q], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[(t$95$0 + t$95$1), $MachinePrecision] + N[Max[p, 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 =
        	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 t_2 = (t_1 + t_0) 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 <= (13794507328810668619156376964629912633960626899521951595040570690959828473361667823676133021833913456891686927954629088062880145463883480805167862685905783081341610017763703192070831355122635775287639547295718412272971027805181936839015426362491967666070434633524002798601435871353010675448421012554018111789223100500419232131000891821015404747518237511631506086743047216610669381945315351587548668321322042981057531436651903310555880577085695171035193456652266641437876160125597380101680755615234375e-693)) THEN (((5e-1) * t_2) + (abs(q))) ELSE ((5e-1) * ((t_0 + t_1) + tmp_8)) ENDIF IN
        				LET tmp_3 = IF (tmp_4 <= (-151148944748820068806338667855015955865383148193359375e-54)) THEN ((5e-1) * ((- tmp_5) + t_2)) ELSE tmp_6 ENDIF IN
        	tmp_3
        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 := t\_1 + t\_0\\
        \mathbf{if}\;\mathsf{min}\left(p, r\right) \leq -0.15114894474882007:\\
        \;\;\;\;0.5 \cdot \left(\left(-\mathsf{min}\left(p, r\right)\right) + t\_2\right)\\
        
        \mathbf{elif}\;\mathsf{min}\left(p, r\right) \leq 1.379450732881067 \cdot 10^{-194}:\\
        \;\;\;\;\mathsf{fma}\left(0.5, t\_2, \left|q\right|\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;0.5 \cdot \left(\left(t\_0 + t\_1\right) + \mathsf{max}\left(p, r\right)\right)\\
        
        
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if p < -0.15114894474882007

          1. Initial program 45.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. Step-by-step derivation
            1. Applied rewrites24.5%

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

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

            if -0.15114894474882007 < p < 1.3794507328810669e-194

            1. Initial program 45.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. Step-by-step derivation
              1. Applied rewrites26.0%

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

                \[\leadsto q - -0.5 \cdot \left(\left|r\right| + \left|p\right|\right) \]
              3. Applied rewrites28.4%

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

              if 1.3794507328810669e-194 < p

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

                  \[\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. Taylor expanded in p around 0

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

                    \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
                  2. Evaluated real constant25.1%

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

                Alternative 4: 57.2% accurate, 2.2× speedup?

                \[\begin{array}{l} t_0 := \left|\mathsf{max}\left(p, r\right)\right|\\ t_1 := \left|\mathsf{min}\left(p, r\right)\right|\\ \mathbf{if}\;\mathsf{max}\left(p, r\right) \leq 1.5181833995066405 \cdot 10^{+167}:\\ \;\;\;\;\mathsf{fma}\left(0.5, t\_0 + t\_1, \left|q\right|\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(t\_1 + t\_0\right) + \mathsf{max}\left(p, r\right)\right)\\ \end{array} \]
                (FPCore (p r q)
                  :precision binary64
                  :pre TRUE
                  (let* ((t_0 (fabs (fmax p r))) (t_1 (fabs (fmin p r))))
                  (if (<= (fmax p r) 1.5181833995066405e+167)
                    (fma 0.5 (+ t_0 t_1) (fabs q))
                    (* 0.5 (+ (+ t_1 t_0) (fmax p r))))))
                double code(double p, double r, double q) {
                	double t_0 = fabs(fmax(p, r));
                	double t_1 = fabs(fmin(p, r));
                	double tmp;
                	if (fmax(p, r) <= 1.5181833995066405e+167) {
                		tmp = fma(0.5, (t_0 + t_1), fabs(q));
                	} else {
                		tmp = 0.5 * ((t_1 + t_0) + fmax(p, r));
                	}
                	return tmp;
                }
                
                function code(p, r, q)
                	t_0 = abs(fmax(p, r))
                	t_1 = abs(fmin(p, r))
                	tmp = 0.0
                	if (fmax(p, r) <= 1.5181833995066405e+167)
                		tmp = fma(0.5, Float64(t_0 + t_1), abs(q));
                	else
                		tmp = Float64(0.5 * Float64(Float64(t_1 + t_0) + fmax(p, r)));
                	end
                	return tmp
                end
                
                code[p_, r_, q_] := Block[{t$95$0 = N[Abs[N[Max[p, r], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[Min[p, r], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[Max[p, r], $MachinePrecision], 1.5181833995066405e+167], N[(0.5 * N[(t$95$0 + t$95$1), $MachinePrecision] + N[Abs[q], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[(t$95$1 + t$95$0), $MachinePrecision] + N[Max[p, 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 =
                	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_3 = IF (p > r) THEN p ELSE r ENDIF IN
                			LET tmp_4 = IF (p > r) THEN p ELSE r ENDIF IN
                			LET tmp_2 = IF (tmp_3 <= (151818339950664054647376353253265021555209059867919151991725209839336129128940599991811517152258909840982498211274833077379541025117984649687663921951991162197602467840)) THEN (((5e-1) * (t_0 + t_1)) + (abs(q))) ELSE ((5e-1) * ((t_1 + t_0) + tmp_4)) ENDIF IN
                	tmp_2
                END code
                \begin{array}{l}
                t_0 := \left|\mathsf{max}\left(p, r\right)\right|\\
                t_1 := \left|\mathsf{min}\left(p, r\right)\right|\\
                \mathbf{if}\;\mathsf{max}\left(p, r\right) \leq 1.5181833995066405 \cdot 10^{+167}:\\
                \;\;\;\;\mathsf{fma}\left(0.5, t\_0 + t\_1, \left|q\right|\right)\\
                
                \mathbf{else}:\\
                \;\;\;\;0.5 \cdot \left(\left(t\_1 + t\_0\right) + \mathsf{max}\left(p, r\right)\right)\\
                
                
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if r < 1.5181833995066405e167

                  1. Initial program 45.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. Step-by-step derivation
                    1. Applied rewrites26.0%

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

                      \[\leadsto q - -0.5 \cdot \left(\left|r\right| + \left|p\right|\right) \]
                    3. Applied rewrites28.4%

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

                    if 1.5181833995066405e167 < r

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

                        \[\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. Taylor expanded in p around 0

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

                          \[\leadsto \frac{1}{2} \cdot \left(\left(\left|p\right| + \left|r\right|\right) + r\right) \]
                        2. Evaluated real constant25.1%

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

                      Alternative 5: 44.9% accurate, 5.1× speedup?

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

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

                          \[\leadsto q - -0.5 \cdot \left(\left|r\right| + \left|p\right|\right) \]
                        3. Applied rewrites28.4%

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

                        Alternative 6: 38.8% accurate, 1.9× speedup?

                        \[\begin{array}{l} \mathbf{if}\;{\left(\left|q\right|\right)}^{2} \leq 10^{-206}:\\ \;\;\;\;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) 1e-206)
                          (* 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) <= 1e-206) {
                        		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) <= 1d-206) 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) <= 1e-206) {
                        		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) <= 1e-206:
                        		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) <= 1e-206)
                        		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) <= 1e-206)
                        		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], 1e-206], 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)) <= (1000000000000000028744861868504177561146719241146806747487196028557965644111623810031001581665200787843274172655354933451469773534535808018399869126785044893552804933800208460050093327111666427937886598974346843058033569204162008468756111131622465602620067122836086961830504745239089017417231637691472711696999377193365743421769173476450734636817322647197129200578492365220673246922878341697578513376235823748411417377132323662929710493178880080126376881833747982617725108140959014170254096944001620528297280543483793735504150390625e-737)) 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 10^{-206}:\\
                        \;\;\;\;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)) < 1e-206

                          1. Initial program 45.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. Step-by-step derivation
                            1. Applied rewrites26.0%

                              \[\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.4%

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

                              if 1e-206 < (pow.f64 q #s(literal 2 binary64))

                              1. Initial program 45.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. Step-by-step derivation
                                1. Applied rewrites26.0%

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

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

                                    \[\leadsto q \cdot 1 \]
                                  3. Step-by-step derivation
                                    1. Applied rewrites17.6%

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

                                  Alternative 7: 34.9% 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 45.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. Step-by-step derivation
                                    1. Applied rewrites26.0%

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

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

                                        \[\leadsto q \cdot 1 \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites17.6%

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

                                        Alternative 8: 18.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 45.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. Step-by-step derivation
                                          1. Applied rewrites18.5%

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

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

                                          Reproduce

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