?

Average Error: 8.7 → 2.7
Time: 8.9s
Precision: binary64
Cost: 59204

?

\[0.001 \leq x \land x \leq 1.5\]
\[\tan x - x \]
\[\begin{array}{l} \mathbf{if}\;\tan x - x \leq 2 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x \cdot \left(-x\right), x, {x}^{3}\right) + \left({\tan x}^{3} - {x}^{3}\right)}{\mathsf{fma}\left(x, x, \tan x \cdot \left(x + \tan x\right)\right)}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (tan x) x))
(FPCore (x)
 :precision binary64
 (if (<= (- (tan x) x) 2e-5)
   (fma
    0.3333333333333333
    (pow x 3.0)
    (fma
     0.05396825396825397
     (pow x 7.0)
     (fma
      0.13333333333333333
      (pow x 5.0)
      (* 0.021869488536155203 (pow x 9.0)))))
   (/
    (+ (fma (* x (- x)) x (pow x 3.0)) (- (pow (tan x) 3.0) (pow x 3.0)))
    (fma x x (* (tan x) (+ x (tan x)))))))
double code(double x) {
	return tan(x) - x;
}
double code(double x) {
	double tmp;
	if ((tan(x) - x) <= 2e-5) {
		tmp = fma(0.3333333333333333, pow(x, 3.0), fma(0.05396825396825397, pow(x, 7.0), fma(0.13333333333333333, pow(x, 5.0), (0.021869488536155203 * pow(x, 9.0)))));
	} else {
		tmp = (fma((x * -x), x, pow(x, 3.0)) + (pow(tan(x), 3.0) - pow(x, 3.0))) / fma(x, x, (tan(x) * (x + tan(x))));
	}
	return tmp;
}
function code(x)
	return Float64(tan(x) - x)
end
function code(x)
	tmp = 0.0
	if (Float64(tan(x) - x) <= 2e-5)
		tmp = fma(0.3333333333333333, (x ^ 3.0), fma(0.05396825396825397, (x ^ 7.0), fma(0.13333333333333333, (x ^ 5.0), Float64(0.021869488536155203 * (x ^ 9.0)))));
	else
		tmp = Float64(Float64(fma(Float64(x * Float64(-x)), x, (x ^ 3.0)) + Float64((tan(x) ^ 3.0) - (x ^ 3.0))) / fma(x, x, Float64(tan(x) * Float64(x + tan(x)))));
	end
	return tmp
end
code[x_] := N[(N[Tan[x], $MachinePrecision] - x), $MachinePrecision]
code[x_] := If[LessEqual[N[(N[Tan[x], $MachinePrecision] - x), $MachinePrecision], 2e-5], N[(0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision] + N[(0.05396825396825397 * N[Power[x, 7.0], $MachinePrecision] + N[(0.13333333333333333 * N[Power[x, 5.0], $MachinePrecision] + N[(0.021869488536155203 * N[Power[x, 9.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(x * (-x)), $MachinePrecision] * x + N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[Power[N[Tan[x], $MachinePrecision], 3.0], $MachinePrecision] - N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x + N[(N[Tan[x], $MachinePrecision] * N[(x + N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\tan x - x
\begin{array}{l}
\mathbf{if}\;\tan x - x \leq 2 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x \cdot \left(-x\right), x, {x}^{3}\right) + \left({\tan x}^{3} - {x}^{3}\right)}{\mathsf{fma}\left(x, x, \tan x \cdot \left(x + \tan x\right)\right)}\\


\end{array}

Error?

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (tan.f64 x) x) < 2.00000000000000016e-5

    1. Initial program 13.7

      \[\tan x - x \]
    2. Taylor expanded in x around 0 1.6

      \[\leadsto \color{blue}{0.05396825396825397 \cdot {x}^{7} + \left(0.3333333333333333 \cdot {x}^{3} + \left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)\right)} \]
    3. Simplified1.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)\right)\right)} \]
      Proof

      [Start]1.6

      \[ 0.05396825396825397 \cdot {x}^{7} + \left(0.3333333333333333 \cdot {x}^{3} + \left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)\right) \]

      +-commutative [=>]1.6

      \[ 0.05396825396825397 \cdot {x}^{7} + \color{blue}{\left(\left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right) + 0.3333333333333333 \cdot {x}^{3}\right)} \]

      associate-+r+ [=>]1.5

      \[ \color{blue}{\left(0.05396825396825397 \cdot {x}^{7} + \left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)\right) + 0.3333333333333333 \cdot {x}^{3}} \]

      +-commutative [<=]1.5

      \[ \color{blue}{0.3333333333333333 \cdot {x}^{3} + \left(0.05396825396825397 \cdot {x}^{7} + \left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)\right)} \]

      fma-def [=>]1.5

      \[ \color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, 0.05396825396825397 \cdot {x}^{7} + \left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)\right)} \]

      fma-def [=>]1.5

      \[ \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \color{blue}{\mathsf{fma}\left(0.05396825396825397, {x}^{7}, 0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)}\right) \]

      +-commutative [=>]1.5

      \[ \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \color{blue}{0.13333333333333333 \cdot {x}^{5} + 0.021869488536155203 \cdot {x}^{9}}\right)\right) \]

      fma-def [=>]1.5

      \[ \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \color{blue}{\mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)}\right)\right) \]

    if 2.00000000000000016e-5 < (-.f64 (tan.f64 x) x)

    1. Initial program 3.7

      \[\tan x - x \]
    2. Applied egg-rr3.8

      \[\leadsto \color{blue}{\left({\tan x}^{3} - {x}^{3}\right) \cdot \frac{1}{{\tan x}^{2} + x \cdot \left(x + \tan x\right)}} \]
    3. Simplified3.8

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

      [Start]3.8

      \[ \left({\tan x}^{3} - {x}^{3}\right) \cdot \frac{1}{{\tan x}^{2} + x \cdot \left(x + \tan x\right)} \]

      associate-*r/ [=>]3.8

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

      *-rgt-identity [=>]3.8

      \[ \frac{\color{blue}{{\tan x}^{3} - {x}^{3}}}{{\tan x}^{2} + x \cdot \left(x + \tan x\right)} \]

      +-commutative [=>]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\color{blue}{x \cdot \left(x + \tan x\right) + {\tan x}^{2}}} \]

      distribute-rgt-in [=>]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\color{blue}{\left(x \cdot x + \tan x \cdot x\right)} + {\tan x}^{2}} \]

      sqr-neg [<=]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\left(\color{blue}{\left(-x\right) \cdot \left(-x\right)} + \tan x \cdot x\right) + {\tan x}^{2}} \]

      associate-+l+ [=>]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\color{blue}{\left(-x\right) \cdot \left(-x\right) + \left(\tan x \cdot x + {\tan x}^{2}\right)}} \]

      remove-double-neg [<=]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\left(-x\right) \cdot \left(-x\right) + \left(\color{blue}{\left(-\left(-\tan x \cdot x\right)\right)} + {\tan x}^{2}\right)} \]

      distribute-rgt-neg-out [<=]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\left(-x\right) \cdot \left(-x\right) + \left(\left(-\color{blue}{\tan x \cdot \left(-x\right)}\right) + {\tan x}^{2}\right)} \]

      sqr-neg [=>]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\color{blue}{x \cdot x} + \left(\left(-\tan x \cdot \left(-x\right)\right) + {\tan x}^{2}\right)} \]

      fma-def [=>]3.8

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

      distribute-rgt-neg-out [=>]3.8

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

      remove-double-neg [=>]3.8

      \[ \frac{{\tan x}^{3} - {x}^{3}}{\mathsf{fma}\left(x, x, \color{blue}{\tan x \cdot x} + {\tan x}^{2}\right)} \]
    4. Applied egg-rr3.9

      \[\leadsto \frac{\color{blue}{{\tan x}^{3} + \left(\left(-{x}^{3}\right) + \mathsf{fma}\left(-x \cdot x, x, {x}^{3}\right)\right)}}{\mathsf{fma}\left(x, x, \tan x \cdot \left(x + \tan x\right)\right)} \]
    5. Simplified3.8

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

      [Start]3.9

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

      associate-+r+ [=>]3.8

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

      sub-neg [<=]3.8

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

      +-commutative [<=]3.8

      \[ \frac{\color{blue}{\mathsf{fma}\left(-x \cdot x, x, {x}^{3}\right) + \left({\tan x}^{3} - {x}^{3}\right)}}{\mathsf{fma}\left(x, x, \tan x \cdot \left(x + \tan x\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification2.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;\tan x - x \leq 2 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x \cdot \left(-x\right), x, {x}^{3}\right) + \left({\tan x}^{3} - {x}^{3}\right)}{\mathsf{fma}\left(x, x, \tan x \cdot \left(x + \tan x\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error2.7
Cost52292
\[\begin{array}{l} t_0 := \tan x - x\\ \mathbf{if}\;t_0 \leq 2 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, \mathsf{fma}\left(0.021869488536155203, {x}^{9}, 0.3333333333333333 \cdot {x}^{3}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\log t_0}\\ \end{array} \]
Alternative 2
Error2.7
Cost52292
\[\begin{array}{l} t_0 := \tan x - x\\ \mathbf{if}\;t_0 \leq 2 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\log t_0}\\ \end{array} \]
Alternative 3
Error2.6
Cost52292
\[\begin{array}{l} t_0 := \tan x - x\\ \mathbf{if}\;t_0 \leq 2 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.05396825396825397, {x}^{7}, \mathsf{fma}\left(0.13333333333333333, {x}^{5}, 0.021869488536155203 \cdot {x}^{9}\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\log t_0}\\ \end{array} \]
Alternative 4
Error2.7
Cost33476
\[\begin{array}{l} t_0 := \tan x - x\\ \mathbf{if}\;t_0 \leq 2 \cdot 10^{-5}:\\ \;\;\;\;0.05396825396825397 \cdot {x}^{7} + \left(0.3333333333333333 \cdot {x}^{3} + \left(0.021869488536155203 \cdot {x}^{9} + 0.13333333333333333 \cdot {x}^{5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\log t_0}\\ \end{array} \]
Alternative 5
Error4.4
Cost26756
\[\begin{array}{l} t_0 := \tan x - x\\ \mathbf{if}\;t_0 \leq 8.5 \cdot 10^{-7}:\\ \;\;\;\;0.05396825396825397 \cdot {x}^{7} + \left(0.3333333333333333 \cdot {x}^{3} + 0.13333333333333333 \cdot {x}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\log t_0}\\ \end{array} \]
Alternative 6
Error8.7
Cost19392
\[e^{\log \left(\tan x - x\right)} \]
Alternative 7
Error8.7
Cost13184
\[{\left(\frac{1}{\tan x - x}\right)}^{-1} \]
Alternative 8
Error8.7
Cost6592
\[\tan x - x \]
Alternative 9
Error63.0
Cost128
\[-x \]

Error

Reproduce?

herbie shell --seed 1 
(FPCore (x)
  :name "tan(x)-x"
  :precision binary64
  :pre (and (<= 0.001 x) (<= x 1.5))
  (- (tan x) x))