
(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 (/ 0.5 a)) (cbrt (* g -2.0))) (cbrt (* (- g g) (/ -0.5 a)))))
double code(double g, double h, double a) {
return (cbrt((0.5 / a)) * cbrt((g * -2.0))) + cbrt(((g - g) * (-0.5 / a)));
}
public static double code(double g, double h, double a) {
return (Math.cbrt((0.5 / a)) * Math.cbrt((g * -2.0))) + Math.cbrt(((g - g) * (-0.5 / a)));
}
function code(g, h, a) return Float64(Float64(cbrt(Float64(0.5 / a)) * cbrt(Float64(g * -2.0))) + cbrt(Float64(Float64(g - g) * Float64(-0.5 / a)))) end
code[g_, h_, a_] := N[(N[(N[Power[N[(0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(g * -2.0), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision] + N[Power[N[(N[(g - g), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{0.5}{a}} \cdot \sqrt[3]{g \cdot -2} + \sqrt[3]{\left(g - g\right) \cdot \frac{-0.5}{a}}
\end{array}
Initial program 42.6%
Simplified42.6%
Taylor expanded in g around -inf 24.8%
*-commutative24.8%
Simplified24.8%
Taylor expanded in g around -inf 73.0%
neg-mul-173.0%
Simplified73.0%
cbrt-prod96.0%
Applied egg-rr96.0%
Final simplification96.0%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (cbrt (* (- g g) (/ -0.5 a)))))
(if (<= a -2.9e-55)
(+ t_0 (cbrt (* (/ 0.5 a) (* g -2.0))))
(if (<= a 2.75e-24)
(+ (cbrt -1.0) (/ (cbrt (- g)) (cbrt a)))
(+ 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.9e-55) {
tmp = t_0 + cbrt(((0.5 / a) * (g * -2.0)));
} else if (a <= 2.75e-24) {
tmp = cbrt(-1.0) + (cbrt(-g) / cbrt(a));
} 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.9e-55) {
tmp = t_0 + Math.cbrt(((0.5 / a) * (g * -2.0)));
} else if (a <= 2.75e-24) {
tmp = Math.cbrt(-1.0) + (Math.cbrt(-g) / Math.cbrt(a));
} 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.9e-55) tmp = Float64(t_0 + cbrt(Float64(Float64(0.5 / a) * Float64(g * -2.0)))); elseif (a <= 2.75e-24) tmp = Float64(cbrt(-1.0) + Float64(cbrt(Float64(-g)) / cbrt(a))); 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.9e-55], N[(t$95$0 + N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g * -2.0), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], If[LessEqual[a, 2.75e-24], N[(N[Power[-1.0, 1/3], $MachinePrecision] + N[(N[Power[(-g), 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $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.9 \cdot 10^{-55}:\\
\;\;\;\;t\_0 + \sqrt[3]{\frac{0.5}{a} \cdot \left(g \cdot -2\right)}\\
\mathbf{elif}\;a \leq 2.75 \cdot 10^{-24}:\\
\;\;\;\;\sqrt[3]{-1} + \frac{\sqrt[3]{-g}}{\sqrt[3]{a}}\\
\mathbf{else}:\\
\;\;\;\;t\_0 + \sqrt[3]{-\frac{g}{a}}\\
\end{array}
\end{array}
if a < -2.9e-55Initial program 52.2%
Simplified52.2%
Taylor expanded in g around -inf 30.4%
*-commutative30.4%
Simplified30.4%
Taylor expanded in g around -inf 87.6%
neg-mul-187.6%
Simplified87.6%
if -2.9e-55 < a < 2.7499999999999999e-24Initial program 37.9%
Simplified37.9%
Taylor expanded in g around -inf 20.8%
*-commutative20.8%
Simplified20.8%
Taylor expanded in g around inf 12.1%
Taylor expanded in a around 0 12.1%
Simplified48.9%
add-sqr-sqrt21.2%
sqrt-unprod8.4%
*-commutative8.4%
*-commutative8.4%
swap-sqr5.6%
frac-times5.6%
metadata-eval5.6%
metadata-eval5.6%
frac-times5.6%
count-25.6%
count-25.6%
swap-sqr5.6%
metadata-eval5.6%
metadata-eval5.6%
swap-sqr5.6%
*-commutative5.6%
*-commutative5.6%
swap-sqr8.4%
sqrt-unprod21.2%
add-sqr-sqrt48.9%
Applied egg-rr91.5%
if 2.7499999999999999e-24 < a Initial program 40.2%
Simplified40.2%
Taylor expanded in g around -inf 25.6%
*-commutative25.6%
Simplified25.6%
Taylor expanded in g around -inf 91.2%
neg-mul-191.2%
Simplified91.2%
associate-*l/91.3%
*-commutative91.3%
associate-*r*91.3%
metadata-eval91.3%
neg-mul-191.3%
Applied egg-rr91.3%
Final simplification90.4%
(FPCore (g h a) :precision binary64 (if (or (<= a -1.05e-35) (not (<= a 4.2e-9))) (+ (cbrt (/ -0.25 (* a g))) (cbrt (* (/ -0.5 a) (+ g g)))) (- (cbrt (* (/ 0.5 a) (* g -2.0))) (cbrt -2.0))))
double code(double g, double h, double a) {
double tmp;
if ((a <= -1.05e-35) || !(a <= 4.2e-9)) {
tmp = cbrt((-0.25 / (a * g))) + cbrt(((-0.5 / a) * (g + g)));
} else {
tmp = cbrt(((0.5 / a) * (g * -2.0))) - cbrt(-2.0);
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if ((a <= -1.05e-35) || !(a <= 4.2e-9)) {
tmp = Math.cbrt((-0.25 / (a * g))) + Math.cbrt(((-0.5 / a) * (g + g)));
} else {
tmp = Math.cbrt(((0.5 / a) * (g * -2.0))) - Math.cbrt(-2.0);
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if ((a <= -1.05e-35) || !(a <= 4.2e-9)) tmp = Float64(cbrt(Float64(-0.25 / Float64(a * g))) + cbrt(Float64(Float64(-0.5 / a) * Float64(g + g)))); else tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(g * -2.0))) - cbrt(-2.0)); end return tmp end
code[g_, h_, a_] := If[Or[LessEqual[a, -1.05e-35], N[Not[LessEqual[a, 4.2e-9]], $MachinePrecision]], N[(N[Power[N[(-0.25 / N[(a * g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(N[(-0.5 / a), $MachinePrecision] * N[(g + g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g * -2.0), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] - N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1.05 \cdot 10^{-35} \lor \neg \left(a \leq 4.2 \cdot 10^{-9}\right):\\
\;\;\;\;\sqrt[3]{\frac{-0.25}{a \cdot g}} + \sqrt[3]{\frac{-0.5}{a} \cdot \left(g + g\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \left(g \cdot -2\right)} - \sqrt[3]{-2}\\
\end{array}
\end{array}
if a < -1.05e-35 or 4.20000000000000039e-9 < a Initial program 44.2%
Simplified44.2%
Taylor expanded in g around -inf 28.2%
*-commutative28.2%
Simplified28.2%
Taylor expanded in g around inf 17.8%
Applied egg-rr73.6%
if -1.05e-35 < a < 4.20000000000000039e-9Initial program 40.8%
Simplified40.8%
Taylor expanded in g around -inf 21.1%
*-commutative21.1%
Simplified21.1%
Taylor expanded in g around inf 12.4%
add-sqr-sqrt24.3%
sqrt-unprod10.1%
*-commutative10.1%
*-commutative10.1%
swap-sqr7.6%
frac-times7.6%
metadata-eval7.6%
metadata-eval7.6%
frac-times7.6%
count-27.6%
count-27.6%
swap-sqr7.6%
metadata-eval7.6%
metadata-eval7.6%
swap-sqr7.6%
*-commutative7.6%
*-commutative7.6%
swap-sqr10.1%
sqrt-unprod24.3%
add-sqr-sqrt50.0%
Applied egg-rr12.4%
Simplified50.2%
Final simplification62.3%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (cbrt (* (/ 0.5 a) (* g -2.0)))))
(if (or (<= a -0.25) (not (<= a 0.135)))
(- t_0 (cbrt (/ 0.5 a)))
(- t_0 (cbrt -2.0)))))
double code(double g, double h, double a) {
double t_0 = cbrt(((0.5 / a) * (g * -2.0)));
double tmp;
if ((a <= -0.25) || !(a <= 0.135)) {
tmp = t_0 - cbrt((0.5 / a));
} else {
tmp = t_0 - cbrt(-2.0);
}
return tmp;
}
public static double code(double g, double h, double a) {
double t_0 = Math.cbrt(((0.5 / a) * (g * -2.0)));
double tmp;
if ((a <= -0.25) || !(a <= 0.135)) {
tmp = t_0 - Math.cbrt((0.5 / a));
} else {
tmp = t_0 - Math.cbrt(-2.0);
}
return tmp;
}
function code(g, h, a) t_0 = cbrt(Float64(Float64(0.5 / a) * Float64(g * -2.0))) tmp = 0.0 if ((a <= -0.25) || !(a <= 0.135)) tmp = Float64(t_0 - cbrt(Float64(0.5 / a))); else tmp = Float64(t_0 - cbrt(-2.0)); end return tmp end
code[g_, h_, a_] := Block[{t$95$0 = N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g * -2.0), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]}, If[Or[LessEqual[a, -0.25], N[Not[LessEqual[a, 0.135]], $MachinePrecision]], N[(t$95$0 - N[Power[N[(0.5 / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], N[(t$95$0 - N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\frac{0.5}{a} \cdot \left(g \cdot -2\right)}\\
\mathbf{if}\;a \leq -0.25 \lor \neg \left(a \leq 0.135\right):\\
\;\;\;\;t\_0 - \sqrt[3]{\frac{0.5}{a}}\\
\mathbf{else}:\\
\;\;\;\;t\_0 - \sqrt[3]{-2}\\
\end{array}
\end{array}
if a < -0.25 or 0.13500000000000001 < a Initial program 44.1%
Simplified44.1%
Taylor expanded in g around -inf 28.2%
*-commutative28.2%
Simplified28.2%
Taylor expanded in g around inf 17.7%
Applied egg-rr0.0%
Simplified71.3%
if -0.25 < a < 0.13500000000000001Initial program 41.1%
Simplified41.1%
Taylor expanded in g around -inf 21.3%
*-commutative21.3%
Simplified21.3%
Taylor expanded in g around inf 12.6%
add-sqr-sqrt24.6%
sqrt-unprod10.7%
*-commutative10.7%
*-commutative10.7%
swap-sqr8.2%
frac-times8.2%
metadata-eval8.2%
metadata-eval8.2%
frac-times8.2%
count-28.2%
count-28.2%
swap-sqr8.2%
metadata-eval8.2%
metadata-eval8.2%
swap-sqr8.2%
*-commutative8.2%
*-commutative8.2%
swap-sqr10.7%
sqrt-unprod24.6%
add-sqr-sqrt50.0%
Applied egg-rr12.6%
Simplified50.1%
Final simplification60.7%
(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 42.6%
Simplified42.6%
Taylor expanded in g around -inf 24.8%
*-commutative24.8%
Simplified24.8%
Taylor expanded in g around -inf 73.0%
neg-mul-173.0%
Simplified73.0%
associate-*l/73.0%
*-commutative73.0%
associate-*r*73.0%
metadata-eval73.0%
neg-mul-173.0%
Applied egg-rr73.0%
Final simplification73.0%
(FPCore (g h a) :precision binary64 (+ (cbrt -1.0) (cbrt (- (/ g a)))))
double code(double g, double h, double a) {
return cbrt(-1.0) + cbrt(-(g / a));
}
public static double code(double g, double h, double a) {
return Math.cbrt(-1.0) + Math.cbrt(-(g / a));
}
function code(g, h, a) return Float64(cbrt(-1.0) + cbrt(Float64(-Float64(g / a)))) end
code[g_, h_, a_] := N[(N[Power[-1.0, 1/3], $MachinePrecision] + N[Power[(-N[(g / a), $MachinePrecision]), 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{-1} + \sqrt[3]{-\frac{g}{a}}
\end{array}
Initial program 42.6%
Simplified42.6%
Taylor expanded in g around -inf 24.8%
*-commutative24.8%
Simplified24.8%
Taylor expanded in g around inf 15.2%
Taylor expanded in a around 0 15.2%
Simplified46.2%
Taylor expanded in g around 0 46.2%
neg-mul-146.2%
distribute-neg-frac46.2%
Simplified46.2%
Final simplification46.2%
(FPCore (g h a) :precision binary64 (- (cbrt -1.0) (cbrt -2.0)))
double code(double g, double h, double a) {
return cbrt(-1.0) - cbrt(-2.0);
}
public static double code(double g, double h, double a) {
return Math.cbrt(-1.0) - Math.cbrt(-2.0);
}
function code(g, h, a) return Float64(cbrt(-1.0) - cbrt(-2.0)) end
code[g_, h_, a_] := N[(N[Power[-1.0, 1/3], $MachinePrecision] - N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{-1} - \sqrt[3]{-2}
\end{array}
Initial program 42.6%
Simplified42.6%
Taylor expanded in g around -inf 24.8%
*-commutative24.8%
Simplified24.8%
Taylor expanded in g around inf 15.2%
Taylor expanded in a around 0 15.2%
Simplified46.2%
add-sqr-sqrt22.6%
sqrt-unprod14.1%
*-commutative14.1%
*-commutative14.1%
swap-sqr6.4%
frac-times6.4%
metadata-eval6.4%
metadata-eval6.4%
frac-times6.4%
count-26.4%
count-26.4%
swap-sqr6.4%
metadata-eval6.4%
metadata-eval6.4%
swap-sqr6.4%
*-commutative6.4%
*-commutative6.4%
swap-sqr14.1%
sqrt-unprod22.6%
add-sqr-sqrt46.2%
Applied egg-rr45.8%
Simplified4.5%
Final simplification4.5%
herbie shell --seed 2024034
(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))))))))