
(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
(let* ((t_0 (* (/ h g) h)))
(*
(cbrt (/ 0.5 a))
(+ (cbrt (- (- g) (fma t_0 -0.5 g))) (cbrt (* t_0 -0.5))))))
double code(double g, double h, double a) {
double t_0 = (h / g) * h;
return cbrt((0.5 / a)) * (cbrt((-g - fma(t_0, -0.5, g))) + cbrt((t_0 * -0.5)));
}
function code(g, h, a) t_0 = Float64(Float64(h / g) * h) return Float64(cbrt(Float64(0.5 / a)) * Float64(cbrt(Float64(Float64(-g) - fma(t_0, -0.5, g))) + cbrt(Float64(t_0 * -0.5)))) end
code[g_, h_, a_] := Block[{t$95$0 = N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision]}, N[(N[Power[N[(0.5 / a), $MachinePrecision], 1/3], $MachinePrecision] * N[(N[Power[N[((-g) - N[(t$95$0 * -0.5 + g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(t$95$0 * -0.5), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{h}{g} \cdot h\\
\sqrt[3]{\frac{0.5}{a}} \cdot \left(\sqrt[3]{\left(-g\right) - \mathsf{fma}\left(t\_0, -0.5, g\right)} + \sqrt[3]{t\_0 \cdot -0.5}\right)
\end{array}
\end{array}
Initial program 41.8%
Taylor expanded in g around inf
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6423.7
Applied rewrites23.7%
Taylor expanded in h around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6472.1
Applied rewrites72.1%
Applied rewrites98.3%
(FPCore (g h a) :precision binary64 (+ (cbrt (* (pow (* 2.0 a) -1.0) (* -2.0 g))) (cbrt (/ (- g) a))))
double code(double g, double h, double a) {
return cbrt((pow((2.0 * a), -1.0) * (-2.0 * g))) + cbrt((-g / a));
}
public static double code(double g, double h, double a) {
return Math.cbrt((Math.pow((2.0 * a), -1.0) * (-2.0 * g))) + Math.cbrt((-g / a));
}
function code(g, h, a) return Float64(cbrt(Float64((Float64(2.0 * a) ^ -1.0) * Float64(-2.0 * g))) + cbrt(Float64(Float64(-g) / a))) end
code[g_, h_, a_] := N[(N[Power[N[(N[Power[N[(2.0 * a), $MachinePrecision], -1.0], $MachinePrecision] * N[(-2.0 * g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{{\left(2 \cdot a\right)}^{-1} \cdot \left(-2 \cdot g\right)} + \sqrt[3]{\frac{-g}{a}}
\end{array}
Initial program 41.8%
Taylor expanded in g around inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6426.4
Applied rewrites26.4%
Taylor expanded in g around -inf
lower-*.f6415.2
Applied rewrites15.2%
Final simplification15.2%
(FPCore (g h a)
:precision binary64
(let* ((t_0 (* (/ h g) h)))
(+
(cbrt (* (- (- g) (fma t_0 -0.5 g)) (/ 0.5 a)))
(cbrt (* (* t_0 -0.5) (/ 0.5 a))))))
double code(double g, double h, double a) {
double t_0 = (h / g) * h;
return cbrt(((-g - fma(t_0, -0.5, g)) * (0.5 / a))) + cbrt(((t_0 * -0.5) * (0.5 / a)));
}
function code(g, h, a) t_0 = Float64(Float64(h / g) * h) return Float64(cbrt(Float64(Float64(Float64(-g) - fma(t_0, -0.5, g)) * Float64(0.5 / a))) + cbrt(Float64(Float64(t_0 * -0.5) * Float64(0.5 / a)))) end
code[g_, h_, a_] := Block[{t$95$0 = N[(N[(h / g), $MachinePrecision] * h), $MachinePrecision]}, N[(N[Power[N[(N[((-g) - N[(t$95$0 * -0.5 + g), $MachinePrecision]), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(N[(t$95$0 * -0.5), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{h}{g} \cdot h\\
\sqrt[3]{\left(\left(-g\right) - \mathsf{fma}\left(t\_0, -0.5, g\right)\right) \cdot \frac{0.5}{a}} + \sqrt[3]{\left(t\_0 \cdot -0.5\right) \cdot \frac{0.5}{a}}
\end{array}
\end{array}
Initial program 41.8%
Taylor expanded in g around inf
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6423.7
Applied rewrites23.7%
Taylor expanded in h around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6472.1
Applied rewrites72.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
Applied rewrites74.8%
(FPCore (g h a) :precision binary64 (+ (cbrt (/ (- g) a)) (cbrt (* -0.25 (* (/ h g) (/ h a))))))
double code(double g, double h, double a) {
return cbrt((-g / a)) + cbrt((-0.25 * ((h / g) * (h / a))));
}
public static double code(double g, double h, double a) {
return Math.cbrt((-g / a)) + Math.cbrt((-0.25 * ((h / g) * (h / a))));
}
function code(g, h, a) return Float64(cbrt(Float64(Float64(-g) / a)) + cbrt(Float64(-0.25 * Float64(Float64(h / g) * Float64(h / a))))) end
code[g_, h_, a_] := N[(N[Power[N[((-g) / a), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(-0.25 * N[(N[(h / g), $MachinePrecision] * N[(h / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{-g}{a}} + \sqrt[3]{-0.25 \cdot \left(\frac{h}{g} \cdot \frac{h}{a}\right)}
\end{array}
Initial program 41.8%
Taylor expanded in g around inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6426.4
Applied rewrites26.4%
Taylor expanded in g around inf
lower-*.f64N/A
lower-cbrt.f64N/A
unpow2N/A
times-fracN/A
lower-*.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-cbrt.f64N/A
lower-cbrt.f6474.7
Applied rewrites74.7%
lift-+.f64N/A
+-commutativeN/A
lower-+.f6474.7
Applied rewrites74.7%
(FPCore (g h a) :precision binary64 0.0)
double code(double g, double h, double a) {
return 0.0;
}
real(8) function code(g, h, a)
real(8), intent (in) :: g
real(8), intent (in) :: h
real(8), intent (in) :: a
code = 0.0d0
end function
public static double code(double g, double h, double a) {
return 0.0;
}
def code(g, h, a): return 0.0
function code(g, h, a) return 0.0 end
function tmp = code(g, h, a) tmp = 0.0; end
code[g_, h_, a_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 41.8%
lift-cbrt.f64N/A
lift-*.f64N/A
lift-/.f64N/A
associate-*l/N/A
lift-*.f64N/A
*-commutativeN/A
times-fracN/A
cbrt-prodN/A
lower-*.f64N/A
lower-cbrt.f64N/A
inv-powN/A
lower-pow.f64N/A
lower-cbrt.f64N/A
div-invN/A
metadata-evalN/A
Applied rewrites45.2%
Taylor expanded in g around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-*.f64N/A
lower-cbrt.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
lower-*.f64N/A
lower-cbrt.f642.9
Applied rewrites2.9%
Applied rewrites2.9%
herbie shell --seed 2024312
(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))))))))