
(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 (acos (/ (* -1.0 g) h))))
(-
(*
(sin (* PI 1.1666666666666667))
(sin
(fma
(pow (pow PI 0.3333333333333333) 2.0)
(/ (pow PI 0.3333333333333333) 2.0)
(* 0.3333333333333333 (acos (/ (* g -1.0) h))))))
(-
(* (sin (/ t_0 3.0)) (sin (* 0.6666666666666666 PI)))
(sin
(/
(fma (fma (* 2.0 (pow PI 0.5)) (pow PI 0.5) t_0) 2.0 (* 3.0 PI))
6.0))))))
double code(double g, double h) {
double t_0 = acos(((-1.0 * g) / h));
return (sin((((double) M_PI) * 1.1666666666666667)) * sin(fma(pow(pow(((double) M_PI), 0.3333333333333333), 2.0), (pow(((double) M_PI), 0.3333333333333333) / 2.0), (0.3333333333333333 * acos(((g * -1.0) / h)))))) - ((sin((t_0 / 3.0)) * sin((0.6666666666666666 * ((double) M_PI)))) - sin((fma(fma((2.0 * pow(((double) M_PI), 0.5)), pow(((double) M_PI), 0.5), t_0), 2.0, (3.0 * ((double) M_PI))) / 6.0)));
}
function code(g, h) t_0 = acos(Float64(Float64(-1.0 * g) / h)) return Float64(Float64(sin(Float64(pi * 1.1666666666666667)) * sin(fma(((pi ^ 0.3333333333333333) ^ 2.0), Float64((pi ^ 0.3333333333333333) / 2.0), Float64(0.3333333333333333 * acos(Float64(Float64(g * -1.0) / h)))))) - Float64(Float64(sin(Float64(t_0 / 3.0)) * sin(Float64(0.6666666666666666 * pi))) - sin(Float64(fma(fma(Float64(2.0 * (pi ^ 0.5)), (pi ^ 0.5), t_0), 2.0, Float64(3.0 * pi)) / 6.0)))) end
code[g_, h_] := Block[{t$95$0 = N[ArcCos[N[(N[(-1.0 * g), $MachinePrecision] / h), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[Sin[N[(Pi * 1.1666666666666667), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(N[Power[N[Power[Pi, 0.3333333333333333], $MachinePrecision], 2.0], $MachinePrecision] * N[(N[Power[Pi, 0.3333333333333333], $MachinePrecision] / 2.0), $MachinePrecision] + N[(0.3333333333333333 * N[ArcCos[N[(N[(g * -1.0), $MachinePrecision] / h), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[N[(t$95$0 / 3.0), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(0.6666666666666666 * Pi), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[Sin[N[(N[(N[(N[(2.0 * N[Power[Pi, 0.5], $MachinePrecision]), $MachinePrecision] * N[Power[Pi, 0.5], $MachinePrecision] + t$95$0), $MachinePrecision] * 2.0 + N[(3.0 * Pi), $MachinePrecision]), $MachinePrecision] / 6.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos^{-1} \left(\frac{-1 \cdot g}{h}\right)\\
\sin \left(\pi \cdot 1.1666666666666667\right) \cdot \sin \left(\mathsf{fma}\left({\left({\pi}^{0.3333333333333333}\right)}^{2}, \frac{{\pi}^{0.3333333333333333}}{2}, 0.3333333333333333 \cdot \cos^{-1} \left(\frac{g \cdot -1}{h}\right)\right)\right) - \left(\sin \left(\frac{t\_0}{3}\right) \cdot \sin \left(0.6666666666666666 \cdot \pi\right) - \sin \left(\frac{\mathsf{fma}\left(\mathsf{fma}\left(2 \cdot {\pi}^{0.5}, {\pi}^{0.5}, t\_0\right), 2, 3 \cdot \pi\right)}{6}\right)\right)
\end{array}
\end{array}
Initial program 98.4%
Applied rewrites97.5%
Taylor expanded in g around 0
*-commutativeN/A
lower-*.f64N/A
lower-sin.f64N/A
distribute-rgt-outN/A
lower-*.f64N/A
lift-PI.f64N/A
metadata-evalN/A
sin-+PI/2-revN/A
lower-sin.f64N/A
Applied rewrites98.5%
lift-fma.f64N/A
lift-acos.f64N/A
lift-*.f64N/A
lift-/.f64N/A
+-commutativeN/A
acos-asinN/A
associate-*l/N/A
*-commutativeN/A
acos-asinN/A
lift-PI.f64N/A
lift-/.f64N/A
add-cube-cbrtN/A
associate-/l*N/A
lower-fma.f64N/A
Applied rewrites99.9%
(FPCore (g h)
:precision binary64
(let* ((t_0 (/ (acos (/ (* -1.0 g) h)) 3.0))
(t_1 (cos t_0))
(t_2 (* t_1 (sin (fma (/ PI 3.0) 2.0 (/ PI 2.0)))))
(t_3 (* (sin t_0) (sin (* 0.6666666666666666 PI)))))
(*
2.0
(/
(- (pow t_2 3.0) (pow t_3 3.0))
(fma t_2 t_2 (fma t_3 t_3 (* (* t_1 (cos (* (/ PI 3.0) 2.0))) t_3)))))))
double code(double g, double h) {
double t_0 = acos(((-1.0 * g) / h)) / 3.0;
double t_1 = cos(t_0);
double t_2 = t_1 * sin(fma((((double) M_PI) / 3.0), 2.0, (((double) M_PI) / 2.0)));
double t_3 = sin(t_0) * sin((0.6666666666666666 * ((double) M_PI)));
return 2.0 * ((pow(t_2, 3.0) - pow(t_3, 3.0)) / fma(t_2, t_2, fma(t_3, t_3, ((t_1 * cos(((((double) M_PI) / 3.0) * 2.0))) * t_3))));
}
function code(g, h) t_0 = Float64(acos(Float64(Float64(-1.0 * g) / h)) / 3.0) t_1 = cos(t_0) t_2 = Float64(t_1 * sin(fma(Float64(pi / 3.0), 2.0, Float64(pi / 2.0)))) t_3 = Float64(sin(t_0) * sin(Float64(0.6666666666666666 * pi))) return Float64(2.0 * Float64(Float64((t_2 ^ 3.0) - (t_3 ^ 3.0)) / fma(t_2, t_2, fma(t_3, t_3, Float64(Float64(t_1 * cos(Float64(Float64(pi / 3.0) * 2.0))) * t_3))))) end
code[g_, h_] := Block[{t$95$0 = N[(N[ArcCos[N[(N[(-1.0 * g), $MachinePrecision] / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]}, Block[{t$95$1 = N[Cos[t$95$0], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 * N[Sin[N[(N[(Pi / 3.0), $MachinePrecision] * 2.0 + N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Sin[t$95$0], $MachinePrecision] * N[Sin[N[(0.6666666666666666 * Pi), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(2.0 * N[(N[(N[Power[t$95$2, 3.0], $MachinePrecision] - N[Power[t$95$3, 3.0], $MachinePrecision]), $MachinePrecision] / N[(t$95$2 * t$95$2 + N[(t$95$3 * t$95$3 + N[(N[(t$95$1 * N[Cos[N[(N[(Pi / 3.0), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\cos^{-1} \left(\frac{-1 \cdot g}{h}\right)}{3}\\
t_1 := \cos t\_0\\
t_2 := t\_1 \cdot \sin \left(\mathsf{fma}\left(\frac{\pi}{3}, 2, \frac{\pi}{2}\right)\right)\\
t_3 := \sin t\_0 \cdot \sin \left(0.6666666666666666 \cdot \pi\right)\\
2 \cdot \frac{{t\_2}^{3} - {t\_3}^{3}}{\mathsf{fma}\left(t\_2, t\_2, \mathsf{fma}\left(t\_3, t\_3, \left(t\_1 \cdot \cos \left(\frac{\pi}{3} \cdot 2\right)\right) \cdot t\_3\right)\right)}
\end{array}
\end{array}
Initial program 98.4%
Applied rewrites99.8%
lift-sin.f64N/A
lift-fma.f64N/A
lift-PI.f64N/A
lift-/.f64N/A
lift-PI.f64N/A
lift-/.f64N/A
lift-/.f64N/A
lift-PI.f64N/A
sin-+PI/2N/A
lower-cos.f64N/A
lower-*.f6499.9
Applied rewrites99.9%
(FPCore (g h)
:precision binary64
(let* ((t_0
(*
-1.0
(/
(fma (acos (/ (* -1.0 g) h)) -3.0 (* 3.0 (* -2.0 PI)))
(* -1.0 -9.0)))))
(* 2.0 (* (cos (/ (+ t_0 t_0) 2.0)) (cos (/ 0.0 2.0))))))
double code(double g, double h) {
double t_0 = -1.0 * (fma(acos(((-1.0 * g) / h)), -3.0, (3.0 * (-2.0 * ((double) M_PI)))) / (-1.0 * -9.0));
return 2.0 * (cos(((t_0 + t_0) / 2.0)) * cos((0.0 / 2.0)));
}
function code(g, h) t_0 = Float64(-1.0 * Float64(fma(acos(Float64(Float64(-1.0 * g) / h)), -3.0, Float64(3.0 * Float64(-2.0 * pi))) / Float64(-1.0 * -9.0))) return Float64(2.0 * Float64(cos(Float64(Float64(t_0 + t_0) / 2.0)) * cos(Float64(0.0 / 2.0)))) end
code[g_, h_] := Block[{t$95$0 = N[(-1.0 * N[(N[(N[ArcCos[N[(N[(-1.0 * g), $MachinePrecision] / h), $MachinePrecision]], $MachinePrecision] * -3.0 + N[(3.0 * N[(-2.0 * Pi), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(-1.0 * -9.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(2.0 * N[(N[Cos[N[(N[(t$95$0 + t$95$0), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.0 / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -1 \cdot \frac{\mathsf{fma}\left(\cos^{-1} \left(\frac{-1 \cdot g}{h}\right), -3, 3 \cdot \left(-2 \cdot \pi\right)\right)}{-1 \cdot -9}\\
2 \cdot \left(\cos \left(\frac{t\_0 + t\_0}{2}\right) \cdot \cos \left(\frac{0}{2}\right)\right)
\end{array}
\end{array}
Initial program 98.4%
Applied rewrites98.5%
Final simplification98.5%
(FPCore (g h) :precision binary64 (* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (* -1.0 g) h)) 3.0)))))
double code(double g, double h) {
return 2.0 * cos((((2.0 * ((double) M_PI)) / 3.0) + (acos(((-1.0 * 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(((-1.0 * g) / h)) / 3.0)));
}
def code(g, h): return 2.0 * math.cos((((2.0 * math.pi) / 3.0) + (math.acos(((-1.0 * 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(-1.0 * g) / h)) / 3.0)))) end
function tmp = code(g, h) tmp = 2.0 * cos((((2.0 * pi) / 3.0) + (acos(((-1.0 * 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[(N[(-1.0 * g), $MachinePrecision] / 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{-1 \cdot g}{h}\right)}{3}\right)
\end{array}
Initial program 98.4%
Final simplification98.4%
herbie shell --seed 2025065
(FPCore (g h)
:name "2-ancestry mixing, negative discriminant"
:precision binary64
(* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))