cos2 (problem 3.4.1)

Percentage Accurate: 50.4% → 99.6%
Time: 11.9s
Alternatives: 8
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 8 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: 50.4% 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.6% accurate, 0.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.1:\\ \;\;\;\;\frac{x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m \cdot x\_m, -2.48015873015873 \cdot 10^{-5}, 0.001388888888888889\right), -0.041666666666666664\right), 0.5\right)}{x\_m}\\ \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.1)
   (/
    (*
     x_m
     (fma
      x_m
      (*
       x_m
       (fma
        x_m
        (* x_m (fma (* x_m x_m) -2.48015873015873e-5 0.001388888888888889))
        -0.041666666666666664))
      0.5))
    x_m)
   (/ (/ (- 1.0 (cos x_m)) x_m) x_m)))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if (x_m <= 0.1) {
		tmp = (x_m * fma(x_m, (x_m * fma(x_m, (x_m * fma((x_m * x_m), -2.48015873015873e-5, 0.001388888888888889)), -0.041666666666666664)), 0.5)) / x_m;
	} else {
		tmp = ((1.0 - cos(x_m)) / x_m) / x_m;
	}
	return tmp;
}
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (x_m <= 0.1)
		tmp = Float64(Float64(x_m * fma(x_m, Float64(x_m * fma(x_m, Float64(x_m * fma(Float64(x_m * x_m), -2.48015873015873e-5, 0.001388888888888889)), -0.041666666666666664)), 0.5)) / x_m);
	else
		tmp = Float64(Float64(Float64(1.0 - cos(x_m)) / x_m) / x_m);
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[x$95$m, 0.1], N[(N[(x$95$m * N[(x$95$m * N[(x$95$m * N[(x$95$m * N[(x$95$m * N[(N[(x$95$m * x$95$m), $MachinePrecision] * -2.48015873015873e-5 + 0.001388888888888889), $MachinePrecision]), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision], 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.1:\\
\;\;\;\;\frac{x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m \cdot x\_m, -2.48015873015873 \cdot 10^{-5}, 0.001388888888888889\right), -0.041666666666666664\right), 0.5\right)}{x\_m}\\

\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 < 0.10000000000000001

    1. Initial program 3.6%

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

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

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

        \[\leadsto \color{blue}{\frac{-1}{x \cdot x}} \cdot \left(\cos x + -1\right) \]
      3. associate-*l/N/A

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

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

        \[\leadsto \frac{-1 \cdot \color{blue}{\left(-1 + \cos x\right)}}{x \cdot x} \]
      6. distribute-lft-inN/A

        \[\leadsto \frac{\color{blue}{-1 \cdot -1 + -1 \cdot \cos x}}{x \cdot x} \]
      7. metadata-evalN/A

        \[\leadsto \frac{\color{blue}{1} + -1 \cdot \cos x}{x \cdot x} \]
      8. neg-mul-1N/A

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

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

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

        \[\leadsto \frac{1 - \cos x}{\color{blue}{x \cdot x}} \]
      12. associate-/r*N/A

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

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

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

        \[\leadsto \frac{\frac{1 - \color{blue}{\cos x}}{x}}{x} \]
      16. lower--.f645.3

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

      \[\leadsto \color{blue}{\frac{\frac{1 - \cos x}{x}}{x}} \]
    6. Taylor expanded in x around 0

      \[\leadsto \frac{\color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right)\right)}}{x} \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \frac{\color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right)\right)}}{x} \]
      2. +-commutativeN/A

        \[\leadsto \frac{x \cdot \color{blue}{\left({x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right) + \frac{1}{2}\right)}}{x} \]
      3. unpow2N/A

        \[\leadsto \frac{x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right) + \frac{1}{2}\right)}{x} \]
      4. associate-*l*N/A

        \[\leadsto \frac{x \cdot \left(\color{blue}{x \cdot \left(x \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right)\right)} + \frac{1}{2}\right)}{x} \]
      5. lower-fma.f64N/A

        \[\leadsto \frac{x \cdot \color{blue}{\mathsf{fma}\left(x, x \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right), \frac{1}{2}\right)}}{x} \]
    8. Applied rewrites100.0%

      \[\leadsto \frac{\color{blue}{x \cdot \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, -2.48015873015873 \cdot 10^{-5}, 0.001388888888888889\right), -0.041666666666666664\right), 0.5\right)}}{x} \]

    if 0.10000000000000001 < x

    1. Initial program 98.5%

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

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

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

        \[\leadsto \color{blue}{\frac{-1}{x \cdot x}} \cdot \left(\cos x + -1\right) \]
      3. associate-*l/N/A

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

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

        \[\leadsto \frac{-1 \cdot \color{blue}{\left(-1 + \cos x\right)}}{x \cdot x} \]
      6. distribute-lft-inN/A

        \[\leadsto \frac{\color{blue}{-1 \cdot -1 + -1 \cdot \cos x}}{x \cdot x} \]
      7. metadata-evalN/A

        \[\leadsto \frac{\color{blue}{1} + -1 \cdot \cos x}{x \cdot x} \]
      8. neg-mul-1N/A

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

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

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

        \[\leadsto \frac{1 - \cos x}{\color{blue}{x \cdot x}} \]
      12. associate-/r*N/A

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

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

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

        \[\leadsto \frac{\frac{1 - \color{blue}{\cos x}}{x}}{x} \]
      16. lower--.f6499.2

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

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

Alternative 2: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.1:\\ \;\;\;\;\frac{x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m \cdot x\_m, -2.48015873015873 \cdot 10^{-5}, 0.001388888888888889\right), -0.041666666666666664\right), 0.5\right)}{x\_m}\\ \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.1)
   (/
    (*
     x_m
     (fma
      x_m
      (*
       x_m
       (fma
        x_m
        (* x_m (fma (* x_m x_m) -2.48015873015873e-5 0.001388888888888889))
        -0.041666666666666664))
      0.5))
    x_m)
   (/ (- 1.0 (cos x_m)) (* x_m x_m))))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if (x_m <= 0.1) {
		tmp = (x_m * fma(x_m, (x_m * fma(x_m, (x_m * fma((x_m * x_m), -2.48015873015873e-5, 0.001388888888888889)), -0.041666666666666664)), 0.5)) / x_m;
	} else {
		tmp = (1.0 - cos(x_m)) / (x_m * x_m);
	}
	return tmp;
}
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (x_m <= 0.1)
		tmp = Float64(Float64(x_m * fma(x_m, Float64(x_m * fma(x_m, Float64(x_m * fma(Float64(x_m * x_m), -2.48015873015873e-5, 0.001388888888888889)), -0.041666666666666664)), 0.5)) / x_m);
	else
		tmp = Float64(Float64(1.0 - cos(x_m)) / Float64(x_m * x_m));
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[x$95$m, 0.1], N[(N[(x$95$m * N[(x$95$m * N[(x$95$m * N[(x$95$m * N[(x$95$m * N[(N[(x$95$m * x$95$m), $MachinePrecision] * -2.48015873015873e-5 + 0.001388888888888889), $MachinePrecision]), $MachinePrecision] + -0.041666666666666664), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision], 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.1:\\
\;\;\;\;\frac{x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m, x\_m \cdot \mathsf{fma}\left(x\_m \cdot x\_m, -2.48015873015873 \cdot 10^{-5}, 0.001388888888888889\right), -0.041666666666666664\right), 0.5\right)}{x\_m}\\

\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 < 0.10000000000000001

    1. Initial program 2.7%

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

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

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

        \[\leadsto \color{blue}{\frac{-1}{x \cdot x}} \cdot \left(\cos x + -1\right) \]
      3. associate-*l/N/A

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

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

        \[\leadsto \frac{-1 \cdot \color{blue}{\left(-1 + \cos x\right)}}{x \cdot x} \]
      6. distribute-lft-inN/A

        \[\leadsto \frac{\color{blue}{-1 \cdot -1 + -1 \cdot \cos x}}{x \cdot x} \]
      7. metadata-evalN/A

        \[\leadsto \frac{\color{blue}{1} + -1 \cdot \cos x}{x \cdot x} \]
      8. neg-mul-1N/A

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

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

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

        \[\leadsto \frac{1 - \cos x}{\color{blue}{x \cdot x}} \]
      12. associate-/r*N/A

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

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

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

        \[\leadsto \frac{\frac{1 - \color{blue}{\cos x}}{x}}{x} \]
      16. lower--.f644.1

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

      \[\leadsto \color{blue}{\frac{\frac{1 - \cos x}{x}}{x}} \]
    6. Taylor expanded in x around 0

      \[\leadsto \frac{\color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right)\right)}}{x} \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \frac{\color{blue}{x \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right)\right)}}{x} \]
      2. +-commutativeN/A

        \[\leadsto \frac{x \cdot \color{blue}{\left({x}^{2} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right) + \frac{1}{2}\right)}}{x} \]
      3. unpow2N/A

        \[\leadsto \frac{x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right) + \frac{1}{2}\right)}{x} \]
      4. associate-*l*N/A

        \[\leadsto \frac{x \cdot \left(\color{blue}{x \cdot \left(x \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right)\right)} + \frac{1}{2}\right)}{x} \]
      5. lower-fma.f64N/A

        \[\leadsto \frac{x \cdot \color{blue}{\mathsf{fma}\left(x, x \cdot \left({x}^{2} \cdot \left(\frac{1}{720} + \frac{-1}{40320} \cdot {x}^{2}\right) - \frac{1}{24}\right), \frac{1}{2}\right)}}{x} \]
    8. Applied rewrites100.0%

      \[\leadsto \frac{\color{blue}{x \cdot \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, -2.48015873015873 \cdot 10^{-5}, 0.001388888888888889\right), -0.041666666666666664\right), 0.5\right)}}{x} \]

    if 0.10000000000000001 < x

    1. Initial program 98.3%

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

Reproduce

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