Average Error: 36.5 → 0.3
Time: 15.1s
Precision: binary64
\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ t_1 := -\tan x\\ \mathbf{if}\;\varepsilon \leq -0.00027961796206420387:\\ \;\;\;\;\begin{array}{l} t_2 := \tan x \cdot \tan \varepsilon\\ \mathsf{fma}\left(\frac{t_0}{1 - t_2 \cdot t_2}, 1 + t_2, t_1\right) \end{array}\\ \mathbf{elif}\;\varepsilon \leq 0.00023084122125612065:\\ \;\;\;\;\begin{array}{l} t_3 := {\sin x}^{3}\\ t_4 := {\cos x}^{2}\\ t_5 := {\cos x}^{3}\\ t_6 := {\sin x}^{2}\\ \frac{{\varepsilon}^{2} \cdot t_3}{t_5} + \left(\frac{{\varepsilon}^{2} \cdot \sin x}{\cos x} + \left(\varepsilon + \left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(1.6666666666666667 \cdot \frac{t_3 \cdot {\varepsilon}^{4}}{t_5} + \left(\frac{{\varepsilon}^{4} \cdot {\sin x}^{5}}{{\cos x}^{5}} + \left(1.3333333333333333 \cdot \frac{{\varepsilon}^{3} \cdot t_6}{t_4} + \left(\frac{\varepsilon \cdot t_6}{t_4} + \left(0.6666666666666666 \cdot \frac{\sin x \cdot {\varepsilon}^{4}}{\cos x} + {\varepsilon}^{3} \cdot 0.3333333333333333\right)\right)\right)\right)\right)\right)\right)\right) \end{array}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_7 := \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\\ \mathsf{fma}\left(\frac{t_0}{1 - {t_7}^{3}}, \mathsf{fma}\left(t_7, 1 + t_7, 1\right), t_1\right) \end{array}\\ \end{array} \]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := \tan x + \tan \varepsilon\\
t_1 := -\tan x\\
\mathbf{if}\;\varepsilon \leq -0.00027961796206420387:\\
\;\;\;\;\begin{array}{l}
t_2 := \tan x \cdot \tan \varepsilon\\
\mathsf{fma}\left(\frac{t_0}{1 - t_2 \cdot t_2}, 1 + t_2, t_1\right)
\end{array}\\

\mathbf{elif}\;\varepsilon \leq 0.00023084122125612065:\\
\;\;\;\;\begin{array}{l}
t_3 := {\sin x}^{3}\\
t_4 := {\cos x}^{2}\\
t_5 := {\cos x}^{3}\\
t_6 := {\sin x}^{2}\\
\frac{{\varepsilon}^{2} \cdot t_3}{t_5} + \left(\frac{{\varepsilon}^{2} \cdot \sin x}{\cos x} + \left(\varepsilon + \left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(1.6666666666666667 \cdot \frac{t_3 \cdot {\varepsilon}^{4}}{t_5} + \left(\frac{{\varepsilon}^{4} \cdot {\sin x}^{5}}{{\cos x}^{5}} + \left(1.3333333333333333 \cdot \frac{{\varepsilon}^{3} \cdot t_6}{t_4} + \left(\frac{\varepsilon \cdot t_6}{t_4} + \left(0.6666666666666666 \cdot \frac{\sin x \cdot {\varepsilon}^{4}}{\cos x} + {\varepsilon}^{3} \cdot 0.3333333333333333\right)\right)\right)\right)\right)\right)\right)\right)
\end{array}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_7 := \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\\
\mathsf{fma}\left(\frac{t_0}{1 - {t_7}^{3}}, \mathsf{fma}\left(t_7, 1 + t_7, 1\right), t_1\right)
\end{array}\\


\end{array}
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (+ (tan x) (tan eps))) (t_1 (- (tan x))))
   (if (<= eps -0.00027961796206420387)
     (let* ((t_2 (* (tan x) (tan eps))))
       (fma (/ t_0 (- 1.0 (* t_2 t_2))) (+ 1.0 t_2) t_1))
     (if (<= eps 0.00023084122125612065)
       (let* ((t_3 (pow (sin x) 3.0))
              (t_4 (pow (cos x) 2.0))
              (t_5 (pow (cos x) 3.0))
              (t_6 (pow (sin x) 2.0)))
         (+
          (/ (* (pow eps 2.0) t_3) t_5)
          (+
           (/ (* (pow eps 2.0) (sin x)) (cos x))
           (+
            eps
            (+
             (/ (* (pow eps 3.0) (pow (sin x) 4.0)) (pow (cos x) 4.0))
             (+
              (* 1.6666666666666667 (/ (* t_3 (pow eps 4.0)) t_5))
              (+
               (/ (* (pow eps 4.0) (pow (sin x) 5.0)) (pow (cos x) 5.0))
               (+
                (* 1.3333333333333333 (/ (* (pow eps 3.0) t_6) t_4))
                (+
                 (/ (* eps t_6) t_4)
                 (+
                  (* 0.6666666666666666 (/ (* (sin x) (pow eps 4.0)) (cos x)))
                  (* (pow eps 3.0) 0.3333333333333333)))))))))))
       (let* ((t_7 (/ (* (tan x) (sin eps)) (cos eps))))
         (fma (/ t_0 (- 1.0 (pow t_7 3.0))) (fma t_7 (+ 1.0 t_7) 1.0) t_1))))))
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 t_1 = -tan(x);
	double tmp;
	if (eps <= -0.00027961796206420387) {
		double t_2_1 = tan(x) * tan(eps);
		tmp = fma((t_0 / (1.0 - (t_2_1 * t_2_1))), (1.0 + t_2_1), t_1);
	} else if (eps <= 0.00023084122125612065) {
		double t_3 = pow(sin(x), 3.0);
		double t_4 = pow(cos(x), 2.0);
		double t_5 = pow(cos(x), 3.0);
		double t_6 = pow(sin(x), 2.0);
		tmp = ((pow(eps, 2.0) * t_3) / t_5) + (((pow(eps, 2.0) * sin(x)) / cos(x)) + (eps + (((pow(eps, 3.0) * pow(sin(x), 4.0)) / pow(cos(x), 4.0)) + ((1.6666666666666667 * ((t_3 * pow(eps, 4.0)) / t_5)) + (((pow(eps, 4.0) * pow(sin(x), 5.0)) / pow(cos(x), 5.0)) + ((1.3333333333333333 * ((pow(eps, 3.0) * t_6) / t_4)) + (((eps * t_6) / t_4) + ((0.6666666666666666 * ((sin(x) * pow(eps, 4.0)) / cos(x))) + (pow(eps, 3.0) * 0.3333333333333333)))))))));
	} else {
		double t_7 = (tan(x) * sin(eps)) / cos(eps);
		tmp = fma((t_0 / (1.0 - pow(t_7, 3.0))), fma(t_7, (1.0 + t_7), 1.0), t_1);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus eps

Target

Original36.5
Target14.6
Herbie0.3
\[\frac{\sin \varepsilon}{\cos x \cdot \cos \left(x + \varepsilon\right)} \]

Derivation

  1. Split input into 3 regimes
  2. if eps < -2.79617962064203872e-4

    1. Initial program 28.7

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

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

      \[\leadsto \frac{\tan x + \tan \varepsilon}{\color{blue}{\frac{1 \cdot 1 - \left(\tan x \cdot \tan \varepsilon\right) \cdot \left(\tan x \cdot \tan \varepsilon\right)}{1 + \tan x \cdot \tan \varepsilon}}} - \tan x \]
    4. Applied associate-/r/_binary640.4

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

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

    if -2.79617962064203872e-4 < eps < 2.3084122125612065e-4

    1. Initial program 44.2

      \[\tan \left(x + \varepsilon\right) - \tan x \]
    2. 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 + \left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(1.6666666666666667 \cdot \frac{{\varepsilon}^{4} \cdot {\sin x}^{3}}{{\cos x}^{3}} + \left(\frac{{\varepsilon}^{4} \cdot {\sin x}^{5}}{{\cos x}^{5}} + \left(1.3333333333333333 \cdot \frac{{\varepsilon}^{3} \cdot {\sin x}^{2}}{{\cos x}^{2}} + \left(\frac{\varepsilon \cdot {\sin x}^{2}}{{\cos x}^{2}} + \left(0.6666666666666666 \cdot \frac{{\varepsilon}^{4} \cdot \sin x}{\cos x} + 0.3333333333333333 \cdot {\varepsilon}^{3}\right)\right)\right)\right)\right)\right)\right)\right)} \]

    if 2.3084122125612065e-4 < eps

    1. Initial program 29.1

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

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

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

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}}} - \tan x \]
    5. Applied add-cube-cbrt_binary640.7

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}} - \color{blue}{\left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right) \cdot \sqrt[3]{\tan x}} \]
    6. Applied flip3--_binary640.7

      \[\leadsto \frac{\tan x + \tan \varepsilon}{\color{blue}{\frac{{1}^{3} - {\left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right)}^{3}}{1 \cdot 1 + \left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon} \cdot \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon} + 1 \cdot \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right)}}} - \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right) \cdot \sqrt[3]{\tan x} \]
    7. Applied associate-/r/_binary640.7

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{{1}^{3} - {\left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right)}^{3}} \cdot \left(1 \cdot 1 + \left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon} \cdot \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon} + 1 \cdot \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right)\right)} - \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right) \cdot \sqrt[3]{\tan x} \]
    8. Applied prod-diff_binary640.7

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{{1}^{3} - {\left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right)}^{3}}, 1 \cdot 1 + \left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon} \cdot \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon} + 1 \cdot \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right), -\sqrt[3]{\tan x} \cdot \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right)\right) + \mathsf{fma}\left(-\sqrt[3]{\tan x}, \sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}, \sqrt[3]{\tan x} \cdot \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right)\right)} \]
    9. Simplified0.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{1 - {\left(\frac{\sin \varepsilon \cdot \tan x}{\cos \varepsilon}\right)}^{3}}, \mathsf{fma}\left(\frac{\sin \varepsilon \cdot \tan x}{\cos \varepsilon}, 1 + \frac{\sin \varepsilon \cdot \tan x}{\cos \varepsilon}, 1\right), -\tan x\right)} + \mathsf{fma}\left(-\sqrt[3]{\tan x}, \sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}, \sqrt[3]{\tan x} \cdot \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right)\right) \]
    10. Simplified0.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq -0.00027961796206420387:\\ \;\;\;\;\mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{1 - \left(\tan x \cdot \tan \varepsilon\right) \cdot \left(\tan x \cdot \tan \varepsilon\right)}, 1 + \tan x \cdot \tan \varepsilon, -\tan x\right)\\ \mathbf{elif}\;\varepsilon \leq 0.00023084122125612065:\\ \;\;\;\;\frac{{\varepsilon}^{2} \cdot {\sin x}^{3}}{{\cos x}^{3}} + \left(\frac{{\varepsilon}^{2} \cdot \sin x}{\cos x} + \left(\varepsilon + \left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(1.6666666666666667 \cdot \frac{{\sin x}^{3} \cdot {\varepsilon}^{4}}{{\cos x}^{3}} + \left(\frac{{\varepsilon}^{4} \cdot {\sin x}^{5}}{{\cos x}^{5}} + \left(1.3333333333333333 \cdot \frac{{\varepsilon}^{3} \cdot {\sin x}^{2}}{{\cos x}^{2}} + \left(\frac{\varepsilon \cdot {\sin x}^{2}}{{\cos x}^{2}} + \left(0.6666666666666666 \cdot \frac{\sin x \cdot {\varepsilon}^{4}}{\cos x} + {\varepsilon}^{3} \cdot 0.3333333333333333\right)\right)\right)\right)\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{1 - {\left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}\right)}^{3}}, \mathsf{fma}\left(\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}, 1 + \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}, 1\right), -\tan x\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022081 
(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)))