2tan (problem 3.3.2)

?

Percentage Accurate: 41.8% → 98.8%
Time: 19.7s
Precision: binary64
Cost: 45833

?

\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -3.8 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 1.45 \cdot 10^{-26}\right):\\ \;\;\;\;\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{\frac{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)}{-1}}, -\tan x\right)\\ \mathbf{else}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \end{array} \]
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
(FPCore (x eps)
 :precision binary64
 (if (or (<= eps -3.8e-9) (not (<= eps 1.45e-26)))
   (fma
    (+ (tan x) (tan eps))
    (/ 1.0 (/ (fma (tan x) (tan eps) -1.0) -1.0))
    (- (tan x)))
   (+ eps (* eps (/ (pow (sin x) 2.0) (pow (cos x) 2.0))))))
double code(double x, double eps) {
	return tan((x + eps)) - tan(x);
}
double code(double x, double eps) {
	double tmp;
	if ((eps <= -3.8e-9) || !(eps <= 1.45e-26)) {
		tmp = fma((tan(x) + tan(eps)), (1.0 / (fma(tan(x), tan(eps), -1.0) / -1.0)), -tan(x));
	} else {
		tmp = eps + (eps * (pow(sin(x), 2.0) / pow(cos(x), 2.0)));
	}
	return tmp;
}
function code(x, eps)
	return Float64(tan(Float64(x + eps)) - tan(x))
end
function code(x, eps)
	tmp = 0.0
	if ((eps <= -3.8e-9) || !(eps <= 1.45e-26))
		tmp = fma(Float64(tan(x) + tan(eps)), Float64(1.0 / Float64(fma(tan(x), tan(eps), -1.0) / -1.0)), Float64(-tan(x)));
	else
		tmp = Float64(eps + Float64(eps * Float64((sin(x) ^ 2.0) / (cos(x) ^ 2.0))));
	end
	return tmp
end
code[x_, eps_] := N[(N[Tan[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]
code[x_, eps_] := If[Or[LessEqual[eps, -3.8e-9], N[Not[LessEqual[eps, 1.45e-26]], $MachinePrecision]], N[(N[(N[Tan[x], $MachinePrecision] + N[Tan[eps], $MachinePrecision]), $MachinePrecision] * N[(1.0 / N[(N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision] + -1.0), $MachinePrecision] / -1.0), $MachinePrecision]), $MachinePrecision] + (-N[Tan[x], $MachinePrecision])), $MachinePrecision], 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]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -3.8 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 1.45 \cdot 10^{-26}\right):\\
\;\;\;\;\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{\frac{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)}{-1}}, -\tan x\right)\\

\mathbf{else}:\\
\;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 11 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Target

Original41.8%
Target77.2%
Herbie98.8%
\[\frac{\sin \varepsilon}{\cos x \cdot \cos \left(x + \varepsilon\right)} \]

Derivation?

  1. Split input into 2 regimes
  2. if eps < -3.80000000000000011e-9 or 1.4499999999999999e-26 < eps

    1. Initial program 55.7%

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

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

      [Start]55.7%

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

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

      fma-neg [=>]99.4%

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

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

      [Start]99.4%

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

      /-rgt-identity [<=]99.4%

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

      frac-2neg [=>]99.4%

      \[ \mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{\color{blue}{\frac{-\left(1 - \tan x \cdot \tan \varepsilon\right)}{-1}}}, -\tan x\right) \]
    4. Simplified99.5%

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

      [Start]99.4%

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

      +-commutative [=>]99.4%

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

      fma-def [=>]99.5%

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

    if -3.80000000000000011e-9 < eps < 1.4499999999999999e-26

    1. Initial program 27.1%

      \[\tan \left(x + \varepsilon\right) - \tan x \]
    2. Taylor expanded in eps around 0 99.6%

      \[\leadsto \color{blue}{\varepsilon \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)} \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}} \]
      Step-by-step derivation

      [Start]99.6%

      \[ \varepsilon \cdot \left(1 - -1 \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\right) \]

      cancel-sign-sub-inv [=>]99.6%

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

      metadata-eval [=>]99.6%

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

      *-lft-identity [=>]99.6%

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

      distribute-lft-in [=>]99.8%

      \[ \color{blue}{\varepsilon \cdot 1 + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}} \]

      *-rgt-identity [=>]99.8%

      \[ \color{blue}{\varepsilon} + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.6%

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

Alternatives

Alternative 1
Accuracy98.8%
Cost45833
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -3.8 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 1.45 \cdot 10^{-26}\right):\\ \;\;\;\;\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{\frac{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)}{-1}}, -\tan x\right)\\ \mathbf{else}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \end{array} \]
Alternative 2
Accuracy98.8%
Cost39304
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -3.5 \cdot 10^{-9}:\\ \;\;\;\;\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-\tan x\right) - \tan \varepsilon}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} - \tan x\\ \end{array} \]
Alternative 3
Accuracy98.8%
Cost39304
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)\\ \mathbf{if}\;\varepsilon \leq -6.2 \cdot 10^{-9}:\\ \;\;\;\;\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{-t_0} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-\tan x\right) - \tan \varepsilon}{t_0} - \tan x\\ \end{array} \]
Alternative 4
Accuracy98.8%
Cost39304
\[\begin{array}{l} t_0 := -\tan x\\ \mathbf{if}\;\varepsilon \leq -2.5 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(\tan x + \tan \varepsilon, \frac{1}{1 - \tan x \cdot \tan \varepsilon}, t_0\right)\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0 - \tan \varepsilon}{\mathsf{fma}\left(\tan x, \tan \varepsilon, -1\right)} - \tan x\\ \end{array} \]
Alternative 5
Accuracy98.8%
Cost33096
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -6.2 \cdot 10^{-9}:\\ \;\;\;\;t_0 \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{1 - \frac{\tan x}{\frac{1}{\tan \varepsilon}}} - \tan x\\ \end{array} \]
Alternative 6
Accuracy98.8%
Cost32969
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -1.9 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 1.45 \cdot 10^{-26}\right):\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \mathbf{else}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \end{array} \]
Alternative 7
Accuracy98.8%
Cost32968
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ t_1 := 1 - \tan x \cdot \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -4 \cdot 10^{-9}:\\ \;\;\;\;t_0 \cdot \frac{1}{t_1} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{t_1} - \tan x\\ \end{array} \]
Alternative 8
Accuracy77.6%
Cost26440
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -6.8 \cdot 10^{-6}:\\ \;\;\;\;\tan \varepsilon\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon \cdot \left(1 + \frac{{\sin x}^{2}}{{\cos x}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;\tan \varepsilon\\ \end{array} \]
Alternative 9
Accuracy77.6%
Cost26440
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -6.5 \cdot 10^{-6}:\\ \;\;\;\;\tan \varepsilon\\ \mathbf{elif}\;\varepsilon \leq 1.45 \cdot 10^{-26}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot \frac{{\sin x}^{2}}{{\cos x}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\tan \varepsilon\\ \end{array} \]
Alternative 10
Accuracy58.1%
Cost6464
\[\tan \varepsilon \]
Alternative 11
Accuracy31.7%
Cost64
\[\varepsilon \]

Reproduce?

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