
(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 2 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
(let* ((t_0 (sqrt (- (* g g) (* h h))))
(t_1 (- t_0 g))
(t_2
(+
(cbrt (* (/ 1.0 (* 2.0 a)) t_1))
(cbrt (* (+ g t_0) (/ -1.0 (* 2.0 a)))))))
(if (<= t_2 -5e-106)
(+ (cbrt (* (/ -0.25 a) (/ (pow h 2.0) g))) (cbrt (* (+ g g) (/ -0.5 a))))
(if (<= t_2 0.0)
(+ (cbrt (* t_1 (/ 0.5 a))) (* (cbrt (/ -0.5 a)) (cbrt (+ g g))))
(+ (cbrt (* (/ 0.5 a) (- g g))) (cbrt (/ (- g) a)))))))
double code(double g, double h, double a) {
double t_0 = sqrt(((g * g) - (h * h)));
double t_1 = t_0 - g;
double t_2 = cbrt(((1.0 / (2.0 * a)) * t_1)) + cbrt(((g + t_0) * (-1.0 / (2.0 * a))));
double tmp;
if (t_2 <= -5e-106) {
tmp = cbrt(((-0.25 / a) * (pow(h, 2.0) / g))) + cbrt(((g + g) * (-0.5 / a)));
} else if (t_2 <= 0.0) {
tmp = cbrt((t_1 * (0.5 / a))) + (cbrt((-0.5 / a)) * cbrt((g + g)));
} else {
tmp = cbrt(((0.5 / a) * (g - g))) + cbrt((-g / a));
}
return tmp;
}
public static double code(double g, double h, double a) {
double t_0 = Math.sqrt(((g * g) - (h * h)));
double t_1 = t_0 - g;
double t_2 = Math.cbrt(((1.0 / (2.0 * a)) * t_1)) + Math.cbrt(((g + t_0) * (-1.0 / (2.0 * a))));
double tmp;
if (t_2 <= -5e-106) {
tmp = Math.cbrt(((-0.25 / a) * (Math.pow(h, 2.0) / g))) + Math.cbrt(((g + g) * (-0.5 / a)));
} else if (t_2 <= 0.0) {
tmp = Math.cbrt((t_1 * (0.5 / a))) + (Math.cbrt((-0.5 / a)) * Math.cbrt((g + g)));
} else {
tmp = Math.cbrt(((0.5 / a) * (g - g))) + Math.cbrt((-g / a));
}
return tmp;
}
function code(g, h, a) t_0 = sqrt(Float64(Float64(g * g) - Float64(h * h))) t_1 = Float64(t_0 - g) t_2 = Float64(cbrt(Float64(Float64(1.0 / Float64(2.0 * a)) * t_1)) + cbrt(Float64(Float64(g + t_0) * Float64(-1.0 / Float64(2.0 * a))))) tmp = 0.0 if (t_2 <= -5e-106) tmp = Float64(cbrt(Float64(Float64(-0.25 / a) * Float64((h ^ 2.0) / g))) + cbrt(Float64(Float64(g + g) * Float64(-0.5 / a)))); elseif (t_2 <= 0.0) tmp = Float64(cbrt(Float64(t_1 * Float64(0.5 / a))) + Float64(cbrt(Float64(-0.5 / a)) * cbrt(Float64(g + g)))); else tmp = Float64(cbrt(Float64(Float64(0.5 / a) * Float64(g - g))) + cbrt(Float64(Float64(-g) / a))); end return tmp end
code[g_, h_, a_] := Block[{t$95$0 = N[Sqrt[N[(N[(g * g), $MachinePrecision] - N[(h * h), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 - g), $MachinePrecision]}, Block[{t$95$2 = N[(N[Power[N[(N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(N[(g + t$95$0), $MachinePrecision] * N[(-1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -5e-106], N[(N[Power[N[(N[(-0.25 / a), $MachinePrecision] * N[(N[Power[h, 2.0], $MachinePrecision] / g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(N[(g + g), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$2, 0.0], N[(N[Power[N[(t$95$1 * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[N[(-0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(g + g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g - g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{g \cdot g - h \cdot h}\\
t_1 := t\_0 - g\\
t_2 := \sqrt[3]{\frac{1}{2 \cdot a} \cdot t\_1} + \sqrt[3]{\left(g + t\_0\right) \cdot \frac{-1}{2 \cdot a}}\\
\mathbf{if}\;t\_2 \leq -5 \cdot 10^{-106}:\\
\;\;\;\;\sqrt[3]{\frac{-0.25}{a} \cdot \frac{{h}^{2}}{g}} + \sqrt[3]{\left(g + g\right) \cdot \frac{-0.5}{a}}\\
\mathbf{elif}\;t\_2 \leq 0:\\
\;\;\;\;\sqrt[3]{t\_1 \cdot \frac{0.5}{a}} + \sqrt[3]{\frac{-0.5}{a}} \cdot \sqrt[3]{g + g}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{\frac{0.5}{a} \cdot \left(g - g\right)} + \sqrt[3]{\frac{-g}{a}}\\
\end{array}
\end{array}
if (+.f64 (cbrt.f64 (*.f64 (/.f64 1 (*.f64 2 a)) (+.f64 (neg.f64 g) (sqrt.f64 (-.f64 (*.f64 g g) (*.f64 h h)))))) (cbrt.f64 (*.f64 (/.f64 1 (*.f64 2 a)) (-.f64 (neg.f64 g) (sqrt.f64 (-.f64 (*.f64 g g) (*.f64 h h))))))) < -4.99999999999999983e-106Initial program 81.7%
Simplified81.7%
Taylor expanded in g around inf 44.2%
difference-of-squares44.2%
+-commutative44.2%
sqrt-unprod26.9%
*-commutative26.9%
fma-neg17.3%
+-commutative17.3%
Applied egg-rr17.3%
Taylor expanded in h around 0 77.9%
associate-*r/77.9%
times-frac91.1%
Simplified91.1%
if -4.99999999999999983e-106 < (+.f64 (cbrt.f64 (*.f64 (/.f64 1 (*.f64 2 a)) (+.f64 (neg.f64 g) (sqrt.f64 (-.f64 (*.f64 g g) (*.f64 h h)))))) (cbrt.f64 (*.f64 (/.f64 1 (*.f64 2 a)) (-.f64 (neg.f64 g) (sqrt.f64 (-.f64 (*.f64 g g) (*.f64 h h))))))) < 0.0Initial program 4.5%
Simplified4.5%
*-commutative4.5%
cbrt-prod33.2%
pow233.2%
pow233.2%
Applied egg-rr33.2%
Taylor expanded in g around inf 83.2%
if 0.0 < (+.f64 (cbrt.f64 (*.f64 (/.f64 1 (*.f64 2 a)) (+.f64 (neg.f64 g) (sqrt.f64 (-.f64 (*.f64 g g) (*.f64 h h)))))) (cbrt.f64 (*.f64 (/.f64 1 (*.f64 2 a)) (-.f64 (neg.f64 g) (sqrt.f64 (-.f64 (*.f64 g g) (*.f64 h h))))))) Initial program 33.9%
Simplified33.9%
Taylor expanded in g around inf 21.0%
Taylor expanded in g around inf 76.3%
Taylor expanded in g around 0 76.3%
associate-*r/76.3%
mul-1-neg76.3%
Simplified76.3%
Final simplification80.1%
(FPCore (g h a) :precision binary64 (+ (cbrt (* (/ 0.5 a) (* g -2.0))) (/ 0.0 (cbrt a))))
double code(double g, double h, double a) {
return cbrt(((0.5 / a) * (g * -2.0))) + (0.0 / cbrt(a));
}
public static double code(double g, double h, double a) {
return Math.cbrt(((0.5 / a) * (g * -2.0))) + (0.0 / Math.cbrt(a));
}
function code(g, h, a) return Float64(cbrt(Float64(Float64(0.5 / a) * Float64(g * -2.0))) + Float64(0.0 / cbrt(a))) end
code[g_, h_, a_] := N[(N[Power[N[(N[(0.5 / a), $MachinePrecision] * N[(g * -2.0), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(0.0 / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{0.5}{a} \cdot \left(g \cdot -2\right)} + \frac{0}{\sqrt[3]{a}}
\end{array}
Initial program 43.6%
Simplified43.6%
Taylor expanded in g around inf 25.6%
Taylor expanded in g around -inf 15.6%
Simplified15.6%
Applied egg-rr0.0%
Simplified75.9%
Final simplification75.9%
herbie shell --seed 2024041
(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))))))))