Average Error: 37.3 → 0.3
Time: 31.4s
Precision: binary64
\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -3.231461580256822 \cdot 10^{-5}:\\ \;\;\;\;\frac{t_0}{1 - \log \left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right)} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 6.61700448350665 \cdot 10^{-5}:\\ \;\;\;\;\begin{array}{l} t_1 := {\cos x}^{2}\\ t_2 := \frac{{\sin x}^{2}}{t_1}\\ \left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(\varepsilon + \left(\varepsilon \cdot t_2 + \mathsf{fma}\left(0.3333333333333333, {\varepsilon}^{3}, 1.3333333333333333 \cdot \left({\varepsilon}^{3} \cdot t_2\right)\right)\right)\right)\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{t_1}\right) \end{array}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_3 := \tan x \cdot \tan \varepsilon\\ \mathsf{fma}\left(\frac{t_0}{1 - t_3 \cdot t_3}, \mathsf{fma}\left(\tan x, \tan \varepsilon, 1\right), -\tan x\right) \end{array}\\ \end{array} \]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := \tan x + \tan \varepsilon\\
\mathbf{if}\;\varepsilon \leq -3.231461580256822 \cdot 10^{-5}:\\
\;\;\;\;\frac{t_0}{1 - \log \left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right)} - \tan x\\

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

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_3 := \tan x \cdot \tan \varepsilon\\
\mathsf{fma}\left(\frac{t_0}{1 - t_3 \cdot t_3}, \mathsf{fma}\left(\tan x, \tan \varepsilon, 1\right), -\tan x\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))))
   (if (<= eps -3.231461580256822e-5)
     (- (/ t_0 (- 1.0 (log (pow (exp (tan x)) (tan eps))))) (tan x))
     (if (<= eps 6.61700448350665e-5)
       (let* ((t_1 (pow (cos x) 2.0)) (t_2 (/ (pow (sin x) 2.0) t_1)))
         (+
          (+
           (/ (* (pow eps 3.0) (pow (sin x) 4.0)) (pow (cos x) 4.0))
           (+
            eps
            (+
             (* eps t_2)
             (fma
              0.3333333333333333
              (pow eps 3.0)
              (* 1.3333333333333333 (* (pow eps 3.0) t_2))))))
          (* (/ (* eps eps) (cos x)) (+ (sin x) (/ (pow (sin x) 3.0) t_1)))))
       (let* ((t_3 (* (tan x) (tan eps))))
         (fma
          (/ t_0 (- 1.0 (* t_3 t_3)))
          (fma (tan x) (tan eps) 1.0)
          (- (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 <= -3.231461580256822e-5) {
		tmp = (t_0 / (1.0 - log(pow(exp(tan(x)), tan(eps))))) - tan(x);
	} else if (eps <= 6.61700448350665e-5) {
		double t_1 = pow(cos(x), 2.0);
		double t_2 = pow(sin(x), 2.0) / t_1;
		tmp = (((pow(eps, 3.0) * pow(sin(x), 4.0)) / pow(cos(x), 4.0)) + (eps + ((eps * t_2) + fma(0.3333333333333333, pow(eps, 3.0), (1.3333333333333333 * (pow(eps, 3.0) * t_2)))))) + (((eps * eps) / cos(x)) * (sin(x) + (pow(sin(x), 3.0) / t_1)));
	} else {
		double t_3 = tan(x) * tan(eps);
		tmp = fma((t_0 / (1.0 - (t_3 * t_3))), fma(tan(x), tan(eps), 1.0), -tan(x));
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus eps

Target

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

Derivation

  1. Split input into 3 regimes
  2. if eps < -3.2314615802568222e-5

    1. Initial program 30.4

      \[\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 add-log-exp_binary640.4

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\log \left(e^{\tan x \cdot \tan \varepsilon}\right)}} - \tan x \]
    4. Simplified0.5

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \log \color{blue}{\left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right)}} - \tan x \]
    5. Applied *-un-lft-identity_binary640.5

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \log \left({\left(e^{\tan x}\right)}^{\color{blue}{\left(1 \cdot \tan \varepsilon\right)}}\right)} - \tan x \]
    6. Applied pow-unpow_binary640.5

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \log \color{blue}{\left({\left({\left(e^{\tan x}\right)}^{1}\right)}^{\tan \varepsilon}\right)}} - \tan x \]

    if -3.2314615802568222e-5 < eps < 6.61700448350664982e-5

    1. Initial program 44.9

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

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

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\log \left(e^{\tan x \cdot \tan \varepsilon}\right)}} - \tan x \]
    4. Simplified44.3

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \log \color{blue}{\left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right)}} - \tan x \]
    5. Taylor expanded in eps around 0 0.2

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

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

    if 6.61700448350664982e-5 < eps

    1. Initial program 29.7

      \[\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 add-cube-cbrt_binary640.7

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

      \[\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}}} - \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right) \cdot \sqrt[3]{\tan x} \]
    5. Applied associate-/r/_binary640.7

      \[\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)} - \left(\sqrt[3]{\tan x} \cdot \sqrt[3]{\tan x}\right) \cdot \sqrt[3]{\tan x} \]
    6. Applied prod-diff_binary640.7

      \[\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, -\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)} \]
    7. Simplified0.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{1 - \left(\tan x \cdot \tan \varepsilon\right) \cdot \left(\tan x \cdot \tan \varepsilon\right)}, \mathsf{fma}\left(\tan x, \tan \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) \]
    8. Simplified0.4

      \[\leadsto \mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{1 - \left(\tan x \cdot \tan \varepsilon\right) \cdot \left(\tan x \cdot \tan \varepsilon\right)}, \mathsf{fma}\left(\tan x, \tan \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 -3.231461580256822 \cdot 10^{-5}:\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \log \left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right)} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 6.61700448350665 \cdot 10^{-5}:\\ \;\;\;\;\left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(\varepsilon + \left(\varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}} + \mathsf{fma}\left(0.3333333333333333, {\varepsilon}^{3}, 1.3333333333333333 \cdot \left({\varepsilon}^{3} \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\right)\right)\right)\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{{\cos x}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\tan x + \tan \varepsilon}{1 - \left(\tan x \cdot \tan \varepsilon\right) \cdot \left(\tan x \cdot \tan \varepsilon\right)}, \mathsf{fma}\left(\tan x, \tan \varepsilon, 1\right), -\tan x\right)\\ \end{array} \]

Reproduce

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