cos2 (problem 3.4.1)

Percentage Accurate: 50.9% → 99.6%
Time: 10.1s
Alternatives: 6
Speedup: 17.8×

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: 50.9% 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.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x\_m \cdot x\_m\right)\\ \mathbf{else}:\\ \;\;\;\;{x\_m}^{-2} \cdot \left(1 - \cos x\_m\right)\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (if (<= x_m 0.005)
   (+ 0.5 (* -0.041666666666666664 (* x_m x_m)))
   (* (pow x_m -2.0) (- 1.0 (cos x_m)))))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if (x_m <= 0.005) {
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	} else {
		tmp = pow(x_m, -2.0) * (1.0 - cos(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.005d0) then
        tmp = 0.5d0 + ((-0.041666666666666664d0) * (x_m * x_m))
    else
        tmp = (x_m ** (-2.0d0)) * (1.0d0 - cos(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.005) {
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	} else {
		tmp = Math.pow(x_m, -2.0) * (1.0 - Math.cos(x_m));
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m):
	tmp = 0
	if x_m <= 0.005:
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m))
	else:
		tmp = math.pow(x_m, -2.0) * (1.0 - math.cos(x_m))
	return tmp
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (x_m <= 0.005)
		tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x_m * x_m)));
	else
		tmp = Float64((x_m ^ -2.0) * Float64(1.0 - cos(x_m)));
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m)
	tmp = 0.0;
	if (x_m <= 0.005)
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	else
		tmp = (x_m ^ -2.0) * (1.0 - cos(x_m));
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[x$95$m, 0.005], N[(0.5 + N[(-0.041666666666666664 * N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[x$95$m, -2.0], $MachinePrecision] * N[(1.0 - N[Cos[x$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 0.005:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x\_m \cdot x\_m\right)\\

\mathbf{else}:\\
\;\;\;\;{x\_m}^{-2} \cdot \left(1 - \cos x\_m\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 0.0050000000000000001

    1. Initial program 36.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{-1}{24} \cdot {x}^{2}\right)}\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \color{blue}{\left({x}^{2}\right)}\right)\right) \]
      3. unpow2N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \left(x \cdot \color{blue}{x}\right)\right)\right) \]
      4. *-lowering-*.f6464.5%

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \mathsf{*.f64}\left(x, \color{blue}{x}\right)\right)\right) \]
    5. Simplified64.5%

      \[\leadsto \color{blue}{0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)} \]

    if 0.0050000000000000001 < x

    1. Initial program 98.2%

      \[\frac{1 - \cos x}{x \cdot x} \]
    2. Add Preprocessing
    3. Applied egg-rr99.4%

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

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

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

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

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

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

        \[\leadsto \frac{1 - \cos x}{\color{blue}{x} \cdot x} \]
      7. clear-numN/A

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

        \[\leadsto \frac{1}{x \cdot x} \cdot \color{blue}{\left(1 - \cos x\right)} \]
      9. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\left(\frac{1}{x \cdot x}\right), \color{blue}{\left(1 - \cos x\right)}\right) \]
      10. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(1, \left(x \cdot x\right)\right), \left(\color{blue}{1} - \cos x\right)\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(1, \mathsf{*.f64}\left(x, x\right)\right), \left(1 - \cos x\right)\right) \]
      12. --lowering--.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(1, \mathsf{*.f64}\left(x, x\right)\right), \mathsf{\_.f64}\left(1, \color{blue}{\cos x}\right)\right) \]
      13. cos-lowering-cos.f6498.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(1, \mathsf{*.f64}\left(x, x\right)\right), \mathsf{\_.f64}\left(1, \mathsf{cos.f64}\left(x\right)\right)\right) \]
    5. Applied egg-rr98.1%

      \[\leadsto \color{blue}{\frac{1}{x \cdot x} \cdot \left(1 - \cos x\right)} \]
    6. Step-by-step derivation
      1. pow2N/A

        \[\leadsto \mathsf{*.f64}\left(\left(\frac{1}{{x}^{2}}\right), \mathsf{\_.f64}\left(1, \mathsf{cos.f64}\left(x\right)\right)\right) \]
      2. pow-flipN/A

        \[\leadsto \mathsf{*.f64}\left(\left({x}^{\left(\mathsf{neg}\left(2\right)\right)}\right), \mathsf{\_.f64}\left(\color{blue}{1}, \mathsf{cos.f64}\left(x\right)\right)\right) \]
      3. pow-lowering-pow.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{pow.f64}\left(x, \left(\mathsf{neg}\left(2\right)\right)\right), \mathsf{\_.f64}\left(\color{blue}{1}, \mathsf{cos.f64}\left(x\right)\right)\right) \]
      4. metadata-eval99.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{pow.f64}\left(x, -2\right), \mathsf{\_.f64}\left(1, \mathsf{cos.f64}\left(x\right)\right)\right) \]
    7. Applied egg-rr99.5%

      \[\leadsto \color{blue}{{x}^{-2}} \cdot \left(1 - \cos x\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x\_m \cdot x\_m\right)\\ \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.005)
   (+ 0.5 (* -0.041666666666666664 (* x_m 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.005) {
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	} 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.005d0) then
        tmp = 0.5d0 + ((-0.041666666666666664d0) * (x_m * x_m))
    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.005) {
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	} 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.005:
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m))
	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.005)
		tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x_m * x_m)));
	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.005)
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	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.005], N[(0.5 + N[(-0.041666666666666664 * N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $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.005:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x\_m \cdot x\_m\right)\\

\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.0050000000000000001

    1. Initial program 36.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{-1}{24} \cdot {x}^{2}\right)}\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \color{blue}{\left({x}^{2}\right)}\right)\right) \]
      3. unpow2N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \left(x \cdot \color{blue}{x}\right)\right)\right) \]
      4. *-lowering-*.f6464.5%

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \mathsf{*.f64}\left(x, \color{blue}{x}\right)\right)\right) \]
    5. Simplified64.5%

      \[\leadsto \color{blue}{0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)} \]

    if 0.0050000000000000001 < x

    1. Initial program 98.2%

      \[\frac{1 - \cos x}{x \cdot x} \]
    2. Add Preprocessing
    3. Applied egg-rr99.4%

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

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

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

        \[\leadsto \mathsf{/.f64}\left(\left(\frac{\mathsf{neg}\left(\left(\cos x + -1\right)\right)}{x}\right), x\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(\left(\frac{\mathsf{neg}\left(\left(-1 + \cos x\right)\right)}{x}\right), x\right) \]
      5. distribute-neg-inN/A

        \[\leadsto \mathsf{/.f64}\left(\left(\frac{\left(\mathsf{neg}\left(-1\right)\right) + \left(\mathsf{neg}\left(\cos x\right)\right)}{x}\right), x\right) \]
      6. metadata-evalN/A

        \[\leadsto \mathsf{/.f64}\left(\left(\frac{1 + \left(\mathsf{neg}\left(\cos x\right)\right)}{x}\right), x\right) \]
      7. sub-negN/A

        \[\leadsto \mathsf{/.f64}\left(\left(\frac{1 - \cos x}{x}\right), x\right) \]
      8. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{/.f64}\left(\left(1 - \cos x\right), x\right), x\right) \]
      9. --lowering--.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{\_.f64}\left(1, \cos x\right), x\right), x\right) \]
      10. cos-lowering-cos.f6499.4%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{\_.f64}\left(1, \mathsf{cos.f64}\left(x\right)\right), x\right), x\right) \]
    5. Applied egg-rr99.4%

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

Alternative 3: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.005:\\ \;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x\_m \cdot x\_m\right)\\ \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.005)
   (+ 0.5 (* -0.041666666666666664 (* x_m 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.005) {
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	} 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.005d0) then
        tmp = 0.5d0 + ((-0.041666666666666664d0) * (x_m * x_m))
    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.005) {
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	} 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.005:
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m))
	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.005)
		tmp = Float64(0.5 + Float64(-0.041666666666666664 * Float64(x_m * x_m)));
	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.005)
		tmp = 0.5 + (-0.041666666666666664 * (x_m * x_m));
	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.005], N[(0.5 + N[(-0.041666666666666664 * N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $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.005:\\
\;\;\;\;0.5 + -0.041666666666666664 \cdot \left(x\_m \cdot x\_m\right)\\

\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.0050000000000000001

    1. Initial program 36.9%

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

      \[\leadsto \color{blue}{\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{-1}{24} \cdot {x}^{2}\right)}\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \color{blue}{\left({x}^{2}\right)}\right)\right) \]
      3. unpow2N/A

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \left(x \cdot \color{blue}{x}\right)\right)\right) \]
      4. *-lowering-*.f6464.5%

        \[\leadsto \mathsf{+.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{24}, \mathsf{*.f64}\left(x, \color{blue}{x}\right)\right)\right) \]
    5. Simplified64.5%

      \[\leadsto \color{blue}{0.5 + -0.041666666666666664 \cdot \left(x \cdot x\right)} \]

    if 0.0050000000000000001 < x

    1. Initial program 98.2%

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

Alternative 4: 75.8% accurate, 5.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 1.02 \cdot 10^{+68}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{x\_m \cdot \frac{1}{x\_m \cdot x\_m} + \frac{-1}{x\_m}}{x\_m}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (if (<= x_m 1.02e+68)
   0.5
   (/ (+ (* x_m (/ 1.0 (* x_m x_m))) (/ -1.0 x_m)) x_m)))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if (x_m <= 1.02e+68) {
		tmp = 0.5;
	} else {
		tmp = ((x_m * (1.0 / (x_m * x_m))) + (-1.0 / 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 <= 1.02d+68) then
        tmp = 0.5d0
    else
        tmp = ((x_m * (1.0d0 / (x_m * x_m))) + ((-1.0d0) / 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 <= 1.02e+68) {
		tmp = 0.5;
	} else {
		tmp = ((x_m * (1.0 / (x_m * x_m))) + (-1.0 / x_m)) / x_m;
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m):
	tmp = 0
	if x_m <= 1.02e+68:
		tmp = 0.5
	else:
		tmp = ((x_m * (1.0 / (x_m * x_m))) + (-1.0 / x_m)) / x_m
	return tmp
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (x_m <= 1.02e+68)
		tmp = 0.5;
	else
		tmp = Float64(Float64(Float64(x_m * Float64(1.0 / Float64(x_m * x_m))) + Float64(-1.0 / x_m)) / x_m);
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m)
	tmp = 0.0;
	if (x_m <= 1.02e+68)
		tmp = 0.5;
	else
		tmp = ((x_m * (1.0 / (x_m * x_m))) + (-1.0 / x_m)) / x_m;
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[x$95$m, 1.02e+68], 0.5, N[(N[(N[(x$95$m * N[(1.0 / N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x$95$m), $MachinePrecision]), $MachinePrecision] / x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 1.02 \cdot 10^{+68}:\\
\;\;\;\;0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.02e68

    1. Initial program 41.5%

      \[\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. Simplified61.1%

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

      if 1.02e68 < x

      1. Initial program 98.0%

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

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

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

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

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

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

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

          \[\leadsto \frac{1 - \cos x}{\color{blue}{x} \cdot x} \]
        7. sub-divN/A

          \[\leadsto \frac{1}{x \cdot x} - \color{blue}{\frac{\cos x}{x \cdot x}} \]
        8. /-rgt-identityN/A

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

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

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

          \[\leadsto \frac{\frac{1}{x \cdot x} \cdot x - 1 \cdot \frac{\cos x}{x}}{x} \]
        12. /-lowering-/.f64N/A

          \[\leadsto \mathsf{/.f64}\left(\left(\frac{1}{x \cdot x} \cdot x - 1 \cdot \frac{\cos x}{x}\right), \color{blue}{x}\right) \]
      5. Applied egg-rr97.7%

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

        \[\leadsto \mathsf{/.f64}\left(\mathsf{\_.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(1, \mathsf{*.f64}\left(x, x\right)\right), x\right), \color{blue}{\left(\frac{1}{x}\right)}\right), x\right) \]
      7. Step-by-step derivation
        1. /-lowering-/.f6452.7%

          \[\leadsto \mathsf{/.f64}\left(\mathsf{\_.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(1, \mathsf{*.f64}\left(x, x\right)\right), x\right), \mathsf{/.f64}\left(1, x\right)\right), x\right) \]
      8. Simplified52.7%

        \[\leadsto \frac{\frac{1}{x \cdot x} \cdot x - \color{blue}{\frac{1}{x}}}{x} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification59.3%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.02 \cdot 10^{+68}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot \frac{1}{x \cdot x} + \frac{-1}{x}}{x}\\ \end{array} \]
    7. Add Preprocessing

    Alternative 5: 75.8% accurate, 17.8× speedup?

    \[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 1.3 \cdot 10^{+77}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
    x_m = (fabs.f64 x)
    (FPCore (x_m) :precision binary64 (if (<= x_m 1.3e+77) 0.5 0.0))
    x_m = fabs(x);
    double code(double x_m) {
    	double tmp;
    	if (x_m <= 1.3e+77) {
    		tmp = 0.5;
    	} else {
    		tmp = 0.0;
    	}
    	return tmp;
    }
    
    x_m = abs(x)
    real(8) function code(x_m)
        real(8), intent (in) :: x_m
        real(8) :: tmp
        if (x_m <= 1.3d+77) then
            tmp = 0.5d0
        else
            tmp = 0.0d0
        end if
        code = tmp
    end function
    
    x_m = Math.abs(x);
    public static double code(double x_m) {
    	double tmp;
    	if (x_m <= 1.3e+77) {
    		tmp = 0.5;
    	} else {
    		tmp = 0.0;
    	}
    	return tmp;
    }
    
    x_m = math.fabs(x)
    def code(x_m):
    	tmp = 0
    	if x_m <= 1.3e+77:
    		tmp = 0.5
    	else:
    		tmp = 0.0
    	return tmp
    
    x_m = abs(x)
    function code(x_m)
    	tmp = 0.0
    	if (x_m <= 1.3e+77)
    		tmp = 0.5;
    	else
    		tmp = 0.0;
    	end
    	return tmp
    end
    
    x_m = abs(x);
    function tmp_2 = code(x_m)
    	tmp = 0.0;
    	if (x_m <= 1.3e+77)
    		tmp = 0.5;
    	else
    		tmp = 0.0;
    	end
    	tmp_2 = tmp;
    end
    
    x_m = N[Abs[x], $MachinePrecision]
    code[x$95$m_] := If[LessEqual[x$95$m, 1.3e+77], 0.5, 0.0]
    
    \begin{array}{l}
    x_m = \left|x\right|
    
    \\
    \begin{array}{l}
    \mathbf{if}\;x\_m \leq 1.3 \cdot 10^{+77}:\\
    \;\;\;\;0.5\\
    
    \mathbf{else}:\\
    \;\;\;\;0\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if x < 1.3000000000000001e77

      1. Initial program 42.3%

        \[\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. Simplified60.2%

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

        if 1.3000000000000001e77 < x

        1. Initial program 97.9%

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

          \[\leadsto \mathsf{/.f64}\left(\mathsf{\_.f64}\left(1, \color{blue}{1}\right), \mathsf{*.f64}\left(x, x\right)\right) \]
        4. Step-by-step derivation
          1. Simplified55.2%

            \[\leadsto \frac{1 - \color{blue}{1}}{x \cdot x} \]
          2. Step-by-step derivation
            1. metadata-evalN/A

              \[\leadsto \frac{0}{\color{blue}{x} \cdot x} \]
            2. div055.2%

              \[\leadsto 0 \]
          3. Applied egg-rr55.2%

            \[\leadsto \color{blue}{0} \]
        5. Recombined 2 regimes into one program.
        6. Add Preprocessing

        Alternative 6: 27.6% accurate, 107.0× speedup?

        \[\begin{array}{l} x_m = \left|x\right| \\ 0 \end{array} \]
        x_m = (fabs.f64 x)
        (FPCore (x_m) :precision binary64 0.0)
        x_m = fabs(x);
        double code(double x_m) {
        	return 0.0;
        }
        
        x_m = abs(x)
        real(8) function code(x_m)
            real(8), intent (in) :: x_m
            code = 0.0d0
        end function
        
        x_m = Math.abs(x);
        public static double code(double x_m) {
        	return 0.0;
        }
        
        x_m = math.fabs(x)
        def code(x_m):
        	return 0.0
        
        x_m = abs(x)
        function code(x_m)
        	return 0.0
        end
        
        x_m = abs(x);
        function tmp = code(x_m)
        	tmp = 0.0;
        end
        
        x_m = N[Abs[x], $MachinePrecision]
        code[x$95$m_] := 0.0
        
        \begin{array}{l}
        x_m = \left|x\right|
        
        \\
        0
        \end{array}
        
        Derivation
        1. Initial program 53.6%

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

          \[\leadsto \mathsf{/.f64}\left(\mathsf{\_.f64}\left(1, \color{blue}{1}\right), \mathsf{*.f64}\left(x, x\right)\right) \]
        4. Step-by-step derivation
          1. Simplified25.4%

            \[\leadsto \frac{1 - \color{blue}{1}}{x \cdot x} \]
          2. Step-by-step derivation
            1. metadata-evalN/A

              \[\leadsto \frac{0}{\color{blue}{x} \cdot x} \]
            2. div026.2%

              \[\leadsto 0 \]
          3. Applied egg-rr26.2%

            \[\leadsto \color{blue}{0} \]
          4. Add Preprocessing

          Reproduce

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