Average Error: 37.1 → 0.4
Time: 8.7s
Precision: binary64
\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := -\tan x\\ t_1 := \tan x + \tan \varepsilon\\ t_2 := {\sin x}^{2}\\ t_3 := {\cos x}^{2}\\ \mathbf{if}\;\varepsilon \leq -4.2 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{1 - \frac{\tan \varepsilon \cdot \sin x}{\cos x}}, t_0\right)\\ \mathbf{elif}\;\varepsilon \leq 5.8 \cdot 10^{-5}:\\ \;\;\;\;\left(\mathsf{fma}\left(\varepsilon, \frac{t_2}{t_3}, \mathsf{fma}\left(1.3333333333333333, t_2 \cdot \frac{{\varepsilon}^{3}}{t_3}, {\varepsilon}^{3} \cdot 0.3333333333333333\right)\right) + \mathsf{fma}\left(\frac{{\varepsilon}^{3}}{{\cos x}^{4}}, {\sin x}^{4}, \varepsilon\right)\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{t_3}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{1 - {\left(\tan x \cdot \tan \varepsilon\right)}^{3}} \cdot \mathsf{fma}\left(\log \left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right), \mathsf{fma}\left(\tan x, \tan \varepsilon, 1\right), 1\right), t_0\right)\\ \end{array} \]
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (- (tan x)))
        (t_1 (+ (tan x) (tan eps)))
        (t_2 (pow (sin x) 2.0))
        (t_3 (pow (cos x) 2.0)))
   (if (<= eps -4.2e-5)
     (fma t_1 (/ 1.0 (- 1.0 (/ (* (tan eps) (sin x)) (cos x)))) t_0)
     (if (<= eps 5.8e-5)
       (+
        (+
         (fma
          eps
          (/ t_2 t_3)
          (fma
           1.3333333333333333
           (* t_2 (/ (pow eps 3.0) t_3))
           (* (pow eps 3.0) 0.3333333333333333)))
         (fma (/ (pow eps 3.0) (pow (cos x) 4.0)) (pow (sin x) 4.0) eps))
        (* (/ (* eps eps) (cos x)) (+ (sin x) (/ (pow (sin x) 3.0) t_3))))
       (fma
        t_1
        (*
         (/ 1.0 (- 1.0 (pow (* (tan x) (tan eps)) 3.0)))
         (fma
          (log (pow (exp (tan x)) (tan eps)))
          (fma (tan x) (tan eps) 1.0)
          1.0))
        t_0)))))
double code(double x, double eps) {
	return tan((x + eps)) - tan(x);
}
double code(double x, double eps) {
	double t_0 = -tan(x);
	double t_1 = tan(x) + tan(eps);
	double t_2 = pow(sin(x), 2.0);
	double t_3 = pow(cos(x), 2.0);
	double tmp;
	if (eps <= -4.2e-5) {
		tmp = fma(t_1, (1.0 / (1.0 - ((tan(eps) * sin(x)) / cos(x)))), t_0);
	} else if (eps <= 5.8e-5) {
		tmp = (fma(eps, (t_2 / t_3), fma(1.3333333333333333, (t_2 * (pow(eps, 3.0) / t_3)), (pow(eps, 3.0) * 0.3333333333333333))) + fma((pow(eps, 3.0) / pow(cos(x), 4.0)), pow(sin(x), 4.0), eps)) + (((eps * eps) / cos(x)) * (sin(x) + (pow(sin(x), 3.0) / t_3)));
	} else {
		tmp = fma(t_1, ((1.0 / (1.0 - pow((tan(x) * tan(eps)), 3.0))) * fma(log(pow(exp(tan(x)), tan(eps))), fma(tan(x), tan(eps), 1.0), 1.0)), t_0);
	}
	return tmp;
}
function code(x, eps)
	return Float64(tan(Float64(x + eps)) - tan(x))
end
function code(x, eps)
	t_0 = Float64(-tan(x))
	t_1 = Float64(tan(x) + tan(eps))
	t_2 = sin(x) ^ 2.0
	t_3 = cos(x) ^ 2.0
	tmp = 0.0
	if (eps <= -4.2e-5)
		tmp = fma(t_1, Float64(1.0 / Float64(1.0 - Float64(Float64(tan(eps) * sin(x)) / cos(x)))), t_0);
	elseif (eps <= 5.8e-5)
		tmp = Float64(Float64(fma(eps, Float64(t_2 / t_3), fma(1.3333333333333333, Float64(t_2 * Float64((eps ^ 3.0) / t_3)), Float64((eps ^ 3.0) * 0.3333333333333333))) + fma(Float64((eps ^ 3.0) / (cos(x) ^ 4.0)), (sin(x) ^ 4.0), eps)) + Float64(Float64(Float64(eps * eps) / cos(x)) * Float64(sin(x) + Float64((sin(x) ^ 3.0) / t_3))));
	else
		tmp = fma(t_1, Float64(Float64(1.0 / Float64(1.0 - (Float64(tan(x) * tan(eps)) ^ 3.0))) * fma(log((exp(tan(x)) ^ tan(eps))), fma(tan(x), tan(eps), 1.0), 1.0)), t_0);
	end
	return tmp
end
code[x_, eps_] := N[(N[Tan[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]
code[x_, eps_] := Block[{t$95$0 = (-N[Tan[x], $MachinePrecision])}, Block[{t$95$1 = N[(N[Tan[x], $MachinePrecision] + N[Tan[eps], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$3 = N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[eps, -4.2e-5], N[(t$95$1 * N[(1.0 / N[(1.0 - N[(N[(N[Tan[eps], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision], If[LessEqual[eps, 5.8e-5], N[(N[(N[(eps * N[(t$95$2 / t$95$3), $MachinePrecision] + N[(1.3333333333333333 * N[(t$95$2 * N[(N[Power[eps, 3.0], $MachinePrecision] / t$95$3), $MachinePrecision]), $MachinePrecision] + N[(N[Power[eps, 3.0], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Power[eps, 3.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 4.0], $MachinePrecision]), $MachinePrecision] * N[Power[N[Sin[x], $MachinePrecision], 4.0], $MachinePrecision] + eps), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(eps * eps), $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] + N[(N[Power[N[Sin[x], $MachinePrecision], 3.0], $MachinePrecision] / t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 * N[(N[(1.0 / N[(1.0 - N[Power[N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision]), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(N[Log[N[Power[N[Exp[N[Tan[x], $MachinePrecision]], $MachinePrecision], N[Tan[eps], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision] + 1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]]]]]]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := -\tan x\\
t_1 := \tan x + \tan \varepsilon\\
t_2 := {\sin x}^{2}\\
t_3 := {\cos x}^{2}\\
\mathbf{if}\;\varepsilon \leq -4.2 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{1 - \frac{\tan \varepsilon \cdot \sin x}{\cos x}}, t_0\right)\\

\mathbf{elif}\;\varepsilon \leq 5.8 \cdot 10^{-5}:\\
\;\;\;\;\left(\mathsf{fma}\left(\varepsilon, \frac{t_2}{t_3}, \mathsf{fma}\left(1.3333333333333333, t_2 \cdot \frac{{\varepsilon}^{3}}{t_3}, {\varepsilon}^{3} \cdot 0.3333333333333333\right)\right) + \mathsf{fma}\left(\frac{{\varepsilon}^{3}}{{\cos x}^{4}}, {\sin x}^{4}, \varepsilon\right)\right) + \frac{\varepsilon \cdot \varepsilon}{\cos x} \cdot \left(\sin x + \frac{{\sin x}^{3}}{t_3}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{1 - {\left(\tan x \cdot \tan \varepsilon\right)}^{3}} \cdot \mathsf{fma}\left(\log \left({\left(e^{\tan x}\right)}^{\tan \varepsilon}\right), \mathsf{fma}\left(\tan x, \tan \varepsilon, 1\right), 1\right), t_0\right)\\


\end{array}

Error

Bits error versus x

Bits error versus eps

Target

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

Derivation

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

    1. Initial program 29.7

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, -\tan x\right)} \]
    3. Applied egg-rr0.4

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

    if -4.19999999999999977e-5 < eps < 5.8e-5

    1. Initial program 44.5

      \[\tan \left(x + \varepsilon\right) - \tan x \]
    2. Applied egg-rr43.8

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

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

    if 5.8e-5 < eps

    1. Initial program 29.9

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, -\tan x\right)} \]
    3. Applied egg-rr0.4

      \[\leadsto \mathsf{fma}\left(\tan x + \tan \varepsilon, \color{blue}{\frac{1}{1 - {\left(\tan x \cdot \tan \varepsilon\right)}^{3}} \cdot \mathsf{fma}\left(\tan x \cdot \tan \varepsilon, \mathsf{fma}\left(\tan x, \tan \varepsilon, 1\right), 1\right)}, -\tan x\right) \]
    4. Applied egg-rr0.6

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

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

Reproduce

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