2-ancestry mixing, positive discriminant

Percentage Accurate: 43.8% → 95.9%
Time: 9.7s
Alternatives: 3
Speedup: 2.6×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{2 \cdot a}\\ t_1 := \sqrt{g \cdot g - h \cdot h}\\ \sqrt[3]{t\_0 \cdot \left(\left(-g\right) + t\_1\right)} + \sqrt[3]{t\_0 \cdot \left(\left(-g\right) - t\_1\right)} \end{array} \end{array} \]
(FPCore (g h a)
 :precision binary64
 (let* ((t_0 (/ 1.0 (* 2.0 a))) (t_1 (sqrt (- (* g g) (* h h)))))
   (+ (cbrt (* t_0 (+ (- g) t_1))) (cbrt (* t_0 (- (- g) t_1))))))
double code(double g, double h, double a) {
	double t_0 = 1.0 / (2.0 * a);
	double t_1 = sqrt(((g * g) - (h * h)));
	return cbrt((t_0 * (-g + t_1))) + cbrt((t_0 * (-g - t_1)));
}
public static double code(double g, double h, double a) {
	double t_0 = 1.0 / (2.0 * a);
	double t_1 = Math.sqrt(((g * g) - (h * h)));
	return Math.cbrt((t_0 * (-g + t_1))) + Math.cbrt((t_0 * (-g - t_1)));
}
function code(g, h, a)
	t_0 = Float64(1.0 / Float64(2.0 * a))
	t_1 = sqrt(Float64(Float64(g * g) - Float64(h * h)))
	return Float64(cbrt(Float64(t_0 * Float64(Float64(-g) + t_1))) + cbrt(Float64(t_0 * Float64(Float64(-g) - t_1))))
end
code[g_, h_, a_] := Block[{t$95$0 = N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(N[(g * g), $MachinePrecision] - N[(h * h), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, N[(N[Power[N[(t$95$0 * N[((-g) + t$95$1), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(t$95$0 * N[((-g) - t$95$1), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{2 \cdot a}\\
t_1 := \sqrt{g \cdot g - h \cdot h}\\
\sqrt[3]{t\_0 \cdot \left(\left(-g\right) + t\_1\right)} + \sqrt[3]{t\_0 \cdot \left(\left(-g\right) - t\_1\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 3 alternatives:

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

Initial Program: 43.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{2 \cdot a}\\ t_1 := \sqrt{g \cdot g - h \cdot h}\\ \sqrt[3]{t\_0 \cdot \left(\left(-g\right) + t\_1\right)} + \sqrt[3]{t\_0 \cdot \left(\left(-g\right) - t\_1\right)} \end{array} \end{array} \]
(FPCore (g h a)
 :precision binary64
 (let* ((t_0 (/ 1.0 (* 2.0 a))) (t_1 (sqrt (- (* g g) (* h h)))))
   (+ (cbrt (* t_0 (+ (- g) t_1))) (cbrt (* t_0 (- (- g) t_1))))))
double code(double g, double h, double a) {
	double t_0 = 1.0 / (2.0 * a);
	double t_1 = sqrt(((g * g) - (h * h)));
	return cbrt((t_0 * (-g + t_1))) + cbrt((t_0 * (-g - t_1)));
}
public static double code(double g, double h, double a) {
	double t_0 = 1.0 / (2.0 * a);
	double t_1 = Math.sqrt(((g * g) - (h * h)));
	return Math.cbrt((t_0 * (-g + t_1))) + Math.cbrt((t_0 * (-g - t_1)));
}
function code(g, h, a)
	t_0 = Float64(1.0 / Float64(2.0 * a))
	t_1 = sqrt(Float64(Float64(g * g) - Float64(h * h)))
	return Float64(cbrt(Float64(t_0 * Float64(Float64(-g) + t_1))) + cbrt(Float64(t_0 * Float64(Float64(-g) - t_1))))
end
code[g_, h_, a_] := Block[{t$95$0 = N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(N[(g * g), $MachinePrecision] - N[(h * h), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, N[(N[Power[N[(t$95$0 * N[((-g) + t$95$1), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(t$95$0 * N[((-g) - t$95$1), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{2 \cdot a}\\
t_1 := \sqrt{g \cdot g - h \cdot h}\\
\sqrt[3]{t\_0 \cdot \left(\left(-g\right) + t\_1\right)} + \sqrt[3]{t\_0 \cdot \left(\left(-g\right) - t\_1\right)}
\end{array}
\end{array}

Alternative 1: 95.9% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \frac{-\sqrt[3]{g}}{\sqrt[3]{a}} \end{array} \]
(FPCore (g h a) :precision binary64 (/ (- (cbrt g)) (cbrt a)))
double code(double g, double h, double a) {
	return -cbrt(g) / cbrt(a);
}
public static double code(double g, double h, double a) {
	return -Math.cbrt(g) / Math.cbrt(a);
}
function code(g, h, a)
	return Float64(Float64(-cbrt(g)) / cbrt(a))
end
code[g_, h_, a_] := N[((-N[Power[g, 1/3], $MachinePrecision]) / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{-\sqrt[3]{g}}{\sqrt[3]{a}}
\end{array}
Derivation
  1. Initial program 45.0%

    \[\sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) + \sqrt{g \cdot g - h \cdot h}\right)} + \sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) - \sqrt{g \cdot g - h \cdot h}\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in g around -inf

    \[\leadsto \color{blue}{-1 \cdot \left(\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right)\right)} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \mathsf{neg}\left(\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right)\right) \]
    2. lower-neg.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right) \]
    3. cbrt-unprodN/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \sqrt[3]{\frac{1}{2} \cdot 2} \]
    4. metadata-evalN/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \sqrt[3]{1} \]
    5. metadata-evalN/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    6. lower-*.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    7. lower-cbrt.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    8. lower-/.f6473.0

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
  5. Applied rewrites73.0%

    \[\leadsto \color{blue}{-\sqrt[3]{\frac{g}{a}} \cdot 1} \]
  6. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    2. lift-cbrt.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    3. cbrt-divN/A

      \[\leadsto -\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot 1 \]
    4. lower-/.f64N/A

      \[\leadsto -\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot 1 \]
    5. lower-cbrt.f64N/A

      \[\leadsto -\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot 1 \]
    6. lower-cbrt.f6495.7

      \[\leadsto -\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot 1 \]
  7. Applied rewrites95.7%

    \[\leadsto -\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot 1 \]
  8. Final simplification95.7%

    \[\leadsto \frac{-\sqrt[3]{g}}{\sqrt[3]{a}} \]
  9. Add Preprocessing

Alternative 2: 74.7% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{2 \cdot a}\\ \mathbf{if}\;t\_0 \leq 2 \cdot 10^{+65} \lor \neg \left(t\_0 \leq 2 \cdot 10^{+118}\right):\\ \;\;\;\;-\sqrt[3]{\frac{g}{a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-\sqrt[3]{\left(a \cdot a\right) \cdot g}}{a}\\ \end{array} \end{array} \]
(FPCore (g h a)
 :precision binary64
 (let* ((t_0 (/ 1.0 (* 2.0 a))))
   (if (or (<= t_0 2e+65) (not (<= t_0 2e+118)))
     (- (cbrt (/ g a)))
     (/ (- (cbrt (* (* a a) g))) a))))
double code(double g, double h, double a) {
	double t_0 = 1.0 / (2.0 * a);
	double tmp;
	if ((t_0 <= 2e+65) || !(t_0 <= 2e+118)) {
		tmp = -cbrt((g / a));
	} else {
		tmp = -cbrt(((a * a) * g)) / a;
	}
	return tmp;
}
public static double code(double g, double h, double a) {
	double t_0 = 1.0 / (2.0 * a);
	double tmp;
	if ((t_0 <= 2e+65) || !(t_0 <= 2e+118)) {
		tmp = -Math.cbrt((g / a));
	} else {
		tmp = -Math.cbrt(((a * a) * g)) / a;
	}
	return tmp;
}
function code(g, h, a)
	t_0 = Float64(1.0 / Float64(2.0 * a))
	tmp = 0.0
	if ((t_0 <= 2e+65) || !(t_0 <= 2e+118))
		tmp = Float64(-cbrt(Float64(g / a)));
	else
		tmp = Float64(Float64(-cbrt(Float64(Float64(a * a) * g))) / a);
	end
	return tmp
end
code[g_, h_, a_] := Block[{t$95$0 = N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, 2e+65], N[Not[LessEqual[t$95$0, 2e+118]], $MachinePrecision]], (-N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision]), N[((-N[Power[N[(N[(a * a), $MachinePrecision] * g), $MachinePrecision], 1/3], $MachinePrecision]) / a), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{2 \cdot a}\\
\mathbf{if}\;t\_0 \leq 2 \cdot 10^{+65} \lor \neg \left(t\_0 \leq 2 \cdot 10^{+118}\right):\\
\;\;\;\;-\sqrt[3]{\frac{g}{a}}\\

\mathbf{else}:\\
\;\;\;\;\frac{-\sqrt[3]{\left(a \cdot a\right) \cdot g}}{a}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a)) < 2e65 or 1.99999999999999993e118 < (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a))

    1. Initial program 46.4%

      \[\sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) + \sqrt{g \cdot g - h \cdot h}\right)} + \sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) - \sqrt{g \cdot g - h \cdot h}\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in g around -inf

      \[\leadsto \color{blue}{-1 \cdot \left(\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right)\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right)\right) \]
      2. lower-neg.f64N/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right) \]
      3. cbrt-unprodN/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \sqrt[3]{\frac{1}{2} \cdot 2} \]
      4. metadata-evalN/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \sqrt[3]{1} \]
      5. metadata-evalN/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
      6. lower-*.f64N/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
      7. lower-cbrt.f64N/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
      8. lower-/.f6475.2

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    5. Applied rewrites75.2%

      \[\leadsto \color{blue}{-\sqrt[3]{\frac{g}{a}} \cdot 1} \]
    6. Taylor expanded in g around 0

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
    7. Step-by-step derivation
      1. lift-cbrt.f64N/A

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
      2. lift-/.f6475.2

        \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
    8. Applied rewrites75.2%

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \]

    if 2e65 < (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a)) < 1.99999999999999993e118

    1. Initial program 16.7%

      \[\sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) + \sqrt{g \cdot g - h \cdot h}\right)} + \sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) - \sqrt{g \cdot g - h \cdot h}\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

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

        \[\leadsto \frac{\sqrt[3]{{a}^{2} \cdot \left(g + \sqrt{{g}^{2} - {h}^{2}}\right)} \cdot \sqrt[3]{\frac{-1}{2}} + \sqrt[3]{{a}^{2} \cdot \left(\sqrt{{g}^{2} - {h}^{2}} - g\right)} \cdot \sqrt[3]{\frac{1}{2}}}{\color{blue}{a}} \]
    5. Applied rewrites16.4%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\sqrt[3]{\left(\sqrt{\left(g + h\right) \cdot \left(g - h\right)} - g\right) \cdot \left(a \cdot a\right)}, \sqrt[3]{0.5}, \sqrt[3]{\left(\left(\sqrt{\left(g + h\right) \cdot \left(g - h\right)} + g\right) \cdot \left(a \cdot a\right)\right) \cdot -0.5}\right)}{a}} \]
    6. Taylor expanded in g around -inf

      \[\leadsto \frac{-1 \cdot \left(g \cdot \left(\sqrt[3]{\frac{{a}^{2} \cdot \left(1 + {\left(\sqrt{-1}\right)}^{2}\right)}{{g}^{2}}} \cdot \sqrt[3]{\frac{1}{2}} + \sqrt[3]{\frac{{a}^{2} \cdot \left({\left(\sqrt{-1}\right)}^{2} - 1\right)}{{g}^{2}}} \cdot \sqrt[3]{\frac{-1}{2}}\right)\right)}{a} \]
    7. Applied rewrites18.5%

      \[\leadsto \frac{-1 \cdot \left(g \cdot \mathsf{fma}\left(\sqrt[3]{\frac{\left(a \cdot a\right) \cdot 0}{g \cdot g}}, \sqrt[3]{0.5}, \sqrt[3]{\frac{\left(a \cdot a\right) \cdot -2}{g \cdot g} \cdot -0.5}\right)\right)}{a} \]
    8. Taylor expanded in g around 0

      \[\leadsto \frac{-1 \cdot \sqrt[3]{{a}^{2} \cdot g}}{a} \]
    9. Step-by-step derivation
      1. lower-cbrt.f64N/A

        \[\leadsto \frac{-1 \cdot \sqrt[3]{{a}^{2} \cdot g}}{a} \]
      2. lower-*.f64N/A

        \[\leadsto \frac{-1 \cdot \sqrt[3]{{a}^{2} \cdot g}}{a} \]
      3. pow2N/A

        \[\leadsto \frac{-1 \cdot \sqrt[3]{\left(a \cdot a\right) \cdot g}}{a} \]
      4. lift-*.f6495.5

        \[\leadsto \frac{-1 \cdot \sqrt[3]{\left(a \cdot a\right) \cdot g}}{a} \]
    10. Applied rewrites95.5%

      \[\leadsto \frac{-1 \cdot \sqrt[3]{\left(a \cdot a\right) \cdot g}}{a} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification76.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1}{2 \cdot a} \leq 2 \cdot 10^{+65} \lor \neg \left(\frac{1}{2 \cdot a} \leq 2 \cdot 10^{+118}\right):\\ \;\;\;\;-\sqrt[3]{\frac{g}{a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-\sqrt[3]{\left(a \cdot a\right) \cdot g}}{a}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 74.0% accurate, 2.6× speedup?

\[\begin{array}{l} \\ -\sqrt[3]{\frac{g}{a}} \end{array} \]
(FPCore (g h a) :precision binary64 (- (cbrt (/ g a))))
double code(double g, double h, double a) {
	return -cbrt((g / a));
}
public static double code(double g, double h, double a) {
	return -Math.cbrt((g / a));
}
function code(g, h, a)
	return Float64(-cbrt(Float64(g / a)))
end
code[g_, h_, a_] := (-N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision])
\begin{array}{l}

\\
-\sqrt[3]{\frac{g}{a}}
\end{array}
Derivation
  1. Initial program 45.0%

    \[\sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) + \sqrt{g \cdot g - h \cdot h}\right)} + \sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(-g\right) - \sqrt{g \cdot g - h \cdot h}\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in g around -inf

    \[\leadsto \color{blue}{-1 \cdot \left(\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right)\right)} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \mathsf{neg}\left(\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right)\right) \]
    2. lower-neg.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \left(\sqrt[3]{\frac{1}{2}} \cdot \sqrt[3]{2}\right) \]
    3. cbrt-unprodN/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \sqrt[3]{\frac{1}{2} \cdot 2} \]
    4. metadata-evalN/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot \sqrt[3]{1} \]
    5. metadata-evalN/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    6. lower-*.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    7. lower-cbrt.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
    8. lower-/.f6473.0

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \cdot 1 \]
  5. Applied rewrites73.0%

    \[\leadsto \color{blue}{-\sqrt[3]{\frac{g}{a}} \cdot 1} \]
  6. Taylor expanded in g around 0

    \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
  7. Step-by-step derivation
    1. lift-cbrt.f64N/A

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
    2. lift-/.f6473.0

      \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
  8. Applied rewrites73.0%

    \[\leadsto -\sqrt[3]{\frac{g}{a}} \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2025060 
(FPCore (g h a)
  :name "2-ancestry mixing, positive discriminant"
  :precision binary64
  (+ (cbrt (* (/ 1.0 (* 2.0 a)) (+ (- g) (sqrt (- (* g g) (* h h)))))) (cbrt (* (/ 1.0 (* 2.0 a)) (- (- g) (sqrt (- (* g g) (* h h))))))))