Average Error: 37.0 → 0.6
Time: 21.8s
Precision: binary64
Cost: 39172
\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -0.0018077685951439057:\\ \;\;\;\;\frac{t_0}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \frac{1}{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
 (let* ((t_0 (+ (tan x) (tan eps))))
   (if (<= eps -0.0018077685951439057)
     (- (/ t_0 (fma (tan x) (- (tan eps)) 1.0)) (tan x))
     (if (<= eps 1.1799764769038227e-18)
       (+ eps (* eps (/ (pow (sin x) 2.0) (pow (cos x) 2.0))))
       (- (* t_0 (/ 1.0 (- 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 t_0 = tan(x) + tan(eps);
	double tmp;
	if (eps <= -0.0018077685951439057) {
		tmp = (t_0 / fma(tan(x), -tan(eps), 1.0)) - tan(x);
	} else if (eps <= 1.1799764769038227e-18) {
		tmp = eps + (eps * (pow(sin(x), 2.0) / pow(cos(x), 2.0)));
	} else {
		tmp = (t_0 * (1.0 / (1.0 - (tan(x) * tan(eps))))) - tan(x);
	}
	return tmp;
}
function code(x, eps)
	return Float64(tan(Float64(x + eps)) - tan(x))
end
function code(x, eps)
	t_0 = Float64(tan(x) + tan(eps))
	tmp = 0.0
	if (eps <= -0.0018077685951439057)
		tmp = Float64(Float64(t_0 / fma(tan(x), Float64(-tan(eps)), 1.0)) - tan(x));
	elseif (eps <= 1.1799764769038227e-18)
		tmp = Float64(eps + Float64(eps * Float64((sin(x) ^ 2.0) / (cos(x) ^ 2.0))));
	else
		tmp = Float64(Float64(t_0 * Float64(1.0 / Float64(1.0 - Float64(tan(x) * tan(eps))))) - 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[(N[Tan[x], $MachinePrecision] + N[Tan[eps], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[eps, -0.0018077685951439057], N[(N[(t$95$0 / N[(N[Tan[x], $MachinePrecision] * (-N[Tan[eps], $MachinePrecision]) + 1.0), $MachinePrecision]), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 1.1799764769038227e-18], N[(eps + N[(eps * N[(N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(t$95$0 * N[(1.0 / N[(1.0 - N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]]]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := \tan x + \tan \varepsilon\\
\mathbf{if}\;\varepsilon \leq -0.0018077685951439057:\\
\;\;\;\;\frac{t_0}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)} - \tan x\\

\mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\
\;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\

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


\end{array}

Error

Target

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

Derivation

  1. Split input into 3 regimes
  2. if eps < -0.00180776859514390569

    1. Initial program 29.9

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\tan \left(x + \varepsilon\right) - \tan x\right)\right)} \]
    3. Applied egg-rr2.2

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

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

    if -0.00180776859514390569 < eps < 1.1799764769038227e-18

    1. Initial program 45.0

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\tan \left(x + \varepsilon\right) - \tan x\right)\right)} \]
    3. Taylor expanded in eps around 0 0.6

      \[\leadsto \color{blue}{\varepsilon \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)} \]
    4. Simplified0.5

      \[\leadsto \color{blue}{\varepsilon + \frac{{\sin x}^{2}}{{\cos x}^{2}} \cdot \varepsilon} \]
      Proof
      (+.f64 eps (*.f64 (/.f64 (pow.f64 (sin.f64 x) 2) (pow.f64 (cos.f64 x) 2)) eps)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= *-lft-identity_binary64 (*.f64 1 eps)) (*.f64 (/.f64 (pow.f64 (sin.f64 x) 2) (pow.f64 (cos.f64 x) 2)) eps)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-rgt-in_binary64 (*.f64 eps (+.f64 1 (/.f64 (pow.f64 (sin.f64 x) 2) (pow.f64 (cos.f64 x) 2))))): 31 points increase in error, 12 points decrease in error
      (*.f64 eps (+.f64 1 (Rewrite<= *-lft-identity_binary64 (*.f64 1 (/.f64 (pow.f64 (sin.f64 x) 2) (pow.f64 (cos.f64 x) 2)))))): 0 points increase in error, 0 points decrease in error
      (*.f64 eps (+.f64 1 (*.f64 (Rewrite<= metadata-eval (neg.f64 -1)) (/.f64 (pow.f64 (sin.f64 x) 2) (pow.f64 (cos.f64 x) 2))))): 0 points increase in error, 0 points decrease in error
      (*.f64 eps (Rewrite<= cancel-sign-sub-inv_binary64 (-.f64 1 (*.f64 -1 (/.f64 (pow.f64 (sin.f64 x) 2) (pow.f64 (cos.f64 x) 2)))))): 0 points increase in error, 0 points decrease in error

    if 1.1799764769038227e-18 < eps

    1. Initial program 29.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq -0.0018077685951439057:\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \end{array} \]

Alternatives

Alternative 1
Error0.5
Cost52288
\[\begin{array}{l} t_0 := \frac{\sin \varepsilon}{\cos x}\\ \frac{1}{\frac{\cos x \cdot \cos \varepsilon}{t_0} - \frac{\sin \varepsilon \cdot \sin x}{t_0}} \end{array} \]
Alternative 2
Error0.5
Cost39168
\[\frac{1}{\frac{\cos \varepsilon \cdot {\cos x}^{2}}{\sin \varepsilon} - \cos x \cdot \sin x} \]
Alternative 3
Error0.5
Cost39168
\[\frac{1}{\cos \varepsilon \cdot \frac{{\cos x}^{2}}{\sin \varepsilon} - \cos x \cdot \sin x} \]
Alternative 4
Error0.6
Cost33096
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ t_1 := 1 - \tan x \cdot \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -0.0018077685951439057:\\ \;\;\;\;\frac{t_0}{t_1} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \frac{1}{t_1} - \tan x\\ \end{array} \]
Alternative 5
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 -0.0018077685951439057:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error15.3
Cost19776
\[\frac{\sin \varepsilon}{\cos x} \cdot \frac{1}{\cos \left(x + \varepsilon\right)} \]
Alternative 7
Error15.0
Cost19720
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -22.76615002971735:\\ \;\;\;\;\tan \varepsilon - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\ \;\;\;\;\sin \varepsilon \cdot {\cos x}^{-2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sin \varepsilon}{\cos \varepsilon}\\ \end{array} \]
Alternative 8
Error15.0
Cost13320
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -22.76615002971735:\\ \;\;\;\;\tan \varepsilon - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.1799764769038227 \cdot 10^{-18}:\\ \;\;\;\;\frac{\varepsilon}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sin \varepsilon}{\cos \varepsilon}\\ \end{array} \]
Alternative 9
Error26.9
Cost12992
\[\frac{\sin \varepsilon}{\cos \varepsilon} \]
Alternative 10
Error41.8
Cost6464
\[\sin \varepsilon \]
Alternative 11
Error44.3
Cost64
\[\varepsilon \]

Error

Reproduce

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