
(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:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (g h a)
:precision binary64
(if (<= (* h h) 4e+247)
(+
(cbrt (* (/ 0.5 a) (/ (* -0.5 (pow h 2.0)) g)))
(* (cbrt (/ -0.5 a)) (cbrt (* 2.0 g))))
(fma
(/ (cbrt g) (cbrt a))
(* (cbrt -0.5) (sqrt (cbrt 4.0)))
(* (cbrt (/ (pow h -2.0) (* a g))) (* (cbrt -0.5) (cbrt 0.5))))))
double code(double g, double h, double a) {
double tmp;
if ((h * h) <= 4e+247) {
tmp = cbrt(((0.5 / a) * ((-0.5 * pow(h, 2.0)) / g))) + (cbrt((-0.5 / a)) * cbrt((2.0 * g)));
} else {
tmp = fma((cbrt(g) / cbrt(a)), (cbrt(-0.5) * sqrt(cbrt(4.0))), (cbrt((pow(h, -2.0) / (a * g))) * (cbrt(-0.5) * cbrt(0.5))));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if (Float64(h * h) <= 4e+247) tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(Float64(-0.5 * (h ^ 2.0)) / g))) + Float64(cbrt(Float64(-0.5 / a)) * cbrt(Float64(2.0 * g)))); else tmp = fma(Float64(cbrt(g) / cbrt(a)), Float64(cbrt(-0.5) * sqrt(cbrt(4.0))), Float64(cbrt(Float64((h ^ -2.0) / Float64(a * g))) * Float64(cbrt(-0.5) * cbrt(0.5)))); end return tmp end
code[g_, h_, a_] := If[LessEqual[N[(h * h), $MachinePrecision], 4e+247], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(N[(-0.5 * N[Power[h, 2.0], $MachinePrecision]), $MachinePrecision] / g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[N[(-0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(2.0 * g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[g, 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[(N[Power[-0.5, 1/3], $MachinePrecision] * N[Sqrt[N[Power[4.0, 1/3], $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Power[N[(N[Power[h, -2.0], $MachinePrecision] / N[(a * g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] * N[(N[Power[-0.5, 1/3], $MachinePrecision] * N[Power[0.5, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;h \cdot h \leq 4 \cdot 10^{+247}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \frac{-0.5 \cdot {h}^{2}}{g}} + \sqrt[3]{\frac{-0.5}{a}} \cdot \sqrt[3]{2 \cdot g}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\sqrt[3]{g}}{\sqrt[3]{a}}, \sqrt[3]{-0.5} \cdot \sqrt{\sqrt[3]{4}}, \sqrt[3]{\frac{{h}^{-2}}{a \cdot g}} \cdot \left(\sqrt[3]{-0.5} \cdot \sqrt[3]{0.5}\right)\right)\\
\end{array}
\end{array}
if (*.f64 h h) < 3.99999999999999981e247Initial program 48.5%
Simplified48.5%
Taylor expanded in g around inf 29.5%
Taylor expanded in g around inf 77.3%
associate-*r/77.3%
Simplified77.3%
*-commutative77.3%
cbrt-prod97.2%
count-297.2%
Applied egg-rr97.2%
if 3.99999999999999981e247 < (*.f64 h h) Initial program 0.2%
Simplified0.2%
Taylor expanded in h around 0 12.6%
fma-define12.6%
*-commutative12.6%
*-commutative12.6%
Simplified12.6%
Applied egg-rr19.5%
Applied egg-rr19.8%
Applied egg-rr88.5%
rem-cube-cbrt88.5%
associate-/l/88.5%
Simplified88.5%
Final simplification96.2%
(FPCore (g h a)
:precision binary64
(if (<= (* h h) 4e+247)
(+
(cbrt (* (/ 0.5 a) (/ (* -0.5 (pow h 2.0)) g)))
(* (cbrt (/ -0.5 a)) (cbrt (* 2.0 g))))
(fma
(/ (cbrt g) (cbrt a))
(* (cbrt -0.5) (cbrt 2.0))
(* (cbrt (/ (pow h -2.0) (* a g))) (* (cbrt -0.5) (cbrt 0.5))))))
double code(double g, double h, double a) {
double tmp;
if ((h * h) <= 4e+247) {
tmp = cbrt(((0.5 / a) * ((-0.5 * pow(h, 2.0)) / g))) + (cbrt((-0.5 / a)) * cbrt((2.0 * g)));
} else {
tmp = fma((cbrt(g) / cbrt(a)), (cbrt(-0.5) * cbrt(2.0)), (cbrt((pow(h, -2.0) / (a * g))) * (cbrt(-0.5) * cbrt(0.5))));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if (Float64(h * h) <= 4e+247) tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(Float64(-0.5 * (h ^ 2.0)) / g))) + Float64(cbrt(Float64(-0.5 / a)) * cbrt(Float64(2.0 * g)))); else tmp = fma(Float64(cbrt(g) / cbrt(a)), Float64(cbrt(-0.5) * cbrt(2.0)), Float64(cbrt(Float64((h ^ -2.0) / Float64(a * g))) * Float64(cbrt(-0.5) * cbrt(0.5)))); end return tmp end
code[g_, h_, a_] := If[LessEqual[N[(h * h), $MachinePrecision], 4e+247], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(N[(-0.5 * N[Power[h, 2.0], $MachinePrecision]), $MachinePrecision] / g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[N[(-0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(2.0 * g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[g, 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[(N[Power[-0.5, 1/3], $MachinePrecision] * N[Power[2.0, 1/3], $MachinePrecision]), $MachinePrecision] + N[(N[Power[N[(N[Power[h, -2.0], $MachinePrecision] / N[(a * g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] * N[(N[Power[-0.5, 1/3], $MachinePrecision] * N[Power[0.5, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;h \cdot h \leq 4 \cdot 10^{+247}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \frac{-0.5 \cdot {h}^{2}}{g}} + \sqrt[3]{\frac{-0.5}{a}} \cdot \sqrt[3]{2 \cdot g}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\sqrt[3]{g}}{\sqrt[3]{a}}, \sqrt[3]{-0.5} \cdot \sqrt[3]{2}, \sqrt[3]{\frac{{h}^{-2}}{a \cdot g}} \cdot \left(\sqrt[3]{-0.5} \cdot \sqrt[3]{0.5}\right)\right)\\
\end{array}
\end{array}
if (*.f64 h h) < 3.99999999999999981e247Initial program 48.5%
Simplified48.5%
Taylor expanded in g around inf 29.5%
Taylor expanded in g around inf 77.3%
associate-*r/77.3%
Simplified77.3%
*-commutative77.3%
cbrt-prod97.2%
count-297.2%
Applied egg-rr97.2%
if 3.99999999999999981e247 < (*.f64 h h) Initial program 0.2%
Simplified0.2%
Taylor expanded in h around 0 12.6%
fma-define12.6%
*-commutative12.6%
*-commutative12.6%
Simplified12.6%
Applied egg-rr19.5%
Applied egg-rr87.7%
rem-cube-cbrt88.5%
associate-/l/88.5%
Simplified87.7%
Final simplification96.2%
(FPCore (g h a)
:precision binary64
(if (<= h 1.35e+154)
(+
(cbrt (* (/ 0.5 a) (/ (* -0.5 (pow h 2.0)) g)))
(* (cbrt (/ -0.5 a)) (cbrt (* 2.0 g))))
(fma
(/ 1.0 (cbrt (/ a g)))
(* (cbrt -0.5) (cbrt 2.0))
(* (cbrt (/ (pow h -2.0) (* a g))) (* (cbrt -0.5) (cbrt 0.5))))))
double code(double g, double h, double a) {
double tmp;
if (h <= 1.35e+154) {
tmp = cbrt(((0.5 / a) * ((-0.5 * pow(h, 2.0)) / g))) + (cbrt((-0.5 / a)) * cbrt((2.0 * g)));
} else {
tmp = fma((1.0 / cbrt((a / g))), (cbrt(-0.5) * cbrt(2.0)), (cbrt((pow(h, -2.0) / (a * g))) * (cbrt(-0.5) * cbrt(0.5))));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if (h <= 1.35e+154) tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(Float64(-0.5 * (h ^ 2.0)) / g))) + Float64(cbrt(Float64(-0.5 / a)) * cbrt(Float64(2.0 * g)))); else tmp = fma(Float64(1.0 / cbrt(Float64(a / g))), Float64(cbrt(-0.5) * cbrt(2.0)), Float64(cbrt(Float64((h ^ -2.0) / Float64(a * g))) * Float64(cbrt(-0.5) * cbrt(0.5)))); end return tmp end
code[g_, h_, a_] := If[LessEqual[h, 1.35e+154], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(N[(-0.5 * N[Power[h, 2.0], $MachinePrecision]), $MachinePrecision] / g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[N[(-0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(2.0 * g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / N[Power[N[(a / g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision] * N[(N[Power[-0.5, 1/3], $MachinePrecision] * N[Power[2.0, 1/3], $MachinePrecision]), $MachinePrecision] + N[(N[Power[N[(N[Power[h, -2.0], $MachinePrecision] / N[(a * g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] * N[(N[Power[-0.5, 1/3], $MachinePrecision] * N[Power[0.5, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;h \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \frac{-0.5 \cdot {h}^{2}}{g}} + \sqrt[3]{\frac{-0.5}{a}} \cdot \sqrt[3]{2 \cdot g}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{\sqrt[3]{\frac{a}{g}}}, \sqrt[3]{-0.5} \cdot \sqrt[3]{2}, \sqrt[3]{\frac{{h}^{-2}}{a \cdot g}} \cdot \left(\sqrt[3]{-0.5} \cdot \sqrt[3]{0.5}\right)\right)\\
\end{array}
\end{array}
if h < 1.35000000000000003e154Initial program 45.3%
Simplified45.3%
Taylor expanded in g around inf 27.7%
Taylor expanded in g around inf 73.7%
associate-*r/73.7%
Simplified73.7%
*-commutative73.7%
cbrt-prod93.0%
count-293.0%
Applied egg-rr93.0%
if 1.35000000000000003e154 < h Initial program 0.0%
Simplified0.0%
Taylor expanded in h around 0 1.4%
fma-define1.4%
*-commutative1.4%
*-commutative1.4%
Simplified1.4%
Applied egg-rr1.4%
clear-num1.4%
inv-pow1.4%
cbrt-undiv1.4%
Applied egg-rr1.4%
Simplified1.4%
Applied egg-rr83.0%
rem-cube-cbrt83.4%
associate-/l/83.4%
Simplified83.0%
Final simplification92.6%
(FPCore (g h a)
:precision binary64
(if (<= h 1.35e+154)
(+
(cbrt (* (/ 0.5 a) (/ (* -0.5 (pow h 2.0)) g)))
(* (cbrt (/ -0.5 a)) (cbrt (* 2.0 g))))
(- (cbrt (/ g a)))))
double code(double g, double h, double a) {
double tmp;
if (h <= 1.35e+154) {
tmp = cbrt(((0.5 / a) * ((-0.5 * pow(h, 2.0)) / g))) + (cbrt((-0.5 / a)) * cbrt((2.0 * g)));
} else {
tmp = -cbrt((g / a));
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if (h <= 1.35e+154) {
tmp = Math.cbrt(((0.5 / a) * ((-0.5 * Math.pow(h, 2.0)) / g))) + (Math.cbrt((-0.5 / a)) * Math.cbrt((2.0 * g)));
} else {
tmp = -Math.cbrt((g / a));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if (h <= 1.35e+154) tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(Float64(-0.5 * (h ^ 2.0)) / g))) + Float64(cbrt(Float64(-0.5 / a)) * cbrt(Float64(2.0 * g)))); else tmp = Float64(-cbrt(Float64(g / a))); end return tmp end
code[g_, h_, a_] := If[LessEqual[h, 1.35e+154], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(N[(-0.5 * N[Power[h, 2.0], $MachinePrecision]), $MachinePrecision] / g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[N[(-0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(2.0 * g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], (-N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;h \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \frac{-0.5 \cdot {h}^{2}}{g}} + \sqrt[3]{\frac{-0.5}{a}} \cdot \sqrt[3]{2 \cdot g}\\
\mathbf{else}:\\
\;\;\;\;-\sqrt[3]{\frac{g}{a}}\\
\end{array}
\end{array}
if h < 1.35000000000000003e154Initial program 45.3%
Simplified45.3%
Taylor expanded in g around inf 27.7%
Taylor expanded in g around inf 73.7%
associate-*r/73.7%
Simplified73.7%
*-commutative73.7%
cbrt-prod93.0%
count-293.0%
Applied egg-rr93.0%
if 1.35000000000000003e154 < h Initial program 0.0%
Simplified0.0%
Taylor expanded in h around 0 1.4%
fma-define1.4%
*-commutative1.4%
*-commutative1.4%
Simplified1.4%
Applied egg-rr1.4%
add-cbrt-cube1.4%
pow31.4%
Applied egg-rr1.4%
Taylor expanded in g around -inf 81.0%
mul-1-neg81.0%
Simplified81.0%
(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}
Initial program 43.4%
Simplified43.4%
Taylor expanded in h around 0 67.1%
fma-define67.1%
*-commutative67.1%
*-commutative67.1%
Simplified67.1%
Applied egg-rr85.5%
add-cbrt-cube67.0%
pow367.0%
Applied egg-rr67.6%
Taylor expanded in g around -inf 75.9%
mul-1-neg75.9%
Simplified75.9%
herbie shell --seed 2024144
(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))))))))