Hyperbolic tangent

Percentage Accurate: 8.8% → 97.5%
Time: 9.7s
Alternatives: 8
Speedup: 409.0×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-x}\\ \frac{e^{x} - t\_0}{e^{x} + t\_0} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (exp (- x)))) (/ (- (exp x) t_0) (+ (exp x) t_0))))
double code(double x) {
	double t_0 = exp(-x);
	return (exp(x) - t_0) / (exp(x) + t_0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = exp(-x)
    code = (exp(x) - t_0) / (exp(x) + t_0)
end function
public static double code(double x) {
	double t_0 = Math.exp(-x);
	return (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
def code(x):
	t_0 = math.exp(-x)
	return (math.exp(x) - t_0) / (math.exp(x) + t_0)
function code(x)
	t_0 = exp(Float64(-x))
	return Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0))
end
function tmp = code(x)
	t_0 = exp(-x);
	tmp = (exp(x) - t_0) / (exp(x) + t_0);
end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t\_0}{e^{x} + t\_0}
\end{array}
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 8 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 8.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-x}\\ \frac{e^{x} - t\_0}{e^{x} + t\_0} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (exp (- x)))) (/ (- (exp x) t_0) (+ (exp x) t_0))))
double code(double x) {
	double t_0 = exp(-x);
	return (exp(x) - t_0) / (exp(x) + t_0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = exp(-x)
    code = (exp(x) - t_0) / (exp(x) + t_0)
end function
public static double code(double x) {
	double t_0 = Math.exp(-x);
	return (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
def code(x):
	t_0 = math.exp(-x)
	return (math.exp(x) - t_0) / (math.exp(x) + t_0)
function code(x)
	t_0 = exp(Float64(-x))
	return Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0))
end
function tmp = code(x)
	t_0 = exp(-x);
	tmp = (exp(x) - t_0) / (exp(x) + t_0);
end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t\_0}{e^{x} + t\_0}
\end{array}
\end{array}

Alternative 1: 97.5% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, 2 + {x}^{4} \cdot \left(0.016666666666666666 + {x}^{2} \cdot 0.0003968253968253968\right)\right)}{e^{x} + e^{-x}} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  (*
   x
   (fma
    (pow x 2.0)
    0.3333333333333333
    (+
     2.0
     (*
      (pow x 4.0)
      (+ 0.016666666666666666 (* (pow x 2.0) 0.0003968253968253968))))))
  (+ (exp x) (exp (- x)))))
double code(double x) {
	return (x * fma(pow(x, 2.0), 0.3333333333333333, (2.0 + (pow(x, 4.0) * (0.016666666666666666 + (pow(x, 2.0) * 0.0003968253968253968)))))) / (exp(x) + exp(-x));
}
function code(x)
	return Float64(Float64(x * fma((x ^ 2.0), 0.3333333333333333, Float64(2.0 + Float64((x ^ 4.0) * Float64(0.016666666666666666 + Float64((x ^ 2.0) * 0.0003968253968253968)))))) / Float64(exp(x) + exp(Float64(-x))))
end
code[x_] := N[(N[(x * N[(N[Power[x, 2.0], $MachinePrecision] * 0.3333333333333333 + N[(2.0 + N[(N[Power[x, 4.0], $MachinePrecision] * N[(0.016666666666666666 + N[(N[Power[x, 2.0], $MachinePrecision] * 0.0003968253968253968), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, 2 + {x}^{4} \cdot \left(0.016666666666666666 + {x}^{2} \cdot 0.0003968253968253968\right)\right)}{e^{x} + e^{-x}}
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)}}{e^{x} + e^{-x}} \]
  4. Step-by-step derivation
    1. +-commutative96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\left({x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)}}{e^{x} + e^{-x}} \]
    2. distribute-lft-in96.6%

      \[\leadsto \frac{x \cdot \left(\color{blue}{\left({x}^{2} \cdot 0.3333333333333333 + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)} + 2\right)}{e^{x} + e^{-x}} \]
    3. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(\left(\color{blue}{0.3333333333333333 \cdot {x}^{2}} + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right) + 2\right)}{e^{x} + e^{-x}} \]
    4. associate-+l+96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + \left({x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)\right)}}{e^{x} + e^{-x}} \]
    5. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(\color{blue}{{x}^{2} \cdot 0.3333333333333333} + \left({x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)\right)}{e^{x} + e^{-x}} \]
    6. fma-define96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, 0.3333333333333333, {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)}}{e^{x} + e^{-x}} \]
    7. associate-*r*96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left({x}^{2} \cdot {x}^{2}\right) \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)} + 2\right)}{e^{x} + e^{-x}} \]
    8. *-commutative96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right) \cdot \left({x}^{2} \cdot {x}^{2}\right)} + 2\right)}{e^{x} + e^{-x}} \]
    9. fma-define96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\mathsf{fma}\left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}, {x}^{2} \cdot {x}^{2}, 2\right)}\right)}{e^{x} + e^{-x}} \]
  5. Simplified96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \mathsf{fma}\left(\mathsf{fma}\left({x}^{2}, 0.0003968253968253968, 0.016666666666666666\right), {x}^{4}, 2\right)\right)}}{e^{x} + e^{-x}} \]
  6. Taylor expanded in x around 0 96.6%

    \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{2 + {x}^{4} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)}\right)}{e^{x} + e^{-x}} \]
  7. Step-by-step derivation
    1. +-commutative96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{{x}^{4} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right) + 2}\right)}{e^{x} + e^{-x}} \]
    2. fma-define96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\mathsf{fma}\left({x}^{4}, 0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}, 2\right)}\right)}{e^{x} + e^{-x}} \]
    3. metadata-eval96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \mathsf{fma}\left({x}^{\color{blue}{\left(2 \cdot 2\right)}}, 0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}, 2\right)\right)}{e^{x} + e^{-x}} \]
    4. pow-sqr96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot {x}^{2}}, 0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}, 2\right)\right)}{e^{x} + e^{-x}} \]
    5. fma-define96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left({x}^{2} \cdot {x}^{2}\right) \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right) + 2}\right)}{e^{x} + e^{-x}} \]
    6. associate-*r*96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{{x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)} + 2\right)}{e^{x} + e^{-x}} \]
    7. +-commutative96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{2 + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)}\right)}{e^{x} + e^{-x}} \]
    8. associate-*r*96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, 2 + \color{blue}{\left({x}^{2} \cdot {x}^{2}\right) \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)}\right)}{e^{x} + e^{-x}} \]
    9. pow-sqr96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, 2 + \color{blue}{{x}^{\left(2 \cdot 2\right)}} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)}{e^{x} + e^{-x}} \]
    10. metadata-eval96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, 2 + {x}^{\color{blue}{4}} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)}{e^{x} + e^{-x}} \]
    11. *-commutative96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, 2 + {x}^{4} \cdot \left(0.016666666666666666 + \color{blue}{{x}^{2} \cdot 0.0003968253968253968}\right)\right)}{e^{x} + e^{-x}} \]
  8. Simplified96.6%

    \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{2 + {x}^{4} \cdot \left(0.016666666666666666 + {x}^{2} \cdot 0.0003968253968253968\right)}\right)}{e^{x} + e^{-x}} \]
  9. Add Preprocessing

Alternative 2: 97.5% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \frac{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + {x}^{2} \cdot 0.0003968253968253968\right)\right)\right)}{e^{x} + e^{-x}} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  (*
   x
   (+
    2.0
    (*
     (pow x 2.0)
     (+
      0.3333333333333333
      (*
       (pow x 2.0)
       (+ 0.016666666666666666 (* (pow x 2.0) 0.0003968253968253968)))))))
  (+ (exp x) (exp (- x)))))
double code(double x) {
	return (x * (2.0 + (pow(x, 2.0) * (0.3333333333333333 + (pow(x, 2.0) * (0.016666666666666666 + (pow(x, 2.0) * 0.0003968253968253968))))))) / (exp(x) + exp(-x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (x * (2.0d0 + ((x ** 2.0d0) * (0.3333333333333333d0 + ((x ** 2.0d0) * (0.016666666666666666d0 + ((x ** 2.0d0) * 0.0003968253968253968d0))))))) / (exp(x) + exp(-x))
end function
public static double code(double x) {
	return (x * (2.0 + (Math.pow(x, 2.0) * (0.3333333333333333 + (Math.pow(x, 2.0) * (0.016666666666666666 + (Math.pow(x, 2.0) * 0.0003968253968253968))))))) / (Math.exp(x) + Math.exp(-x));
}
def code(x):
	return (x * (2.0 + (math.pow(x, 2.0) * (0.3333333333333333 + (math.pow(x, 2.0) * (0.016666666666666666 + (math.pow(x, 2.0) * 0.0003968253968253968))))))) / (math.exp(x) + math.exp(-x))
function code(x)
	return Float64(Float64(x * Float64(2.0 + Float64((x ^ 2.0) * Float64(0.3333333333333333 + Float64((x ^ 2.0) * Float64(0.016666666666666666 + Float64((x ^ 2.0) * 0.0003968253968253968))))))) / Float64(exp(x) + exp(Float64(-x))))
end
function tmp = code(x)
	tmp = (x * (2.0 + ((x ^ 2.0) * (0.3333333333333333 + ((x ^ 2.0) * (0.016666666666666666 + ((x ^ 2.0) * 0.0003968253968253968))))))) / (exp(x) + exp(-x));
end
code[x_] := N[(N[(x * N[(2.0 + N[(N[Power[x, 2.0], $MachinePrecision] * N[(0.3333333333333333 + N[(N[Power[x, 2.0], $MachinePrecision] * N[(0.016666666666666666 + N[(N[Power[x, 2.0], $MachinePrecision] * 0.0003968253968253968), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + {x}^{2} \cdot 0.0003968253968253968\right)\right)\right)}{e^{x} + e^{-x}}
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)}}{e^{x} + e^{-x}} \]
  4. Step-by-step derivation
    1. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + \color{blue}{{x}^{2} \cdot 0.0003968253968253968}\right)\right)\right)}{e^{x} + e^{-x}} \]
  5. Simplified96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + {x}^{2} \cdot 0.0003968253968253968\right)\right)\right)}}{e^{x} + e^{-x}} \]
  6. Add Preprocessing

Alternative 3: 97.4% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, {x}^{4} \cdot 0.08333333333333333\right)} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  (* x (+ 2.0 (* (pow x 2.0) 0.3333333333333333)))
  (+ 2.0 (fma x x (* (pow x 4.0) 0.08333333333333333)))))
double code(double x) {
	return (x * (2.0 + (pow(x, 2.0) * 0.3333333333333333))) / (2.0 + fma(x, x, (pow(x, 4.0) * 0.08333333333333333)));
}
function code(x)
	return Float64(Float64(x * Float64(2.0 + Float64((x ^ 2.0) * 0.3333333333333333))) / Float64(2.0 + fma(x, x, Float64((x ^ 4.0) * 0.08333333333333333))))
end
code[x_] := N[(N[(x * N[(2.0 + N[(N[Power[x, 2.0], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 + N[(x * x + N[(N[Power[x, 4.0], $MachinePrecision] * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, {x}^{4} \cdot 0.08333333333333333\right)}
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.2%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + 0.3333333333333333 \cdot {x}^{2}\right)}}{e^{x} + e^{-x}} \]
  4. Step-by-step derivation
    1. *-commutative96.2%

      \[\leadsto \frac{x \cdot \left(2 + \color{blue}{{x}^{2} \cdot 0.3333333333333333}\right)}{e^{x} + e^{-x}} \]
  5. Simplified96.2%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}}{e^{x} + e^{-x}} \]
  6. Taylor expanded in x around 0 96.5%

    \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{2 + {x}^{2} \cdot \left(1 + 0.08333333333333333 \cdot {x}^{2}\right)}} \]
  7. Step-by-step derivation
    1. *-commutative96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + {x}^{2} \cdot \left(1 + \color{blue}{{x}^{2} \cdot 0.08333333333333333}\right)} \]
  8. Simplified96.5%

    \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{2 + {x}^{2} \cdot \left(1 + {x}^{2} \cdot 0.08333333333333333\right)}} \]
  9. Step-by-step derivation
    1. distribute-rgt-in96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \color{blue}{\left(1 \cdot {x}^{2} + \left({x}^{2} \cdot 0.08333333333333333\right) \cdot {x}^{2}\right)}} \]
    2. *-un-lft-identity96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \left(\color{blue}{{x}^{2}} + \left({x}^{2} \cdot 0.08333333333333333\right) \cdot {x}^{2}\right)} \]
    3. unpow296.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \left(\color{blue}{x \cdot x} + \left({x}^{2} \cdot 0.08333333333333333\right) \cdot {x}^{2}\right)} \]
    4. fma-define96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \color{blue}{\mathsf{fma}\left(x, x, \left({x}^{2} \cdot 0.08333333333333333\right) \cdot {x}^{2}\right)}} \]
    5. *-commutative96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, \color{blue}{\left(0.08333333333333333 \cdot {x}^{2}\right)} \cdot {x}^{2}\right)} \]
    6. associate-*l*96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, \color{blue}{0.08333333333333333 \cdot \left({x}^{2} \cdot {x}^{2}\right)}\right)} \]
    7. pow-prod-up96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \color{blue}{{x}^{\left(2 + 2\right)}}\right)} \]
    8. metadata-eval96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{\color{blue}{4}}\right)} \]
  10. Applied egg-rr96.5%

    \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)}} \]
  11. Final simplification96.5%

    \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{2 + \mathsf{fma}\left(x, x, {x}^{4} \cdot 0.08333333333333333\right)} \]
  12. Add Preprocessing

Alternative 4: 97.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ x \cdot \left(1 + {x}^{2} \cdot \left({x}^{2} \cdot 0.13333333333333333 - 0.3333333333333333\right)\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (*
  x
  (+
   1.0
   (*
    (pow x 2.0)
    (- (* (pow x 2.0) 0.13333333333333333) 0.3333333333333333)))))
double code(double x) {
	return x * (1.0 + (pow(x, 2.0) * ((pow(x, 2.0) * 0.13333333333333333) - 0.3333333333333333)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x * (1.0d0 + ((x ** 2.0d0) * (((x ** 2.0d0) * 0.13333333333333333d0) - 0.3333333333333333d0)))
end function
public static double code(double x) {
	return x * (1.0 + (Math.pow(x, 2.0) * ((Math.pow(x, 2.0) * 0.13333333333333333) - 0.3333333333333333)));
}
def code(x):
	return x * (1.0 + (math.pow(x, 2.0) * ((math.pow(x, 2.0) * 0.13333333333333333) - 0.3333333333333333)))
function code(x)
	return Float64(x * Float64(1.0 + Float64((x ^ 2.0) * Float64(Float64((x ^ 2.0) * 0.13333333333333333) - 0.3333333333333333))))
end
function tmp = code(x)
	tmp = x * (1.0 + ((x ^ 2.0) * (((x ^ 2.0) * 0.13333333333333333) - 0.3333333333333333)));
end
code[x_] := N[(x * N[(1.0 + N[(N[Power[x, 2.0], $MachinePrecision] * N[(N[(N[Power[x, 2.0], $MachinePrecision] * 0.13333333333333333), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(1 + {x}^{2} \cdot \left({x}^{2} \cdot 0.13333333333333333 - 0.3333333333333333\right)\right)
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.5%

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(0.13333333333333333 \cdot {x}^{2} - 0.3333333333333333\right)\right)} \]
  4. Final simplification96.5%

    \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left({x}^{2} \cdot 0.13333333333333333 - 0.3333333333333333\right)\right) \]
  5. Add Preprocessing

Alternative 5: 97.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\mathsf{fma}\left(x, x, 2\right)} \end{array} \]
(FPCore (x)
 :precision binary64
 (/ (* x (+ 2.0 (* (pow x 2.0) 0.3333333333333333))) (fma x x 2.0)))
double code(double x) {
	return (x * (2.0 + (pow(x, 2.0) * 0.3333333333333333))) / fma(x, x, 2.0);
}
function code(x)
	return Float64(Float64(x * Float64(2.0 + Float64((x ^ 2.0) * 0.3333333333333333))) / fma(x, x, 2.0))
end
code[x_] := N[(N[(x * N[(2.0 + N[(N[Power[x, 2.0], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x + 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\mathsf{fma}\left(x, x, 2\right)}
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.2%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + 0.3333333333333333 \cdot {x}^{2}\right)}}{e^{x} + e^{-x}} \]
  4. Step-by-step derivation
    1. *-commutative96.2%

      \[\leadsto \frac{x \cdot \left(2 + \color{blue}{{x}^{2} \cdot 0.3333333333333333}\right)}{e^{x} + e^{-x}} \]
  5. Simplified96.2%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}}{e^{x} + e^{-x}} \]
  6. Taylor expanded in x around 0 96.5%

    \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{2 + {x}^{2}}} \]
  7. Step-by-step derivation
    1. +-commutative96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{{x}^{2} + 2}} \]
    2. unpow296.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{x \cdot x} + 2} \]
    3. fma-define96.5%

      \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{\mathsf{fma}\left(x, x, 2\right)}} \]
  8. Simplified96.5%

    \[\leadsto \frac{x \cdot \left(2 + {x}^{2} \cdot 0.3333333333333333\right)}{\color{blue}{\mathsf{fma}\left(x, x, 2\right)}} \]
  9. Add Preprocessing

Alternative 6: 97.0% accurate, 3.9× speedup?

\[\begin{array}{l} \\ x + -0.3333333333333333 \cdot {x}^{3} \end{array} \]
(FPCore (x) :precision binary64 (+ x (* -0.3333333333333333 (pow x 3.0))))
double code(double x) {
	return x + (-0.3333333333333333 * pow(x, 3.0));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x + ((-0.3333333333333333d0) * (x ** 3.0d0))
end function
public static double code(double x) {
	return x + (-0.3333333333333333 * Math.pow(x, 3.0));
}
def code(x):
	return x + (-0.3333333333333333 * math.pow(x, 3.0))
function code(x)
	return Float64(x + Float64(-0.3333333333333333 * (x ^ 3.0)))
end
function tmp = code(x)
	tmp = x + (-0.3333333333333333 * (x ^ 3.0));
end
code[x_] := N[(x + N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + -0.3333333333333333 \cdot {x}^{3}
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)}}{e^{x} + e^{-x}} \]
  4. Step-by-step derivation
    1. +-commutative96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\left({x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)}}{e^{x} + e^{-x}} \]
    2. distribute-lft-in96.6%

      \[\leadsto \frac{x \cdot \left(\color{blue}{\left({x}^{2} \cdot 0.3333333333333333 + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)} + 2\right)}{e^{x} + e^{-x}} \]
    3. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(\left(\color{blue}{0.3333333333333333 \cdot {x}^{2}} + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right) + 2\right)}{e^{x} + e^{-x}} \]
    4. associate-+l+96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + \left({x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)\right)}}{e^{x} + e^{-x}} \]
    5. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(\color{blue}{{x}^{2} \cdot 0.3333333333333333} + \left({x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)\right)}{e^{x} + e^{-x}} \]
    6. fma-define96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, 0.3333333333333333, {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)}}{e^{x} + e^{-x}} \]
    7. associate-*r*96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left({x}^{2} \cdot {x}^{2}\right) \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)} + 2\right)}{e^{x} + e^{-x}} \]
    8. *-commutative96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right) \cdot \left({x}^{2} \cdot {x}^{2}\right)} + 2\right)}{e^{x} + e^{-x}} \]
    9. fma-define96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\mathsf{fma}\left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}, {x}^{2} \cdot {x}^{2}, 2\right)}\right)}{e^{x} + e^{-x}} \]
  5. Simplified96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \mathsf{fma}\left(\mathsf{fma}\left({x}^{2}, 0.0003968253968253968, 0.016666666666666666\right), {x}^{4}, 2\right)\right)}}{e^{x} + e^{-x}} \]
  6. Taylor expanded in x around 0 96.1%

    \[\leadsto \color{blue}{x \cdot \left(1 + -0.3333333333333333 \cdot {x}^{2}\right)} \]
  7. Step-by-step derivation
    1. distribute-rgt-in96.1%

      \[\leadsto \color{blue}{1 \cdot x + \left(-0.3333333333333333 \cdot {x}^{2}\right) \cdot x} \]
    2. *-lft-identity96.1%

      \[\leadsto \color{blue}{x} + \left(-0.3333333333333333 \cdot {x}^{2}\right) \cdot x \]
    3. associate-*r*96.1%

      \[\leadsto x + \color{blue}{-0.3333333333333333 \cdot \left({x}^{2} \cdot x\right)} \]
    4. unpow296.1%

      \[\leadsto x + -0.3333333333333333 \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot x\right) \]
    5. unpow396.1%

      \[\leadsto x + -0.3333333333333333 \cdot \color{blue}{{x}^{3}} \]
  8. Simplified96.1%

    \[\leadsto \color{blue}{x + -0.3333333333333333 \cdot {x}^{3}} \]
  9. Add Preprocessing

Alternative 7: 96.9% accurate, 409.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x) :precision binary64 x)
double code(double x) {
	return x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x
end function
public static double code(double x) {
	return x;
}
def code(x):
	return x
function code(x)
	return x
end
function tmp = code(x)
	tmp = x;
end
code[x_] := x
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.1%

    \[\leadsto \color{blue}{x} \]
  4. Add Preprocessing

Alternative 8: 4.0% accurate, 409.0× speedup?

\[\begin{array}{l} \\ 1.3503968253968255 \end{array} \]
(FPCore (x) :precision binary64 1.3503968253968255)
double code(double x) {
	return 1.3503968253968255;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.3503968253968255d0
end function
public static double code(double x) {
	return 1.3503968253968255;
}
def code(x):
	return 1.3503968253968255
function code(x)
	return 1.3503968253968255
end
function tmp = code(x)
	tmp = 1.3503968253968255;
end
code[x_] := 1.3503968253968255
\begin{array}{l}

\\
1.3503968253968255
\end{array}
Derivation
  1. Initial program 9.1%

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \left(2 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)}}{e^{x} + e^{-x}} \]
  4. Step-by-step derivation
    1. +-commutative96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\left({x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)}}{e^{x} + e^{-x}} \]
    2. distribute-lft-in96.6%

      \[\leadsto \frac{x \cdot \left(\color{blue}{\left({x}^{2} \cdot 0.3333333333333333 + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right)} + 2\right)}{e^{x} + e^{-x}} \]
    3. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(\left(\color{blue}{0.3333333333333333 \cdot {x}^{2}} + {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right)\right) + 2\right)}{e^{x} + e^{-x}} \]
    4. associate-+l+96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + \left({x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)\right)}}{e^{x} + e^{-x}} \]
    5. *-commutative96.6%

      \[\leadsto \frac{x \cdot \left(\color{blue}{{x}^{2} \cdot 0.3333333333333333} + \left({x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)\right)}{e^{x} + e^{-x}} \]
    6. fma-define96.6%

      \[\leadsto \frac{x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, 0.3333333333333333, {x}^{2} \cdot \left({x}^{2} \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)\right) + 2\right)}}{e^{x} + e^{-x}} \]
    7. associate-*r*96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left({x}^{2} \cdot {x}^{2}\right) \cdot \left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right)} + 2\right)}{e^{x} + e^{-x}} \]
    8. *-commutative96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}\right) \cdot \left({x}^{2} \cdot {x}^{2}\right)} + 2\right)}{e^{x} + e^{-x}} \]
    9. fma-define96.6%

      \[\leadsto \frac{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \color{blue}{\mathsf{fma}\left(0.016666666666666666 + 0.0003968253968253968 \cdot {x}^{2}, {x}^{2} \cdot {x}^{2}, 2\right)}\right)}{e^{x} + e^{-x}} \]
  5. Simplified96.6%

    \[\leadsto \frac{\color{blue}{x \cdot \mathsf{fma}\left({x}^{2}, 0.3333333333333333, \mathsf{fma}\left(\mathsf{fma}\left({x}^{2}, 0.0003968253968253968, 0.016666666666666666\right), {x}^{4}, 2\right)\right)}}{e^{x} + e^{-x}} \]
  6. Applied egg-rr4.2%

    \[\leadsto \color{blue}{1.3503968253968255} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2024130 
(FPCore (x)
  :name "Hyperbolic tangent"
  :precision binary64
  (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))