
(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 7 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 (+ (/ (cbrt (- g)) (cbrt a)) (cbrt (* (- g g) (/ -0.5 a)))))
double code(double g, double h, double a) {
return (cbrt(-g) / cbrt(a)) + cbrt(((g - g) * (-0.5 / a)));
}
public static double code(double g, double h, double a) {
return (Math.cbrt(-g) / Math.cbrt(a)) + Math.cbrt(((g - g) * (-0.5 / a)));
}
function code(g, h, a) return Float64(Float64(cbrt(Float64(-g)) / cbrt(a)) + cbrt(Float64(Float64(g - g) * Float64(-0.5 / a)))) end
code[g_, h_, a_] := N[(N[(N[Power[(-g), 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] + N[Power[N[(N[(g - g), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sqrt[3]{-g}}{\sqrt[3]{a}} + \sqrt[3]{\left(g - g\right) \cdot \frac{-0.5}{a}}
\end{array}
Initial program 39.1%
Simplified39.1%
Taylor expanded in g around -inf 25.8%
*-commutative25.8%
Simplified25.8%
Taylor expanded in g around -inf 74.7%
neg-mul-174.7%
Simplified74.7%
associate-*l/74.8%
cbrt-div95.7%
Applied egg-rr95.7%
*-commutative95.7%
associate-*l*95.7%
metadata-eval95.7%
*-commutative95.7%
mul-1-neg95.7%
Simplified95.7%
Final simplification95.7%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (cbrt (* (- g g) (/ -0.5 a)))))
(if (<= a -2.4e-75)
(+ t_0 (cbrt (/ 1.0 (/ a (- g)))))
(if (<= a 6.1e-77)
(+ (/ (cbrt (- g)) (cbrt a)) (cbrt -2.0))
(+ t_0 (cbrt (/ (- g) a)))))))
double code(double g, double h, double a) {
double t_0 = cbrt(((g - g) * (-0.5 / a)));
double tmp;
if (a <= -2.4e-75) {
tmp = t_0 + cbrt((1.0 / (a / -g)));
} else if (a <= 6.1e-77) {
tmp = (cbrt(-g) / cbrt(a)) + cbrt(-2.0);
} else {
tmp = t_0 + cbrt((-g / a));
}
return tmp;
}
public static double code(double g, double h, double a) {
double t_0 = Math.cbrt(((g - g) * (-0.5 / a)));
double tmp;
if (a <= -2.4e-75) {
tmp = t_0 + Math.cbrt((1.0 / (a / -g)));
} else if (a <= 6.1e-77) {
tmp = (Math.cbrt(-g) / Math.cbrt(a)) + Math.cbrt(-2.0);
} else {
tmp = t_0 + Math.cbrt((-g / a));
}
return tmp;
}
function code(g, h, a) t_0 = cbrt(Float64(Float64(g - g) * Float64(-0.5 / a))) tmp = 0.0 if (a <= -2.4e-75) tmp = Float64(t_0 + cbrt(Float64(1.0 / Float64(a / Float64(-g))))); elseif (a <= 6.1e-77) tmp = Float64(Float64(cbrt(Float64(-g)) / cbrt(a)) + cbrt(-2.0)); else tmp = Float64(t_0 + cbrt(Float64(Float64(-g) / a))); end return tmp end
code[g_, h_, a_] := Block[{t$95$0 = N[Power[N[(N[(g - g), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]}, If[LessEqual[a, -2.4e-75], N[(t$95$0 + N[Power[N[(1.0 / N[(a / (-g)), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], If[LessEqual[a, 6.1e-77], N[(N[(N[Power[(-g), 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] + N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision], N[(t$95$0 + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\left(g - g\right) \cdot \frac{-0.5}{a}}\\
\mathbf{if}\;a \leq -2.4 \cdot 10^{-75}:\\
\;\;\;\;t_0 + \sqrt[3]{\frac{1}{\frac{a}{-g}}}\\
\mathbf{elif}\;a \leq 6.1 \cdot 10^{-77}:\\
\;\;\;\;\frac{\sqrt[3]{-g}}{\sqrt[3]{a}} + \sqrt[3]{-2}\\
\mathbf{else}:\\
\;\;\;\;t_0 + \sqrt[3]{\frac{-g}{a}}\\
\end{array}
\end{array}
if a < -2.40000000000000019e-75Initial program 40.3%
Simplified40.3%
Taylor expanded in g around -inf 28.2%
*-commutative28.2%
Simplified28.2%
Taylor expanded in g around -inf 89.4%
neg-mul-189.4%
Simplified89.4%
associate-*l/89.5%
clear-num89.7%
*-commutative89.7%
associate-*r*89.7%
metadata-eval89.7%
neg-mul-189.7%
Applied egg-rr89.7%
if -2.40000000000000019e-75 < a < 6.1000000000000002e-77Initial program 35.3%
Simplified35.3%
Taylor expanded in g around -inf 24.7%
*-commutative24.7%
Simplified24.7%
Taylor expanded in g around inf 11.4%
add-sqr-sqrt4.7%
sqrt-unprod3.3%
swap-sqr5.1%
frac-times5.1%
metadata-eval5.1%
metadata-eval5.1%
frac-times5.1%
*-commutative5.1%
*-commutative5.1%
swap-sqr5.1%
metadata-eval5.1%
metadata-eval5.1%
swap-sqr5.1%
count-25.1%
count-25.1%
swap-sqr3.3%
*-commutative3.3%
*-commutative3.3%
sqrt-unprod4.7%
add-sqr-sqrt11.4%
expm1-log1p-u5.1%
Applied egg-rr0.0%
Simplified41.1%
add-sqr-sqrt16.3%
sqrt-unprod5.8%
*-commutative5.8%
*-commutative5.8%
swap-sqr2.8%
frac-times2.8%
metadata-eval2.8%
metadata-eval2.8%
frac-times2.8%
count-22.8%
count-22.8%
swap-sqr2.8%
metadata-eval2.8%
metadata-eval2.8%
swap-sqr2.8%
*-commutative2.8%
*-commutative2.8%
swap-sqr5.8%
sqrt-unprod16.3%
Applied egg-rr89.6%
if 6.1000000000000002e-77 < a Initial program 42.0%
Simplified42.0%
Taylor expanded in g around -inf 24.3%
*-commutative24.3%
Simplified24.3%
Taylor expanded in g around -inf 88.6%
neg-mul-188.6%
Simplified88.6%
associate-*l/88.8%
*-commutative88.8%
associate-*r*88.8%
metadata-eval88.8%
neg-mul-188.8%
Applied egg-rr88.8%
Final simplification89.4%
(FPCore (g h a) :precision binary64 (if (or (<= g -5.2e+24) (not (<= g 1.1e+16))) (+ (cbrt (* (/ 0.5 a) (* g -2.0))) (/ -2.0 (cbrt a))) (+ (cbrt g) (cbrt (* (/ -0.5 a) (+ g g))))))
double code(double g, double h, double a) {
double tmp;
if ((g <= -5.2e+24) || !(g <= 1.1e+16)) {
tmp = cbrt(((0.5 / a) * (g * -2.0))) + (-2.0 / cbrt(a));
} else {
tmp = cbrt(g) + cbrt(((-0.5 / a) * (g + g)));
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if ((g <= -5.2e+24) || !(g <= 1.1e+16)) {
tmp = Math.cbrt(((0.5 / a) * (g * -2.0))) + (-2.0 / Math.cbrt(a));
} else {
tmp = Math.cbrt(g) + Math.cbrt(((-0.5 / a) * (g + g)));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if ((g <= -5.2e+24) || !(g <= 1.1e+16)) tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(g * -2.0))) + Float64(-2.0 / cbrt(a))); else tmp = Float64(cbrt(g) + cbrt(Float64(Float64(-0.5 / a) * Float64(g + g)))); end return tmp end
code[g_, h_, a_] := If[Or[LessEqual[g, -5.2e+24], N[Not[LessEqual[g, 1.1e+16]], $MachinePrecision]], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g * -2.0), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(-2.0 / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[g, 1/3], $MachinePrecision] + N[Power[N[(N[(-0.5 / a), $MachinePrecision] * N[(g + g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;g \leq -5.2 \cdot 10^{+24} \lor \neg \left(g \leq 1.1 \cdot 10^{+16}\right):\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \left(g \cdot -2\right)} + \frac{-2}{\sqrt[3]{a}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{g} + \sqrt[3]{\frac{-0.5}{a} \cdot \left(g + g\right)}\\
\end{array}
\end{array}
if g < -5.1999999999999997e24 or 1.1e16 < g Initial program 27.6%
Simplified27.6%
Taylor expanded in g around -inf 18.3%
*-commutative18.3%
Simplified18.3%
Taylor expanded in g around inf 15.0%
expm1-log1p-u9.1%
expm1-udef21.5%
Applied egg-rr0.0%
Simplified70.1%
if -5.1999999999999997e24 < g < 1.1e16Initial program 71.0%
Simplified71.0%
Taylor expanded in g around -inf 46.5%
*-commutative46.5%
Simplified46.5%
Taylor expanded in g around inf 16.7%
Taylor expanded in a around 0 16.7%
Simplified57.1%
Final simplification66.7%
(FPCore (g h a) :precision binary64 (if (or (<= g -1.75e+21) (not (<= g 0.00034))) (+ (cbrt -2.0) (cbrt (/ (- g) a))) (+ (cbrt g) (cbrt (* (/ -0.5 a) (+ g g))))))
double code(double g, double h, double a) {
double tmp;
if ((g <= -1.75e+21) || !(g <= 0.00034)) {
tmp = cbrt(-2.0) + cbrt((-g / a));
} else {
tmp = cbrt(g) + cbrt(((-0.5 / a) * (g + g)));
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if ((g <= -1.75e+21) || !(g <= 0.00034)) {
tmp = Math.cbrt(-2.0) + Math.cbrt((-g / a));
} else {
tmp = Math.cbrt(g) + Math.cbrt(((-0.5 / a) * (g + g)));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if ((g <= -1.75e+21) || !(g <= 0.00034)) tmp = Float64(cbrt(-2.0) + cbrt(Float64(Float64(-g) / a))); else tmp = Float64(cbrt(g) + cbrt(Float64(Float64(-0.5 / a) * Float64(g + g)))); end return tmp end
code[g_, h_, a_] := If[Or[LessEqual[g, -1.75e+21], N[Not[LessEqual[g, 0.00034]], $MachinePrecision]], N[(N[Power[-2.0, 1/3], $MachinePrecision] + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], N[(N[Power[g, 1/3], $MachinePrecision] + N[Power[N[(N[(-0.5 / a), $MachinePrecision] * N[(g + g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;g \leq -1.75 \cdot 10^{+21} \lor \neg \left(g \leq 0.00034\right):\\
\;\;\;\;\sqrt[3]{-2} + \sqrt[3]{\frac{-g}{a}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{g} + \sqrt[3]{\frac{-0.5}{a} \cdot \left(g + g\right)}\\
\end{array}
\end{array}
if g < -1.75e21 or 3.4e-4 < g Initial program 27.8%
Simplified27.8%
Taylor expanded in g around -inf 18.3%
*-commutative18.3%
Simplified18.3%
Taylor expanded in g around inf 15.1%
add-sqr-sqrt6.9%
sqrt-unprod10.5%
swap-sqr11.0%
frac-times11.6%
metadata-eval11.6%
metadata-eval11.6%
frac-times11.0%
*-commutative11.0%
*-commutative11.0%
swap-sqr11.0%
metadata-eval11.0%
metadata-eval11.0%
swap-sqr11.0%
count-211.0%
count-211.0%
swap-sqr10.5%
*-commutative10.5%
*-commutative10.5%
sqrt-unprod6.9%
add-sqr-sqrt15.1%
expm1-log1p-u9.1%
Applied egg-rr0.0%
Simplified48.5%
Taylor expanded in g around 0 48.6%
associate-*r/48.6%
mul-1-neg48.6%
Simplified48.6%
if -1.75e21 < g < 3.4e-4Initial program 70.9%
Simplified70.9%
Taylor expanded in g around -inf 46.9%
*-commutative46.9%
Simplified46.9%
Taylor expanded in g around inf 16.6%
Taylor expanded in a around 0 16.6%
Simplified56.8%
Final simplification50.8%
(FPCore (g h a) :precision binary64 (+ (cbrt (* (- g g) (/ -0.5 a))) (cbrt (/ (- g) a))))
double code(double g, double h, double a) {
return cbrt(((g - g) * (-0.5 / a))) + cbrt((-g / a));
}
public static double code(double g, double h, double a) {
return Math.cbrt(((g - g) * (-0.5 / a))) + Math.cbrt((-g / a));
}
function code(g, h, a) return Float64(cbrt(Float64(Float64(g - g) * Float64(-0.5 / a))) + cbrt(Float64(Float64(-g) / a))) end
code[g_, h_, a_] := N[(N[Power[N[(N[(g - g), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\left(g - g\right) \cdot \frac{-0.5}{a}} + \sqrt[3]{\frac{-g}{a}}
\end{array}
Initial program 39.1%
Simplified39.1%
Taylor expanded in g around -inf 25.8%
*-commutative25.8%
Simplified25.8%
Taylor expanded in g around -inf 74.7%
neg-mul-174.7%
Simplified74.7%
associate-*l/74.8%
*-commutative74.8%
associate-*r*74.8%
metadata-eval74.8%
neg-mul-174.8%
Applied egg-rr74.8%
Final simplification74.8%
(FPCore (g h a) :precision binary64 (+ (cbrt -2.0) (cbrt (/ (- g) a))))
double code(double g, double h, double a) {
return cbrt(-2.0) + cbrt((-g / a));
}
public static double code(double g, double h, double a) {
return Math.cbrt(-2.0) + Math.cbrt((-g / a));
}
function code(g, h, a) return Float64(cbrt(-2.0) + cbrt(Float64(Float64(-g) / a))) end
code[g_, h_, a_] := N[(N[Power[-2.0, 1/3], $MachinePrecision] + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{-2} + \sqrt[3]{\frac{-g}{a}}
\end{array}
Initial program 39.1%
Simplified39.1%
Taylor expanded in g around -inf 25.8%
*-commutative25.8%
Simplified25.8%
Taylor expanded in g around inf 15.5%
add-sqr-sqrt6.8%
sqrt-unprod13.4%
swap-sqr14.8%
frac-times15.2%
metadata-eval15.2%
metadata-eval15.2%
frac-times14.8%
*-commutative14.8%
*-commutative14.8%
swap-sqr14.8%
metadata-eval14.8%
metadata-eval14.8%
swap-sqr14.8%
count-214.8%
count-214.8%
swap-sqr13.4%
*-commutative13.4%
*-commutative13.4%
sqrt-unprod6.8%
add-sqr-sqrt15.5%
expm1-log1p-u9.9%
Applied egg-rr0.0%
Simplified45.0%
Taylor expanded in g around 0 45.0%
associate-*r/45.0%
mul-1-neg45.0%
Simplified45.0%
Final simplification45.0%
(FPCore (g h a) :precision binary64 (cbrt -2.0))
double code(double g, double h, double a) {
return cbrt(-2.0);
}
public static double code(double g, double h, double a) {
return Math.cbrt(-2.0);
}
function code(g, h, a) return cbrt(-2.0) end
code[g_, h_, a_] := N[Power[-2.0, 1/3], $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{-2}
\end{array}
Initial program 39.1%
Simplified39.1%
Taylor expanded in g around -inf 25.8%
*-commutative25.8%
Simplified25.8%
Taylor expanded in g around inf 15.5%
add-sqr-sqrt6.8%
sqrt-unprod13.4%
swap-sqr14.8%
frac-times15.2%
metadata-eval15.2%
metadata-eval15.2%
frac-times14.8%
*-commutative14.8%
*-commutative14.8%
swap-sqr14.8%
metadata-eval14.8%
metadata-eval14.8%
swap-sqr14.8%
count-214.8%
count-214.8%
swap-sqr13.4%
*-commutative13.4%
*-commutative13.4%
sqrt-unprod6.8%
add-sqr-sqrt15.5%
expm1-log1p-u9.9%
Applied egg-rr0.0%
Simplified45.0%
Taylor expanded in g around 0 4.9%
Final simplification4.9%
herbie shell --seed 2024015
(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))))))))