
(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 47.6%
Simplified47.6%
Taylor expanded in g around -inf 26.8%
*-commutative26.8%
Simplified26.8%
Taylor expanded in g around -inf 74.6%
neg-mul-174.6%
Simplified74.6%
associate-*l/74.6%
cbrt-div96.2%
*-commutative96.2%
associate-*r*96.2%
metadata-eval96.2%
neg-mul-196.2%
Applied egg-rr96.2%
Final simplification96.2%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (cbrt (* (- g g) (/ -0.5 a)))))
(if (<= a -6.8e-81)
(+ t_0 (cbrt (/ (- g) a)))
(if (<= a 7.5e-40)
(+ (/ (cbrt (- g)) (cbrt a)) (cbrt -2.0))
(+ t_0 (cbrt (* g (/ 1.0 (- a)))))))))
double code(double g, double h, double a) {
double t_0 = cbrt(((g - g) * (-0.5 / a)));
double tmp;
if (a <= -6.8e-81) {
tmp = t_0 + cbrt((-g / a));
} else if (a <= 7.5e-40) {
tmp = (cbrt(-g) / cbrt(a)) + cbrt(-2.0);
} else {
tmp = t_0 + cbrt((g * (1.0 / -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 <= -6.8e-81) {
tmp = t_0 + Math.cbrt((-g / a));
} else if (a <= 7.5e-40) {
tmp = (Math.cbrt(-g) / Math.cbrt(a)) + Math.cbrt(-2.0);
} else {
tmp = t_0 + Math.cbrt((g * (1.0 / -a)));
}
return tmp;
}
function code(g, h, a) t_0 = cbrt(Float64(Float64(g - g) * Float64(-0.5 / a))) tmp = 0.0 if (a <= -6.8e-81) tmp = Float64(t_0 + cbrt(Float64(Float64(-g) / a))); elseif (a <= 7.5e-40) tmp = Float64(Float64(cbrt(Float64(-g)) / cbrt(a)) + cbrt(-2.0)); else tmp = Float64(t_0 + cbrt(Float64(g * Float64(1.0 / Float64(-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, -6.8e-81], N[(t$95$0 + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], If[LessEqual[a, 7.5e-40], 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 * N[(1.0 / (-a)), $MachinePrecision]), $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 -6.8 \cdot 10^{-81}:\\
\;\;\;\;t_0 + \sqrt[3]{\frac{-g}{a}}\\
\mathbf{elif}\;a \leq 7.5 \cdot 10^{-40}:\\
\;\;\;\;\frac{\sqrt[3]{-g}}{\sqrt[3]{a}} + \sqrt[3]{-2}\\
\mathbf{else}:\\
\;\;\;\;t_0 + \sqrt[3]{g \cdot \frac{1}{-a}}\\
\end{array}
\end{array}
if a < -6.7999999999999997e-81Initial program 53.9%
Simplified53.9%
Taylor expanded in g around -inf 28.8%
*-commutative28.8%
Simplified28.8%
Taylor expanded in g around -inf 92.6%
neg-mul-192.6%
Simplified92.6%
associate-*l/92.6%
*-commutative92.6%
associate-*r*92.6%
metadata-eval92.6%
neg-mul-192.6%
Applied egg-rr92.6%
if -6.7999999999999997e-81 < a < 7.50000000000000069e-40Initial program 40.7%
Simplified40.7%
Taylor expanded in g around -inf 25.5%
*-commutative25.5%
Simplified25.5%
Taylor expanded in g around inf 11.6%
associate-*l/11.6%
associate-/l*11.6%
add-sqr-sqrt5.8%
sqrt-unprod7.5%
*-commutative7.5%
*-commutative7.5%
swap-sqr7.5%
metadata-eval7.5%
metadata-eval7.5%
swap-sqr7.5%
count-27.5%
count-27.5%
sqrt-unprod1.1%
add-sqr-sqrt1.8%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified48.1%
add-sqr-sqrt24.6%
sqrt-unprod12.4%
swap-sqr7.5%
count-27.5%
count-27.5%
swap-sqr7.5%
metadata-eval7.5%
metadata-eval7.5%
swap-sqr7.5%
*-commutative7.5%
*-commutative7.5%
frac-times7.5%
metadata-eval7.5%
metadata-eval7.5%
frac-times7.5%
swap-sqr12.4%
*-commutative12.4%
*-commutative12.4%
sqrt-unprod24.6%
add-sqr-sqrt48.1%
Applied egg-rr94.9%
if 7.50000000000000069e-40 < a Initial program 50.8%
Simplified50.8%
Taylor expanded in g around -inf 26.4%
*-commutative26.4%
Simplified26.4%
Taylor expanded in g around -inf 90.8%
neg-mul-190.8%
Simplified90.8%
associate-*l/90.7%
*-commutative90.7%
associate-*r*90.7%
metadata-eval90.7%
neg-mul-190.7%
add-sqr-sqrt90.7%
associate-/r*90.8%
Applied egg-rr90.8%
associate-/l/90.7%
add-sqr-sqrt90.7%
frac-2neg90.7%
div-inv90.8%
remove-double-neg90.8%
Applied egg-rr90.8%
Final simplification93.0%
(FPCore (g h a) :precision binary64 (if (or (<= g -7.7) (not (<= g 0.00035))) (+ (cbrt (/ (- g) a)) (cbrt -2.0)) (+ (cbrt (/ -0.5 (/ -2.0 g))) (cbrt (* (/ -0.5 a) (+ g g))))))
double code(double g, double h, double a) {
double tmp;
if ((g <= -7.7) || !(g <= 0.00035)) {
tmp = cbrt((-g / a)) + cbrt(-2.0);
} else {
tmp = cbrt((-0.5 / (-2.0 / g))) + cbrt(((-0.5 / a) * (g + g)));
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if ((g <= -7.7) || !(g <= 0.00035)) {
tmp = Math.cbrt((-g / a)) + Math.cbrt(-2.0);
} else {
tmp = Math.cbrt((-0.5 / (-2.0 / g))) + Math.cbrt(((-0.5 / a) * (g + g)));
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if ((g <= -7.7) || !(g <= 0.00035)) tmp = Float64(cbrt(Float64(Float64(-g) / a)) + cbrt(-2.0)); else tmp = Float64(cbrt(Float64(-0.5 / Float64(-2.0 / g))) + cbrt(Float64(Float64(-0.5 / a) * Float64(g + g)))); end return tmp end
code[g_, h_, a_] := If[Or[LessEqual[g, -7.7], N[Not[LessEqual[g, 0.00035]], $MachinePrecision]], N[(N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision], N[(N[Power[N[(-0.5 / N[(-2.0 / g), $MachinePrecision]), $MachinePrecision], 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 -7.7 \lor \neg \left(g \leq 0.00035\right):\\
\;\;\;\;\sqrt[3]{\frac{-g}{a}} + \sqrt[3]{-2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{\frac{-0.5}{\frac{-2}{g}}} + \sqrt[3]{\frac{-0.5}{a} \cdot \left(g + g\right)}\\
\end{array}
\end{array}
if g < -7.70000000000000018 or 3.49999999999999996e-4 < g Initial program 36.4%
Simplified36.4%
Taylor expanded in g around -inf 21.3%
*-commutative21.3%
Simplified21.3%
Taylor expanded in g around inf 14.7%
associate-*l/14.7%
associate-/l*14.7%
add-sqr-sqrt7.1%
sqrt-unprod5.1%
*-commutative5.1%
*-commutative5.1%
swap-sqr5.1%
metadata-eval5.1%
metadata-eval5.1%
swap-sqr5.1%
count-25.1%
count-25.1%
sqrt-unprod1.4%
add-sqr-sqrt2.4%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified49.4%
Taylor expanded in g around 0 49.4%
neg-mul-149.4%
distribute-neg-frac49.4%
Simplified49.4%
if -7.70000000000000018 < g < 3.49999999999999996e-4Initial program 78.4%
Simplified78.4%
Taylor expanded in g around -inf 41.9%
*-commutative41.9%
Simplified41.9%
Taylor expanded in g around inf 17.5%
Applied egg-rr15.8%
Simplified48.5%
Final simplification49.2%
(FPCore (g h a) :precision binary64 (if (or (<= g -0.3) (not (<= g 0.00035))) (+ (cbrt (/ (- g) a)) (cbrt -2.0)) (+ (cbrt (* (/ 0.5 a) (* g -2.0))) (cbrt g))))
double code(double g, double h, double a) {
double tmp;
if ((g <= -0.3) || !(g <= 0.00035)) {
tmp = cbrt((-g / a)) + cbrt(-2.0);
} else {
tmp = cbrt(((0.5 / a) * (g * -2.0))) + cbrt(g);
}
return tmp;
}
public static double code(double g, double h, double a) {
double tmp;
if ((g <= -0.3) || !(g <= 0.00035)) {
tmp = Math.cbrt((-g / a)) + Math.cbrt(-2.0);
} else {
tmp = Math.cbrt(((0.5 / a) * (g * -2.0))) + Math.cbrt(g);
}
return tmp;
}
function code(g, h, a) tmp = 0.0 if ((g <= -0.3) || !(g <= 0.00035)) tmp = Float64(cbrt(Float64(Float64(-g) / a)) + cbrt(-2.0)); else tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(g * -2.0))) + cbrt(g)); end return tmp end
code[g_, h_, a_] := If[Or[LessEqual[g, -0.3], N[Not[LessEqual[g, 0.00035]], $MachinePrecision]], N[(N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[-2.0, 1/3], $MachinePrecision]), $MachinePrecision], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g * -2.0), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[g, 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;g \leq -0.3 \lor \neg \left(g \leq 0.00035\right):\\
\;\;\;\;\sqrt[3]{\frac{-g}{a}} + \sqrt[3]{-2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \left(g \cdot -2\right)} + \sqrt[3]{g}\\
\end{array}
\end{array}
if g < -0.299999999999999989 or 3.49999999999999996e-4 < g Initial program 36.4%
Simplified36.4%
Taylor expanded in g around -inf 21.3%
*-commutative21.3%
Simplified21.3%
Taylor expanded in g around inf 14.7%
associate-*l/14.7%
associate-/l*14.7%
add-sqr-sqrt7.1%
sqrt-unprod5.1%
*-commutative5.1%
*-commutative5.1%
swap-sqr5.1%
metadata-eval5.1%
metadata-eval5.1%
swap-sqr5.1%
count-25.1%
count-25.1%
sqrt-unprod1.4%
add-sqr-sqrt2.4%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified49.4%
Taylor expanded in g around 0 49.4%
neg-mul-149.4%
distribute-neg-frac49.4%
Simplified49.4%
if -0.299999999999999989 < g < 3.49999999999999996e-4Initial program 78.4%
Simplified78.4%
Taylor expanded in g around -inf 41.9%
*-commutative41.9%
Simplified41.9%
Taylor expanded in g around inf 17.5%
expm1-log1p-u14.2%
expm1-udef51.9%
Applied egg-rr49.0%
Simplified48.4%
Final simplification49.1%
(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 47.6%
Simplified47.6%
Taylor expanded in g around -inf 26.8%
*-commutative26.8%
Simplified26.8%
Taylor expanded in g around -inf 74.6%
neg-mul-174.6%
Simplified74.6%
associate-*l/74.6%
*-commutative74.6%
associate-*r*74.6%
metadata-eval74.6%
neg-mul-174.6%
Applied egg-rr74.6%
Final simplification74.6%
(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 47.6%
Simplified47.6%
Taylor expanded in g around -inf 26.8%
*-commutative26.8%
Simplified26.8%
Taylor expanded in g around inf 15.4%
associate-*l/15.4%
associate-/l*15.6%
add-sqr-sqrt7.6%
sqrt-unprod8.1%
*-commutative8.1%
*-commutative8.1%
swap-sqr8.5%
metadata-eval8.5%
metadata-eval8.5%
swap-sqr8.1%
count-28.1%
count-28.1%
sqrt-unprod1.6%
add-sqr-sqrt3.0%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified45.2%
Taylor expanded in g around 0 45.2%
neg-mul-145.2%
distribute-neg-frac45.2%
Simplified45.2%
Final simplification45.2%
(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 47.6%
Simplified47.6%
Taylor expanded in g around -inf 26.8%
*-commutative26.8%
Simplified26.8%
Taylor expanded in g around inf 15.4%
associate-*l/15.4%
associate-/l*15.6%
add-sqr-sqrt7.6%
sqrt-unprod8.1%
*-commutative8.1%
*-commutative8.1%
swap-sqr8.5%
metadata-eval8.5%
metadata-eval8.5%
swap-sqr8.1%
count-28.1%
count-28.1%
sqrt-unprod1.6%
add-sqr-sqrt3.0%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified45.2%
Taylor expanded in g around 0 4.6%
Final simplification4.6%
herbie shell --seed 2023318
(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))))))))