Average Error: 31.4 → 0.2
Time: 8.7s
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{expm1}\left(\mathsf{log1p}\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{expm1}\left(\mathsf{log1p}\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 r27703 = 1.0;
        double r27704 = x;
        double r27705 = cos(r27704);
        double r27706 = r27703 - r27705;
        double r27707 = r27704 * r27704;
        double r27708 = r27706 / r27707;
        return r27708;
}

double f(double x) {
        double r27709 = x;
        double r27710 = -0.029841445064018916;
        bool r27711 = r27709 <= r27710;
        double r27712 = 0.030256823677706827;
        bool r27713 = r27709 <= r27712;
        double r27714 = !r27713;
        bool r27715 = r27711 || r27714;
        double r27716 = 1.0;
        double r27717 = cos(r27709);
        double r27718 = r27716 - r27717;
        double r27719 = r27718 / r27709;
        double r27720 = r27719 / r27709;
        double r27721 = log1p(r27720);
        double r27722 = expm1(r27721);
        double r27723 = 0.001388888888888889;
        double r27724 = 4.0;
        double r27725 = pow(r27709, r27724);
        double r27726 = 0.5;
        double r27727 = fma(r27723, r27725, r27726);
        double r27728 = 0.041666666666666664;
        double r27729 = 2.0;
        double r27730 = pow(r27709, r27729);
        double r27731 = r27728 * r27730;
        double r27732 = r27727 - r27731;
        double r27733 = r27715 ? r27722 : r27732;
        return r27733;
}

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 expm1-log1p-u0.6

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

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\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. Using strategy rm
    3. Applied add-sqr-sqrt62.2

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

      \[\leadsto \color{blue}{\frac{\sqrt{1 - \cos x}}{x} \cdot \frac{\sqrt{1 - \cos x}}{x}}\]
    5. 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}}\]
    6. 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{expm1}\left(\mathsf{log1p}\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)))