
(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 4 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}
h_m = (fabs.f64 h)
(FPCore (g h_m a)
:precision binary64
(if (<= h_m 4.5e+153)
(+
(* (cbrt (* 0.5 (pow a -1.0))) (cbrt (+ (* -1.0 g) (* -1.0 g))))
(cbrt (* (/ 1.0 (* 2.0 a)) (* (/ (* h_m h_m) g) -0.5))))
(* (cbrt (/ g a)) -1.0)))h_m = fabs(h);
double code(double g, double h_m, double a) {
double tmp;
if (h_m <= 4.5e+153) {
tmp = (cbrt((0.5 * pow(a, -1.0))) * cbrt(((-1.0 * g) + (-1.0 * g)))) + cbrt(((1.0 / (2.0 * a)) * (((h_m * h_m) / g) * -0.5)));
} else {
tmp = cbrt((g / a)) * -1.0;
}
return tmp;
}
h_m = Math.abs(h);
public static double code(double g, double h_m, double a) {
double tmp;
if (h_m <= 4.5e+153) {
tmp = (Math.cbrt((0.5 * Math.pow(a, -1.0))) * Math.cbrt(((-1.0 * g) + (-1.0 * g)))) + Math.cbrt(((1.0 / (2.0 * a)) * (((h_m * h_m) / g) * -0.5)));
} else {
tmp = Math.cbrt((g / a)) * -1.0;
}
return tmp;
}
h_m = abs(h) function code(g, h_m, a) tmp = 0.0 if (h_m <= 4.5e+153) tmp = Float64(Float64(cbrt(Float64(0.5 * (a ^ -1.0))) * cbrt(Float64(Float64(-1.0 * g) + Float64(-1.0 * g)))) + cbrt(Float64(Float64(1.0 / Float64(2.0 * a)) * Float64(Float64(Float64(h_m * h_m) / g) * -0.5)))); else tmp = Float64(cbrt(Float64(g / a)) * -1.0); end return tmp end
h_m = N[Abs[h], $MachinePrecision] code[g_, h$95$m_, a_] := If[LessEqual[h$95$m, 4.5e+153], N[(N[(N[Power[N[(0.5 * N[Power[a, -1.0], $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[N[(N[(-1.0 * g), $MachinePrecision] + N[(-1.0 * g), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision] + N[Power[N[(N[(1.0 / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] * N[(N[(N[(h$95$m * h$95$m), $MachinePrecision] / g), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], N[(N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision] * -1.0), $MachinePrecision]]
\begin{array}{l}
h_m = \left|h\right|
\\
\begin{array}{l}
\mathbf{if}\;h\_m \leq 4.5 \cdot 10^{+153}:\\
\;\;\;\;\sqrt[3]{0.5 \cdot {a}^{-1}} \cdot \sqrt[3]{-1 \cdot g + -1 \cdot g} + \sqrt[3]{\frac{1}{2 \cdot a} \cdot \left(\frac{h\_m \cdot h\_m}{g} \cdot -0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{\frac{g}{a}} \cdot -1\\
\end{array}
\end{array}
if h < 4.5000000000000001e153Initial program 45.6%
Taylor expanded in g around -inf
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
pow2N/A
lift-*.f6423.3
Applied rewrites23.3%
Taylor expanded in g around -inf
lift-*.f6476.4
Applied rewrites76.4%
lift-cbrt.f64N/A
lift-*.f64N/A
cbrt-prodN/A
lower-*.f64N/A
lower-cbrt.f64N/A
lift-*.f64N/A
lift-/.f64N/A
inv-powN/A
unpow-prod-downN/A
metadata-evalN/A
inv-powN/A
lower-*.f64N/A
inv-powN/A
lift-pow.f64N/A
lower-cbrt.f6491.2
Applied rewrites91.2%
if 4.5000000000000001e153 < h Initial program 0.0%
Taylor expanded in g around -inf
*-commutativeN/A
lower-*.f64N/A
cbrt-unprodN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
pow1/3N/A
lower-pow.f64N/A
lower-/.f6421.6
Applied rewrites21.6%
lift-/.f64N/A
lift-pow.f64N/A
pow1/3N/A
lower-cbrt.f64N/A
lift-/.f6442.5
Applied rewrites42.5%
Final simplification88.0%
h_m = (fabs.f64 h) (FPCore (g h_m a) :precision binary64 (if (<= g 9e-206) (* (cbrt (/ g a)) -1.0) (* (/ (pow g 0.3333333333333333) (cbrt a)) -1.0)))
h_m = fabs(h);
double code(double g, double h_m, double a) {
double tmp;
if (g <= 9e-206) {
tmp = cbrt((g / a)) * -1.0;
} else {
tmp = (pow(g, 0.3333333333333333) / cbrt(a)) * -1.0;
}
return tmp;
}
h_m = Math.abs(h);
public static double code(double g, double h_m, double a) {
double tmp;
if (g <= 9e-206) {
tmp = Math.cbrt((g / a)) * -1.0;
} else {
tmp = (Math.pow(g, 0.3333333333333333) / Math.cbrt(a)) * -1.0;
}
return tmp;
}
h_m = abs(h) function code(g, h_m, a) tmp = 0.0 if (g <= 9e-206) tmp = Float64(cbrt(Float64(g / a)) * -1.0); else tmp = Float64(Float64((g ^ 0.3333333333333333) / cbrt(a)) * -1.0); end return tmp end
h_m = N[Abs[h], $MachinePrecision] code[g_, h$95$m_, a_] := If[LessEqual[g, 9e-206], N[(N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision] * -1.0), $MachinePrecision], N[(N[(N[Power[g, 0.3333333333333333], $MachinePrecision] / N[Power[a, 1/3], $MachinePrecision]), $MachinePrecision] * -1.0), $MachinePrecision]]
\begin{array}{l}
h_m = \left|h\right|
\\
\begin{array}{l}
\mathbf{if}\;g \leq 9 \cdot 10^{-206}:\\
\;\;\;\;\sqrt[3]{\frac{g}{a}} \cdot -1\\
\mathbf{else}:\\
\;\;\;\;\frac{{g}^{0.3333333333333333}}{\sqrt[3]{a}} \cdot -1\\
\end{array}
\end{array}
if g < 8.9999999999999996e-206Initial program 37.7%
Taylor expanded in g around -inf
*-commutativeN/A
lower-*.f64N/A
cbrt-unprodN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
pow1/3N/A
lower-pow.f64N/A
lower-/.f6435.7
Applied rewrites35.7%
lift-/.f64N/A
lift-pow.f64N/A
pow1/3N/A
lower-cbrt.f64N/A
lift-/.f6471.7
Applied rewrites71.7%
if 8.9999999999999996e-206 < g Initial program 47.3%
Taylor expanded in g around -inf
*-commutativeN/A
lower-*.f64N/A
cbrt-unprodN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
pow1/3N/A
lower-pow.f64N/A
lower-/.f6440.4
Applied rewrites40.4%
lift-/.f64N/A
lift-pow.f64N/A
pow1/3N/A
cbrt-divN/A
lower-/.f64N/A
pow1/3N/A
lower-pow.f64N/A
lower-cbrt.f6490.2
Applied rewrites90.2%
Final simplification81.1%
h_m = (fabs.f64 h) (FPCore (g h_m a) :precision binary64 (* (cbrt (/ g a)) -1.0))
h_m = fabs(h);
double code(double g, double h_m, double a) {
return cbrt((g / a)) * -1.0;
}
h_m = Math.abs(h);
public static double code(double g, double h_m, double a) {
return Math.cbrt((g / a)) * -1.0;
}
h_m = abs(h) function code(g, h_m, a) return Float64(cbrt(Float64(g / a)) * -1.0) end
h_m = N[Abs[h], $MachinePrecision] code[g_, h$95$m_, a_] := N[(N[Power[N[(g / a), $MachinePrecision], 1/3], $MachinePrecision] * -1.0), $MachinePrecision]
\begin{array}{l}
h_m = \left|h\right|
\\
\sqrt[3]{\frac{g}{a}} \cdot -1
\end{array}
Initial program 42.6%
Taylor expanded in g around -inf
*-commutativeN/A
lower-*.f64N/A
cbrt-unprodN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
pow1/3N/A
lower-pow.f64N/A
lower-/.f6438.1
Applied rewrites38.1%
lift-/.f64N/A
lift-pow.f64N/A
pow1/3N/A
lower-cbrt.f64N/A
lift-/.f6475.4
Applied rewrites75.4%
Final simplification75.4%
h_m = (fabs.f64 h) (FPCore (g h_m a) :precision binary64 (* (pow (exp 0.3333333333333333) (- (log g) (log a))) -1.0))
h_m = fabs(h);
double code(double g, double h_m, double a) {
return pow(exp(0.3333333333333333), (log(g) - log(a))) * -1.0;
}
h_m = private
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(g, h_m, a)
use fmin_fmax_functions
real(8), intent (in) :: g
real(8), intent (in) :: h_m
real(8), intent (in) :: a
code = (exp(0.3333333333333333d0) ** (log(g) - log(a))) * (-1.0d0)
end function
h_m = Math.abs(h);
public static double code(double g, double h_m, double a) {
return Math.pow(Math.exp(0.3333333333333333), (Math.log(g) - Math.log(a))) * -1.0;
}
h_m = math.fabs(h) def code(g, h_m, a): return math.pow(math.exp(0.3333333333333333), (math.log(g) - math.log(a))) * -1.0
h_m = abs(h) function code(g, h_m, a) return Float64((exp(0.3333333333333333) ^ Float64(log(g) - log(a))) * -1.0) end
h_m = abs(h); function tmp = code(g, h_m, a) tmp = (exp(0.3333333333333333) ^ (log(g) - log(a))) * -1.0; end
h_m = N[Abs[h], $MachinePrecision] code[g_, h$95$m_, a_] := N[(N[Power[N[Exp[0.3333333333333333], $MachinePrecision], N[(N[Log[g], $MachinePrecision] - N[Log[a], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -1.0), $MachinePrecision]
\begin{array}{l}
h_m = \left|h\right|
\\
{\left(e^{0.3333333333333333}\right)}^{\left(\log g - \log a\right)} \cdot -1
\end{array}
Initial program 42.6%
Taylor expanded in g around -inf
*-commutativeN/A
lower-*.f64N/A
cbrt-unprodN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
pow1/3N/A
lower-pow.f64N/A
lower-/.f6438.1
Applied rewrites38.1%
lift-/.f64N/A
lift-pow.f64N/A
pow1/3N/A
lower-cbrt.f64N/A
lift-/.f6475.4
Applied rewrites75.4%
lift-/.f64N/A
lift-cbrt.f64N/A
pow1/3N/A
exp-to-powN/A
lift-log.f64N/A
lift-/.f64N/A
lift-*.f64N/A
lift-exp.f6438.1
lift-/.f64N/A
lift-log.f64N/A
log-divN/A
lower--.f64N/A
lower-log.f64N/A
lower-log.f6423.0
Applied rewrites23.0%
Taylor expanded in g around inf
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
lower--.f64N/A
lower-*.f64N/A
log-divN/A
metadata-evalN/A
lower--.f64N/A
lift-log.f64N/A
lift-log.f6423.1
Applied rewrites23.1%
Final simplification23.1%
herbie shell --seed 2025065
(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))))))))