cos(PI*x)

Percentage Accurate: 57.5% → 98.9%
Time: 1.4s
Alternatives: 2
Speedup: 106.0×

Specification

?
\[-1.79 \cdot 10^{+308} \leq x \land x \leq 1.79 \cdot 10^{+308}\]
\[\begin{array}{l} \\ \cos \left(\mathsf{PI}\left(\right) \cdot x\right) \end{array} \]
(FPCore (x) :precision binary64 (cos (* (PI) x)))
\begin{array}{l}

\\
\cos \left(\mathsf{PI}\left(\right) \cdot x\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: 57.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos \left(\mathsf{PI}\left(\right) \cdot x\right) \end{array} \]
(FPCore (x) :precision binary64 (cos (* (PI) x)))
\begin{array}{l}

\\
\cos \left(\mathsf{PI}\left(\right) \cdot x\right)
\end{array}

Alternative 1: 98.9% accurate, 0.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} t_0 := \mathsf{PI}\left(\right) \cdot x\_m\\ \mathbf{if}\;t\_0 \leq 50000000000000:\\ \;\;\;\;\cos t\_0\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (let* ((t_0 (* (PI) x_m))) (if (<= t_0 50000000000000.0) (cos t_0) 1.0)))
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot x\_m\\
\mathbf{if}\;t\_0 \leq 50000000000000:\\
\;\;\;\;\cos t\_0\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (PI.f64) x) < 5e13

    1. Initial program 72.7%

      \[\cos \left(\mathsf{PI}\left(\right) \cdot x\right) \]
    2. Add Preprocessing

    if 5e13 < (*.f64 (PI.f64) x)

    1. Initial program 12.3%

      \[\cos \left(\mathsf{PI}\left(\right) \cdot x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \]
    4. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \color{blue}{1} \]
    5. Recombined 2 regimes into one program.
    6. Add Preprocessing

    Alternative 2: 97.2% accurate, 106.0× speedup?

    \[\begin{array}{l} x_m = \left|x\right| \\ 1 \end{array} \]
    x_m = (fabs.f64 x)
    (FPCore (x_m) :precision binary64 1.0)
    x_m = fabs(x);
    double code(double x_m) {
    	return 1.0;
    }
    
    x_m = abs(x)
    real(8) function code(x_m)
        real(8), intent (in) :: x_m
        code = 1.0d0
    end function
    
    x_m = Math.abs(x);
    public static double code(double x_m) {
    	return 1.0;
    }
    
    x_m = math.fabs(x)
    def code(x_m):
    	return 1.0
    
    x_m = abs(x)
    function code(x_m)
    	return 1.0
    end
    
    x_m = abs(x);
    function tmp = code(x_m)
    	tmp = 1.0;
    end
    
    x_m = N[Abs[x], $MachinePrecision]
    code[x$95$m_] := 1.0
    
    \begin{array}{l}
    x_m = \left|x\right|
    
    \\
    1
    \end{array}
    
    Derivation
    1. Initial program 56.4%

      \[\cos \left(\mathsf{PI}\left(\right) \cdot x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1} \]
    4. Step-by-step derivation
      1. Applied rewrites97.7%

        \[\leadsto \color{blue}{1} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 1 
      (FPCore (x)
        :name "cos(PI*x)"
        :precision binary64
        :pre (and (<= -1.79e+308 x) (<= x 1.79e+308))
        (cos (* (PI) x)))