\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := -\tan x\\
t_1 := \tan x + \tan \varepsilon\\
\mathbf{if}\;\varepsilon \leq -0.00028767276362063015:\\
\;\;\;\;\begin{array}{l}
t_2 := \tan x \cdot \tan \varepsilon\\
\mathsf{fma}\left(t_1, \frac{1}{1 - {t_2}^{3}} \cdot \left(1 + \left(t_2 + t_2 \cdot t_2\right)\right), t_0\right)
\end{array}\\
\mathbf{elif}\;\varepsilon \leq 4.0940859351585484 \cdot 10^{-13}:\\
\;\;\;\;\begin{array}{l}
t_3 := {\cos x}^{2}\\
t_4 := {\sin x}^{3}\\
t_5 := {\sin x}^{2}\\
t_6 := {\cos x}^{3}\\
\frac{{\varepsilon}^{2} \cdot t_4}{t_6} + \left(\frac{{\varepsilon}^{2} \cdot \sin x}{\cos x} + \left(\varepsilon + \left(\frac{{\varepsilon}^{3} \cdot {\sin x}^{4}}{{\cos x}^{4}} + \left(1.6666666666666667 \cdot \frac{t_4 \cdot {\varepsilon}^{4}}{t_6} + \left(\frac{{\varepsilon}^{4} \cdot {\sin x}^{5}}{{\cos x}^{5}} + \left(1.3333333333333333 \cdot \frac{{\varepsilon}^{3} \cdot t_5}{t_3} + \left(\frac{\varepsilon \cdot t_5}{t_3} + \left(0.6666666666666666 \cdot \frac{\sin x \cdot {\varepsilon}^{4}}{\cos x} + {\varepsilon}^{3} \cdot 0.3333333333333333\right)\right)\right)\right)\right)\right)\right)\right)
\end{array}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t_1, \frac{1}{1 - \frac{\sqrt[3]{{\left(\tan \varepsilon \cdot \sin x\right)}^{3}}}{\cos x}}, 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))))
(if (<= eps -0.00028767276362063015)
(let* ((t_2 (* (tan x) (tan eps))))
(fma
t_1
(* (/ 1.0 (- 1.0 (pow t_2 3.0))) (+ 1.0 (+ t_2 (* t_2 t_2))))
t_0))
(if (<= eps 4.0940859351585484e-13)
(let* ((t_3 (pow (cos x) 2.0))
(t_4 (pow (sin x) 3.0))
(t_5 (pow (sin x) 2.0))
(t_6 (pow (cos x) 3.0)))
(+
(/ (* (pow eps 2.0) t_4) t_6)
(+
(/ (* (pow eps 2.0) (sin x)) (cos x))
(+
eps
(+
(/ (* (pow eps 3.0) (pow (sin x) 4.0)) (pow (cos x) 4.0))
(+
(* 1.6666666666666667 (/ (* t_4 (pow eps 4.0)) t_6))
(+
(/ (* (pow eps 4.0) (pow (sin x) 5.0)) (pow (cos x) 5.0))
(+
(* 1.3333333333333333 (/ (* (pow eps 3.0) t_5) t_3))
(+
(/ (* eps t_5) t_3)
(+
(* 0.6666666666666666 (/ (* (sin x) (pow eps 4.0)) (cos x)))
(* (pow eps 3.0) 0.3333333333333333)))))))))))
(fma
t_1
(/ 1.0 (- 1.0 (/ (cbrt (pow (* (tan eps) (sin x)) 3.0)) (cos x))))
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 tmp;
if (eps <= -0.00028767276362063015) {
double t_2_1 = tan(x) * tan(eps);
tmp = fma(t_1, ((1.0 / (1.0 - pow(t_2_1, 3.0))) * (1.0 + (t_2_1 + (t_2_1 * t_2_1)))), t_0);
} else if (eps <= 4.0940859351585484e-13) {
double t_3 = pow(cos(x), 2.0);
double t_4 = pow(sin(x), 3.0);
double t_5 = pow(sin(x), 2.0);
double t_6 = pow(cos(x), 3.0);
tmp = ((pow(eps, 2.0) * t_4) / t_6) + (((pow(eps, 2.0) * sin(x)) / cos(x)) + (eps + (((pow(eps, 3.0) * pow(sin(x), 4.0)) / pow(cos(x), 4.0)) + ((1.6666666666666667 * ((t_4 * pow(eps, 4.0)) / t_6)) + (((pow(eps, 4.0) * pow(sin(x), 5.0)) / pow(cos(x), 5.0)) + ((1.3333333333333333 * ((pow(eps, 3.0) * t_5) / t_3)) + (((eps * t_5) / t_3) + ((0.6666666666666666 * ((sin(x) * pow(eps, 4.0)) / cos(x))) + (pow(eps, 3.0) * 0.3333333333333333)))))))));
} else {
tmp = fma(t_1, (1.0 / (1.0 - (cbrt(pow((tan(eps) * sin(x)), 3.0)) / cos(x)))), t_0);
}
return tmp;
}




Bits error versus x




Bits error versus eps
| Original | 37.6 |
|---|---|
| Target | 15.2 |
| Herbie | 0.4 |
if eps < -2.8767276362063015e-4Initial program 29.8
Applied tan-sum_binary640.3
Applied div-inv_binary640.3
Applied fma-neg_binary640.3
Applied flip3--_binary640.4
Applied associate-/r/_binary640.4
Simplified0.4
if -2.8767276362063015e-4 < eps < 4.0940859351585484e-13Initial program 45.6
Taylor expanded in eps around 0 0.2
if 4.0940859351585484e-13 < eps Initial program 30.2
Applied tan-sum_binary640.8
Applied div-inv_binary640.9
Applied fma-neg_binary640.8
Applied tan-quot_binary640.9
Applied associate-*l/_binary640.9
Applied add-cbrt-cube_binary640.9
Simplified0.9
Final simplification0.4
herbie shell --seed 2022019
(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)))