Average Error: 31.4 → 0.2
Time: 8.2s
Precision: 64
\[\frac{1 - \cos x}{x \cdot x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.029841445064018916 \lor \neg \left(x \le 0.0302568236777068268\right):\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{720}, {x}^{4}, \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}\\ \end{array}\]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
\mathbf{if}\;x \le -0.029841445064018916 \lor \neg \left(x \le 0.0302568236777068268\right):\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{720}, {x}^{4}, \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}\\

\end{array}
double f(double x) {
        double r33582 = 1.0;
        double r33583 = x;
        double r33584 = cos(r33583);
        double r33585 = r33582 - r33584;
        double r33586 = r33583 * r33583;
        double r33587 = r33585 / r33586;
        return r33587;
}

double f(double x) {
        double r33588 = x;
        double r33589 = -0.029841445064018916;
        bool r33590 = r33588 <= r33589;
        double r33591 = 0.030256823677706827;
        bool r33592 = r33588 <= r33591;
        double r33593 = !r33592;
        bool r33594 = r33590 || r33593;
        double r33595 = 1.0;
        double r33596 = cos(r33588);
        double r33597 = r33595 - r33596;
        double r33598 = r33597 / r33588;
        double r33599 = r33598 / r33588;
        double r33600 = expm1(r33599);
        double r33601 = log1p(r33600);
        double r33602 = 0.001388888888888889;
        double r33603 = 4.0;
        double r33604 = pow(r33588, r33603);
        double r33605 = 0.5;
        double r33606 = fma(r33602, r33604, r33605);
        double r33607 = 0.041666666666666664;
        double r33608 = 2.0;
        double r33609 = pow(r33588, r33608);
        double r33610 = r33607 * r33609;
        double r33611 = r33606 - r33610;
        double r33612 = r33594 ? r33601 : r33611;
        return r33612;
}

Error

Bits error versus x

Derivation

  1. Split input into 2 regimes
  2. if x < -0.029841445064018916 or 0.030256823677706827 < x

    1. Initial program 1.0

      \[\frac{1 - \cos x}{x \cdot x}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt1.1

      \[\leadsto \frac{\color{blue}{\sqrt{1 - \cos x} \cdot \sqrt{1 - \cos x}}}{x \cdot x}\]
    4. Applied times-frac0.6

      \[\leadsto \color{blue}{\frac{\sqrt{1 - \cos x}}{x} \cdot \frac{\sqrt{1 - \cos x}}{x}}\]
    5. Using strategy rm
    6. Applied log1p-expm1-u0.6

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\sqrt{1 - \cos x}}{x} \cdot \frac{\sqrt{1 - \cos x}}{x}\right)\right)}\]
    7. Simplified0.5

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)}\right)\]

    if -0.029841445064018916 < x < 0.030256823677706827

    1. Initial program 62.2

      \[\frac{1 - \cos x}{x \cdot x}\]
    2. Taylor expanded around 0 0.0

      \[\leadsto \color{blue}{\left(\frac{1}{720} \cdot {x}^{4} + \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}}\]
    3. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{720}, {x}^{4}, \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -0.029841445064018916 \lor \neg \left(x \le 0.0302568236777068268\right):\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{720}, {x}^{4}, \frac{1}{2}\right) - \frac{1}{24} \cdot {x}^{2}\\ \end{array}\]

Reproduce

herbie shell --seed 2020045 +o rules:numerics
(FPCore (x)
  :name "cos2 (problem 3.4.1)"
  :precision binary64
  (/ (- 1 (cos x)) (* x x)))