Average Error: 31.6 → 0.2
Time: 9.6s
Precision: 64
\[\frac{1 - \cos x}{x \cdot x}\]
\[\begin{array}{l} \mathbf{if}\;x \le -0.0266096722673715806:\\ \;\;\;\;\frac{1}{x} \cdot \frac{\mathsf{log1p}\left(\mathsf{expm1}\left(1 - \cos x\right)\right)}{x}\\ \mathbf{elif}\;x \le 0.0240834512536182467:\\ \;\;\;\;\mathsf{fma}\left({x}^{2}, \frac{-1}{24}, \mathsf{fma}\left(\frac{1}{720}, {x}^{4}, \frac{1}{2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)\right)\\ \end{array}\]
\frac{1 - \cos x}{x \cdot x}
\begin{array}{l}
\mathbf{if}\;x \le -0.0266096722673715806:\\
\;\;\;\;\frac{1}{x} \cdot \frac{\mathsf{log1p}\left(\mathsf{expm1}\left(1 - \cos x\right)\right)}{x}\\

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

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)\right)\\

\end{array}
double f(double x) {
        double r25054 = 1.0;
        double r25055 = x;
        double r25056 = cos(r25055);
        double r25057 = r25054 - r25056;
        double r25058 = r25055 * r25055;
        double r25059 = r25057 / r25058;
        return r25059;
}

double f(double x) {
        double r25060 = x;
        double r25061 = -0.02660967226737158;
        bool r25062 = r25060 <= r25061;
        double r25063 = 1.0;
        double r25064 = r25063 / r25060;
        double r25065 = 1.0;
        double r25066 = cos(r25060);
        double r25067 = r25065 - r25066;
        double r25068 = expm1(r25067);
        double r25069 = log1p(r25068);
        double r25070 = r25069 / r25060;
        double r25071 = r25064 * r25070;
        double r25072 = 0.024083451253618247;
        bool r25073 = r25060 <= r25072;
        double r25074 = 2.0;
        double r25075 = pow(r25060, r25074);
        double r25076 = -0.041666666666666664;
        double r25077 = 0.001388888888888889;
        double r25078 = 4.0;
        double r25079 = pow(r25060, r25078);
        double r25080 = 0.5;
        double r25081 = fma(r25077, r25079, r25080);
        double r25082 = fma(r25075, r25076, r25081);
        double r25083 = r25067 / r25060;
        double r25084 = r25083 / r25060;
        double r25085 = log1p(r25084);
        double r25086 = expm1(r25085);
        double r25087 = r25073 ? r25082 : r25086;
        double r25088 = r25062 ? r25071 : r25087;
        return r25088;
}

Error

Bits error versus x

Derivation

  1. Split input into 3 regimes
  2. if x < -0.02660967226737158

    1. Initial program 0.9

      \[\frac{1 - \cos x}{x \cdot x}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity0.9

      \[\leadsto \frac{\color{blue}{1 \cdot \left(1 - \cos x\right)}}{x \cdot x}\]
    4. Applied times-frac0.5

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

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

    if -0.02660967226737158 < x < 0.024083451253618247

    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({x}^{2}, \frac{-1}{24}, \mathsf{fma}\left(\frac{1}{720}, {x}^{4}, \frac{1}{2}\right)\right)}\]

    if 0.024083451253618247 < x

    1. Initial program 1.0

      \[\frac{1 - \cos x}{x \cdot x}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity1.0

      \[\leadsto \frac{\color{blue}{1 \cdot \left(1 - \cos x\right)}}{x \cdot x}\]
    4. Applied times-frac0.5

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

      \[\leadsto \frac{1}{x} \cdot \frac{\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(1 - \cos x\right)\right)}}{x}\]
    7. Using strategy rm
    8. Applied expm1-log1p-u0.5

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

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\left(\frac{\frac{1 - \cos x}{x}}{x}\right)}\right)\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.2

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

Reproduce

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