Average Error: 36.8 → 1.3
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\\ \mathbf{if}\;\varepsilon \leq -4 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{\mathsf{fma}\left(-1, \tan x \cdot \tan \varepsilon, 1\right)}, t_0\right)\\ \mathbf{elif}\;\varepsilon \leq 1.6 \cdot 10^{-51}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{{\cos x}^{2}}, \varepsilon\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 - \frac{t_1}{-\mathsf{fma}\left(\tan \varepsilon, t_0, 1\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))))
   (if (<= eps -4e-9)
     (fma t_1 (/ 1.0 (fma -1.0 (* (tan x) (tan eps)) 1.0)) t_0)
     (if (<= eps 1.6e-51)
       (fma eps (/ (pow (sin x) 2.0) (pow (cos x) 2.0)) eps)
       (- t_0 (/ t_1 (- (fma (tan eps) t_0 1.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 tmp;
	if (eps <= -4e-9) {
		tmp = fma(t_1, (1.0 / fma(-1.0, (tan(x) * tan(eps)), 1.0)), t_0);
	} else if (eps <= 1.6e-51) {
		tmp = fma(eps, (pow(sin(x), 2.0) / pow(cos(x), 2.0)), eps);
	} else {
		tmp = t_0 - (t_1 / -fma(tan(eps), t_0, 1.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))
	tmp = 0.0
	if (eps <= -4e-9)
		tmp = fma(t_1, Float64(1.0 / fma(-1.0, Float64(tan(x) * tan(eps)), 1.0)), t_0);
	elseif (eps <= 1.6e-51)
		tmp = fma(eps, Float64((sin(x) ^ 2.0) / (cos(x) ^ 2.0)), eps);
	else
		tmp = Float64(t_0 - Float64(t_1 / Float64(-fma(tan(eps), t_0, 1.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]}, If[LessEqual[eps, -4e-9], N[(t$95$1 * N[(1.0 / N[(-1.0 * N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision], If[LessEqual[eps, 1.6e-51], N[(eps * N[(N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] + eps), $MachinePrecision], N[(t$95$0 - N[(t$95$1 / (-N[(N[Tan[eps], $MachinePrecision] * t$95$0 + 1.0), $MachinePrecision])), $MachinePrecision]), $MachinePrecision]]]]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := -\tan x\\
t_1 := \tan x + \tan \varepsilon\\
\mathbf{if}\;\varepsilon \leq -4 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{\mathsf{fma}\left(-1, \tan x \cdot \tan \varepsilon, 1\right)}, t_0\right)\\

\mathbf{elif}\;\varepsilon \leq 1.6 \cdot 10^{-51}:\\
\;\;\;\;\mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{{\cos x}^{2}}, \varepsilon\right)\\

\mathbf{else}:\\
\;\;\;\;t_0 - \frac{t_1}{-\mathsf{fma}\left(\tan \varepsilon, t_0, 1\right)}\\


\end{array}

Error

Bits error versus x

Bits error versus eps

Target

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

Derivation

  1. Split input into 3 regimes
  2. if eps < -4.00000000000000025e-9

    1. Initial program 29.9

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

      \[\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.5

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

    if -4.00000000000000025e-9 < eps < 1.6e-51

    1. Initial program 45.5

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

      \[\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.3

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\varepsilon, \frac{{\sin x}^{2}}{{\cos x}^{2}}, \varepsilon\right)} \]

    if 1.6e-51 < eps

    1. Initial program 29.9

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

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]
    3. Applied egg-rr3.5

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

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

Reproduce

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