
(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 (+ (* (/ 1.0 (cbrt a)) (cbrt (- g))) (cbrt (* (- g g) (/ -0.5 a)))))
double code(double g, double h, double a) {
return ((1.0 / cbrt(a)) * cbrt(-g)) + cbrt(((g - g) * (-0.5 / a)));
}
public static double code(double g, double h, double a) {
return ((1.0 / Math.cbrt(a)) * Math.cbrt(-g)) + Math.cbrt(((g - g) * (-0.5 / a)));
}
function code(g, h, a) return Float64(Float64(Float64(1.0 / cbrt(a)) * cbrt(Float64(-g))) + cbrt(Float64(Float64(g - g) * Float64(-0.5 / a)))) end
code[g_, h_, a_] := N[(N[(N[(1.0 / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * N[Power[(-g), 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{1}{\sqrt[3]{a}} \cdot \sqrt[3]{-g} + \sqrt[3]{\left(g - g\right) \cdot \frac{-0.5}{a}}
\end{array}
Initial program 41.7%
Simplified41.7%
Taylor expanded in g around -inf 28.6%
associate-*r/28.6%
neg-mul-128.6%
Simplified28.6%
pow1/313.1%
div-inv13.1%
unpow-prod-down13.5%
pow1/316.6%
Applied egg-rr16.6%
*-commutative16.6%
unpow1/334.6%
Simplified34.6%
Taylor expanded in g around -inf 95.9%
neg-mul-195.9%
Simplified95.9%
*-un-lft-identity95.9%
cbrt-div95.9%
metadata-eval95.9%
Applied egg-rr95.9%
Simplified95.9%
Final simplification95.9%
(FPCore (g h a) :precision binary64 (* (cbrt (- g)) (cbrt (/ 1.0 a))))
double code(double g, double h, double a) {
return cbrt(-g) * cbrt((1.0 / a));
}
public static double code(double g, double h, double a) {
return Math.cbrt(-g) * Math.cbrt((1.0 / a));
}
function code(g, h, a) return Float64(cbrt(Float64(-g)) * cbrt(Float64(1.0 / a))) end
code[g_, h_, a_] := N[(N[Power[(-g), 1/3], $MachinePrecision] * N[Power[N[(1.0 / a), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{-g} \cdot \sqrt[3]{\frac{1}{a}}
\end{array}
Initial program 41.7%
Simplified41.7%
Taylor expanded in g around -inf 28.6%
associate-*r/28.6%
neg-mul-128.6%
Simplified28.6%
pow1/313.1%
div-inv13.1%
unpow-prod-down13.5%
pow1/316.6%
Applied egg-rr16.6%
*-commutative16.6%
unpow1/334.6%
Simplified34.6%
Taylor expanded in g around -inf 95.9%
neg-mul-195.9%
Simplified95.9%
Taylor expanded in g around 0 95.9%
Final simplification95.9%
(FPCore (g h a) :precision binary64 (* (cbrt (/ g a)) -2.0))
double code(double g, double h, double a) {
return cbrt((g / a)) * -2.0;
}
public static double code(double g, double h, double a) {
return Math.cbrt((g / a)) * -2.0;
}
function code(g, h, a) return Float64(cbrt(Float64(g / a)) * -2.0) end
code[g_, h_, a_] := N[(N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision] * -2.0), $MachinePrecision]
\begin{array}{l}
\\
\sqrt[3]{\frac{g}{a}} \cdot -2
\end{array}
Initial program 41.7%
Simplified41.7%
Taylor expanded in g around -inf 28.6%
associate-*r/28.6%
neg-mul-128.6%
Simplified28.6%
Taylor expanded in g around inf 15.5%
Taylor expanded in g around -inf 15.5%
Simplified15.5%
(FPCore (g h a) :precision binary64 (if (<= a -2e-310) g (- g)))
double code(double g, double h, double a) {
double tmp;
if (a <= -2e-310) {
tmp = g;
} else {
tmp = -g;
}
return tmp;
}
real(8) function code(g, h, a)
real(8), intent (in) :: g
real(8), intent (in) :: h
real(8), intent (in) :: a
real(8) :: tmp
if (a <= (-2d-310)) then
tmp = g
else
tmp = -g
end if
code = tmp
end function
public static double code(double g, double h, double a) {
double tmp;
if (a <= -2e-310) {
tmp = g;
} else {
tmp = -g;
}
return tmp;
}
def code(g, h, a): tmp = 0 if a <= -2e-310: tmp = g else: tmp = -g return tmp
function code(g, h, a) tmp = 0.0 if (a <= -2e-310) tmp = g; else tmp = Float64(-g); end return tmp end
function tmp_2 = code(g, h, a) tmp = 0.0; if (a <= -2e-310) tmp = g; else tmp = -g; end tmp_2 = tmp; end
code[g_, h_, a_] := If[LessEqual[a, -2e-310], g, (-g)]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2 \cdot 10^{-310}:\\
\;\;\;\;g\\
\mathbf{else}:\\
\;\;\;\;-g\\
\end{array}
\end{array}
if a < -1.999999999999994e-310Initial program 44.8%
Simplified44.8%
Taylor expanded in g around -inf 30.3%
associate-*r/30.3%
neg-mul-130.3%
Simplified30.3%
Taylor expanded in g around inf 8.9%
flip-+7.2%
Applied egg-rr0.0%
Simplified0.0%
Applied egg-rr3.7%
rem-log-exp6.1%
Simplified6.1%
if -1.999999999999994e-310 < a Initial program 38.6%
Simplified38.6%
Taylor expanded in g around -inf 27.0%
associate-*r/27.0%
neg-mul-127.0%
Simplified27.0%
Taylor expanded in g around inf 9.5%
flip-+8.0%
Applied egg-rr0.0%
Simplified0.0%
Applied egg-rr6.4%
(FPCore (g h a) :precision binary64 g)
double code(double g, double h, double a) {
return g;
}
real(8) function code(g, h, a)
real(8), intent (in) :: g
real(8), intent (in) :: h
real(8), intent (in) :: a
code = g
end function
public static double code(double g, double h, double a) {
return g;
}
def code(g, h, a): return g
function code(g, h, a) return g end
function tmp = code(g, h, a) tmp = g; end
code[g_, h_, a_] := g
\begin{array}{l}
\\
g
\end{array}
Initial program 41.7%
Simplified41.7%
Taylor expanded in g around -inf 28.6%
associate-*r/28.6%
neg-mul-128.6%
Simplified28.6%
Taylor expanded in g around inf 9.2%
flip-+7.6%
Applied egg-rr0.0%
Simplified0.0%
Applied egg-rr2.5%
rem-log-exp3.7%
Simplified3.7%
herbie shell --seed 2024112
(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))))))))