
(FPCore (g h) :precision binary64 (* 2.0 (cos (+ (/ (* 2.0 (PI)) 3.0) (/ (acos (/ (- g) h)) 3.0)))))
\begin{array}{l}
\\
2 \cdot \cos \left(\frac{2 \cdot \mathsf{PI}\left(\right)}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (g h) :precision binary64 (* 2.0 (cos (+ (/ (* 2.0 (PI)) 3.0) (/ (acos (/ (- g) h)) 3.0)))))
\begin{array}{l}
\\
2 \cdot \cos \left(\frac{2 \cdot \mathsf{PI}\left(\right)}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)
\end{array}
(FPCore (g h)
:precision binary64
(let* ((t_0 (acos (- (/ g h)))) (t_1 (* 0.3333333333333333 t_0)))
(-
(* (sin t_1) (- (sqrt 3.0)))
(cos (* (sqrt (* 0.3333333333333333 t_1)) (sqrt t_0))))))
double code(double g, double h) {
double t_0 = acos(-(g / h));
double t_1 = 0.3333333333333333 * t_0;
return (sin(t_1) * -sqrt(3.0)) - cos((sqrt((0.3333333333333333 * t_1)) * sqrt(t_0)));
}
real(8) function code(g, h)
real(8), intent (in) :: g
real(8), intent (in) :: h
real(8) :: t_0
real(8) :: t_1
t_0 = acos(-(g / h))
t_1 = 0.3333333333333333d0 * t_0
code = (sin(t_1) * -sqrt(3.0d0)) - cos((sqrt((0.3333333333333333d0 * t_1)) * sqrt(t_0)))
end function
public static double code(double g, double h) {
double t_0 = Math.acos(-(g / h));
double t_1 = 0.3333333333333333 * t_0;
return (Math.sin(t_1) * -Math.sqrt(3.0)) - Math.cos((Math.sqrt((0.3333333333333333 * t_1)) * Math.sqrt(t_0)));
}
def code(g, h): t_0 = math.acos(-(g / h)) t_1 = 0.3333333333333333 * t_0 return (math.sin(t_1) * -math.sqrt(3.0)) - math.cos((math.sqrt((0.3333333333333333 * t_1)) * math.sqrt(t_0)))
function code(g, h) t_0 = acos(Float64(-Float64(g / h))) t_1 = Float64(0.3333333333333333 * t_0) return Float64(Float64(sin(t_1) * Float64(-sqrt(3.0))) - cos(Float64(sqrt(Float64(0.3333333333333333 * t_1)) * sqrt(t_0)))) end
function tmp = code(g, h) t_0 = acos(-(g / h)); t_1 = 0.3333333333333333 * t_0; tmp = (sin(t_1) * -sqrt(3.0)) - cos((sqrt((0.3333333333333333 * t_1)) * sqrt(t_0))); end
code[g_, h_] := Block[{t$95$0 = N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision]}, Block[{t$95$1 = N[(0.3333333333333333 * t$95$0), $MachinePrecision]}, N[(N[(N[Sin[t$95$1], $MachinePrecision] * (-N[Sqrt[3.0], $MachinePrecision])), $MachinePrecision] - N[Cos[N[(N[Sqrt[N[(0.3333333333333333 * t$95$1), $MachinePrecision]], $MachinePrecision] * N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos^{-1} \left(-\frac{g}{h}\right)\\
t_1 := 0.3333333333333333 \cdot t\_0\\
\sin t\_1 \cdot \left(-\sqrt{3}\right) - \cos \left(\sqrt{0.3333333333333333 \cdot t\_1} \cdot \sqrt{t\_0}\right)
\end{array}
\end{array}
Initial program 98.5%
Applied rewrites98.4%
Applied rewrites99.9%
Applied rewrites99.9%
lift-/.f64N/A
lift-neg.f64N/A
lift-acos.f64N/A
lift-*.f6499.9
rem-square-sqrtN/A
lift-sqrt.f64N/A
lift-*.f64N/A
sqrt-prodN/A
associate-*r*N/A
lower-*.f64N/A
Applied rewrites100.0%
Final simplification100.0%
(FPCore (g h) :precision binary64 (let* ((t_0 (* 0.3333333333333333 (acos (- (/ g h)))))) (- (* (sin t_0) (- (sqrt 3.0))) (cos t_0))))
double code(double g, double h) {
double t_0 = 0.3333333333333333 * acos(-(g / h));
return (sin(t_0) * -sqrt(3.0)) - cos(t_0);
}
real(8) function code(g, h)
real(8), intent (in) :: g
real(8), intent (in) :: h
real(8) :: t_0
t_0 = 0.3333333333333333d0 * acos(-(g / h))
code = (sin(t_0) * -sqrt(3.0d0)) - cos(t_0)
end function
public static double code(double g, double h) {
double t_0 = 0.3333333333333333 * Math.acos(-(g / h));
return (Math.sin(t_0) * -Math.sqrt(3.0)) - Math.cos(t_0);
}
def code(g, h): t_0 = 0.3333333333333333 * math.acos(-(g / h)) return (math.sin(t_0) * -math.sqrt(3.0)) - math.cos(t_0)
function code(g, h) t_0 = Float64(0.3333333333333333 * acos(Float64(-Float64(g / h)))) return Float64(Float64(sin(t_0) * Float64(-sqrt(3.0))) - cos(t_0)) end
function tmp = code(g, h) t_0 = 0.3333333333333333 * acos(-(g / h)); tmp = (sin(t_0) * -sqrt(3.0)) - cos(t_0); end
code[g_, h_] := Block[{t$95$0 = N[(0.3333333333333333 * N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]}, N[(N[(N[Sin[t$95$0], $MachinePrecision] * (-N[Sqrt[3.0], $MachinePrecision])), $MachinePrecision] - N[Cos[t$95$0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.3333333333333333 \cdot \cos^{-1} \left(-\frac{g}{h}\right)\\
\sin t\_0 \cdot \left(-\sqrt{3}\right) - \cos t\_0
\end{array}
\end{array}
Initial program 98.5%
Applied rewrites98.4%
Applied rewrites99.9%
Applied rewrites99.9%
(FPCore (g h) :precision binary64 (let* ((t_0 (* 0.3333333333333333 (acos (- (/ g h)))))) (- (fma (sin t_0) (sqrt 3.0) (cos t_0)))))
double code(double g, double h) {
double t_0 = 0.3333333333333333 * acos(-(g / h));
return -fma(sin(t_0), sqrt(3.0), cos(t_0));
}
function code(g, h) t_0 = Float64(0.3333333333333333 * acos(Float64(-Float64(g / h)))) return Float64(-fma(sin(t_0), sqrt(3.0), cos(t_0))) end
code[g_, h_] := Block[{t$95$0 = N[(0.3333333333333333 * N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]}, (-N[(N[Sin[t$95$0], $MachinePrecision] * N[Sqrt[3.0], $MachinePrecision] + N[Cos[t$95$0], $MachinePrecision]), $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.3333333333333333 \cdot \cos^{-1} \left(-\frac{g}{h}\right)\\
-\mathsf{fma}\left(\sin t\_0, \sqrt{3}, \cos t\_0\right)
\end{array}
\end{array}
Initial program 98.5%
Applied rewrites98.4%
Taylor expanded in g around 0
Applied rewrites99.9%
Final simplification99.9%
(FPCore (g h) :precision binary64 (* 2.0 (cos (* 0.3333333333333333 (fma 2.0 (PI) (acos (- (/ g h))))))))
\begin{array}{l}
\\
2 \cdot \cos \left(0.3333333333333333 \cdot \mathsf{fma}\left(2, \mathsf{PI}\left(\right), \cos^{-1} \left(-\frac{g}{h}\right)\right)\right)
\end{array}
Initial program 98.5%
lift-PI.f64N/A
lift-*.f64N/A
frac-2negN/A
lift-neg.f64N/A
lift-/.f64N/A
lift-acos.f64N/A
frac-2negN/A
frac-2negN/A
div-invN/A
frac-2negN/A
div-invN/A
distribute-rgt-outN/A
lower-*.f64N/A
metadata-evalN/A
lift-*.f64N/A
lower-fma.f6498.5
lift-/.f64N/A
frac-2negN/A
Applied rewrites98.5%
Final simplification98.5%
herbie shell --seed 2024216
(FPCore (g h)
:name "2-ancestry mixing, negative discriminant"
:precision binary64
(* 2.0 (cos (+ (/ (* 2.0 (PI)) 3.0) (/ (acos (/ (- g) h)) 3.0)))))