cos2 (problem 3.4.1)

Percentage Accurate: 51.1% → 99.4%
Time: 10.9s
Alternatives: 6
Speedup: 120.0×

Specification

?
\[\begin{array}{l} \\ \frac{1 - \cos x}{x \cdot x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / (x * x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / (x * x);
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / Float64(x * x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - \cos x}{x \cdot x}
\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 6 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: 51.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1 - \cos x}{x \cdot x} \end{array} \]
(FPCore (x) :precision binary64 (/ (- 1.0 (cos x)) (* x x)))
double code(double x) {
	return (1.0 - cos(x)) / (x * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 - cos(x)) / (x * x)
end function
public static double code(double x) {
	return (1.0 - Math.cos(x)) / (x * x);
}
def code(x):
	return (1.0 - math.cos(x)) / (x * x)
function code(x)
	return Float64(Float64(1.0 - cos(x)) / Float64(x * x))
end
function tmp = code(x)
	tmp = (1.0 - cos(x)) / (x * x);
end
code[x_] := N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1 - \cos x}{x \cdot x}
\end{array}

Alternative 1: 99.4% accurate, 0.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.000145:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1 - \cos x\_m}{x\_m}}{x\_m}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (if (<= x_m 0.000145) 0.5 (/ (/ (- 1.0 (cos x_m)) x_m) x_m)))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if (x_m <= 0.000145) {
		tmp = 0.5;
	} else {
		tmp = ((1.0 - cos(x_m)) / x_m) / x_m;
	}
	return tmp;
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    real(8) :: tmp
    if (x_m <= 0.000145d0) then
        tmp = 0.5d0
    else
        tmp = ((1.0d0 - cos(x_m)) / x_m) / x_m
    end if
    code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	double tmp;
	if (x_m <= 0.000145) {
		tmp = 0.5;
	} else {
		tmp = ((1.0 - Math.cos(x_m)) / x_m) / x_m;
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m):
	tmp = 0
	if x_m <= 0.000145:
		tmp = 0.5
	else:
		tmp = ((1.0 - math.cos(x_m)) / x_m) / x_m
	return tmp
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (x_m <= 0.000145)
		tmp = 0.5;
	else
		tmp = Float64(Float64(Float64(1.0 - cos(x_m)) / x_m) / x_m);
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m)
	tmp = 0.0;
	if (x_m <= 0.000145)
		tmp = 0.5;
	else
		tmp = ((1.0 - cos(x_m)) / x_m) / x_m;
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[x$95$m, 0.000145], 0.5, N[(N[(N[(1.0 - N[Cos[x$95$m], $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 0.000145:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1 - \cos x\_m}{x\_m}}{x\_m}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.45e-4

    1. Initial program 1.4%

      \[\frac{1 - \cos x}{x \cdot x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

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

        \[\leadsto \color{blue}{0.5} \]

      if 1.45e-4 < x

      1. Initial program 98.1%

        \[\frac{1 - \cos x}{x \cdot x} \]
      2. Add Preprocessing
      3. Applied rewrites99.6%

        \[\leadsto \color{blue}{\frac{\frac{\cos x + -1}{x}}{-x}} \]
      4. Step-by-step derivation
        1. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{\frac{\cos x + -1}{x}}{\mathsf{neg}\left(x\right)}} \]
        2. lift-neg.f64N/A

          \[\leadsto \frac{\frac{\cos x + -1}{x}}{\color{blue}{\mathsf{neg}\left(x\right)}} \]
        3. distribute-frac-neg2N/A

          \[\leadsto \color{blue}{\mathsf{neg}\left(\frac{\frac{\cos x + -1}{x}}{x}\right)} \]
        4. distribute-frac-negN/A

          \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\frac{\cos x + -1}{x}\right)}{x}} \]
        5. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\frac{\cos x + -1}{x}\right)}{x}} \]
        6. lift-/.f64N/A

          \[\leadsto \frac{\mathsf{neg}\left(\color{blue}{\frac{\cos x + -1}{x}}\right)}{x} \]
        7. distribute-neg-fracN/A

          \[\leadsto \frac{\color{blue}{\frac{\mathsf{neg}\left(\left(\cos x + -1\right)\right)}{x}}}{x} \]
        8. lift-+.f64N/A

          \[\leadsto \frac{\frac{\mathsf{neg}\left(\color{blue}{\left(\cos x + -1\right)}\right)}{x}}{x} \]
        9. +-commutativeN/A

          \[\leadsto \frac{\frac{\mathsf{neg}\left(\color{blue}{\left(-1 + \cos x\right)}\right)}{x}}{x} \]
        10. distribute-neg-inN/A

          \[\leadsto \frac{\frac{\color{blue}{\left(\mathsf{neg}\left(-1\right)\right) + \left(\mathsf{neg}\left(\cos x\right)\right)}}{x}}{x} \]
        11. metadata-evalN/A

          \[\leadsto \frac{\frac{\color{blue}{1} + \left(\mathsf{neg}\left(\cos x\right)\right)}{x}}{x} \]
        12. sub-negN/A

          \[\leadsto \frac{\frac{\color{blue}{1 - \cos x}}{x}}{x} \]
        13. lift--.f64N/A

          \[\leadsto \frac{\frac{\color{blue}{1 - \cos x}}{x}}{x} \]
        14. lower-/.f6499.6

          \[\leadsto \frac{\color{blue}{\frac{1 - \cos x}{x}}}{x} \]
      5. Applied rewrites99.6%

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

    Alternative 2: 98.9% accurate, 1.0× speedup?

    \[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.000145:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - \cos x\_m}{x\_m \cdot x\_m}\\ \end{array} \end{array} \]
    x_m = (fabs.f64 x)
    (FPCore (x_m)
     :precision binary64
     (if (<= x_m 0.000145) 0.5 (/ (- 1.0 (cos x_m)) (* x_m x_m))))
    x_m = fabs(x);
    double code(double x_m) {
    	double tmp;
    	if (x_m <= 0.000145) {
    		tmp = 0.5;
    	} else {
    		tmp = (1.0 - cos(x_m)) / (x_m * x_m);
    	}
    	return tmp;
    }
    
    x_m = abs(x)
    real(8) function code(x_m)
        real(8), intent (in) :: x_m
        real(8) :: tmp
        if (x_m <= 0.000145d0) then
            tmp = 0.5d0
        else
            tmp = (1.0d0 - cos(x_m)) / (x_m * x_m)
        end if
        code = tmp
    end function
    
    x_m = Math.abs(x);
    public static double code(double x_m) {
    	double tmp;
    	if (x_m <= 0.000145) {
    		tmp = 0.5;
    	} else {
    		tmp = (1.0 - Math.cos(x_m)) / (x_m * x_m);
    	}
    	return tmp;
    }
    
    x_m = math.fabs(x)
    def code(x_m):
    	tmp = 0
    	if x_m <= 0.000145:
    		tmp = 0.5
    	else:
    		tmp = (1.0 - math.cos(x_m)) / (x_m * x_m)
    	return tmp
    
    x_m = abs(x)
    function code(x_m)
    	tmp = 0.0
    	if (x_m <= 0.000145)
    		tmp = 0.5;
    	else
    		tmp = Float64(Float64(1.0 - cos(x_m)) / Float64(x_m * x_m));
    	end
    	return tmp
    end
    
    x_m = abs(x);
    function tmp_2 = code(x_m)
    	tmp = 0.0;
    	if (x_m <= 0.000145)
    		tmp = 0.5;
    	else
    		tmp = (1.0 - cos(x_m)) / (x_m * x_m);
    	end
    	tmp_2 = tmp;
    end
    
    x_m = N[Abs[x], $MachinePrecision]
    code[x$95$m_] := If[LessEqual[x$95$m, 0.000145], 0.5, N[(N[(1.0 - N[Cos[x$95$m], $MachinePrecision]), $MachinePrecision] / N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    x_m = \left|x\right|
    
    \\
    \begin{array}{l}
    \mathbf{if}\;x\_m \leq 0.000145:\\
    \;\;\;\;0.5\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{1 - \cos x\_m}{x\_m \cdot x\_m}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if x < 1.45e-4

      1. Initial program 2.2%

        \[\frac{1 - \cos x}{x \cdot x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{1}{2}} \]
      4. Step-by-step derivation
        1. Applied rewrites99.8%

          \[\leadsto \color{blue}{0.5} \]

        if 1.45e-4 < x

        1. Initial program 98.0%

          \[\frac{1 - \cos x}{x \cdot x} \]
        2. Add Preprocessing
      5. Recombined 2 regimes into one program.
      6. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024226 
      (FPCore (x)
        :name "cos2 (problem 3.4.1)"
        :precision binary64
        (/ (- 1.0 (cos x)) (* x x)))