
(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 (+ (/ (cbrt (- g)) (cbrt a)) (cbrt 0.0)))
double code(double g, double h, double a) {
return (cbrt(-g) / cbrt(a)) + cbrt(0.0);
}
public static double code(double g, double h, double a) {
return (Math.cbrt(-g) / Math.cbrt(a)) + Math.cbrt(0.0);
}
function code(g, h, a) return Float64(Float64(cbrt(Float64(-g)) / cbrt(a)) + cbrt(0.0)) end
code[g_, h_, a_] := N[(N[(N[Power[(-g), 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] + N[Power[0.0, 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sqrt[3]{-g}}{\sqrt[3]{a}} + \sqrt[3]{0}
\end{array}
Initial program 46.5%
Simplified46.5%
Taylor expanded in g around -inf 28.1%
*-commutative28.1%
Simplified28.1%
Taylor expanded in g around inf 15.7%
associate-*l/15.7%
cbrt-div19.4%
*-commutative19.4%
associate-*r*19.4%
metadata-eval19.4%
neg-mul-119.4%
Applied egg-rr19.4%
add-log-exp39.4%
*-commutative39.4%
exp-prod57.7%
add-sqr-sqrt56.5%
sqrt-prod67.8%
sqr-neg67.8%
sqrt-unprod65.1%
add-sqr-sqrt96.2%
sub-neg96.2%
+-inverses96.2%
metadata-eval96.2%
metadata-eval96.2%
Applied egg-rr96.2%
Final simplification96.2%
(FPCore (g h a) :precision binary64 (+ (cbrt (/ (- g) a)) (cbrt (* (- g g) (/ -0.5 a)))))
double code(double g, double h, double a) {
return cbrt((-g / a)) + cbrt(((g - g) * (-0.5 / a)));
}
public static double code(double g, double h, double a) {
return Math.cbrt((-g / a)) + Math.cbrt(((g - g) * (-0.5 / a)));
}
function code(g, h, a) return Float64(cbrt(Float64(Float64(-g) / a)) + cbrt(Float64(Float64(g - g) * Float64(-0.5 / a)))) end
code[g_, h_, a_] := N[(N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(N[(g - g), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{-g}{a}} + \sqrt[3]{\left(g - g\right) \cdot \frac{-0.5}{a}}
\end{array}
Initial program 46.5%
Simplified46.5%
Taylor expanded in g around -inf 28.1%
*-commutative28.1%
Simplified28.1%
Taylor expanded in g around -inf 76.5%
neg-mul-176.5%
Simplified76.5%
associate-*l/76.5%
*-commutative76.5%
associate-*r*76.8%
metadata-eval76.8%
neg-mul-176.8%
Applied egg-rr76.8%
Final simplification76.8%
(FPCore (g h a) :precision binary64 (if (<= a -5e-310) (+ (cbrt -2.0) (cbrt g)) (+ (cbrt -2.0) (/ (cbrt g) -2.0))))
double code(double g, double h, double a) {
double tmp;
if (a <= -5e-310) {
tmp = cbrt(-2.0) + cbrt(g);
} else {
tmp = cbrt(-2.0) + (cbrt(g) / -2.0);
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if (a <= -5e-310) {
tmp = Math.cbrt(-2.0) + Math.cbrt(g);
} else {
tmp = Math.cbrt(-2.0) + (Math.cbrt(g) / -2.0);
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if (a <= -5e-310) tmp = Float64(cbrt(-2.0) + cbrt(g)); else tmp = Float64(cbrt(-2.0) + Float64(cbrt(g) / -2.0)); end return tmp end
code[g_, h_, a_] := If[LessEqual[a, -5e-310], N[(N[Power[-2.0, 1/3], $MachinePrecision] + N[Power[g, 1/3], $MachinePrecision]), $MachinePrecision], N[(N[Power[-2.0, 1/3], $MachinePrecision] + N[(N[Power[g, 1/3], $MachinePrecision] / -2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\sqrt[3]{-2} + \sqrt[3]{g}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{-2} + \frac{\sqrt[3]{g}}{-2}\\
\end{array}
\end{array}
if a < -4.999999999999985e-310Initial program 44.7%
Simplified44.7%
Taylor expanded in g around -inf 26.1%
*-commutative26.1%
Simplified26.1%
Taylor expanded in g around inf 15.8%
associate-*l/15.8%
associate-/l*15.8%
add-sqr-sqrt8.4%
sqrt-unprod9.4%
*-commutative9.4%
*-commutative9.4%
swap-sqr9.4%
metadata-eval9.4%
metadata-eval9.4%
swap-sqr9.4%
count-29.4%
count-29.4%
sqrt-unprod2.1%
add-sqr-sqrt3.5%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified43.1%
expm1-log1p-u18.2%
expm1-udef18.2%
Applied egg-rr2.2%
Simplified6.9%
if -4.999999999999985e-310 < a Initial program 48.3%
Simplified48.3%
Taylor expanded in g around -inf 30.3%
*-commutative30.3%
Simplified30.3%
Taylor expanded in g around inf 15.6%
associate-*l/15.6%
associate-/l*15.6%
add-sqr-sqrt7.3%
sqrt-unprod10.5%
*-commutative10.5%
*-commutative10.5%
swap-sqr10.4%
metadata-eval10.4%
metadata-eval10.4%
swap-sqr10.5%
count-210.5%
count-210.5%
sqrt-unprod1.6%
add-sqr-sqrt3.1%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified37.3%
frac-2neg37.3%
metadata-eval37.3%
associate-*r/37.3%
add-sqr-sqrt22.1%
sqrt-unprod11.9%
count-211.9%
count-211.9%
swap-sqr11.9%
metadata-eval11.9%
metadata-eval11.9%
swap-sqr11.9%
*-commutative11.9%
*-commutative11.9%
sqrt-unprod0.6%
add-sqr-sqrt2.4%
*-commutative2.4%
*-commutative2.4%
associate-*r*2.4%
metadata-eval2.4%
neg-mul-12.4%
Applied egg-rr2.5%
Simplified6.8%
Final simplification6.9%
(FPCore (g h a) :precision binary64 (+ (cbrt (/ (- g) a)) (cbrt -2.0)))
double code(double g, double h, double a) {
return cbrt((-g / a)) + cbrt(-2.0);
}
public static double code(double g, double h, double a) {
return Math.cbrt((-g / a)) + Math.cbrt(-2.0);
}
function code(g, h, a) return Float64(cbrt(Float64(Float64(-g) / a)) + cbrt(-2.0)) end
code[g_, h_, a_] := N[(N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{-g}{a}} + \sqrt[3]{-2}
\end{array}
Initial program 46.5%
Simplified46.5%
Taylor expanded in g around -inf 28.1%
*-commutative28.1%
Simplified28.1%
Taylor expanded in g around inf 15.7%
associate-*l/15.7%
associate-/l*15.7%
add-sqr-sqrt7.9%
sqrt-unprod9.9%
*-commutative9.9%
*-commutative9.9%
swap-sqr9.9%
metadata-eval9.9%
metadata-eval9.9%
swap-sqr9.9%
count-29.9%
count-29.9%
sqrt-unprod1.8%
add-sqr-sqrt3.3%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified40.3%
Taylor expanded in g around 0 40.6%
associate-*r/40.6%
mul-1-neg40.6%
Simplified40.6%
Final simplification40.6%
(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 46.5%
Simplified46.5%
Taylor expanded in g around -inf 28.1%
*-commutative28.1%
Simplified28.1%
Taylor expanded in g around inf 15.7%
associate-*l/15.7%
associate-/l*15.7%
add-sqr-sqrt7.9%
sqrt-unprod9.9%
*-commutative9.9%
*-commutative9.9%
swap-sqr9.9%
metadata-eval9.9%
metadata-eval9.9%
swap-sqr9.9%
count-29.9%
count-29.9%
sqrt-unprod1.8%
add-sqr-sqrt3.3%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified40.3%
Taylor expanded in g around 0 4.7%
Final simplification4.7%
herbie shell --seed 2024024
(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))))))))