Average Error: 36.8 → 0.3
Time: 16.9s
Precision: binary64
\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -2.01426406875669 \cdot 10^{-8}:\\ \;\;\;\;\mathsf{fma}\left(t_0, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, -\tan x\right)\\ \mathbf{elif}\;\varepsilon \leq 3.118191246439813 \cdot 10^{-7}:\\ \;\;\;\;\begin{array}{l} t_1 := {\cos x}^{2}\\ \mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{t_1}, \varepsilon\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{t_1}\right) \end{array}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{1 - \left(\tan x \cdot \sin \varepsilon\right) \cdot \frac{1}{\cos \varepsilon}} - \tan x\\ \end{array} \]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := \tan x + \tan \varepsilon\\
\mathbf{if}\;\varepsilon \leq -2.01426406875669 \cdot 10^{-8}:\\
\;\;\;\;\mathsf{fma}\left(t_0, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, -\tan x\right)\\

\mathbf{elif}\;\varepsilon \leq 3.118191246439813 \cdot 10^{-7}:\\
\;\;\;\;\begin{array}{l}
t_1 := {\cos x}^{2}\\
\mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{t_1}, \varepsilon\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{t_1}\right)
\end{array}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{1 - \left(\tan x \cdot \sin \varepsilon\right) \cdot \frac{1}{\cos \varepsilon}} - \tan x\\


\end{array}
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (+ (tan x) (tan eps))))
   (if (<= eps -2.01426406875669e-8)
     (fma t_0 (/ 1.0 (- 1.0 (* (tan x) (tan eps)))) (- (tan x)))
     (if (<= eps 3.118191246439813e-7)
       (let* ((t_1 (pow (cos x) 2.0)))
         (+
          (fma eps (/ (pow (sin x) 2.0) t_1) eps)
          (* (/ (* eps eps) (cos x)) (+ (sin x) (/ (pow (sin x) 3.0) t_1)))))
       (-
        (/ t_0 (- 1.0 (* (* (tan x) (sin eps)) (/ 1.0 (cos eps)))))
        (tan x))))))
double code(double x, double eps) {
	return tan(x + eps) - tan(x);
}
double code(double x, double eps) {
	double t_0 = tan(x) + tan(eps);
	double tmp;
	if (eps <= -2.01426406875669e-8) {
		tmp = fma(t_0, (1.0 / (1.0 - (tan(x) * tan(eps)))), -tan(x));
	} else if (eps <= 3.118191246439813e-7) {
		double t_1 = pow(cos(x), 2.0);
		tmp = fma(eps, (pow(sin(x), 2.0) / t_1), eps) + (((eps * eps) / cos(x)) * (sin(x) + (pow(sin(x), 3.0) / t_1)));
	} else {
		tmp = (t_0 / (1.0 - ((tan(x) * sin(eps)) * (1.0 / cos(eps))))) - tan(x);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus eps

Target

Original36.8
Target15.2
Herbie0.3
\[\frac{\sin \varepsilon}{\cos x \cdot \cos \left(x + \varepsilon\right)} \]

Derivation

  1. Split input into 3 regimes
  2. if eps < -2.01426406875669e-8

    1. Initial program 29.5

      \[\tan \left(x + \varepsilon\right) - \tan x \]
    2. Applied tan-sum_binary640.5

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]
    3. Applied div-inv_binary640.5

      \[\leadsto \color{blue}{\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]
    4. Applied fma-neg_binary640.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, -\tan x\right)} \]

    if -2.01426406875669e-8 < eps < 3.118191246439813e-7

    1. Initial program 44.4

      \[\tan \left(x + \varepsilon\right) - \tan x \]
    2. Applied tan-sum_binary6444.1

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]
    3. Taylor expanded in eps around 0 0.2

      \[\leadsto \color{blue}{\frac{{\varepsilon}^{2} \cdot {\sin x}^{3}}{{\cos x}^{3}} + \left(\frac{{\varepsilon}^{2} \cdot \sin x}{\cos x} + \left(\varepsilon + \frac{\varepsilon \cdot {\sin x}^{2}}{{\cos x}^{2}}\right)\right)} \]
    4. Simplified0.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{{\cos x}^{2}}, \varepsilon\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\frac{{\sin x}^{3}}{{\cos x}^{2}} + \sin x\right)} \]

    if 3.118191246439813e-7 < eps

    1. Initial program 29.9

      \[\tan \left(x + \varepsilon\right) - \tan x \]
    2. Applied tan-sum_binary640.4

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]
    3. Applied tan-quot_binary640.4

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \color{blue}{\frac{\sin \varepsilon}{\cos \varepsilon}}} - \tan x \]
    4. Applied associate-*r/_binary640.4

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}}} - \tan x \]
    5. Applied div-inv_binary640.4

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\left(\tan x \cdot \sin \varepsilon\right) \cdot \frac{1}{\cos \varepsilon}}} - \tan x \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq -2.01426406875669 \cdot 10^{-8}:\\ \;\;\;\;\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, -\tan x\right)\\ \mathbf{elif}\;\varepsilon \leq 3.118191246439813 \cdot 10^{-7}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{{\cos x}^{2}}, \varepsilon\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{{\cos x}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \left(\tan x \cdot \sin \varepsilon\right) \cdot \frac{1}{\cos \varepsilon}} - \tan x\\ \end{array} \]

Reproduce

herbie shell --seed 2021280 
(FPCore (x eps)
  :name "2tan (problem 3.3.2)"
  :precision binary64

  :herbie-target
  (/ (sin eps) (* (cos x) (cos (+ x eps))))

  (- (tan (+ x eps)) (tan x)))