2-ancestry mixing, negative discriminant

Percentage Accurate: 98.4% → 98.0%
Time: 9.5s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\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
 (* 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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 98.4% accurate, 1.0× speedup?

\[\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
 (* 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}

Alternative 1: 98.0% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right)\\ t_1 := \sin \left(0.6666666666666666 \cdot \pi\right) \cdot \sin t_0\\ t_2 := \cos t_0\\ t_3 := \cos \left(0.6666666666666666 \cdot \pi\right)\\ 2 \cdot \frac{{\left(t_2 \cdot t_3\right)}^{3} - {t_1}^{3}}{\mathsf{fma}\left(t_1, \cos \left(0.6666666666666666 \cdot \pi - t_0\right), t_2 \cdot \left(t_2 \cdot \left(t_3 \cdot t_3\right)\right)\right)} \end{array} \end{array} \]
(FPCore (g h)
 :precision binary64
 (let* ((t_0 (* 0.3333333333333333 (acos (/ g h))))
        (t_1 (* (sin (* 0.6666666666666666 PI)) (sin t_0)))
        (t_2 (cos t_0))
        (t_3 (cos (* 0.6666666666666666 PI))))
   (*
    2.0
    (/
     (- (pow (* t_2 t_3) 3.0) (pow t_1 3.0))
     (fma
      t_1
      (cos (- (* 0.6666666666666666 PI) t_0))
      (* t_2 (* t_2 (* t_3 t_3))))))))
double code(double g, double h) {
	double t_0 = 0.3333333333333333 * acos((g / h));
	double t_1 = sin((0.6666666666666666 * ((double) M_PI))) * sin(t_0);
	double t_2 = cos(t_0);
	double t_3 = cos((0.6666666666666666 * ((double) M_PI)));
	return 2.0 * ((pow((t_2 * t_3), 3.0) - pow(t_1, 3.0)) / fma(t_1, cos(((0.6666666666666666 * ((double) M_PI)) - t_0)), (t_2 * (t_2 * (t_3 * t_3)))));
}
function code(g, h)
	t_0 = Float64(0.3333333333333333 * acos(Float64(g / h)))
	t_1 = Float64(sin(Float64(0.6666666666666666 * pi)) * sin(t_0))
	t_2 = cos(t_0)
	t_3 = cos(Float64(0.6666666666666666 * pi))
	return Float64(2.0 * Float64(Float64((Float64(t_2 * t_3) ^ 3.0) - (t_1 ^ 3.0)) / fma(t_1, cos(Float64(Float64(0.6666666666666666 * pi) - t_0)), Float64(t_2 * Float64(t_2 * Float64(t_3 * t_3))))))
end
code[g_, h_] := Block[{t$95$0 = N[(0.3333333333333333 * N[ArcCos[N[(g / h), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[N[(0.6666666666666666 * Pi), $MachinePrecision]], $MachinePrecision] * N[Sin[t$95$0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[Cos[t$95$0], $MachinePrecision]}, Block[{t$95$3 = N[Cos[N[(0.6666666666666666 * Pi), $MachinePrecision]], $MachinePrecision]}, N[(2.0 * N[(N[(N[Power[N[(t$95$2 * t$95$3), $MachinePrecision], 3.0], $MachinePrecision] - N[Power[t$95$1, 3.0], $MachinePrecision]), $MachinePrecision] / N[(t$95$1 * N[Cos[N[(N[(0.6666666666666666 * Pi), $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision] + N[(t$95$2 * N[(t$95$2 * N[(t$95$3 * t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right)\\
t_1 := \sin \left(0.6666666666666666 \cdot \pi\right) \cdot \sin t_0\\
t_2 := \cos t_0\\
t_3 := \cos \left(0.6666666666666666 \cdot \pi\right)\\
2 \cdot \frac{{\left(t_2 \cdot t_3\right)}^{3} - {t_1}^{3}}{\mathsf{fma}\left(t_1, \cos \left(0.6666666666666666 \cdot \pi - t_0\right), t_2 \cdot \left(t_2 \cdot \left(t_3 \cdot t_3\right)\right)\right)}
\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 98.5% accurate, 0.8× speedup?

\[\begin{array}{l} \\ 2 \cdot \cos \left(\mathsf{fma}\left(\pi, 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (* 2.0 (cos (fma PI 0.6666666666666666 (/ (acos (/ (- g) h)) 3.0)))))
double code(double g, double h) {
	return 2.0 * cos(fma(((double) M_PI), 0.6666666666666666, (acos((-g / h)) / 3.0)));
}
function code(g, h)
	return Float64(2.0 * cos(fma(pi, 0.6666666666666666, Float64(acos(Float64(Float64(-g) / h)) / 3.0))))
end
code[g_, h_] := N[(2.0 * N[Cos[N[(Pi * 0.6666666666666666 + N[(N[ArcCos[N[((-g) / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \cos \left(\mathsf{fma}\left(\pi, 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 2 \cdot \cos \left(0.6666666666666666 \cdot \pi + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (* 2.0 (cos (+ (* 0.6666666666666666 PI) (/ (acos (/ (- g) h)) 3.0)))))
double code(double g, double h) {
	return 2.0 * cos(((0.6666666666666666 * ((double) M_PI)) + (acos((-g / h)) / 3.0)));
}
public static double code(double g, double h) {
	return 2.0 * Math.cos(((0.6666666666666666 * Math.PI) + (Math.acos((-g / h)) / 3.0)));
}
def code(g, h):
	return 2.0 * math.cos(((0.6666666666666666 * math.pi) + (math.acos((-g / h)) / 3.0)))
function code(g, h)
	return Float64(2.0 * cos(Float64(Float64(0.6666666666666666 * pi) + Float64(acos(Float64(Float64(-g) / h)) / 3.0))))
end
function tmp = code(g, h)
	tmp = 2.0 * cos(((0.6666666666666666 * pi) + (acos((-g / h)) / 3.0)));
end
code[g_, h_] := N[(2.0 * N[Cos[N[(N[(0.6666666666666666 * Pi), $MachinePrecision] + N[(N[ArcCos[N[((-g) / h), $MachinePrecision]], $MachinePrecision] / 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \cos \left(0.6666666666666666 \cdot \pi + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 96.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 2 \cdot \cos \left(0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right) + 0.6666666666666666 \cdot \pi\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (*
  2.0
  (cos (+ (* 0.3333333333333333 (acos (/ g h))) (* 0.6666666666666666 PI)))))
double code(double g, double h) {
	return 2.0 * cos(((0.3333333333333333 * acos((g / h))) + (0.6666666666666666 * ((double) M_PI))));
}
public static double code(double g, double h) {
	return 2.0 * Math.cos(((0.3333333333333333 * Math.acos((g / h))) + (0.6666666666666666 * Math.PI)));
}
def code(g, h):
	return 2.0 * math.cos(((0.3333333333333333 * math.acos((g / h))) + (0.6666666666666666 * math.pi)))
function code(g, h)
	return Float64(2.0 * cos(Float64(Float64(0.3333333333333333 * acos(Float64(g / h))) + Float64(0.6666666666666666 * pi))))
end
function tmp = code(g, h)
	tmp = 2.0 * cos(((0.3333333333333333 * acos((g / h))) + (0.6666666666666666 * pi)));
end
code[g_, h_] := N[(2.0 * N[Cos[N[(N[(0.3333333333333333 * N[ArcCos[N[(g / h), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(0.6666666666666666 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \cos \left(0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right) + 0.6666666666666666 \cdot \pi\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2023343 
(FPCore (g h)
  :name "2-ancestry mixing, negative discriminant"
  :precision binary64
  (* 2.0 (cos (+ (/ (* 2.0 PI) 3.0) (/ (acos (/ (- g) h)) 3.0)))))