Average Error: 36.9 → 14.9
Time: 8.1s
Precision: binary64
\[\tan \left(x + \varepsilon\right) - \tan x\]
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -6.5060705864494465 \cdot 10^{-31}:\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 2.046397525732066 \cdot 10^{-33}:\\ \;\;\;\;\varepsilon + \left(\varepsilon + x\right) \cdot \left(\varepsilon \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\tan x \cdot \tan x - \tan \varepsilon \cdot \tan \varepsilon}{\tan x - \tan \varepsilon}}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \end{array}\]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -6.5060705864494465 \cdot 10^{-31}:\\
\;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}} - \tan x\\

\mathbf{elif}\;\varepsilon \leq 2.046397525732066 \cdot 10^{-33}:\\
\;\;\;\;\varepsilon + \left(\varepsilon + x\right) \cdot \left(\varepsilon \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{\tan x \cdot \tan x - \tan \varepsilon \cdot \tan \varepsilon}{\tan x - \tan \varepsilon}}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\

\end{array}
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
 :precision binary64
 (if (<= eps -6.5060705864494465e-31)
   (-
    (/ (+ (tan x) (tan eps)) (- 1.0 (/ (* (tan x) (sin eps)) (cos eps))))
    (tan x))
   (if (<= eps 2.046397525732066e-33)
     (+ eps (* (+ eps x) (* eps x)))
     (-
      (/
       (/
        (- (* (tan x) (tan x)) (* (tan eps) (tan eps)))
        (- (tan x) (tan eps)))
       (- 1.0 (* (tan x) (tan eps))))
      (tan x)))))
double code(double x, double eps) {
	return tan(x + eps) - tan(x);
}
double code(double x, double eps) {
	double tmp;
	if (eps <= -6.5060705864494465e-31) {
		tmp = ((tan(x) + tan(eps)) / (1.0 - ((tan(x) * sin(eps)) / cos(eps)))) - tan(x);
	} else if (eps <= 2.046397525732066e-33) {
		tmp = eps + ((eps + x) * (eps * x));
	} else {
		tmp = ((((tan(x) * tan(x)) - (tan(eps) * tan(eps))) / (tan(x) - tan(eps))) / (1.0 - (tan(x) * tan(eps)))) - tan(x);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus eps

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original36.9
Target15.4
Herbie14.9
\[\frac{\sin \varepsilon}{\cos x \cdot \cos \left(x + \varepsilon\right)}\]

Derivation

  1. Split input into 3 regimes
  2. if eps < -6.50607058644944646e-31

    1. Initial program 29.5

      \[\tan \left(x + \varepsilon\right) - \tan x\]
    2. Using strategy rm
    3. Applied tan-sum_binary64_15772.0

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x\]
    4. Using strategy rm
    5. Applied tan-quot_binary64_16012.0

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

      \[\leadsto \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}}} - \tan x\]

    if -6.50607058644944646e-31 < eps < 2.046397525732066e-33

    1. Initial program 45.7

      \[\tan \left(x + \varepsilon\right) - \tan x\]
    2. Taylor expanded around 0 31.0

      \[\leadsto \color{blue}{x \cdot {\varepsilon}^{2} + \left(\varepsilon + {x}^{2} \cdot \varepsilon\right)}\]
    3. Simplified30.8

      \[\leadsto \color{blue}{\varepsilon + \left(x + \varepsilon\right) \cdot \left(x \cdot \varepsilon\right)}\]

    if 2.046397525732066e-33 < eps

    1. Initial program 30.2

      \[\tan \left(x + \varepsilon\right) - \tan x\]
    2. Using strategy rm
    3. Applied tan-sum_binary64_15772.5

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x\]
    4. Using strategy rm
    5. Applied flip-+_binary64_14162.6

      \[\leadsto \frac{\color{blue}{\frac{\tan x \cdot \tan x - \tan \varepsilon \cdot \tan \varepsilon}{\tan x - \tan \varepsilon}}}{1 - \tan x \cdot \tan \varepsilon} - \tan x\]
  3. Recombined 3 regimes into one program.
  4. Final simplification14.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq -6.5060705864494465 \cdot 10^{-31}:\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 2.046397525732066 \cdot 10^{-33}:\\ \;\;\;\;\varepsilon + \left(\varepsilon + x\right) \cdot \left(\varepsilon \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\tan x \cdot \tan x - \tan \varepsilon \cdot \tan \varepsilon}{\tan x - \tan \varepsilon}}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \end{array}\]

Reproduce

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