
(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 9 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 (fma (* (/ (cbrt g) (cbrt a)) (cbrt -0.5)) (cbrt 2.0) (* (/ (cbrt (* (/ h g) h)) (cbrt a)) (* (cbrt 0.5) (cbrt -0.5)))))
double code(double g, double h, double a) {
return fma(((cbrt(g) / cbrt(a)) * cbrt(-0.5)), cbrt(2.0), ((cbrt(((h / g) * h)) / cbrt(a)) * (cbrt(0.5) * cbrt(-0.5))));
}
function code(g, h, a) return fma(Float64(Float64(cbrt(g) / cbrt(a)) * cbrt(-0.5)), cbrt(2.0), Float64(Float64(cbrt(Float64(Float64(h / g) * h)) / cbrt(a)) * Float64(cbrt(0.5) * cbrt(-0.5)))) end
code[g_, h_, a_] := N[(N[(N[(N[Power[g, 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[-0.5, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[2.0, 1/3], $MachinePrecision] + N[(N[(N[Power[N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision], 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[(N[Power[0.5, 1/3], $MachinePrecision] * N[Power[-0.5, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot \sqrt[3]{-0.5}, \sqrt[3]{2}, \frac{\sqrt[3]{\frac{h}{g} \cdot h}}{\sqrt[3]{a}} \cdot \left(\sqrt[3]{0.5} \cdot \sqrt[3]{-0.5}\right)\right)
\end{array}
Initial program 36.8%
Taylor expanded in h around 0
Applied rewrites71.0%
Applied rewrites90.4%
Applied rewrites95.1%
Applied rewrites97.0%
(FPCore (g h a) :precision binary64 (fma (/ (cbrt (* g -0.5)) (cbrt a)) (cbrt 2.0) (* (cbrt (/ (* (/ h g) h) a)) (* (cbrt 0.5) (cbrt -0.5)))))
double code(double g, double h, double a) {
return fma((cbrt((g * -0.5)) / cbrt(a)), cbrt(2.0), (cbrt((((h / g) * h) / a)) * (cbrt(0.5) * cbrt(-0.5))));
}
function code(g, h, a) return fma(Float64(cbrt(Float64(g * -0.5)) / cbrt(a)), cbrt(2.0), Float64(cbrt(Float64(Float64(Float64(h / g) * h) / a)) * Float64(cbrt(0.5) * cbrt(-0.5)))) end
code[g_, h_, a_] := N[(N[(N[Power[N[(g * -0.5), $MachinePrecision], 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[2.0, 1/3], $MachinePrecision] + N[(N[Power[N[(N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision] / a), $MachinePrecision], 1/3], $MachinePrecision] * N[(N[Power[0.5, 1/3], $MachinePrecision] * N[Power[-0.5, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\sqrt[3]{g \cdot -0.5}}{\sqrt[3]{a}}, \sqrt[3]{2}, \sqrt[3]{\frac{\frac{h}{g} \cdot h}{a}} \cdot \left(\sqrt[3]{0.5} \cdot \sqrt[3]{-0.5}\right)\right)
\end{array}
Initial program 36.8%
Taylor expanded in h around 0
Applied rewrites71.0%
Applied rewrites90.4%
Applied rewrites95.1%
Applied rewrites95.4%
(FPCore (g h a) :precision binary64 (fma (* (/ (cbrt g) (cbrt a)) (cbrt -0.5)) (cbrt 2.0) (* (cbrt (/ (* (/ h g) h) a)) (cbrt -0.25))))
double code(double g, double h, double a) {
return fma(((cbrt(g) / cbrt(a)) * cbrt(-0.5)), cbrt(2.0), (cbrt((((h / g) * h) / a)) * cbrt(-0.25)));
}
function code(g, h, a) return fma(Float64(Float64(cbrt(g) / cbrt(a)) * cbrt(-0.5)), cbrt(2.0), Float64(cbrt(Float64(Float64(Float64(h / g) * h) / a)) * cbrt(-0.25))) end
code[g_, h_, a_] := N[(N[(N[(N[Power[g, 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[-0.5, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[2.0, 1/3], $MachinePrecision] + N[(N[Power[N[(N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision] / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[-0.25, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot \sqrt[3]{-0.5}, \sqrt[3]{2}, \sqrt[3]{\frac{\frac{h}{g} \cdot h}{a}} \cdot \sqrt[3]{-0.25}\right)
\end{array}
Initial program 36.8%
Taylor expanded in h around 0
Applied rewrites71.0%
Applied rewrites90.4%
Applied rewrites95.1%
Applied rewrites95.1%
(FPCore (g h a) :precision binary64 (fma (* (/ (cbrt g) (cbrt a)) (cbrt -0.5)) (cbrt 2.0) (* (cbrt (* (/ h g) (/ h a))) (cbrt -0.25))))
double code(double g, double h, double a) {
return fma(((cbrt(g) / cbrt(a)) * cbrt(-0.5)), cbrt(2.0), (cbrt(((h / g) * (h / a))) * cbrt(-0.25)));
}
function code(g, h, a) return fma(Float64(Float64(cbrt(g) / cbrt(a)) * cbrt(-0.5)), cbrt(2.0), Float64(cbrt(Float64(Float64(h / g) * Float64(h / a))) * cbrt(-0.25))) end
code[g_, h_, a_] := N[(N[(N[(N[Power[g, 1/3], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[-0.5, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[2.0, 1/3], $MachinePrecision] + N[(N[Power[N[(N[(h / g), $MachinePrecision] * N[(h / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[-0.25, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\sqrt[3]{g}}{\sqrt[3]{a}} \cdot \sqrt[3]{-0.5}, \sqrt[3]{2}, \sqrt[3]{\frac{h}{g} \cdot \frac{h}{a}} \cdot \sqrt[3]{-0.25}\right)
\end{array}
Initial program 36.8%
Taylor expanded in h around 0
Applied rewrites71.0%
Applied rewrites90.4%
Applied rewrites90.4%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (* a (* g g))))
(if (<= (/ 1.0 (* 2.0 a)) 2e+258)
(fma
(* (cbrt -0.25) (cbrt (/ h g)))
(cbrt (/ h a))
(* (cbrt -1.0) (cbrt (/ g a))))
(* g (* (- (cbrt 0.5)) (+ (cbrt (/ 0.0 t_0)) (cbrt (/ 2.0 t_0))))))))
double code(double g, double h, double a) {
double t_0 = a * (g * g);
double tmp;
if ((1.0 / (2.0 * a)) <= 2e+258) {
tmp = fma((cbrt(-0.25) * cbrt((h / g))), cbrt((h / a)), (cbrt(-1.0) * cbrt((g / a))));
} else {
tmp = g * (-cbrt(0.5) * (cbrt((0.0 / t_0)) + cbrt((2.0 / t_0))));
}
return tmp;
}
function code(g, h, a) t_0 = Float64(a * Float64(g * g)) tmp = 0.0 if (Float64(1.0 / Float64(2.0 * a)) <= 2e+258) tmp = fma(Float64(cbrt(-0.25) * cbrt(Float64(h / g))), cbrt(Float64(h / a)), Float64(cbrt(-1.0) * cbrt(Float64(g / a)))); else tmp = Float64(g * Float64(Float64(-cbrt(0.5)) * Float64(cbrt(Float64(0.0 / t_0)) + cbrt(Float64(2.0 / t_0))))); end return tmp end
code[g_, h_, a_] := Block[{t$95$0 = N[(a * N[(g * g), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], 2e+258], N[(N[(N[Power[-0.25, 1/3], $MachinePrecision] * N[Power[N[(h / g), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[N[(h / a), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[-1.0, 1/3], $MachinePrecision] * N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(g * N[((-N[Power[0.5, 1/3], $MachinePrecision]) * N[(N[Power[N[(0.0 / t$95$0), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(2.0 / t$95$0), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := a \cdot \left(g \cdot g\right)\\
\mathbf{if}\;\frac{1}{2 \cdot a} \leq 2 \cdot 10^{+258}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt[3]{-0.25} \cdot \sqrt[3]{\frac{h}{g}}, \sqrt[3]{\frac{h}{a}}, \sqrt[3]{-1} \cdot \sqrt[3]{\frac{g}{a}}\right)\\
\mathbf{else}:\\
\;\;\;\;g \cdot \left(\left(-\sqrt[3]{0.5}\right) \cdot \left(\sqrt[3]{\frac{0}{t\_0}} + \sqrt[3]{\frac{2}{t\_0}}\right)\right)\\
\end{array}
\end{array}
if (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a)) < 2.00000000000000011e258Initial program 38.4%
Taylor expanded in h around 0
Applied rewrites74.0%
Applied rewrites92.4%
Applied rewrites95.7%
Applied rewrites74.8%
if 2.00000000000000011e258 < (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a)) Initial program 1.9%
Applied rewrites1.7%
Taylor expanded in a around 0
Applied rewrites1.9%
Taylor expanded in g around -inf
Applied rewrites46.3%
Final simplification73.6%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (* a (* g g))))
(if (<= (/ 1.0 (* 2.0 a)) 2e+258)
(fma (cbrt (/ g a)) (cbrt -1.0) (cbrt (* (/ (* (/ h g) h) a) -0.25)))
(* g (* (- (cbrt 0.5)) (+ (cbrt (/ 0.0 t_0)) (cbrt (/ 2.0 t_0))))))))
double code(double g, double h, double a) {
double t_0 = a * (g * g);
double tmp;
if ((1.0 / (2.0 * a)) <= 2e+258) {
tmp = fma(cbrt((g / a)), cbrt(-1.0), cbrt(((((h / g) * h) / a) * -0.25)));
} else {
tmp = g * (-cbrt(0.5) * (cbrt((0.0 / t_0)) + cbrt((2.0 / t_0))));
}
return tmp;
}
function code(g, h, a) t_0 = Float64(a * Float64(g * g)) tmp = 0.0 if (Float64(1.0 / Float64(2.0 * a)) <= 2e+258) tmp = fma(cbrt(Float64(g / a)), cbrt(-1.0), cbrt(Float64(Float64(Float64(Float64(h / g) * h) / a) * -0.25))); else tmp = Float64(g * Float64(Float64(-cbrt(0.5)) * Float64(cbrt(Float64(0.0 / t_0)) + cbrt(Float64(2.0 / t_0))))); end return tmp end
code[g_, h_, a_] := Block[{t$95$0 = N[(a * N[(g * g), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], 2e+258], N[(N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[-1.0, 1/3], $MachinePrecision] + N[Power[N[(N[(N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision] / a), $MachinePrecision] * -0.25), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], N[(g * N[((-N[Power[0.5, 1/3], $MachinePrecision]) * N[(N[Power[N[(0.0 / t$95$0), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(2.0 / t$95$0), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := a \cdot \left(g \cdot g\right)\\
\mathbf{if}\;\frac{1}{2 \cdot a} \leq 2 \cdot 10^{+258}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt[3]{\frac{g}{a}}, \sqrt[3]{-1}, \sqrt[3]{\frac{\frac{h}{g} \cdot h}{a} \cdot -0.25}\right)\\
\mathbf{else}:\\
\;\;\;\;g \cdot \left(\left(-\sqrt[3]{0.5}\right) \cdot \left(\sqrt[3]{\frac{0}{t\_0}} + \sqrt[3]{\frac{2}{t\_0}}\right)\right)\\
\end{array}
\end{array}
if (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a)) < 2.00000000000000011e258Initial program 38.4%
Taylor expanded in h around 0
Applied rewrites74.0%
Applied rewrites92.4%
Applied rewrites95.7%
Applied rewrites74.8%
if 2.00000000000000011e258 < (/.f64 #s(literal 1 binary64) (*.f64 #s(literal 2 binary64) a)) Initial program 1.9%
Applied rewrites1.7%
Taylor expanded in a around 0
Applied rewrites1.9%
Taylor expanded in g around -inf
Applied rewrites46.3%
Final simplification73.5%
(FPCore (g h a) :precision binary64 (fma (cbrt (/ g a)) (cbrt -1.0) (cbrt (* (/ (* (/ h g) h) a) -0.25))))
double code(double g, double h, double a) {
return fma(cbrt((g / a)), cbrt(-1.0), cbrt(((((h / g) * h) / a) * -0.25)));
}
function code(g, h, a) return fma(cbrt(Float64(g / a)), cbrt(-1.0), cbrt(Float64(Float64(Float64(Float64(h / g) * h) / a) * -0.25))) end
code[g_, h_, a_] := N[(N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[-1.0, 1/3], $MachinePrecision] + N[Power[N[(N[(N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision] / a), $MachinePrecision] * -0.25), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt[3]{\frac{g}{a}}, \sqrt[3]{-1}, \sqrt[3]{\frac{\frac{h}{g} \cdot h}{a} \cdot -0.25}\right)
\end{array}
Initial program 36.8%
Taylor expanded in h around 0
Applied rewrites71.0%
Applied rewrites90.4%
Applied rewrites95.1%
Applied rewrites71.7%
(FPCore (g h a) :precision binary64 (+ (cbrt (* (/ 1.0 (* 2.0 a)) (* (* h (/ h g)) -0.5))) (cbrt (/ (- g) a))))
double code(double g, double h, double a) {
return cbrt(((1.0 / (2.0 * a)) * ((h * (h / g)) * -0.5))) + cbrt((-g / a));
}
public static double code(double g, double h, double a) {
return Math.cbrt(((1.0 / (2.0 * a)) * ((h * (h / g)) * -0.5))) + Math.cbrt((-g / a));
}
function code(g, h, a) return Float64(cbrt(Float64(Float64(1.0 / Float64(2.0 * a)) * Float64(Float64(h * Float64(h / g)) * -0.5))) + cbrt(Float64(Float64(-g) / a))) end
code[g_, h_, a_] := N[(N[Power[N[(N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] * N[(N[(h * N[(h / g), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\left(h \cdot \frac{h}{g}\right) \cdot -0.5\right)} + \sqrt[3]{\frac{-g}{a}}
\end{array}
Initial program 36.8%
Taylor expanded in g around inf
Applied rewrites25.7%
Taylor expanded in g around inf
Applied rewrites69.7%
Taylor expanded in g around inf
Applied rewrites71.7%
(FPCore (g h a) :precision binary64 (- (cbrt (/ g a))))
double code(double g, double h, double a) {
return -cbrt((g / a));
}
public static double code(double g, double h, double a) {
return -Math.cbrt((g / a));
}
function code(g, h, a) return Float64(-cbrt(Float64(g / a))) end
code[g_, h_, a_] := (-N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision])
\begin{array}{l}
\\
-\sqrt[3]{\frac{g}{a}}
\end{array}
Initial program 36.8%
Taylor expanded in g around inf
Applied rewrites24.4%
lift-cbrt.f64N/A
pow1/3N/A
lower-pow.f6420.3
Applied rewrites18.8%
Taylor expanded in g around -inf
Applied rewrites69.7%
herbie shell --seed 2025019
(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))))))))