
(FPCore (g h) :precision binary64 (* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))
double code(double g, double h) {
return 2.0 * cos((((2.0 * ((double) M_PI)) / 3.0) + (acos((-g / h)) / 3.0)));
}
public static double code(double g, double h) {
return 2.0 * Math.cos((((2.0 * Math.PI) / 3.0) + (Math.acos((-g / h)) / 3.0)));
}
def code(g, h): return 2.0 * math.cos((((2.0 * math.pi) / 3.0) + (math.acos((-g / h)) / 3.0)))
function code(g, h) return Float64(2.0 * cos(Float64(Float64(Float64(2.0 * pi) / 3.0) + Float64(acos(Float64(Float64(-g) / h)) / 3.0)))) end
function tmp = code(g, h) tmp = 2.0 * cos((((2.0 * pi) / 3.0) + (acos((-g / h)) / 3.0))); end
code[g_, h_] := N[(2.0 * N[Cos[N[(N[(N[(2.0 * Pi), $MachinePrecision] / 3.0), $MachinePrecision] + N[(N[ArcCos[N[((-g) / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \cos \left(\frac{2 \cdot \pi}{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)))))
double code(double g, double h) {
return 2.0 * cos((((2.0 * ((double) M_PI)) / 3.0) + (acos((-g / h)) / 3.0)));
}
public static double code(double g, double h) {
return 2.0 * Math.cos((((2.0 * Math.PI) / 3.0) + (Math.acos((-g / h)) / 3.0)));
}
def code(g, h): return 2.0 * math.cos((((2.0 * math.pi) / 3.0) + (math.acos((-g / h)) / 3.0)))
function code(g, h) return Float64(2.0 * cos(Float64(Float64(Float64(2.0 * pi) / 3.0) + Float64(acos(Float64(Float64(-g) / h)) / 3.0)))) end
function tmp = code(g, h) tmp = 2.0 * cos((((2.0 * pi) / 3.0) + (acos((-g / h)) / 3.0))); end
code[g_, h_] := N[(2.0 * N[Cos[N[(N[(N[(2.0 * Pi), $MachinePrecision] / 3.0), $MachinePrecision] + N[(N[ArcCos[N[((-g) / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \cos \left(\frac{2 \cdot \pi}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)
\end{array}
(FPCore (g h)
:precision binary64
(let* ((t_0 (/ (sqrt 3.0) 2.0))
(t_1 (* t_0 t_0))
(t_2 (* (acos (- (/ g h))) 0.3333333333333333))
(t_3 (cos t_2))
(t_4 (* (- 0.25 t_1) t_3))
(t_5 (* (sin t_2) (* (* t_0 0.5) -2.0))))
(*
2.0
(/
(+ (pow t_4 3.0) (pow t_5 3.0))
(+ (pow t_4 2.0) (+ (* t_5 t_5) (* t_5 (* t_3 (- t_1 0.25)))))))))
double code(double g, double h) {
double t_0 = sqrt(3.0) / 2.0;
double t_1 = t_0 * t_0;
double t_2 = acos(-(g / h)) * 0.3333333333333333;
double t_3 = cos(t_2);
double t_4 = (0.25 - t_1) * t_3;
double t_5 = sin(t_2) * ((t_0 * 0.5) * -2.0);
return 2.0 * ((pow(t_4, 3.0) + pow(t_5, 3.0)) / (pow(t_4, 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))));
}
real(8) function code(g, h)
real(8), intent (in) :: g
real(8), intent (in) :: h
real(8) :: t_0
real(8) :: t_1
real(8) :: t_2
real(8) :: t_3
real(8) :: t_4
real(8) :: t_5
t_0 = sqrt(3.0d0) / 2.0d0
t_1 = t_0 * t_0
t_2 = acos(-(g / h)) * 0.3333333333333333d0
t_3 = cos(t_2)
t_4 = (0.25d0 - t_1) * t_3
t_5 = sin(t_2) * ((t_0 * 0.5d0) * (-2.0d0))
code = 2.0d0 * (((t_4 ** 3.0d0) + (t_5 ** 3.0d0)) / ((t_4 ** 2.0d0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25d0))))))
end function
public static double code(double g, double h) {
double t_0 = Math.sqrt(3.0) / 2.0;
double t_1 = t_0 * t_0;
double t_2 = Math.acos(-(g / h)) * 0.3333333333333333;
double t_3 = Math.cos(t_2);
double t_4 = (0.25 - t_1) * t_3;
double t_5 = Math.sin(t_2) * ((t_0 * 0.5) * -2.0);
return 2.0 * ((Math.pow(t_4, 3.0) + Math.pow(t_5, 3.0)) / (Math.pow(t_4, 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))));
}
def code(g, h): t_0 = math.sqrt(3.0) / 2.0 t_1 = t_0 * t_0 t_2 = math.acos(-(g / h)) * 0.3333333333333333 t_3 = math.cos(t_2) t_4 = (0.25 - t_1) * t_3 t_5 = math.sin(t_2) * ((t_0 * 0.5) * -2.0) return 2.0 * ((math.pow(t_4, 3.0) + math.pow(t_5, 3.0)) / (math.pow(t_4, 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25))))))
function code(g, h) t_0 = Float64(sqrt(3.0) / 2.0) t_1 = Float64(t_0 * t_0) t_2 = Float64(acos(Float64(-Float64(g / h))) * 0.3333333333333333) t_3 = cos(t_2) t_4 = Float64(Float64(0.25 - t_1) * t_3) t_5 = Float64(sin(t_2) * Float64(Float64(t_0 * 0.5) * -2.0)) return Float64(2.0 * Float64(Float64((t_4 ^ 3.0) + (t_5 ^ 3.0)) / Float64((t_4 ^ 2.0) + Float64(Float64(t_5 * t_5) + Float64(t_5 * Float64(t_3 * Float64(t_1 - 0.25))))))) end
function tmp = code(g, h) t_0 = sqrt(3.0) / 2.0; t_1 = t_0 * t_0; t_2 = acos(-(g / h)) * 0.3333333333333333; t_3 = cos(t_2); t_4 = (0.25 - t_1) * t_3; t_5 = sin(t_2) * ((t_0 * 0.5) * -2.0); tmp = 2.0 * (((t_4 ^ 3.0) + (t_5 ^ 3.0)) / ((t_4 ^ 2.0) + ((t_5 * t_5) + (t_5 * (t_3 * (t_1 - 0.25)))))); end
code[g_, h_] := Block[{t$95$0 = N[(N[Sqrt[3.0], $MachinePrecision] / 2.0), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]}, Block[{t$95$3 = N[Cos[t$95$2], $MachinePrecision]}, Block[{t$95$4 = N[(N[(0.25 - t$95$1), $MachinePrecision] * t$95$3), $MachinePrecision]}, Block[{t$95$5 = N[(N[Sin[t$95$2], $MachinePrecision] * N[(N[(t$95$0 * 0.5), $MachinePrecision] * -2.0), $MachinePrecision]), $MachinePrecision]}, N[(2.0 * N[(N[(N[Power[t$95$4, 3.0], $MachinePrecision] + N[Power[t$95$5, 3.0], $MachinePrecision]), $MachinePrecision] / N[(N[Power[t$95$4, 2.0], $MachinePrecision] + N[(N[(t$95$5 * t$95$5), $MachinePrecision] + N[(t$95$5 * N[(t$95$3 * N[(t$95$1 - 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sqrt{3}}{2}\\
t_1 := t\_0 \cdot t\_0\\
t_2 := \cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\\
t_3 := \cos t\_2\\
t_4 := \left(0.25 - t\_1\right) \cdot t\_3\\
t_5 := \sin t\_2 \cdot \left(\left(t\_0 \cdot 0.5\right) \cdot -2\right)\\
2 \cdot \frac{{t\_4}^{3} + {t\_5}^{3}}{{t\_4}^{2} + \left(t\_5 \cdot t\_5 + t\_5 \cdot \left(t\_3 \cdot \left(t\_1 - 0.25\right)\right)\right)}
\end{array}
\end{array}
Initial program 98.4%
Applied rewrites100.0%
Final simplification100.0%
(FPCore (g h) :precision binary64 (let* ((t_0 (* (acos (- (/ g h))) 0.3333333333333333))) (- (fma (sqrt 3.0) (sin t_0) (cos t_0)))))
double code(double g, double h) {
double t_0 = acos(-(g / h)) * 0.3333333333333333;
return -fma(sqrt(3.0), sin(t_0), cos(t_0));
}
function code(g, h) t_0 = Float64(acos(Float64(-Float64(g / h))) * 0.3333333333333333) return Float64(-fma(sqrt(3.0), sin(t_0), cos(t_0))) end
code[g_, h_] := Block[{t$95$0 = N[(N[ArcCos[(-N[(g / h), $MachinePrecision])], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]}, (-N[(N[Sqrt[3.0], $MachinePrecision] * N[Sin[t$95$0], $MachinePrecision] + N[Cos[t$95$0], $MachinePrecision]), $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos^{-1} \left(-\frac{g}{h}\right) \cdot 0.3333333333333333\\
-\mathsf{fma}\left(\sqrt{3}, \sin t\_0, \cos t\_0\right)
\end{array}
\end{array}
Initial program 98.5%
Applied rewrites99.9%
Applied rewrites100.0%
Taylor expanded in g around 0
Applied rewrites100.0%
Final simplification100.0%
herbie shell --seed 2024227
(FPCore (g h)
:name "2-ancestry mixing, negative discriminant"
:precision binary64
(* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))