2-ancestry mixing, negative discriminant

Percentage Accurate: 98.5% → 100.0%
Time: 7.1s
Alternatives: 2
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 2 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: 100.0% accurate, 1.1× speedup?

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

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

    \[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 \color{blue}{2 \cdot \cos \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.5%

    \[\leadsto \color{blue}{\cos \left(\left(\cos^{-1} \left(\frac{-g}{h}\right) - \mathsf{PI}\left(\right)\right) \cdot 0.3333333333333333\right) \cdot -2} \]
  5. Step-by-step derivation
    1. Applied rewrites100.0%

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

      \[\leadsto \sin \left(\frac{1}{3} \cdot \left(\cos^{-1} \left(-1 \cdot \frac{g}{h}\right) - \mathsf{PI}\left(\right)\right) + \frac{1}{2} \cdot \mathsf{PI}\left(\right)\right) \cdot -2 \]
    3. Step-by-step derivation
      1. Applied rewrites100.0%

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

      Alternative 2: 98.5% accurate, 1.1× speedup?

      \[\begin{array}{l} \\ \cos \left(-0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right)\right) \cdot -2 \end{array} \]
      (FPCore (g h)
       :precision binary64
       (* (cos (* -0.3333333333333333 (acos (/ g h)))) -2.0))
      double code(double g, double h) {
      	return cos((-0.3333333333333333 * acos((g / h)))) * -2.0;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      real(8) function code(g, h)
      use fmin_fmax_functions
          real(8), intent (in) :: g
          real(8), intent (in) :: h
          code = cos(((-0.3333333333333333d0) * acos((g / h)))) * (-2.0d0)
      end function
      
      public static double code(double g, double h) {
      	return Math.cos((-0.3333333333333333 * Math.acos((g / h)))) * -2.0;
      }
      
      def code(g, h):
      	return math.cos((-0.3333333333333333 * math.acos((g / h)))) * -2.0
      
      function code(g, h)
      	return Float64(cos(Float64(-0.3333333333333333 * acos(Float64(g / h)))) * -2.0)
      end
      
      function tmp = code(g, h)
      	tmp = cos((-0.3333333333333333 * acos((g / h)))) * -2.0;
      end
      
      code[g_, h_] := N[(N[Cos[N[(-0.3333333333333333 * N[ArcCos[N[(g / h), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -2.0), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \cos \left(-0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right)\right) \cdot -2
      \end{array}
      
      Derivation
      1. Initial program 98.4%

        \[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 \color{blue}{2 \cdot \cos \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.5%

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

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

          \[\leadsto \cos \left(\frac{-1}{3} \cdot \cos^{-1} \left(\frac{g}{h}\right)\right) \cdot -2 \]
        3. Step-by-step derivation
          1. Applied rewrites98.5%

            \[\leadsto \cos \left(-0.3333333333333333 \cdot \cos^{-1} \left(\frac{g}{h}\right)\right) \cdot -2 \]
          2. Add Preprocessing

          Reproduce

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