2-ancestry mixing, negative discriminant

Percentage Accurate: 98.5% → 99.9%
Time: 8.8s
Alternatives: 4
Speedup: 1.1×

Specification

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

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.5% accurate, 1.0× speedup?

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

Alternative 1: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right)\\ t_1 := t\_0 \cdot t\_0\\ t_2 := {t\_0}^{3}\\ \frac{t\_2 + t\_2}{\mathsf{fma}\left(t\_0, t\_0, t\_1 - t\_1\right)} \end{array} \end{array} \]
(FPCore (g h)
 :precision binary64
 (let* ((t_0 (cos (fma (PI) 0.6666666666666666 (/ (acos (/ (- g) h)) 3.0))))
        (t_1 (* t_0 t_0))
        (t_2 (pow t_0 3.0)))
   (/ (+ t_2 t_2) (fma t_0 t_0 (- t_1 t_1)))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right)\\
t_1 := t\_0 \cdot t\_0\\
t_2 := {t\_0}^{3}\\
\frac{t\_2 + t\_2}{\mathsf{fma}\left(t\_0, t\_0, t\_1 - t\_1\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 98.5%

    \[2 \cdot \cos \left(\frac{2 \cdot \mathsf{PI}\left(\right)}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \]
  2. Add Preprocessing
  3. Applied rewrites99.9%

    \[\leadsto \color{blue}{\frac{{\cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right)}^{3} + {\cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right)}^{3}}{\mathsf{fma}\left(\cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right), \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right), \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right) \cdot \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right) - \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right) \cdot \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right)\right)\right)}} \]
  4. Add Preprocessing

Alternative 2: 99.9% accurate, 1.1× speedup?

\[\begin{array}{l} \\ 2 \cdot \sin \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 2, \cos^{-1} \left(\frac{-g}{h}\right)\right), -0.3333333333333333, \mathsf{PI}\left(\right) \cdot 0.5\right)\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (*
  2.0
  (sin
   (fma (fma (PI) 2.0 (acos (/ (- g) h))) -0.3333333333333333 (* (PI) 0.5)))))
\begin{array}{l}

\\
2 \cdot \sin \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 2, \cos^{-1} \left(\frac{-g}{h}\right)\right), -0.3333333333333333, \mathsf{PI}\left(\right) \cdot 0.5\right)\right)
\end{array}
Derivation
  1. Initial program 98.5%

    \[2 \cdot \cos \left(\frac{2 \cdot \mathsf{PI}\left(\right)}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \]
  2. Add Preprocessing
  3. Applied rewrites98.5%

    \[\leadsto 2 \cdot \color{blue}{\sin \left(\frac{\mathsf{fma}\left(2, \mathsf{PI}\left(\right), \cos^{-1} \left(\frac{-g}{h}\right)\right)}{-3} + \frac{\mathsf{PI}\left(\right)}{2}\right)} \]
  4. Taylor expanded in g around 0

    \[\leadsto 2 \cdot \sin \color{blue}{\left(\frac{-1}{3} \cdot \left(\cos^{-1} \left(-1 \cdot \frac{g}{h}\right) + 2 \cdot \mathsf{PI}\left(\right)\right) + \frac{1}{2} \cdot \mathsf{PI}\left(\right)\right)} \]
  5. Applied rewrites99.9%

    \[\leadsto 2 \cdot \sin \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 2, \cos^{-1} \left(\frac{-g}{h}\right)\right), -0.3333333333333333, \mathsf{PI}\left(\right) \cdot 0.5\right)\right)} \]
  6. Add Preprocessing

Alternative 3: 98.5% accurate, 1.1× speedup?

\[\begin{array}{l} \\ 2 \cdot \cos \left(\frac{\mathsf{fma}\left(2, \mathsf{PI}\left(\right), \cos^{-1} \left(\frac{-g}{h}\right)\right)}{3}\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (* 2.0 (cos (/ (fma 2.0 (PI) (acos (/ (- g) h))) 3.0))))
\begin{array}{l}

\\
2 \cdot \cos \left(\frac{\mathsf{fma}\left(2, \mathsf{PI}\left(\right), \cos^{-1} \left(\frac{-g}{h}\right)\right)}{3}\right)
\end{array}
Derivation
  1. Initial program 98.5%

    \[2 \cdot \cos \left(\frac{2 \cdot \mathsf{PI}\left(\right)}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \]
  2. Add Preprocessing
  3. Applied rewrites98.5%

    \[\leadsto 2 \cdot \cos \color{blue}{\left(\frac{\mathsf{fma}\left(2, \mathsf{PI}\left(\right), \cos^{-1} \left(\frac{-g}{h}\right)\right)}{3}\right)} \]
  4. Add Preprocessing

Alternative 4: 98.5% accurate, 1.1× speedup?

\[\begin{array}{l} \\ 2 \cdot \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \cos^{-1} \left(\frac{-g}{h}\right) \cdot 0.3333333333333333\right)\right) \end{array} \]
(FPCore (g h)
 :precision binary64
 (*
  2.0
  (cos
   (fma (PI) 0.6666666666666666 (* (acos (/ (- g) h)) 0.3333333333333333)))))
\begin{array}{l}

\\
2 \cdot \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), 0.6666666666666666, \cos^{-1} \left(\frac{-g}{h}\right) \cdot 0.3333333333333333\right)\right)
\end{array}
Derivation
  1. Initial program 98.5%

    \[2 \cdot \cos \left(\frac{2 \cdot \mathsf{PI}\left(\right)}{3} + \frac{\cos^{-1} \left(\frac{-g}{h}\right)}{3}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in g around 0

    \[\leadsto 2 \cdot \cos \color{blue}{\left(\frac{1}{3} \cdot \cos^{-1} \left(-1 \cdot \frac{g}{h}\right) + \frac{2}{3} \cdot \mathsf{PI}\left(\right)\right)} \]
  4. Applied rewrites98.4%

    \[\leadsto 2 \cdot \cos \color{blue}{\left(\mathsf{fma}\left(\cos^{-1} \left(\frac{-g}{h}\right), 0.3333333333333333, \mathsf{PI}\left(\right) \cdot 0.6666666666666666\right)\right)} \]
  5. Applied rewrites98.5%

    \[\leadsto 2 \cdot \cos \left(\mathsf{fma}\left(\mathsf{PI}\left(\right), \color{blue}{0.6666666666666666}, \cos^{-1} \left(\frac{-g}{h}\right) \cdot 0.3333333333333333\right)\right) \]
  6. Add Preprocessing

Reproduce

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