| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 150664 |
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
:precision binary64
(let* ((t_0 (/ (sin x) (cos x)))
(t_1 (+ (tan x) (tan eps)))
(t_2 (/ (pow (sin x) 2.0) (pow (cos x) 2.0)))
(t_3 (+ 1.0 t_2))
(t_4 (pow t_3 2.0)))
(if (<= eps -1.65e-6)
(- (/ t_1 (- 1.0 (/ (sin eps) (/ (cos eps) (tan x))))) (tan x))
(if (<= eps 2e-5)
(log1p
(fma
(* eps eps)
(fma 0.5 t_4 (* t_3 t_0))
(fma
(pow eps 3.0)
(+
(fma 0.16666666666666666 (pow t_3 3.0) (* t_4 t_0))
(-
(- (* t_2 t_3) (fma 0.16666666666666666 t_2 0.16666666666666666))
(+ -0.5 (* t_2 -0.5))))
(+ eps (* eps t_2)))))
(- (/ t_1 (fma (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 = sin(x) / cos(x);
double t_1 = tan(x) + tan(eps);
double t_2 = pow(sin(x), 2.0) / pow(cos(x), 2.0);
double t_3 = 1.0 + t_2;
double t_4 = pow(t_3, 2.0);
double tmp;
if (eps <= -1.65e-6) {
tmp = (t_1 / (1.0 - (sin(eps) / (cos(eps) / tan(x))))) - tan(x);
} else if (eps <= 2e-5) {
tmp = log1p(fma((eps * eps), fma(0.5, t_4, (t_3 * t_0)), fma(pow(eps, 3.0), (fma(0.16666666666666666, pow(t_3, 3.0), (t_4 * t_0)) + (((t_2 * t_3) - fma(0.16666666666666666, t_2, 0.16666666666666666)) - (-0.5 + (t_2 * -0.5)))), (eps + (eps * t_2)))));
} else {
tmp = (t_1 / fma(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 = Float64(sin(x) / cos(x)) t_1 = Float64(tan(x) + tan(eps)) t_2 = Float64((sin(x) ^ 2.0) / (cos(x) ^ 2.0)) t_3 = Float64(1.0 + t_2) t_4 = t_3 ^ 2.0 tmp = 0.0 if (eps <= -1.65e-6) tmp = Float64(Float64(t_1 / Float64(1.0 - Float64(sin(eps) / Float64(cos(eps) / tan(x))))) - tan(x)); elseif (eps <= 2e-5) tmp = log1p(fma(Float64(eps * eps), fma(0.5, t_4, Float64(t_3 * t_0)), fma((eps ^ 3.0), Float64(fma(0.16666666666666666, (t_3 ^ 3.0), Float64(t_4 * t_0)) + Float64(Float64(Float64(t_2 * t_3) - fma(0.16666666666666666, t_2, 0.16666666666666666)) - Float64(-0.5 + Float64(t_2 * -0.5)))), Float64(eps + Float64(eps * t_2))))); else tmp = Float64(Float64(t_1 / fma(tan(x), Float64(-tan(eps)), 1.0)) - 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[Sin[x], $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Tan[x], $MachinePrecision] + N[Tan[eps], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(1.0 + t$95$2), $MachinePrecision]}, Block[{t$95$4 = N[Power[t$95$3, 2.0], $MachinePrecision]}, If[LessEqual[eps, -1.65e-6], N[(N[(t$95$1 / N[(1.0 - N[(N[Sin[eps], $MachinePrecision] / N[(N[Cos[eps], $MachinePrecision] / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 2e-5], N[Log[1 + N[(N[(eps * eps), $MachinePrecision] * N[(0.5 * t$95$4 + N[(t$95$3 * t$95$0), $MachinePrecision]), $MachinePrecision] + N[(N[Power[eps, 3.0], $MachinePrecision] * N[(N[(0.16666666666666666 * N[Power[t$95$3, 3.0], $MachinePrecision] + N[(t$95$4 * t$95$0), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(t$95$2 * t$95$3), $MachinePrecision] - N[(0.16666666666666666 * t$95$2 + 0.16666666666666666), $MachinePrecision]), $MachinePrecision] - N[(-0.5 + N[(t$95$2 * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(eps + N[(eps * t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(t$95$1 / N[(N[Tan[x], $MachinePrecision] * (-N[Tan[eps], $MachinePrecision]) + 1.0), $MachinePrecision]), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]]]]]]]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := \frac{\sin x}{\cos x}\\
t_1 := \tan x + \tan \varepsilon\\
t_2 := \frac{{\sin x}^{2}}{{\cos x}^{2}}\\
t_3 := 1 + t_2\\
t_4 := {t_3}^{2}\\
\mathbf{if}\;\varepsilon \leq -1.65 \cdot 10^{-6}:\\
\;\;\;\;\frac{t_1}{1 - \frac{\sin \varepsilon}{\frac{\cos \varepsilon}{\tan x}}} - \tan x\\
\mathbf{elif}\;\varepsilon \leq 2 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{fma}\left(\varepsilon \cdot \varepsilon, \mathsf{fma}\left(0.5, t_4, t_3 \cdot t_0\right), \mathsf{fma}\left({\varepsilon}^{3}, \mathsf{fma}\left(0.16666666666666666, {t_3}^{3}, t_4 \cdot t_0\right) + \left(\left(t_2 \cdot t_3 - \mathsf{fma}\left(0.16666666666666666, t_2, 0.16666666666666666\right)\right) - \left(-0.5 + t_2 \cdot -0.5\right)\right), \varepsilon + \varepsilon \cdot t_2\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{t_1}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)} - \tan x\\
\end{array}
| Original | 42.7% |
|---|---|
| Target | 76.6% |
| Herbie | 99.5% |
if eps < -1.65000000000000008e-6Initial program 54.6%
Applied egg-rr99.4%
[Start]54.6 | \[ \tan \left(x + \varepsilon\right) - \tan x
\] |
|---|---|
tan-sum [=>]99.4 | \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x
\] |
div-inv [=>]99.4 | \[ \color{blue}{\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon}} - \tan x
\] |
Simplified99.4%
[Start]99.4 | \[ \left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\] |
|---|---|
associate-*r/ [=>]99.4 | \[ \color{blue}{\frac{\left(\tan x + \tan \varepsilon\right) \cdot 1}{1 - \tan x \cdot \tan \varepsilon}} - \tan x
\] |
*-rgt-identity [=>]99.4 | \[ \frac{\color{blue}{\tan x + \tan \varepsilon}}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\] |
Applied egg-rr99.4%
[Start]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\] |
|---|---|
tan-quot [=>]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \color{blue}{\frac{\sin \varepsilon}{\cos \varepsilon}}} - \tan x
\] |
associate-*r/ [=>]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}}} - \tan x
\] |
Simplified99.4%
[Start]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \frac{\tan x \cdot \sin \varepsilon}{\cos \varepsilon}} - \tan x
\] |
|---|---|
*-commutative [=>]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \frac{\color{blue}{\sin \varepsilon \cdot \tan x}}{\cos \varepsilon}} - \tan x
\] |
associate-/l* [=>]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\frac{\sin \varepsilon}{\frac{\cos \varepsilon}{\tan x}}}} - \tan x
\] |
if -1.65000000000000008e-6 < eps < 2.00000000000000016e-5Initial program 31.1%
Applied egg-rr31.1%
[Start]31.1 | \[ \tan \left(x + \varepsilon\right) - \tan x
\] |
|---|---|
log1p-expm1-u [=>]31.1 | \[ \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\tan \left(x + \varepsilon\right) - \tan x\right)\right)}
\] |
Taylor expanded in eps around 0 99.6%
Simplified99.7%
[Start]99.6 | \[ \mathsf{log1p}\left({\varepsilon}^{2} \cdot \left(\frac{\sin x \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}{\cos x} + 0.5 \cdot {\left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}^{2}\right) + \left(\left(0.16666666666666666 \cdot {\left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}^{3} + \left(\frac{\sin x \cdot {\left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}^{2}}{\cos x} + -1 \cdot \left(0.16666666666666666 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}} + \left(0.16666666666666666 + \left(-1 \cdot \frac{{\sin x}^{2} \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}{{\cos x}^{2}} + -0.5 \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\right)\right)\right)\right)\right) \cdot {\varepsilon}^{3} + \varepsilon \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\right)\right)
\] |
|---|---|
fma-def [=>]99.6 | \[ \mathsf{log1p}\left(\color{blue}{\mathsf{fma}\left({\varepsilon}^{2}, \frac{\sin x \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}{\cos x} + 0.5 \cdot {\left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}^{2}, \left(0.16666666666666666 \cdot {\left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}^{3} + \left(\frac{\sin x \cdot {\left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}^{2}}{\cos x} + -1 \cdot \left(0.16666666666666666 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}} + \left(0.16666666666666666 + \left(-1 \cdot \frac{{\sin x}^{2} \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)}{{\cos x}^{2}} + -0.5 \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\right)\right)\right)\right)\right) \cdot {\varepsilon}^{3} + \varepsilon \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\right)}\right)
\] |
if 2.00000000000000016e-5 < eps Initial program 53.3%
Applied egg-rr99.4%
[Start]53.3 | \[ \tan \left(x + \varepsilon\right) - \tan x
\] |
|---|---|
tan-sum [=>]99.4 | \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x
\] |
div-inv [=>]99.4 | \[ \color{blue}{\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon}} - \tan x
\] |
Simplified99.4%
[Start]99.4 | \[ \left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\] |
|---|---|
associate-*r/ [=>]99.4 | \[ \color{blue}{\frac{\left(\tan x + \tan \varepsilon\right) \cdot 1}{1 - \tan x \cdot \tan \varepsilon}} - \tan x
\] |
*-rgt-identity [=>]99.4 | \[ \frac{\color{blue}{\tan x + \tan \varepsilon}}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\] |
Applied egg-rr97.9%
[Start]99.4 | \[ \frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\] |
|---|---|
log1p-expm1-u [=>]97.9 | \[ \frac{\tan x + \tan \varepsilon}{1 - \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)}} - \tan x
\] |
Applied egg-rr99.4%
[Start]97.9 | \[ \frac{\tan x + \tan \varepsilon}{1 - \mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)} - \tan x
\] |
|---|---|
sub-neg [=>]97.9 | \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)} + \left(-\tan x\right)}
\] |
div-inv [=>]97.8 | \[ \color{blue}{\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)}} + \left(-\tan x\right)
\] |
div-inv [<=]97.9 | \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)}} + \left(-\tan x\right)
\] |
sub-neg [=>]97.9 | \[ \frac{\tan x + \tan \varepsilon}{\color{blue}{1 + \left(-\mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)\right)}} + \left(-\tan x\right)
\] |
+-commutative [=>]97.9 | \[ \frac{\tan x + \tan \varepsilon}{\color{blue}{\left(-\mathsf{log1p}\left(\mathsf{expm1}\left(\tan x \cdot \tan \varepsilon\right)\right)\right) + 1}} + \left(-\tan x\right)
\] |
log1p-expm1-u [<=]99.4 | \[ \frac{\tan x + \tan \varepsilon}{\left(-\color{blue}{\tan x \cdot \tan \varepsilon}\right) + 1} + \left(-\tan x\right)
\] |
distribute-rgt-neg-in [=>]99.4 | \[ \frac{\tan x + \tan \varepsilon}{\color{blue}{\tan x \cdot \left(-\tan \varepsilon\right)} + 1} + \left(-\tan x\right)
\] |
fma-def [=>]99.4 | \[ \frac{\tan x + \tan \varepsilon}{\color{blue}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)}} + \left(-\tan x\right)
\] |
Simplified99.4%
[Start]99.4 | \[ \frac{\tan x + \tan \varepsilon}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)} + \left(-\tan x\right)
\] |
|---|---|
sub-neg [<=]99.4 | \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{\mathsf{fma}\left(\tan x, -\tan \varepsilon, 1\right)} - \tan x}
\] |
Final simplification99.5%
| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 150664 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 91848 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 65544 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 39364 |
| Alternative 5 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 39304 |
| Alternative 6 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 32969 |
| Alternative 7 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 32968 |
| Alternative 8 | |
|---|---|
| Accuracy | 77.5% |
| Cost | 26440 |
| Alternative 9 | |
|---|---|
| Accuracy | 77.5% |
| Cost | 26440 |
| Alternative 10 | |
|---|---|
| Accuracy | 31.4% |
| Cost | 6464 |
| Alternative 11 | |
|---|---|
| Accuracy | 58.3% |
| Cost | 6464 |
| Alternative 12 | |
|---|---|
| Accuracy | 4.2% |
| Cost | 64 |
| Alternative 13 | |
|---|---|
| Accuracy | 31.3% |
| Cost | 64 |
herbie shell --seed 2023147
(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)))