Average Error: 37.5 → 0.6
Time: 28.9s
Precision: binary64
Cost: 183624
\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := {\sin x}^{2}\\ t_1 := {\cos x}^{2}\\ t_2 := \frac{t_0}{t_1}\\ t_3 := -1 \cdot t_2\\ t_4 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -0.00013:\\ \;\;\;\;\frac{-1}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} \cdot t_4 - \tan x\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\varepsilon \cdot \left(1 - t_3\right) + \left(-1 \cdot \left({\varepsilon}^{2} \cdot \left(-1 \cdot \frac{t_0 \cdot \sin x}{\cos x \cdot t_1} + -1 \cdot \frac{\sin x}{\cos x}\right)\right) + \left(0.3333333333333333 - \left(t_3 + \left(-0.3333333333333333 \cdot t_2 + -1 \cdot \frac{{t_0}^{2}}{{t_1}^{2}}\right)\right)\right) \cdot {\varepsilon}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_4, \frac{-1}{\tan x \cdot \tan \varepsilon - 1}, -\tan x\right)\\ \end{array} \]
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (pow (sin x) 2.0))
        (t_1 (pow (cos x) 2.0))
        (t_2 (/ t_0 t_1))
        (t_3 (* -1.0 t_2))
        (t_4 (+ (tan x) (tan eps))))
   (if (<= eps -0.00013)
     (- (* (/ -1.0 (fma (tan x) (tan eps) -1.0)) t_4) (tan x))
     (if (<= eps 5.2e-21)
       (+
        (* eps (- 1.0 t_3))
        (+
         (*
          -1.0
          (*
           (pow eps 2.0)
           (+
            (* -1.0 (/ (* t_0 (sin x)) (* (cos x) t_1)))
            (* -1.0 (/ (sin x) (cos x))))))
         (*
          (-
           0.3333333333333333
           (+
            t_3
            (+
             (* -0.3333333333333333 t_2)
             (* -1.0 (/ (pow t_0 2.0) (pow t_1 2.0))))))
          (pow eps 3.0))))
       (fma t_4 (/ -1.0 (- (* (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 = pow(sin(x), 2.0);
	double t_1 = pow(cos(x), 2.0);
	double t_2 = t_0 / t_1;
	double t_3 = -1.0 * t_2;
	double t_4 = tan(x) + tan(eps);
	double tmp;
	if (eps <= -0.00013) {
		tmp = ((-1.0 / fma(tan(x), tan(eps), -1.0)) * t_4) - tan(x);
	} else if (eps <= 5.2e-21) {
		tmp = (eps * (1.0 - t_3)) + ((-1.0 * (pow(eps, 2.0) * ((-1.0 * ((t_0 * sin(x)) / (cos(x) * t_1))) + (-1.0 * (sin(x) / cos(x)))))) + ((0.3333333333333333 - (t_3 + ((-0.3333333333333333 * t_2) + (-1.0 * (pow(t_0, 2.0) / pow(t_1, 2.0)))))) * pow(eps, 3.0)));
	} else {
		tmp = fma(t_4, (-1.0 / ((tan(x) * tan(eps)) - 1.0)), -tan(x));
	}
	return tmp;
}
function code(x, eps)
	return Float64(tan(Float64(x + eps)) - tan(x))
end
function code(x, eps)
	t_0 = sin(x) ^ 2.0
	t_1 = cos(x) ^ 2.0
	t_2 = Float64(t_0 / t_1)
	t_3 = Float64(-1.0 * t_2)
	t_4 = Float64(tan(x) + tan(eps))
	tmp = 0.0
	if (eps <= -0.00013)
		tmp = Float64(Float64(Float64(-1.0 / fma(tan(x), tan(eps), -1.0)) * t_4) - tan(x));
	elseif (eps <= 5.2e-21)
		tmp = Float64(Float64(eps * Float64(1.0 - t_3)) + Float64(Float64(-1.0 * Float64((eps ^ 2.0) * Float64(Float64(-1.0 * Float64(Float64(t_0 * sin(x)) / Float64(cos(x) * t_1))) + Float64(-1.0 * Float64(sin(x) / cos(x)))))) + Float64(Float64(0.3333333333333333 - Float64(t_3 + Float64(Float64(-0.3333333333333333 * t_2) + Float64(-1.0 * Float64((t_0 ^ 2.0) / (t_1 ^ 2.0)))))) * (eps ^ 3.0))));
	else
		tmp = fma(t_4, Float64(-1.0 / Float64(Float64(tan(x) * tan(eps)) - 1.0)), Float64(-tan(x)));
	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[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$1 = N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$0 / t$95$1), $MachinePrecision]}, Block[{t$95$3 = N[(-1.0 * t$95$2), $MachinePrecision]}, Block[{t$95$4 = N[(N[Tan[x], $MachinePrecision] + N[Tan[eps], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[eps, -0.00013], N[(N[(N[(-1.0 / N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision] * t$95$4), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 5.2e-21], N[(N[(eps * N[(1.0 - t$95$3), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0 * N[(N[Power[eps, 2.0], $MachinePrecision] * N[(N[(-1.0 * N[(N[(t$95$0 * N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[(N[Cos[x], $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-1.0 * N[(N[Sin[x], $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(0.3333333333333333 - N[(t$95$3 + N[(N[(-0.3333333333333333 * t$95$2), $MachinePrecision] + N[(-1.0 * N[(N[Power[t$95$0, 2.0], $MachinePrecision] / N[Power[t$95$1, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Power[eps, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$4 * N[(-1.0 / N[(N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision] + (-N[Tan[x], $MachinePrecision])), $MachinePrecision]]]]]]]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := {\sin x}^{2}\\
t_1 := {\cos x}^{2}\\
t_2 := \frac{t_0}{t_1}\\
t_3 := -1 \cdot t_2\\
t_4 := \tan x + \tan \varepsilon\\
\mathbf{if}\;\varepsilon \leq -0.00013:\\
\;\;\;\;\frac{-1}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} \cdot t_4 - \tan x\\

\mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\
\;\;\;\;\varepsilon \cdot \left(1 - t_3\right) + \left(-1 \cdot \left({\varepsilon}^{2} \cdot \left(-1 \cdot \frac{t_0 \cdot \sin x}{\cos x \cdot t_1} + -1 \cdot \frac{\sin x}{\cos x}\right)\right) + \left(0.3333333333333333 - \left(t_3 + \left(-0.3333333333333333 \cdot t_2 + -1 \cdot \frac{{t_0}^{2}}{{t_1}^{2}}\right)\right)\right) \cdot {\varepsilon}^{3}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t_4, \frac{-1}{\tan x \cdot \tan \varepsilon - 1}, -\tan x\right)\\


\end{array}

Error

Target

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

Derivation

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

    1. Initial program 29.9

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

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

    if -1.29999999999999989e-4 < eps < 5.20000000000000035e-21

    1. Initial program 45.7

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

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

      \[\leadsto \frac{\tan x + \tan \varepsilon}{-\color{blue}{\left(\tan x \cdot \tan \varepsilon - 1\right)}} - \tan x \]
    4. Taylor expanded in eps around 0 0.3

      \[\leadsto \color{blue}{\varepsilon \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right) + \left(-1 \cdot \left({\varepsilon}^{2} \cdot \left(-1 \cdot \frac{{\sin x}^{2} \cdot \sin x}{\cos x \cdot {\cos x}^{2}} + -1 \cdot \frac{\sin x}{\cos x}\right)\right) + \left(0.3333333333333333 - \left(-1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}} + \left(-0.3333333333333333 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}} + -1 \cdot \frac{{\left({\sin x}^{2}\right)}^{2}}{{\left({\cos x}^{2}\right)}^{2}}\right)\right)\right) \cdot {\varepsilon}^{3}\right)} \]

    if 5.20000000000000035e-21 < eps

    1. Initial program 29.7

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

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

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

Alternatives

Alternative 1
Error0.6
Cost157128
\[\begin{array}{l} t_0 := {\sin x}^{2}\\ t_1 := {\cos x}^{2}\\ t_2 := \tan x + \tan \varepsilon\\ t_3 := \frac{t_0}{t_1}\\ t_4 := 1 + t_3\\ \mathbf{if}\;\varepsilon \leq -0.00013:\\ \;\;\;\;\frac{-1}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} \cdot t_2 - \tan x\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\left(0.3333333333333333 + \left(\frac{t_0 \cdot t_4}{t_1} + 0.3333333333333333 \cdot t_3\right)\right) \cdot {\varepsilon}^{3} + \left(\varepsilon \cdot t_4 + \frac{{\varepsilon}^{2} \cdot \left(t_4 \cdot \sin x\right)}{\cos x}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_2, \frac{-1}{\tan x \cdot \tan \varepsilon - 1}, -\tan x\right)\\ \end{array} \]
Alternative 2
Error0.6
Cost72264
\[\begin{array}{l} t_0 := 1 - \left(-\frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\\ t_1 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -6.5 \cdot 10^{-7}:\\ \;\;\;\;\frac{-1}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} \cdot t_1 - \tan x\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon, t_0, \frac{\left(\left(\varepsilon \cdot \varepsilon\right) \cdot \sin x\right) \cdot t_0}{\cos x}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{-1}{\tan x \cdot \tan \varepsilon - 1}, -\tan x\right)\\ \end{array} \]
Alternative 3
Error0.6
Cost65608
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)\\ t_1 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -6.8 \cdot 10^{-7}:\\ \;\;\;\;\frac{-1}{t_0} \cdot t_1 - \tan x\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\frac{\frac{\varepsilon \cdot \left(\cos x + \frac{{\sin x}^{2}}{\cos x}\right) + \left(-1 \cdot \sin x + \sin x\right)}{-t_0}}{\cos x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{-1}{\tan x \cdot \tan \varepsilon - 1}, -\tan x\right)\\ \end{array} \]
Alternative 4
Error0.6
Cost39432
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -7.2 \cdot 10^{-9}:\\ \;\;\;\;\frac{-1}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} \cdot t_0 - \tan x\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\varepsilon \cdot {\tan x}^{2} + \varepsilon\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_0, \frac{-1}{\tan x \cdot \tan \varepsilon - 1}, -\tan x\right)\\ \end{array} \]
Alternative 5
Error0.6
Cost39368
\[\begin{array}{l} t_0 := \frac{-1}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} \cdot \left(\tan x + \tan \varepsilon\right) - \tan x\\ \mathbf{if}\;\varepsilon \leq -7.2 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\varepsilon \cdot {\tan x}^{2} + \varepsilon\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error0.6
Cost32968
\[\begin{array}{l} t_0 := \frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \mathbf{if}\;\varepsilon \leq -5.5 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\varepsilon \cdot {\tan x}^{2} + \varepsilon\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error14.8
Cost13448
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -1.2 \cdot 10^{-5}:\\ \;\;\;\;\tan \varepsilon\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\varepsilon \cdot \left(1 + {\tan x}^{2}\right)\\ \mathbf{else}:\\ \;\;\;\;\tan \varepsilon - \tan x\\ \end{array} \]
Alternative 8
Error14.7
Cost13448
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -1.42 \cdot 10^{-5}:\\ \;\;\;\;\tan \varepsilon\\ \mathbf{elif}\;\varepsilon \leq 5.2 \cdot 10^{-21}:\\ \;\;\;\;\varepsilon \cdot {\tan x}^{2} + \varepsilon\\ \mathbf{else}:\\ \;\;\;\;\tan \varepsilon - \tan x\\ \end{array} \]
Alternative 9
Error27.6
Cost6464
\[\tan \varepsilon \]
Alternative 10
Error61.3
Cost64
\[0 \]
Alternative 11
Error44.6
Cost64
\[\varepsilon \]

Error

Reproduce

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