(1-cos(x+0.001)) + 1-cos(x)

Percentage Accurate: 74.7% → 100.0%
Time: 4.1s
Alternatives: 7
Speedup: 2.0×

Specification

?
\[-1 \cdot 10^{-9} \leq x \land x \leq 0\]
\[\begin{array}{l} \\ \left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \end{array} \]
(FPCore (x) :precision binary64 (- (+ (- 1.0 (cos (+ x 0.001))) 1.0) (cos x)))
double code(double x) {
	return ((1.0 - cos((x + 0.001))) + 1.0) - cos(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((1.0d0 - cos((x + 0.001d0))) + 1.0d0) - cos(x)
end function
public static double code(double x) {
	return ((1.0 - Math.cos((x + 0.001))) + 1.0) - Math.cos(x);
}
def code(x):
	return ((1.0 - math.cos((x + 0.001))) + 1.0) - math.cos(x)
function code(x)
	return Float64(Float64(Float64(1.0 - cos(Float64(x + 0.001))) + 1.0) - cos(x))
end
function tmp = code(x)
	tmp = ((1.0 - cos((x + 0.001))) + 1.0) - cos(x);
end
code[x_] := N[(N[(N[(1.0 - N[Cos[N[(x + 0.001), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos 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 7 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: 74.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \end{array} \]
(FPCore (x) :precision binary64 (- (+ (- 1.0 (cos (+ x 0.001))) 1.0) (cos x)))
double code(double x) {
	return ((1.0 - cos((x + 0.001))) + 1.0) - cos(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((1.0d0 - cos((x + 0.001d0))) + 1.0d0) - cos(x)
end function
public static double code(double x) {
	return ((1.0 - Math.cos((x + 0.001))) + 1.0) - Math.cos(x);
}
def code(x):
	return ((1.0 - math.cos((x + 0.001))) + 1.0) - math.cos(x)
function code(x)
	return Float64(Float64(Float64(1.0 - cos(Float64(x + 0.001))) + 1.0) - cos(x))
end
function tmp = code(x)
	tmp = ((1.0 - cos((x + 0.001))) + 1.0) - cos(x);
end
code[x_] := N[(N[(N[(1.0 - N[Cos[N[(x + 0.001), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision] - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x
\end{array}

Alternative 1: 100.0% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos 0.001, 0.5\right), x, \sin 0.001\right), x, \frac{{\sin 0.001}^{2}}{\cos 0.001 + 1}\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (fma
  (fma (fma 0.5 (cos 0.001) 0.5) x (sin 0.001))
  x
  (/ (pow (sin 0.001) 2.0) (+ (cos 0.001) 1.0))))
double code(double x) {
	return fma(fma(fma(0.5, cos(0.001), 0.5), x, sin(0.001)), x, (pow(sin(0.001), 2.0) / (cos(0.001) + 1.0)));
}
function code(x)
	return fma(fma(fma(0.5, cos(0.001), 0.5), x, sin(0.001)), x, Float64((sin(0.001) ^ 2.0) / Float64(cos(0.001) + 1.0)))
end
code[x_] := N[(N[(N[(0.5 * N[Cos[0.001], $MachinePrecision] + 0.5), $MachinePrecision] * x + N[Sin[0.001], $MachinePrecision]), $MachinePrecision] * x + N[(N[Power[N[Sin[0.001], $MachinePrecision], 2.0], $MachinePrecision] / N[(N[Cos[0.001], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos 0.001, 0.5\right), x, \sin 0.001\right), x, \frac{{\sin 0.001}^{2}}{\cos 0.001 + 1}\right)
\end{array}
Derivation
  1. Initial program 74.7%

    \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\left(1 + x \cdot \left(\sin \frac{1152921504606847}{1152921504606846976} + x \cdot \left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right)\right)\right) - \cos \frac{1152921504606847}{1152921504606846976}} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

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

      \[\leadsto \color{blue}{x \cdot \left(\sin \frac{1152921504606847}{1152921504606846976} + x \cdot \left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right)\right) + \left(1 - \cos \frac{1152921504606847}{1152921504606846976}\right)} \]
    3. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\sin \frac{1152921504606847}{1152921504606846976} + x \cdot \left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot x} + \left(1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    4. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \frac{1152921504606847}{1152921504606846976} + x \cdot \left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right)} \]
    5. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right) + \sin \frac{1152921504606847}{1152921504606846976}}, x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    6. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right) \cdot x} + \sin \frac{1152921504606847}{1152921504606846976}, x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    7. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{1}{2} - \frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}, x, \sin \frac{1152921504606847}{1152921504606846976}\right)}, x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    8. sub-negN/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\frac{1}{2} + \left(\mathsf{neg}\left(\frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right)\right)}, x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    9. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{2} \cdot \cos \frac{1152921504606847}{1152921504606846976}\right)\right) + \frac{1}{2}}, x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    10. distribute-lft-neg-inN/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{2}\right)\right) \cdot \cos \frac{1152921504606847}{1152921504606846976}} + \frac{1}{2}, x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    11. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\frac{1}{2}} \cdot \cos \frac{1152921504606847}{1152921504606846976} + \frac{1}{2}, x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    12. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{1}{2}, \cos \frac{1152921504606847}{1152921504606846976}, \frac{1}{2}\right)}, x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    13. lower-cos.f64N/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \color{blue}{\cos \frac{1152921504606847}{1152921504606846976}}, \frac{1}{2}\right), x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    14. lower-sin.f64N/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \cos \frac{1152921504606847}{1152921504606846976}, \frac{1}{2}\right), x, \color{blue}{\sin \frac{1152921504606847}{1152921504606846976}}\right), x, 1 - \cos \frac{1152921504606847}{1152921504606846976}\right) \]
    15. lower--.f64N/A

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \cos \frac{1152921504606847}{1152921504606846976}, \frac{1}{2}\right), x, \sin \frac{1152921504606847}{1152921504606846976}\right), x, \color{blue}{1 - \cos \frac{1152921504606847}{1152921504606846976}}\right) \]
    16. lower-cos.f6474.7

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos 0.001, 0.5\right), x, \sin 0.001\right), x, 1 - \color{blue}{\cos 0.001}\right) \]
  5. Applied rewrites74.7%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos 0.001, 0.5\right), x, \sin 0.001\right), x, 1 - \cos 0.001\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos 0.001, 0.5\right), x, \sin 0.001\right), x, \frac{{\sin 0.001}^{2}}{\cos 0.001 + 1}\right) \]
    2. Add Preprocessing

    Alternative 2: 100.0% accurate, 0.5× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(0.001 + x\right)\\ \mathsf{fma}\left(t\_0, t\_0 \cdot {\left(\cos \left(0.001 + x\right) + 1\right)}^{-1}, \left(x \cdot x\right) \cdot 0.5\right) \end{array} \end{array} \]
    (FPCore (x)
     :precision binary64
     (let* ((t_0 (sin (+ 0.001 x))))
       (fma t_0 (* t_0 (pow (+ (cos (+ 0.001 x)) 1.0) -1.0)) (* (* x x) 0.5))))
    double code(double x) {
    	double t_0 = sin((0.001 + x));
    	return fma(t_0, (t_0 * pow((cos((0.001 + x)) + 1.0), -1.0)), ((x * x) * 0.5));
    }
    
    function code(x)
    	t_0 = sin(Float64(0.001 + x))
    	return fma(t_0, Float64(t_0 * (Float64(cos(Float64(0.001 + x)) + 1.0) ^ -1.0)), Float64(Float64(x * x) * 0.5))
    end
    
    code[x_] := Block[{t$95$0 = N[Sin[N[(0.001 + x), $MachinePrecision]], $MachinePrecision]}, N[(t$95$0 * N[(t$95$0 * N[Power[N[(N[Cos[N[(0.001 + x), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision], -1.0], $MachinePrecision]), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \sin \left(0.001 + x\right)\\
    \mathsf{fma}\left(t\_0, t\_0 \cdot {\left(\cos \left(0.001 + x\right) + 1\right)}^{-1}, \left(x \cdot x\right) \cdot 0.5\right)
    \end{array}
    \end{array}
    
    Derivation
    1. Initial program 74.7%

      \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift--.f64N/A

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

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

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

        \[\leadsto \color{blue}{\left(1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right)} + \left(1 - \cos x\right) \]
      5. flip--N/A

        \[\leadsto \color{blue}{\frac{1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}} + \left(1 - \cos x\right) \]
      6. div-invN/A

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

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

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

        \[\leadsto \left(1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \color{blue}{\cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}\right) \cdot \frac{1}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)} + \left(1 - \cos x\right) \]
      10. 1-sub-cosN/A

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

        \[\leadsto \color{blue}{\sin \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \left(\sin \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \frac{1}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}\right)} + \left(1 - \cos x\right) \]
      12. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \left(x + \frac{1152921504606847}{1152921504606846976}\right), \sin \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \frac{1}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}, 1 - \cos x\right)} \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \left(0.001 + x\right), \sin \left(0.001 + x\right) \cdot {\left(\cos \left(0.001 + x\right) + 1\right)}^{-1}, 1 - \cos x\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \sin \left(\frac{1152921504606847}{1152921504606846976} + x\right) \cdot {\left(\cos \left(\frac{1152921504606847}{1152921504606846976} + x\right) + 1\right)}^{-1}, \color{blue}{\frac{1}{2} \cdot {x}^{2}}\right) \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \sin \left(\frac{1152921504606847}{1152921504606846976} + x\right) \cdot {\left(\cos \left(\frac{1152921504606847}{1152921504606846976} + x\right) + 1\right)}^{-1}, \color{blue}{{x}^{2} \cdot \frac{1}{2}}\right) \]
      2. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \sin \left(\frac{1152921504606847}{1152921504606846976} + x\right) \cdot {\left(\cos \left(\frac{1152921504606847}{1152921504606846976} + x\right) + 1\right)}^{-1}, \color{blue}{{x}^{2} \cdot \frac{1}{2}}\right) \]
      3. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \sin \left(\frac{1152921504606847}{1152921504606846976} + x\right) \cdot {\left(\cos \left(\frac{1152921504606847}{1152921504606846976} + x\right) + 1\right)}^{-1}, \color{blue}{\left(x \cdot x\right)} \cdot \frac{1}{2}\right) \]
      4. lower-*.f64100.0

        \[\leadsto \mathsf{fma}\left(\sin \left(0.001 + x\right), \sin \left(0.001 + x\right) \cdot {\left(\cos \left(0.001 + x\right) + 1\right)}^{-1}, \color{blue}{\left(x \cdot x\right)} \cdot 0.5\right) \]
    7. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(\sin \left(0.001 + x\right), \sin \left(0.001 + x\right) \cdot {\left(\cos \left(0.001 + x\right) + 1\right)}^{-1}, \color{blue}{\left(x \cdot x\right) \cdot 0.5}\right) \]
    8. Add Preprocessing

    Alternative 3: 99.1% accurate, 0.7× speedup?

    \[\begin{array}{l} \\ \frac{{\sin 0.001}^{2}}{\cos 0.001 + 1} \end{array} \]
    (FPCore (x) :precision binary64 (/ (pow (sin 0.001) 2.0) (+ (cos 0.001) 1.0)))
    double code(double x) {
    	return pow(sin(0.001), 2.0) / (cos(0.001) + 1.0);
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = (sin(0.001d0) ** 2.0d0) / (cos(0.001d0) + 1.0d0)
    end function
    
    public static double code(double x) {
    	return Math.pow(Math.sin(0.001), 2.0) / (Math.cos(0.001) + 1.0);
    }
    
    def code(x):
    	return math.pow(math.sin(0.001), 2.0) / (math.cos(0.001) + 1.0)
    
    function code(x)
    	return Float64((sin(0.001) ^ 2.0) / Float64(cos(0.001) + 1.0))
    end
    
    function tmp = code(x)
    	tmp = (sin(0.001) ^ 2.0) / (cos(0.001) + 1.0);
    end
    
    code[x_] := N[(N[Power[N[Sin[0.001], $MachinePrecision], 2.0], $MachinePrecision] / N[(N[Cos[0.001], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \frac{{\sin 0.001}^{2}}{\cos 0.001 + 1}
    \end{array}
    
    Derivation
    1. Initial program 74.7%

      \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{1 - \cos \frac{1152921504606847}{1152921504606846976}} \]
    4. Step-by-step derivation
      1. lower--.f64N/A

        \[\leadsto \color{blue}{1 - \cos \frac{1152921504606847}{1152921504606846976}} \]
      2. lower-cos.f6474.5

        \[\leadsto 1 - \color{blue}{\cos 0.001} \]
    5. Applied rewrites74.5%

      \[\leadsto \color{blue}{1 - \cos 0.001} \]
    6. Step-by-step derivation
      1. Applied rewrites99.2%

        \[\leadsto \frac{{\sin 0.001}^{2}}{\color{blue}{\cos 0.001 + 1}} \]
      2. Add Preprocessing

      Alternative 4: 97.7% accurate, 0.7× speedup?

      \[\begin{array}{l} \\ \mathsf{fma}\left(\sin \left(0.001 + x\right), \tan 0.0005, 1 - \cos x\right) \end{array} \]
      (FPCore (x)
       :precision binary64
       (fma (sin (+ 0.001 x)) (tan 0.0005) (- 1.0 (cos x))))
      double code(double x) {
      	return fma(sin((0.001 + x)), tan(0.0005), (1.0 - cos(x)));
      }
      
      function code(x)
      	return fma(sin(Float64(0.001 + x)), tan(0.0005), Float64(1.0 - cos(x)))
      end
      
      code[x_] := N[(N[Sin[N[(0.001 + x), $MachinePrecision]], $MachinePrecision] * N[Tan[0.0005], $MachinePrecision] + N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \mathsf{fma}\left(\sin \left(0.001 + x\right), \tan 0.0005, 1 - \cos x\right)
      \end{array}
      
      Derivation
      1. Initial program 74.7%

        \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
      2. Add Preprocessing
      3. Step-by-step derivation
        1. lift--.f64N/A

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

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

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

          \[\leadsto \color{blue}{\left(1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right)} + \left(1 - \cos x\right) \]
        5. flip--N/A

          \[\leadsto \color{blue}{\frac{1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}} + \left(1 - \cos x\right) \]
        6. div-invN/A

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

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

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

          \[\leadsto \left(1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \color{blue}{\cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}\right) \cdot \frac{1}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)} + \left(1 - \cos x\right) \]
        10. 1-sub-cosN/A

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

          \[\leadsto \color{blue}{\sin \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \left(\sin \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \frac{1}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}\right)} + \left(1 - \cos x\right) \]
        12. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \left(x + \frac{1152921504606847}{1152921504606846976}\right), \sin \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \frac{1}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}, 1 - \cos x\right)} \]
      4. Applied rewrites99.9%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \left(0.001 + x\right), \sin \left(0.001 + x\right) \cdot {\left(\cos \left(0.001 + x\right) + 1\right)}^{-1}, 1 - \cos x\right)} \]
      5. Taylor expanded in x around 0

        \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \color{blue}{\frac{\sin \frac{1152921504606847}{1152921504606846976}}{1 + \cos \frac{1152921504606847}{1152921504606846976}}}, 1 - \cos x\right) \]
      6. Step-by-step derivation
        1. hang-0p-tanN/A

          \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \color{blue}{\tan \left(\frac{\frac{1152921504606847}{1152921504606846976}}{2}\right)}, 1 - \cos x\right) \]
        2. lower-tan.f64N/A

          \[\leadsto \mathsf{fma}\left(\sin \left(\frac{1152921504606847}{1152921504606846976} + x\right), \color{blue}{\tan \left(\frac{\frac{1152921504606847}{1152921504606846976}}{2}\right)}, 1 - \cos x\right) \]
        3. metadata-eval97.8

          \[\leadsto \mathsf{fma}\left(\sin \left(0.001 + x\right), \tan \color{blue}{0.0005}, 1 - \cos x\right) \]
      7. Applied rewrites97.8%

        \[\leadsto \mathsf{fma}\left(\sin \left(0.001 + x\right), \color{blue}{\tan 0.0005}, 1 - \cos x\right) \]
      8. Add Preprocessing

      Alternative 5: 97.6% accurate, 1.0× speedup?

      \[\begin{array}{l} \\ \sin 0.001 \cdot \tan 0.0005 \end{array} \]
      (FPCore (x) :precision binary64 (* (sin 0.001) (tan 0.0005)))
      double code(double x) {
      	return sin(0.001) * tan(0.0005);
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = sin(0.001d0) * tan(0.0005d0)
      end function
      
      public static double code(double x) {
      	return Math.sin(0.001) * Math.tan(0.0005);
      }
      
      def code(x):
      	return math.sin(0.001) * math.tan(0.0005)
      
      function code(x)
      	return Float64(sin(0.001) * tan(0.0005))
      end
      
      function tmp = code(x)
      	tmp = sin(0.001) * tan(0.0005);
      end
      
      code[x_] := N[(N[Sin[0.001], $MachinePrecision] * N[Tan[0.0005], $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \sin 0.001 \cdot \tan 0.0005
      \end{array}
      
      Derivation
      1. Initial program 74.7%

        \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
      2. Add Preprocessing
      3. Step-by-step derivation
        1. lift--.f64N/A

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

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

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

          \[\leadsto \color{blue}{\left(1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right)} + \left(1 - \cos x\right) \]
        5. flip--N/A

          \[\leadsto \color{blue}{\frac{1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}} + \left(1 - \cos x\right) \]
        6. flip--N/A

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

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

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

          \[\leadsto \frac{1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)} + \frac{1 - \cos x \cdot \color{blue}{\cos x}}{1 + \cos x} \]
        10. sqr-sin-bN/A

          \[\leadsto \frac{1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)}{1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)} + \frac{\color{blue}{\sin x \cdot \sin x}}{1 + \cos x} \]
        11. frac-addN/A

          \[\leadsto \color{blue}{\frac{\left(1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot \left(1 + \cos x\right) + \left(1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot \left(\sin x \cdot \sin x\right)}{\left(1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot \left(1 + \cos x\right)}} \]
        12. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{\left(1 \cdot 1 - \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right) \cdot \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot \left(1 + \cos x\right) + \left(1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot \left(\sin x \cdot \sin x\right)}{\left(1 + \cos \left(x + \frac{1152921504606847}{1152921504606846976}\right)\right) \cdot \left(1 + \cos x\right)}} \]
      4. Applied rewrites100.0%

        \[\leadsto \color{blue}{\frac{\mathsf{fma}\left({\sin \left(0.001 + x\right)}^{2}, 1 + \cos x, \left(\cos \left(0.001 + x\right) + 1\right) \cdot {\sin x}^{2}\right)}{\left(\cos \left(0.001 + x\right) + 1\right) \cdot \left(1 + \cos x\right)}} \]
      5. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{{\sin \frac{1152921504606847}{1152921504606846976}}^{2}}{1 + \cos \frac{1152921504606847}{1152921504606846976}}} \]
      6. Step-by-step derivation
        1. unpow2N/A

          \[\leadsto \frac{\color{blue}{\sin \frac{1152921504606847}{1152921504606846976} \cdot \sin \frac{1152921504606847}{1152921504606846976}}}{1 + \cos \frac{1152921504606847}{1152921504606846976}} \]
        2. associate-/l*N/A

          \[\leadsto \color{blue}{\sin \frac{1152921504606847}{1152921504606846976} \cdot \frac{\sin \frac{1152921504606847}{1152921504606846976}}{1 + \cos \frac{1152921504606847}{1152921504606846976}}} \]
        3. lower-*.f64N/A

          \[\leadsto \color{blue}{\sin \frac{1152921504606847}{1152921504606846976} \cdot \frac{\sin \frac{1152921504606847}{1152921504606846976}}{1 + \cos \frac{1152921504606847}{1152921504606846976}}} \]
        4. lower-sin.f64N/A

          \[\leadsto \color{blue}{\sin \frac{1152921504606847}{1152921504606846976}} \cdot \frac{\sin \frac{1152921504606847}{1152921504606846976}}{1 + \cos \frac{1152921504606847}{1152921504606846976}} \]
        5. hang-0p-tanN/A

          \[\leadsto \sin \frac{1152921504606847}{1152921504606846976} \cdot \color{blue}{\tan \left(\frac{\frac{1152921504606847}{1152921504606846976}}{2}\right)} \]
        6. lower-tan.f64N/A

          \[\leadsto \sin \frac{1152921504606847}{1152921504606846976} \cdot \color{blue}{\tan \left(\frac{\frac{1152921504606847}{1152921504606846976}}{2}\right)} \]
        7. metadata-eval97.7

          \[\leadsto \sin 0.001 \cdot \tan \color{blue}{0.0005} \]
      7. Applied rewrites97.7%

        \[\leadsto \color{blue}{\sin 0.001 \cdot \tan 0.0005} \]
      8. Add Preprocessing

      Alternative 6: 74.7% accurate, 2.0× speedup?

      \[\begin{array}{l} \\ 1 - \cos \left(0.001 + x\right) \end{array} \]
      (FPCore (x) :precision binary64 (- 1.0 (cos (+ 0.001 x))))
      double code(double x) {
      	return 1.0 - cos((0.001 + x));
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = 1.0d0 - cos((0.001d0 + x))
      end function
      
      public static double code(double x) {
      	return 1.0 - Math.cos((0.001 + x));
      }
      
      def code(x):
      	return 1.0 - math.cos((0.001 + x))
      
      function code(x)
      	return Float64(1.0 - cos(Float64(0.001 + x)))
      end
      
      function tmp = code(x)
      	tmp = 1.0 - cos((0.001 + x));
      end
      
      code[x_] := N[(1.0 - N[Cos[N[(0.001 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      1 - \cos \left(0.001 + x\right)
      \end{array}
      
      Derivation
      1. Initial program 74.7%

        \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{2 - \left(\cos x + \cos \left(\frac{1152921504606847}{1152921504606846976} + x\right)\right)} \]
      4. Step-by-step derivation
        1. associate--r+N/A

          \[\leadsto \color{blue}{\left(2 - \cos x\right) - \cos \left(\frac{1152921504606847}{1152921504606846976} + x\right)} \]
        2. lower--.f64N/A

          \[\leadsto \color{blue}{\left(2 - \cos x\right) - \cos \left(\frac{1152921504606847}{1152921504606846976} + x\right)} \]
        3. lower--.f64N/A

          \[\leadsto \color{blue}{\left(2 - \cos x\right)} - \cos \left(\frac{1152921504606847}{1152921504606846976} + x\right) \]
        4. lower-cos.f64N/A

          \[\leadsto \left(2 - \color{blue}{\cos x}\right) - \cos \left(\frac{1152921504606847}{1152921504606846976} + x\right) \]
        5. lower-cos.f64N/A

          \[\leadsto \left(2 - \cos x\right) - \color{blue}{\cos \left(\frac{1152921504606847}{1152921504606846976} + x\right)} \]
        6. lower-+.f6474.7

          \[\leadsto \left(2 - \cos x\right) - \cos \color{blue}{\left(0.001 + x\right)} \]
      5. Applied rewrites74.7%

        \[\leadsto \color{blue}{\left(2 - \cos x\right) - \cos \left(0.001 + x\right)} \]
      6. Taylor expanded in x around 0

        \[\leadsto 1 - \cos \color{blue}{\left(\frac{1152921504606847}{1152921504606846976} + x\right)} \]
      7. Step-by-step derivation
        1. Applied rewrites74.7%

          \[\leadsto 1 - \cos \color{blue}{\left(0.001 + x\right)} \]
        2. Add Preprocessing

        Alternative 7: 74.5% accurate, 2.0× speedup?

        \[\begin{array}{l} \\ 1 - \cos 0.001 \end{array} \]
        (FPCore (x) :precision binary64 (- 1.0 (cos 0.001)))
        double code(double x) {
        	return 1.0 - cos(0.001);
        }
        
        real(8) function code(x)
            real(8), intent (in) :: x
            code = 1.0d0 - cos(0.001d0)
        end function
        
        public static double code(double x) {
        	return 1.0 - Math.cos(0.001);
        }
        
        def code(x):
        	return 1.0 - math.cos(0.001)
        
        function code(x)
        	return Float64(1.0 - cos(0.001))
        end
        
        function tmp = code(x)
        	tmp = 1.0 - cos(0.001);
        end
        
        code[x_] := N[(1.0 - N[Cos[0.001], $MachinePrecision]), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        1 - \cos 0.001
        \end{array}
        
        Derivation
        1. Initial program 74.7%

          \[\left(\left(1 - \cos \left(x + 0.001\right)\right) + 1\right) - \cos x \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{1 - \cos \frac{1152921504606847}{1152921504606846976}} \]
        4. Step-by-step derivation
          1. lower--.f64N/A

            \[\leadsto \color{blue}{1 - \cos \frac{1152921504606847}{1152921504606846976}} \]
          2. lower-cos.f6474.5

            \[\leadsto 1 - \color{blue}{\cos 0.001} \]
        5. Applied rewrites74.5%

          \[\leadsto \color{blue}{1 - \cos 0.001} \]
        6. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 1 
        (FPCore (x)
          :name "(1-cos(x+0.001)) + 1-cos(x)"
          :precision binary64
          :pre (and (<= -1e-9 x) (<= x 0.0))
          (- (+ (- 1.0 (cos (+ x 0.001))) 1.0) (cos x)))