Henrywood and Agarwal, Equation (13)

Percentage Accurate: 24.8% → 56.0%
Time: 19.2s
Alternatives: 9
Speedup: 156.0×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)}\\ \frac{c0}{2 \cdot w} \cdot \left(t\_0 + \sqrt{t\_0 \cdot t\_0 - M \cdot M}\right) \end{array} \end{array} \]
(FPCore (c0 w h D d M)
 :precision binary64
 (let* ((t_0 (/ (* c0 (* d d)) (* (* w h) (* D D)))))
   (* (/ c0 (* 2.0 w)) (+ t_0 (sqrt (- (* t_0 t_0) (* M M)))))))
double code(double c0, double w, double h, double D, double d, double M) {
	double t_0 = (c0 * (d * d)) / ((w * h) * (D * D));
	return (c0 / (2.0 * w)) * (t_0 + sqrt(((t_0 * t_0) - (M * M))));
}
real(8) function code(c0, w, h, d, d_1, m)
    real(8), intent (in) :: c0
    real(8), intent (in) :: w
    real(8), intent (in) :: h
    real(8), intent (in) :: d
    real(8), intent (in) :: d_1
    real(8), intent (in) :: m
    real(8) :: t_0
    t_0 = (c0 * (d_1 * d_1)) / ((w * h) * (d * d))
    code = (c0 / (2.0d0 * w)) * (t_0 + sqrt(((t_0 * t_0) - (m * m))))
end function
public static double code(double c0, double w, double h, double D, double d, double M) {
	double t_0 = (c0 * (d * d)) / ((w * h) * (D * D));
	return (c0 / (2.0 * w)) * (t_0 + Math.sqrt(((t_0 * t_0) - (M * M))));
}
def code(c0, w, h, D, d, M):
	t_0 = (c0 * (d * d)) / ((w * h) * (D * D))
	return (c0 / (2.0 * w)) * (t_0 + math.sqrt(((t_0 * t_0) - (M * M))))
function code(c0, w, h, D, d, M)
	t_0 = Float64(Float64(c0 * Float64(d * d)) / Float64(Float64(w * h) * Float64(D * D)))
	return Float64(Float64(c0 / Float64(2.0 * w)) * Float64(t_0 + sqrt(Float64(Float64(t_0 * t_0) - Float64(M * M)))))
end
function tmp = code(c0, w, h, D, d, M)
	t_0 = (c0 * (d * d)) / ((w * h) * (D * D));
	tmp = (c0 / (2.0 * w)) * (t_0 + sqrt(((t_0 * t_0) - (M * M))));
end
code[c0_, w_, h_, D_, d_, M_] := Block[{t$95$0 = N[(N[(c0 * N[(d * d), $MachinePrecision]), $MachinePrecision] / N[(N[(w * h), $MachinePrecision] * N[(D * D), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(c0 / N[(2.0 * w), $MachinePrecision]), $MachinePrecision] * N[(t$95$0 + N[Sqrt[N[(N[(t$95$0 * t$95$0), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)}\\
\frac{c0}{2 \cdot w} \cdot \left(t\_0 + \sqrt{t\_0 \cdot t\_0 - M \cdot M}\right)
\end{array}
\end{array}

Sampling outcomes in binary64 precision:

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 9 alternatives:

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

Initial Program: 24.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)}\\ \frac{c0}{2 \cdot w} \cdot \left(t\_0 + \sqrt{t\_0 \cdot t\_0 - M \cdot M}\right) \end{array} \end{array} \]
(FPCore (c0 w h D d M)
 :precision binary64
 (let* ((t_0 (/ (* c0 (* d d)) (* (* w h) (* D D)))))
   (* (/ c0 (* 2.0 w)) (+ t_0 (sqrt (- (* t_0 t_0) (* M M)))))))
double code(double c0, double w, double h, double D, double d, double M) {
	double t_0 = (c0 * (d * d)) / ((w * h) * (D * D));
	return (c0 / (2.0 * w)) * (t_0 + sqrt(((t_0 * t_0) - (M * M))));
}
real(8) function code(c0, w, h, d, d_1, m)
    real(8), intent (in) :: c0
    real(8), intent (in) :: w
    real(8), intent (in) :: h
    real(8), intent (in) :: d
    real(8), intent (in) :: d_1
    real(8), intent (in) :: m
    real(8) :: t_0
    t_0 = (c0 * (d_1 * d_1)) / ((w * h) * (d * d))
    code = (c0 / (2.0d0 * w)) * (t_0 + sqrt(((t_0 * t_0) - (m * m))))
end function
public static double code(double c0, double w, double h, double D, double d, double M) {
	double t_0 = (c0 * (d * d)) / ((w * h) * (D * D));
	return (c0 / (2.0 * w)) * (t_0 + Math.sqrt(((t_0 * t_0) - (M * M))));
}
def code(c0, w, h, D, d, M):
	t_0 = (c0 * (d * d)) / ((w * h) * (D * D))
	return (c0 / (2.0 * w)) * (t_0 + math.sqrt(((t_0 * t_0) - (M * M))))
function code(c0, w, h, D, d, M)
	t_0 = Float64(Float64(c0 * Float64(d * d)) / Float64(Float64(w * h) * Float64(D * D)))
	return Float64(Float64(c0 / Float64(2.0 * w)) * Float64(t_0 + sqrt(Float64(Float64(t_0 * t_0) - Float64(M * M)))))
end
function tmp = code(c0, w, h, D, d, M)
	t_0 = (c0 * (d * d)) / ((w * h) * (D * D));
	tmp = (c0 / (2.0 * w)) * (t_0 + sqrt(((t_0 * t_0) - (M * M))));
end
code[c0_, w_, h_, D_, d_, M_] := Block[{t$95$0 = N[(N[(c0 * N[(d * d), $MachinePrecision]), $MachinePrecision] / N[(N[(w * h), $MachinePrecision] * N[(D * D), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(c0 / N[(2.0 * w), $MachinePrecision]), $MachinePrecision] * N[(t$95$0 + N[Sqrt[N[(N[(t$95$0 * t$95$0), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)}\\
\frac{c0}{2 \cdot w} \cdot \left(t\_0 + \sqrt{t\_0 \cdot t\_0 - M \cdot M}\right)
\end{array}
\end{array}

Alternative 1: 56.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{c0}{2 \cdot w}\\ t_1 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)}\\ \mathbf{if}\;t\_0 \cdot \left(t\_1 + \sqrt{t\_1 \cdot t\_1 - M \cdot M}\right) \leq \infty:\\ \;\;\;\;t\_0 \cdot \frac{\left(2 \cdot d\right) \cdot \frac{c0 \cdot d}{D \cdot \left(w \cdot h\right)}}{D}\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot \left(\frac{c0}{c0 \cdot d} \cdot \frac{D \cdot \left(-0.5 \cdot \left(D \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot \left(-w\right)\right)\right)\right)}{c0 \cdot d}\right)\\ \end{array} \end{array} \]
(FPCore (c0 w h D d M)
 :precision binary64
 (let* ((t_0 (/ c0 (* 2.0 w))) (t_1 (/ (* c0 (* d d)) (* (* D D) (* w h)))))
   (if (<= (* t_0 (+ t_1 (sqrt (- (* t_1 t_1) (* M M))))) INFINITY)
     (* t_0 (/ (* (* 2.0 d) (/ (* c0 d) (* D (* w h)))) D))
     (*
      t_0
      (*
       (/ c0 (* c0 d))
       (/ (* D (* -0.5 (* D (* (* h (* M M)) (- w))))) (* c0 d)))))))
double code(double c0, double w, double h, double D, double d, double M) {
	double t_0 = c0 / (2.0 * w);
	double t_1 = (c0 * (d * d)) / ((D * D) * (w * h));
	double tmp;
	if ((t_0 * (t_1 + sqrt(((t_1 * t_1) - (M * M))))) <= ((double) INFINITY)) {
		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D);
	} else {
		tmp = t_0 * ((c0 / (c0 * d)) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (c0 * d)));
	}
	return tmp;
}
public static double code(double c0, double w, double h, double D, double d, double M) {
	double t_0 = c0 / (2.0 * w);
	double t_1 = (c0 * (d * d)) / ((D * D) * (w * h));
	double tmp;
	if ((t_0 * (t_1 + Math.sqrt(((t_1 * t_1) - (M * M))))) <= Double.POSITIVE_INFINITY) {
		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D);
	} else {
		tmp = t_0 * ((c0 / (c0 * d)) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (c0 * d)));
	}
	return tmp;
}
def code(c0, w, h, D, d, M):
	t_0 = c0 / (2.0 * w)
	t_1 = (c0 * (d * d)) / ((D * D) * (w * h))
	tmp = 0
	if (t_0 * (t_1 + math.sqrt(((t_1 * t_1) - (M * M))))) <= math.inf:
		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D)
	else:
		tmp = t_0 * ((c0 / (c0 * d)) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (c0 * d)))
	return tmp
function code(c0, w, h, D, d, M)
	t_0 = Float64(c0 / Float64(2.0 * w))
	t_1 = Float64(Float64(c0 * Float64(d * d)) / Float64(Float64(D * D) * Float64(w * h)))
	tmp = 0.0
	if (Float64(t_0 * Float64(t_1 + sqrt(Float64(Float64(t_1 * t_1) - Float64(M * M))))) <= Inf)
		tmp = Float64(t_0 * Float64(Float64(Float64(2.0 * d) * Float64(Float64(c0 * d) / Float64(D * Float64(w * h)))) / D));
	else
		tmp = Float64(t_0 * Float64(Float64(c0 / Float64(c0 * d)) * Float64(Float64(D * Float64(-0.5 * Float64(D * Float64(Float64(h * Float64(M * M)) * Float64(-w))))) / Float64(c0 * d))));
	end
	return tmp
end
function tmp_2 = code(c0, w, h, D, d, M)
	t_0 = c0 / (2.0 * w);
	t_1 = (c0 * (d * d)) / ((D * D) * (w * h));
	tmp = 0.0;
	if ((t_0 * (t_1 + sqrt(((t_1 * t_1) - (M * M))))) <= Inf)
		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D);
	else
		tmp = t_0 * ((c0 / (c0 * d)) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (c0 * d)));
	end
	tmp_2 = tmp;
end
code[c0_, w_, h_, D_, d_, M_] := Block[{t$95$0 = N[(c0 / N[(2.0 * w), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(c0 * N[(d * d), $MachinePrecision]), $MachinePrecision] / N[(N[(D * D), $MachinePrecision] * N[(w * h), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 * N[(t$95$1 + N[Sqrt[N[(N[(t$95$1 * t$95$1), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], Infinity], N[(t$95$0 * N[(N[(N[(2.0 * d), $MachinePrecision] * N[(N[(c0 * d), $MachinePrecision] / N[(D * N[(w * h), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / D), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(N[(c0 / N[(c0 * d), $MachinePrecision]), $MachinePrecision] * N[(N[(D * N[(-0.5 * N[(D * N[(N[(h * N[(M * M), $MachinePrecision]), $MachinePrecision] * (-w)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c0 * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{c0}{2 \cdot w}\\
t_1 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)}\\
\mathbf{if}\;t\_0 \cdot \left(t\_1 + \sqrt{t\_1 \cdot t\_1 - M \cdot M}\right) \leq \infty:\\
\;\;\;\;t\_0 \cdot \frac{\left(2 \cdot d\right) \cdot \frac{c0 \cdot d}{D \cdot \left(w \cdot h\right)}}{D}\\

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(\frac{c0}{c0 \cdot d} \cdot \frac{D \cdot \left(-0.5 \cdot \left(D \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot \left(-w\right)\right)\right)\right)}{c0 \cdot d}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (/.f64 c0 (*.f64 #s(literal 2 binary64) w)) (+.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (sqrt.f64 (-.f64 (*.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D)))) (*.f64 M M))))) < +inf.0

    1. Initial program 79.2%

      \[\frac{c0}{2 \cdot w} \cdot \left(\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} + \sqrt{\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} \cdot \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} - M \cdot M}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in c0 around inf

      \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(2 \cdot \frac{c0 \cdot {d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)} \]
    4. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\frac{2 \cdot \left(c0 \cdot {d}^{2}\right)}{{D}^{2} \cdot \left(h \cdot w\right)}} \]
      2. lower-/.f64N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\frac{2 \cdot \left(c0 \cdot {d}^{2}\right)}{{D}^{2} \cdot \left(h \cdot w\right)}} \]
      3. lower-*.f64N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{\color{blue}{2 \cdot \left(c0 \cdot {d}^{2}\right)}}{{D}^{2} \cdot \left(h \cdot w\right)} \]
      4. lower-*.f64N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \color{blue}{\left(c0 \cdot {d}^{2}\right)}}{{D}^{2} \cdot \left(h \cdot w\right)} \]
      5. unpow2N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \color{blue}{\left(d \cdot d\right)}\right)}{{D}^{2} \cdot \left(h \cdot w\right)} \]
      6. lower-*.f64N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \color{blue}{\left(d \cdot d\right)}\right)}{{D}^{2} \cdot \left(h \cdot w\right)} \]
      7. lower-*.f64N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\color{blue}{{D}^{2} \cdot \left(h \cdot w\right)}} \]
      8. unpow2N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\color{blue}{\left(D \cdot D\right)} \cdot \left(h \cdot w\right)} \]
      9. lower-*.f64N/A

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\color{blue}{\left(D \cdot D\right)} \cdot \left(h \cdot w\right)} \]
      10. lower-*.f6477.6

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\left(D \cdot D\right) \cdot \color{blue}{\left(h \cdot w\right)}} \]
    5. Applied rewrites77.6%

      \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\left(D \cdot D\right) \cdot \left(h \cdot w\right)}} \]
    6. Step-by-step derivation
      1. Applied rewrites79.8%

        \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{\frac{2 \cdot \left(d \cdot \left(c0 \cdot d\right)\right)}{h \cdot \left(w \cdot D\right)}}{\color{blue}{D}} \]
      2. Step-by-step derivation
        1. Applied rewrites84.8%

          \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{\left(2 \cdot d\right) \cdot \frac{d \cdot c0}{D \cdot \left(h \cdot w\right)}}{D} \]

        if +inf.0 < (*.f64 (/.f64 c0 (*.f64 #s(literal 2 binary64) w)) (+.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (sqrt.f64 (-.f64 (*.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D)))) (*.f64 M M)))))

        1. Initial program 0.0%

          \[\frac{c0}{2 \cdot w} \cdot \left(\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} + \sqrt{\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} \cdot \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} - M \cdot M}\right) \]
        2. Add Preprocessing
        3. Taylor expanded in c0 around -inf

          \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(-1 \cdot \left(c0 \cdot \left(-1 \cdot \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)} + \left(\frac{-1}{2} \cdot \frac{{D}^{2} \cdot \left({M}^{2} \cdot \left(h \cdot w\right)\right)}{{c0}^{2} \cdot {d}^{2}} + \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)\right)\right)\right)} \]
        4. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(\mathsf{neg}\left(c0 \cdot \left(-1 \cdot \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)} + \left(\frac{-1}{2} \cdot \frac{{D}^{2} \cdot \left({M}^{2} \cdot \left(h \cdot w\right)\right)}{{c0}^{2} \cdot {d}^{2}} + \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)\right)\right)\right)} \]
          2. *-commutativeN/A

            \[\leadsto \frac{c0}{2 \cdot w} \cdot \left(\mathsf{neg}\left(\color{blue}{\left(-1 \cdot \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)} + \left(\frac{-1}{2} \cdot \frac{{D}^{2} \cdot \left({M}^{2} \cdot \left(h \cdot w\right)\right)}{{c0}^{2} \cdot {d}^{2}} + \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)\right) \cdot c0}\right)\right) \]
        5. Applied rewrites22.8%

          \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(-\frac{c0 \cdot \left(-0.5 \cdot \left(\left(D \cdot D\right) \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot w\right)\right)\right)}{\left(c0 \cdot c0\right) \cdot \left(d \cdot d\right)}\right)} \]
        6. Step-by-step derivation
          1. Applied rewrites45.2%

            \[\leadsto \frac{c0}{2 \cdot w} \cdot \left(-\frac{D \cdot \left(\left(D \cdot \left(w \cdot \left(h \cdot \left(M \cdot M\right)\right)\right)\right) \cdot -0.5\right)}{c0 \cdot d} \cdot \frac{c0}{c0 \cdot d}\right) \]
        7. Recombined 2 regimes into one program.
        8. Final simplification57.6%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{c0}{2 \cdot w} \cdot \left(\frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)} + \sqrt{\frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)} \cdot \frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)} - M \cdot M}\right) \leq \infty:\\ \;\;\;\;\frac{c0}{2 \cdot w} \cdot \frac{\left(2 \cdot d\right) \cdot \frac{c0 \cdot d}{D \cdot \left(w \cdot h\right)}}{D}\\ \mathbf{else}:\\ \;\;\;\;\frac{c0}{2 \cdot w} \cdot \left(\frac{c0}{c0 \cdot d} \cdot \frac{D \cdot \left(-0.5 \cdot \left(D \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot \left(-w\right)\right)\right)\right)}{c0 \cdot d}\right)\\ \end{array} \]
        9. Add Preprocessing

        Alternative 2: 52.6% accurate, 0.6× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{c0}{2 \cdot w}\\ t_1 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)}\\ \mathbf{if}\;t\_0 \cdot \left(t\_1 + \sqrt{t\_1 \cdot t\_1 - M \cdot M}\right) \leq \infty:\\ \;\;\;\;t\_0 \cdot \frac{\left(2 \cdot d\right) \cdot \frac{c0 \cdot d}{D \cdot \left(w \cdot h\right)}}{D}\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot \left(\frac{c0}{c0} \cdot \frac{D \cdot \left(-0.5 \cdot \left(D \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot \left(-w\right)\right)\right)\right)}{d \cdot \left(c0 \cdot d\right)}\right)\\ \end{array} \end{array} \]
        (FPCore (c0 w h D d M)
         :precision binary64
         (let* ((t_0 (/ c0 (* 2.0 w))) (t_1 (/ (* c0 (* d d)) (* (* D D) (* w h)))))
           (if (<= (* t_0 (+ t_1 (sqrt (- (* t_1 t_1) (* M M))))) INFINITY)
             (* t_0 (/ (* (* 2.0 d) (/ (* c0 d) (* D (* w h)))) D))
             (*
              t_0
              (*
               (/ c0 c0)
               (/ (* D (* -0.5 (* D (* (* h (* M M)) (- w))))) (* d (* c0 d))))))))
        double code(double c0, double w, double h, double D, double d, double M) {
        	double t_0 = c0 / (2.0 * w);
        	double t_1 = (c0 * (d * d)) / ((D * D) * (w * h));
        	double tmp;
        	if ((t_0 * (t_1 + sqrt(((t_1 * t_1) - (M * M))))) <= ((double) INFINITY)) {
        		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D);
        	} else {
        		tmp = t_0 * ((c0 / c0) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (d * (c0 * d))));
        	}
        	return tmp;
        }
        
        public static double code(double c0, double w, double h, double D, double d, double M) {
        	double t_0 = c0 / (2.0 * w);
        	double t_1 = (c0 * (d * d)) / ((D * D) * (w * h));
        	double tmp;
        	if ((t_0 * (t_1 + Math.sqrt(((t_1 * t_1) - (M * M))))) <= Double.POSITIVE_INFINITY) {
        		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D);
        	} else {
        		tmp = t_0 * ((c0 / c0) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (d * (c0 * d))));
        	}
        	return tmp;
        }
        
        def code(c0, w, h, D, d, M):
        	t_0 = c0 / (2.0 * w)
        	t_1 = (c0 * (d * d)) / ((D * D) * (w * h))
        	tmp = 0
        	if (t_0 * (t_1 + math.sqrt(((t_1 * t_1) - (M * M))))) <= math.inf:
        		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D)
        	else:
        		tmp = t_0 * ((c0 / c0) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (d * (c0 * d))))
        	return tmp
        
        function code(c0, w, h, D, d, M)
        	t_0 = Float64(c0 / Float64(2.0 * w))
        	t_1 = Float64(Float64(c0 * Float64(d * d)) / Float64(Float64(D * D) * Float64(w * h)))
        	tmp = 0.0
        	if (Float64(t_0 * Float64(t_1 + sqrt(Float64(Float64(t_1 * t_1) - Float64(M * M))))) <= Inf)
        		tmp = Float64(t_0 * Float64(Float64(Float64(2.0 * d) * Float64(Float64(c0 * d) / Float64(D * Float64(w * h)))) / D));
        	else
        		tmp = Float64(t_0 * Float64(Float64(c0 / c0) * Float64(Float64(D * Float64(-0.5 * Float64(D * Float64(Float64(h * Float64(M * M)) * Float64(-w))))) / Float64(d * Float64(c0 * d)))));
        	end
        	return tmp
        end
        
        function tmp_2 = code(c0, w, h, D, d, M)
        	t_0 = c0 / (2.0 * w);
        	t_1 = (c0 * (d * d)) / ((D * D) * (w * h));
        	tmp = 0.0;
        	if ((t_0 * (t_1 + sqrt(((t_1 * t_1) - (M * M))))) <= Inf)
        		tmp = t_0 * (((2.0 * d) * ((c0 * d) / (D * (w * h)))) / D);
        	else
        		tmp = t_0 * ((c0 / c0) * ((D * (-0.5 * (D * ((h * (M * M)) * -w)))) / (d * (c0 * d))));
        	end
        	tmp_2 = tmp;
        end
        
        code[c0_, w_, h_, D_, d_, M_] := Block[{t$95$0 = N[(c0 / N[(2.0 * w), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(c0 * N[(d * d), $MachinePrecision]), $MachinePrecision] / N[(N[(D * D), $MachinePrecision] * N[(w * h), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 * N[(t$95$1 + N[Sqrt[N[(N[(t$95$1 * t$95$1), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], Infinity], N[(t$95$0 * N[(N[(N[(2.0 * d), $MachinePrecision] * N[(N[(c0 * d), $MachinePrecision] / N[(D * N[(w * h), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / D), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(N[(c0 / c0), $MachinePrecision] * N[(N[(D * N[(-0.5 * N[(D * N[(N[(h * N[(M * M), $MachinePrecision]), $MachinePrecision] * (-w)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(d * N[(c0 * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        t_0 := \frac{c0}{2 \cdot w}\\
        t_1 := \frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)}\\
        \mathbf{if}\;t\_0 \cdot \left(t\_1 + \sqrt{t\_1 \cdot t\_1 - M \cdot M}\right) \leq \infty:\\
        \;\;\;\;t\_0 \cdot \frac{\left(2 \cdot d\right) \cdot \frac{c0 \cdot d}{D \cdot \left(w \cdot h\right)}}{D}\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0 \cdot \left(\frac{c0}{c0} \cdot \frac{D \cdot \left(-0.5 \cdot \left(D \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot \left(-w\right)\right)\right)\right)}{d \cdot \left(c0 \cdot d\right)}\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if (*.f64 (/.f64 c0 (*.f64 #s(literal 2 binary64) w)) (+.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (sqrt.f64 (-.f64 (*.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D)))) (*.f64 M M))))) < +inf.0

          1. Initial program 74.6%

            \[\frac{c0}{2 \cdot w} \cdot \left(\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} + \sqrt{\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} \cdot \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} - M \cdot M}\right) \]
          2. Add Preprocessing
          3. Taylor expanded in c0 around inf

            \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(2 \cdot \frac{c0 \cdot {d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)} \]
          4. Step-by-step derivation
            1. associate-*r/N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\frac{2 \cdot \left(c0 \cdot {d}^{2}\right)}{{D}^{2} \cdot \left(h \cdot w\right)}} \]
            2. lower-/.f64N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\frac{2 \cdot \left(c0 \cdot {d}^{2}\right)}{{D}^{2} \cdot \left(h \cdot w\right)}} \]
            3. lower-*.f64N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{\color{blue}{2 \cdot \left(c0 \cdot {d}^{2}\right)}}{{D}^{2} \cdot \left(h \cdot w\right)} \]
            4. lower-*.f64N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \color{blue}{\left(c0 \cdot {d}^{2}\right)}}{{D}^{2} \cdot \left(h \cdot w\right)} \]
            5. unpow2N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \color{blue}{\left(d \cdot d\right)}\right)}{{D}^{2} \cdot \left(h \cdot w\right)} \]
            6. lower-*.f64N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \color{blue}{\left(d \cdot d\right)}\right)}{{D}^{2} \cdot \left(h \cdot w\right)} \]
            7. lower-*.f64N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\color{blue}{{D}^{2} \cdot \left(h \cdot w\right)}} \]
            8. unpow2N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\color{blue}{\left(D \cdot D\right)} \cdot \left(h \cdot w\right)} \]
            9. lower-*.f64N/A

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\color{blue}{\left(D \cdot D\right)} \cdot \left(h \cdot w\right)} \]
            10. lower-*.f6474.0

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\left(D \cdot D\right) \cdot \color{blue}{\left(h \cdot w\right)}} \]
          5. Applied rewrites74.0%

            \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\frac{2 \cdot \left(c0 \cdot \left(d \cdot d\right)\right)}{\left(D \cdot D\right) \cdot \left(h \cdot w\right)}} \]
          6. Step-by-step derivation
            1. Applied rewrites79.0%

              \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{\frac{2 \cdot \left(d \cdot \left(c0 \cdot d\right)\right)}{h \cdot \left(w \cdot D\right)}}{\color{blue}{D}} \]
            2. Step-by-step derivation
              1. Applied rewrites80.5%

                \[\leadsto \frac{c0}{2 \cdot w} \cdot \frac{\left(2 \cdot d\right) \cdot \frac{d \cdot c0}{D \cdot \left(h \cdot w\right)}}{D} \]

              if +inf.0 < (*.f64 (/.f64 c0 (*.f64 #s(literal 2 binary64) w)) (+.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (sqrt.f64 (-.f64 (*.f64 (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D))) (/.f64 (*.f64 c0 (*.f64 d d)) (*.f64 (*.f64 w h) (*.f64 D D)))) (*.f64 M M)))))

              1. Initial program 0.0%

                \[\frac{c0}{2 \cdot w} \cdot \left(\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} + \sqrt{\frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} \cdot \frac{c0 \cdot \left(d \cdot d\right)}{\left(w \cdot h\right) \cdot \left(D \cdot D\right)} - M \cdot M}\right) \]
              2. Add Preprocessing
              3. Taylor expanded in c0 around -inf

                \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(-1 \cdot \left(c0 \cdot \left(-1 \cdot \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)} + \left(\frac{-1}{2} \cdot \frac{{D}^{2} \cdot \left({M}^{2} \cdot \left(h \cdot w\right)\right)}{{c0}^{2} \cdot {d}^{2}} + \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)\right)\right)\right)} \]
              4. Step-by-step derivation
                1. mul-1-negN/A

                  \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(\mathsf{neg}\left(c0 \cdot \left(-1 \cdot \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)} + \left(\frac{-1}{2} \cdot \frac{{D}^{2} \cdot \left({M}^{2} \cdot \left(h \cdot w\right)\right)}{{c0}^{2} \cdot {d}^{2}} + \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)\right)\right)\right)} \]
                2. *-commutativeN/A

                  \[\leadsto \frac{c0}{2 \cdot w} \cdot \left(\mathsf{neg}\left(\color{blue}{\left(-1 \cdot \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)} + \left(\frac{-1}{2} \cdot \frac{{D}^{2} \cdot \left({M}^{2} \cdot \left(h \cdot w\right)\right)}{{c0}^{2} \cdot {d}^{2}} + \frac{{d}^{2}}{{D}^{2} \cdot \left(h \cdot w\right)}\right)\right) \cdot c0}\right)\right) \]
              5. Applied rewrites21.1%

                \[\leadsto \frac{c0}{2 \cdot w} \cdot \color{blue}{\left(-\frac{c0 \cdot \left(-0.5 \cdot \left(\left(D \cdot D\right) \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot w\right)\right)\right)}{\left(c0 \cdot c0\right) \cdot \left(d \cdot d\right)}\right)} \]
              6. Step-by-step derivation
                1. Applied rewrites38.7%

                  \[\leadsto \frac{c0}{2 \cdot w} \cdot \left(-\frac{c0}{c0} \cdot \frac{D \cdot \left(\left(D \cdot \left(w \cdot \left(h \cdot \left(M \cdot M\right)\right)\right)\right) \cdot -0.5\right)}{d \cdot \left(c0 \cdot d\right)}\right) \]
              7. Recombined 2 regimes into one program.
              8. Final simplification52.6%

                \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{c0}{2 \cdot w} \cdot \left(\frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)} + \sqrt{\frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)} \cdot \frac{c0 \cdot \left(d \cdot d\right)}{\left(D \cdot D\right) \cdot \left(w \cdot h\right)} - M \cdot M}\right) \leq \infty:\\ \;\;\;\;\frac{c0}{2 \cdot w} \cdot \frac{\left(2 \cdot d\right) \cdot \frac{c0 \cdot d}{D \cdot \left(w \cdot h\right)}}{D}\\ \mathbf{else}:\\ \;\;\;\;\frac{c0}{2 \cdot w} \cdot \left(\frac{c0}{c0} \cdot \frac{D \cdot \left(-0.5 \cdot \left(D \cdot \left(\left(h \cdot \left(M \cdot M\right)\right) \cdot \left(-w\right)\right)\right)\right)}{d \cdot \left(c0 \cdot d\right)}\right)\\ \end{array} \]
              9. Add Preprocessing

              Reproduce

              ?
              herbie shell --seed 2024229 
              (FPCore (c0 w h D d M)
                :name "Henrywood and Agarwal, Equation (13)"
                :precision binary64
                (* (/ c0 (* 2.0 w)) (+ (/ (* c0 (* d d)) (* (* w h) (* D D))) (sqrt (- (* (/ (* c0 (* d d)) (* (* w h) (* D D))) (/ (* c0 (* d d)) (* (* w h) (* D D)))) (* M M))))))