2tan (problem 3.3.2)

?

Percentage Accurate: 42.6% → 99.5%
Time: 18.9s
Precision: binary64
Cost: 59144

?

\[\tan \left(x + \varepsilon\right) - \tan x \]
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ t_1 := 1 - \tan x \cdot \tan \varepsilon\\ t_2 := \frac{\sin x}{\cos x}\\ \mathbf{if}\;\varepsilon \leq -4.25 \cdot 10^{-7}:\\ \;\;\;\;\frac{t_0}{t_1} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 8.2 \cdot 10^{-7}:\\ \;\;\;\;\varepsilon \cdot \left(\left(1 + \frac{{\sin x}^{2}}{{\cos x}^{2}}\right) + \varepsilon \cdot \left(t_2 + {t_2}^{3}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \frac{1}{t_1} - \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)))
        (t_1 (- 1.0 (* (tan x) (tan eps))))
        (t_2 (/ (sin x) (cos x))))
   (if (<= eps -4.25e-7)
     (- (/ t_0 t_1) (tan x))
     (if (<= eps 8.2e-7)
       (*
        eps
        (+
         (+ 1.0 (/ (pow (sin x) 2.0) (pow (cos x) 2.0)))
         (* eps (+ t_2 (pow t_2 3.0)))))
       (- (* t_0 (/ 1.0 t_1)) (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 t_1 = 1.0 - (tan(x) * tan(eps));
	double t_2 = sin(x) / cos(x);
	double tmp;
	if (eps <= -4.25e-7) {
		tmp = (t_0 / t_1) - tan(x);
	} else if (eps <= 8.2e-7) {
		tmp = eps * ((1.0 + (pow(sin(x), 2.0) / pow(cos(x), 2.0))) + (eps * (t_2 + pow(t_2, 3.0))));
	} else {
		tmp = (t_0 * (1.0 / t_1)) - tan(x);
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = tan((x + eps)) - tan(x)
end function
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = tan(x) + tan(eps)
    t_1 = 1.0d0 - (tan(x) * tan(eps))
    t_2 = sin(x) / cos(x)
    if (eps <= (-4.25d-7)) then
        tmp = (t_0 / t_1) - tan(x)
    else if (eps <= 8.2d-7) then
        tmp = eps * ((1.0d0 + ((sin(x) ** 2.0d0) / (cos(x) ** 2.0d0))) + (eps * (t_2 + (t_2 ** 3.0d0))))
    else
        tmp = (t_0 * (1.0d0 / t_1)) - tan(x)
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	return Math.tan((x + eps)) - Math.tan(x);
}
public static double code(double x, double eps) {
	double t_0 = Math.tan(x) + Math.tan(eps);
	double t_1 = 1.0 - (Math.tan(x) * Math.tan(eps));
	double t_2 = Math.sin(x) / Math.cos(x);
	double tmp;
	if (eps <= -4.25e-7) {
		tmp = (t_0 / t_1) - Math.tan(x);
	} else if (eps <= 8.2e-7) {
		tmp = eps * ((1.0 + (Math.pow(Math.sin(x), 2.0) / Math.pow(Math.cos(x), 2.0))) + (eps * (t_2 + Math.pow(t_2, 3.0))));
	} else {
		tmp = (t_0 * (1.0 / t_1)) - Math.tan(x);
	}
	return tmp;
}
def code(x, eps):
	return math.tan((x + eps)) - math.tan(x)
def code(x, eps):
	t_0 = math.tan(x) + math.tan(eps)
	t_1 = 1.0 - (math.tan(x) * math.tan(eps))
	t_2 = math.sin(x) / math.cos(x)
	tmp = 0
	if eps <= -4.25e-7:
		tmp = (t_0 / t_1) - math.tan(x)
	elif eps <= 8.2e-7:
		tmp = eps * ((1.0 + (math.pow(math.sin(x), 2.0) / math.pow(math.cos(x), 2.0))) + (eps * (t_2 + math.pow(t_2, 3.0))))
	else:
		tmp = (t_0 * (1.0 / t_1)) - math.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))
	t_1 = Float64(1.0 - Float64(tan(x) * tan(eps)))
	t_2 = Float64(sin(x) / cos(x))
	tmp = 0.0
	if (eps <= -4.25e-7)
		tmp = Float64(Float64(t_0 / t_1) - tan(x));
	elseif (eps <= 8.2e-7)
		tmp = Float64(eps * Float64(Float64(1.0 + Float64((sin(x) ^ 2.0) / (cos(x) ^ 2.0))) + Float64(eps * Float64(t_2 + (t_2 ^ 3.0)))));
	else
		tmp = Float64(Float64(t_0 * Float64(1.0 / t_1)) - tan(x));
	end
	return tmp
end
function tmp = code(x, eps)
	tmp = tan((x + eps)) - tan(x);
end
function tmp_2 = code(x, eps)
	t_0 = tan(x) + tan(eps);
	t_1 = 1.0 - (tan(x) * tan(eps));
	t_2 = sin(x) / cos(x);
	tmp = 0.0;
	if (eps <= -4.25e-7)
		tmp = (t_0 / t_1) - tan(x);
	elseif (eps <= 8.2e-7)
		tmp = eps * ((1.0 + ((sin(x) ^ 2.0) / (cos(x) ^ 2.0))) + (eps * (t_2 + (t_2 ^ 3.0))));
	else
		tmp = (t_0 * (1.0 / t_1)) - tan(x);
	end
	tmp_2 = 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]}, Block[{t$95$1 = N[(1.0 - N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[x], $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[eps, -4.25e-7], N[(N[(t$95$0 / t$95$1), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision], If[LessEqual[eps, 8.2e-7], N[(eps * N[(N[(1.0 + N[(N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(eps * N[(t$95$2 + N[Power[t$95$2, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(t$95$0 * N[(1.0 / t$95$1), $MachinePrecision]), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]]]]]]
\tan \left(x + \varepsilon\right) - \tan x
\begin{array}{l}
t_0 := \tan x + \tan \varepsilon\\
t_1 := 1 - \tan x \cdot \tan \varepsilon\\
t_2 := \frac{\sin x}{\cos x}\\
\mathbf{if}\;\varepsilon \leq -4.25 \cdot 10^{-7}:\\
\;\;\;\;\frac{t_0}{t_1} - \tan x\\

\mathbf{elif}\;\varepsilon \leq 8.2 \cdot 10^{-7}:\\
\;\;\;\;\varepsilon \cdot \left(\left(1 + \frac{{\sin x}^{2}}{{\cos x}^{2}}\right) + \varepsilon \cdot \left(t_2 + {t_2}^{3}\right)\right)\\

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


\end{array}

Local Percentage Accuracy?

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 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original42.6%
Target76.1%
Herbie99.5%
\[\frac{\sin \varepsilon}{\cos x \cdot \cos \left(x + \varepsilon\right)} \]

Derivation?

  1. Split input into 3 regimes
  2. if eps < -4.25000000000000007e-7

    1. Initial program 51.9%

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

      \[\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]51.9

      \[ \tan \left(x + \varepsilon\right) - \tan x \]

      tan-sum [=>]99.6

      \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]

      div-inv [=>]99.6

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

      fma-neg [=>]99.6

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

      \[\leadsto \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x} \]
      Step-by-step derivation

      [Start]99.6

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

      fma-neg [<=]99.6

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

      associate-*r/ [=>]99.6

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

      *-rgt-identity [=>]99.6

      \[ \frac{\color{blue}{\tan x + \tan \varepsilon}}{1 - \tan x \cdot \tan \varepsilon} - \tan x \]

    if -4.25000000000000007e-7 < eps < 8.1999999999999998e-7

    1. Initial program 35.4%

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

      \[\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]35.4

      \[ \tan \left(x + \varepsilon\right) - \tan x \]

      tan-sum [=>]37.1

      \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]

      div-inv [=>]37.1

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

      fma-neg [=>]37.1

      \[ \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 99.6%

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

      \[\leadsto \color{blue}{\varepsilon \cdot \left(\left(1 + \frac{{\sin x}^{2}}{{\cos x}^{2}}\right) + \varepsilon \cdot \left(\frac{\sin x}{\cos x} + {\left(\frac{\sin x}{\cos x}\right)}^{3}\right)\right)} \]
      Step-by-step derivation

      [Start]99.6

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

      +-commutative [=>]99.6

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

      *-commutative [=>]99.6

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

      unpow2 [=>]99.6

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

      associate-*l* [=>]99.6

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

      distribute-lft-out [=>]99.6

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

      cube-mult [=>]99.6

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

      unpow2 [<=]99.6

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

    if 8.1999999999999998e-7 < eps

    1. Initial program 57.6%

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

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

      [Start]57.6

      \[ \tan \left(x + \varepsilon\right) - \tan x \]

      tan-sum [=>]99.3

      \[ \color{blue}{\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon}} - \tan x \]

      div-inv [=>]99.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\varepsilon \leq -4.25 \cdot 10^{-7}:\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 8.2 \cdot 10^{-7}:\\ \;\;\;\;\varepsilon \cdot \left(\left(1 + \frac{{\sin x}^{2}}{{\cos x}^{2}}\right) + \varepsilon \cdot \left(\frac{\sin x}{\cos x} + {\left(\frac{\sin x}{\cos x}\right)}^{3}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\tan x + \tan \varepsilon\right) \cdot \frac{1}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.4%
Cost33096
\[\begin{array}{l} t_0 := \tan x + \tan \varepsilon\\ t_1 := 1 - \tan x \cdot \tan \varepsilon\\ \mathbf{if}\;\varepsilon \leq -5 \cdot 10^{-9}:\\ \;\;\;\;\frac{t_0}{t_1} - \tan x\\ \mathbf{elif}\;\varepsilon \leq 3.3 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon \cdot \tan x, \tan x, \varepsilon\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \frac{1}{t_1} - \tan x\\ \end{array} \]
Alternative 2
Accuracy99.4%
Cost32969
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -6.4 \cdot 10^{-9} \lor \neg \left(\varepsilon \leq 3.5 \cdot 10^{-9}\right):\\ \;\;\;\;\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon \cdot \tan x, \tan x, \varepsilon\right)\\ \end{array} \]
Alternative 3
Accuracy77.3%
Cost19784
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -3.1 \cdot 10^{-6}:\\ \;\;\;\;\tan \varepsilon\\ \mathbf{elif}\;\varepsilon \leq 6.6 \cdot 10^{-7}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon \cdot \tan x, \tan x, \varepsilon\right)\\ \mathbf{else}:\\ \;\;\;\;\tan \varepsilon\\ \end{array} \]
Alternative 4
Accuracy77.3%
Cost13448
\[\begin{array}{l} \mathbf{if}\;\varepsilon \leq -3.8 \cdot 10^{-6}:\\ \;\;\;\;\tan \varepsilon\\ \mathbf{elif}\;\varepsilon \leq 1.22 \cdot 10^{-6}:\\ \;\;\;\;\varepsilon + \varepsilon \cdot {\tan x}^{2}\\ \mathbf{else}:\\ \;\;\;\;\tan \varepsilon\\ \end{array} \]
Alternative 5
Accuracy58.3%
Cost6464
\[\tan \varepsilon \]
Alternative 6
Accuracy30.8%
Cost64
\[\varepsilon \]

Reproduce?

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