Hyperbolic tangent

Percentage Accurate: 8.9% → 100.0%
Time: 12.4s
Alternatives: 5
Speedup: 70.3×

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 5 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.9% 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: 100.0% accurate, 4.2× speedup?

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

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

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. tanh-undefN/A

      \[\leadsto \color{blue}{\tanh x} \]
    2. lower-tanh.f64100.0

      \[\leadsto \color{blue}{\tanh x} \]
  4. Applied egg-rr100.0%

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

Alternative 2: 97.6% accurate, 10.8× speedup?

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

\\
\mathsf{fma}\left(\frac{0.1111111111111111}{\mathsf{fma}\left(x \cdot x, -0.13333333333333333, -0.3333333333333333\right)}, x \cdot \left(x \cdot x\right), x\right)
\end{array}
Derivation
  1. Initial program 8.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)\right)} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) + 1\right)} \]
    2. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)\right) \cdot x + 1 \cdot x} \]
    3. associate-*l*N/A

      \[\leadsto \color{blue}{{x}^{2} \cdot \left(\left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x\right)} + 1 \cdot x \]
    4. *-lft-identityN/A

      \[\leadsto {x}^{2} \cdot \left(\left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x\right) + \color{blue}{x} \]
    5. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right)} \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right) \]
    7. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right) \]
    8. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)}, x\right) \]
    9. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)}, x\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\left(\frac{2}{15} \cdot {x}^{2} + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right)}, x\right) \]
    11. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left(\color{blue}{{x}^{2} \cdot \frac{2}{15}} + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right), x\right) \]
    12. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left({x}^{2} \cdot \frac{2}{15} + \color{blue}{\frac{-1}{3}}\right), x\right) \]
    13. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{2}{15}, \frac{-1}{3}\right)}, x\right) \]
    14. unpow2N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{2}{15}, \frac{-1}{3}\right), x\right) \]
    15. lower-*.f6497.3

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.13333333333333333, -0.3333333333333333\right), x\right) \]
  5. Simplified97.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(x \cdot x, 0.13333333333333333, -0.3333333333333333\right), x\right)} \]
  6. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(x \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15} + \frac{-1}{3}\right)\right) + x \]
    2. lift-*.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \left(x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot \frac{2}{15} + \frac{-1}{3}\right)\right) + x \]
    3. lift-fma.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \left(x \cdot \color{blue}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)}\right) + x \]
    4. lift-*.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)\right)} + x \]
    5. lift-*.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)\right)} + x \]
    6. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot x\right) \cdot x\right) \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)} + x \]
    7. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{\left(x \cdot x\right)} \cdot x\right) \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right) + x \]
    8. pow3N/A

      \[\leadsto \color{blue}{{x}^{3}} \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right) + x \]
    9. lift-fma.f64N/A

      \[\leadsto {x}^{3} \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot \frac{2}{15} + \frac{-1}{3}\right)} + x \]
    10. flip-+N/A

      \[\leadsto {x}^{3} \cdot \color{blue}{\frac{\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}} + x \]
    11. associate-*r/N/A

      \[\leadsto \color{blue}{\frac{{x}^{3} \cdot \left(\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}\right)}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}} + x \]
    12. div-invN/A

      \[\leadsto \color{blue}{\left({x}^{3} \cdot \left(\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}\right)\right) \cdot \frac{1}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}} + x \]
    13. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{3} \cdot \left(\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}\right), \frac{1}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}, x\right)} \]
  7. Applied egg-rr97.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \mathsf{fma}\left(x \cdot x, \left(x \cdot x\right) \cdot 0.017777777777777778, -0.1111111111111111\right), \frac{1}{\mathsf{fma}\left(x \cdot x, 0.13333333333333333, 0.3333333333333333\right)}, x\right)} \]
  8. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-1}{9} \cdot {x}^{3}}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
  9. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{3} \cdot \frac{-1}{9}}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    2. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{3} \cdot \frac{-1}{9}}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    3. cube-multN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot \left(x \cdot x\right)\right)} \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    4. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot \color{blue}{{x}^{2}}\right) \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    5. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot {x}^{2}\right)} \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot \color{blue}{\left(x \cdot x\right)}\right) \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    7. lower-*.f6497.5

      \[\leadsto \mathsf{fma}\left(\left(x \cdot \color{blue}{\left(x \cdot x\right)}\right) \cdot -0.1111111111111111, \frac{1}{\mathsf{fma}\left(x \cdot x, 0.13333333333333333, 0.3333333333333333\right)}, x\right) \]
  10. Simplified97.5%

    \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot \left(x \cdot x\right)\right) \cdot -0.1111111111111111}, \frac{1}{\mathsf{fma}\left(x \cdot x, 0.13333333333333333, 0.3333333333333333\right)}, x\right) \]
  11. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \left(\left(x \cdot \color{blue}{\left(x \cdot x\right)}\right) \cdot \frac{-1}{9}\right) \cdot \frac{1}{\left(x \cdot x\right) \cdot \frac{2}{15} + \frac{1}{3}} + x \]
    2. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{\left(x \cdot \left(x \cdot x\right)\right)} \cdot \frac{-1}{9}\right) \cdot \frac{1}{\left(x \cdot x\right) \cdot \frac{2}{15} + \frac{1}{3}} + x \]
    3. lift-*.f64N/A

      \[\leadsto \left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \frac{-1}{9}\right) \cdot \frac{1}{\color{blue}{\left(x \cdot x\right)} \cdot \frac{2}{15} + \frac{1}{3}} + x \]
    4. lift-fma.f64N/A

      \[\leadsto \left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \frac{-1}{9}\right) \cdot \frac{1}{\color{blue}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}} + x \]
    5. lift-/.f64N/A

      \[\leadsto \left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \frac{-1}{9}\right) \cdot \color{blue}{\frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}} + x \]
    6. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \frac{-1}{9}\right)} \cdot \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)} + x \]
    7. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \frac{-1}{9}\right)} \cdot \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)} + x \]
    8. associate-*l*N/A

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot x\right)\right) \cdot \left(\frac{-1}{9} \cdot \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}\right)} + x \]
    9. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\frac{-1}{9} \cdot \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}\right) \cdot \left(x \cdot \left(x \cdot x\right)\right)} + x \]
    10. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{-1}{9} \cdot \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x \cdot \left(x \cdot x\right), x\right)} \]
  12. Applied egg-rr97.5%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{0.1111111111111111}{\mathsf{fma}\left(x \cdot x, -0.13333333333333333, -0.3333333333333333\right)}, x \cdot \left(x \cdot x\right), x\right)} \]
  13. Add Preprocessing

Alternative 3: 97.4% accurate, 15.1× speedup?

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

\\
\mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(x \cdot 0.13333333333333333, x, -0.3333333333333333\right), x\right)
\end{array}
Derivation
  1. Initial program 8.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)\right)} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) + 1\right)} \]
    2. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)\right) \cdot x + 1 \cdot x} \]
    3. associate-*l*N/A

      \[\leadsto \color{blue}{{x}^{2} \cdot \left(\left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x\right)} + 1 \cdot x \]
    4. *-lft-identityN/A

      \[\leadsto {x}^{2} \cdot \left(\left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x\right) + \color{blue}{x} \]
    5. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right)} \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right) \]
    7. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right) \]
    8. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)}, x\right) \]
    9. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)}, x\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\left(\frac{2}{15} \cdot {x}^{2} + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right)}, x\right) \]
    11. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left(\color{blue}{{x}^{2} \cdot \frac{2}{15}} + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right), x\right) \]
    12. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left({x}^{2} \cdot \frac{2}{15} + \color{blue}{\frac{-1}{3}}\right), x\right) \]
    13. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{2}{15}, \frac{-1}{3}\right)}, x\right) \]
    14. unpow2N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{2}{15}, \frac{-1}{3}\right), x\right) \]
    15. lower-*.f6497.3

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.13333333333333333, -0.3333333333333333\right), x\right) \]
  5. Simplified97.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(x \cdot x, 0.13333333333333333, -0.3333333333333333\right), x\right)} \]
  6. Step-by-step derivation
    1. associate-*l*N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left(\color{blue}{x \cdot \left(x \cdot \frac{2}{15}\right)} + \frac{-1}{3}\right), x\right) \]
    2. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left(\color{blue}{\left(x \cdot \frac{2}{15}\right) \cdot x} + \frac{-1}{3}\right), x\right) \]
    3. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\mathsf{fma}\left(x \cdot \frac{2}{15}, x, \frac{-1}{3}\right)}, x\right) \]
    4. lower-*.f6497.3

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot 0.13333333333333333}, x, -0.3333333333333333\right), x\right) \]
  7. Applied egg-rr97.3%

    \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\mathsf{fma}\left(x \cdot 0.13333333333333333, x, -0.3333333333333333\right)}, x\right) \]
  8. Add Preprocessing

Alternative 4: 97.0% accurate, 24.8× speedup?

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

\\
\mathsf{fma}\left(x, \left(x \cdot x\right) \cdot -0.3333333333333333, x\right)
\end{array}
Derivation
  1. Initial program 8.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + \frac{-1}{3} \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(\frac{-1}{3} \cdot {x}^{2} + 1\right)} \]
    2. distribute-lft-inN/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{-1}{3} \cdot {x}^{2}\right) + x \cdot 1} \]
    3. *-rgt-identityN/A

      \[\leadsto x \cdot \left(\frac{-1}{3} \cdot {x}^{2}\right) + \color{blue}{x} \]
    4. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{-1}{3} \cdot {x}^{2}, x\right)} \]
    5. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{-1}{3} \cdot {x}^{2}}, x\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(x, \frac{-1}{3} \cdot \color{blue}{\left(x \cdot x\right)}, x\right) \]
    7. lower-*.f6497.0

      \[\leadsto \mathsf{fma}\left(x, -0.3333333333333333 \cdot \color{blue}{\left(x \cdot x\right)}, x\right) \]
  5. Simplified97.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, -0.3333333333333333 \cdot \left(x \cdot x\right), x\right)} \]
  6. Final simplification97.0%

    \[\leadsto \mathsf{fma}\left(x, \left(x \cdot x\right) \cdot -0.3333333333333333, x\right) \]
  7. Add Preprocessing

Alternative 5: 16.6% accurate, 70.3× speedup?

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

\\
x \cdot 0.16666666666666666
\end{array}
Derivation
  1. Initial program 8.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)\right)} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) + 1\right)} \]
    2. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)\right) \cdot x + 1 \cdot x} \]
    3. associate-*l*N/A

      \[\leadsto \color{blue}{{x}^{2} \cdot \left(\left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x\right)} + 1 \cdot x \]
    4. *-lft-identityN/A

      \[\leadsto {x}^{2} \cdot \left(\left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x\right) + \color{blue}{x} \]
    5. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right)} \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right) \]
    7. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right) \cdot x, x\right) \]
    8. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)}, x\right) \]
    9. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, \color{blue}{x \cdot \left(\frac{2}{15} \cdot {x}^{2} - \frac{1}{3}\right)}, x\right) \]
    10. sub-negN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\left(\frac{2}{15} \cdot {x}^{2} + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right)}, x\right) \]
    11. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left(\color{blue}{{x}^{2} \cdot \frac{2}{15}} + \left(\mathsf{neg}\left(\frac{1}{3}\right)\right)\right), x\right) \]
    12. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \left({x}^{2} \cdot \frac{2}{15} + \color{blue}{\frac{-1}{3}}\right), x\right) \]
    13. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{2}{15}, \frac{-1}{3}\right)}, x\right) \]
    14. unpow2N/A

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{2}{15}, \frac{-1}{3}\right), x\right) \]
    15. lower-*.f6497.3

      \[\leadsto \mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.13333333333333333, -0.3333333333333333\right), x\right) \]
  5. Simplified97.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, x \cdot \mathsf{fma}\left(x \cdot x, 0.13333333333333333, -0.3333333333333333\right), x\right)} \]
  6. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(x \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15} + \frac{-1}{3}\right)\right) + x \]
    2. lift-*.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \left(x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot \frac{2}{15} + \frac{-1}{3}\right)\right) + x \]
    3. lift-fma.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \left(x \cdot \color{blue}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)}\right) + x \]
    4. lift-*.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)\right)} + x \]
    5. lift-*.f64N/A

      \[\leadsto \left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)\right)} + x \]
    6. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot x\right) \cdot x\right) \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right)} + x \]
    7. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{\left(x \cdot x\right)} \cdot x\right) \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right) + x \]
    8. pow3N/A

      \[\leadsto \color{blue}{{x}^{3}} \cdot \mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{-1}{3}\right) + x \]
    9. lift-fma.f64N/A

      \[\leadsto {x}^{3} \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot \frac{2}{15} + \frac{-1}{3}\right)} + x \]
    10. flip-+N/A

      \[\leadsto {x}^{3} \cdot \color{blue}{\frac{\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}} + x \]
    11. associate-*r/N/A

      \[\leadsto \color{blue}{\frac{{x}^{3} \cdot \left(\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}\right)}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}} + x \]
    12. div-invN/A

      \[\leadsto \color{blue}{\left({x}^{3} \cdot \left(\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}\right)\right) \cdot \frac{1}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}} + x \]
    13. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{3} \cdot \left(\left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) \cdot \left(\left(x \cdot x\right) \cdot \frac{2}{15}\right) - \frac{-1}{3} \cdot \frac{-1}{3}\right), \frac{1}{\left(x \cdot x\right) \cdot \frac{2}{15} - \frac{-1}{3}}, x\right)} \]
  7. Applied egg-rr97.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\left(x \cdot \left(x \cdot x\right)\right) \cdot \mathsf{fma}\left(x \cdot x, \left(x \cdot x\right) \cdot 0.017777777777777778, -0.1111111111111111\right), \frac{1}{\mathsf{fma}\left(x \cdot x, 0.13333333333333333, 0.3333333333333333\right)}, x\right)} \]
  8. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-1}{9} \cdot {x}^{3}}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
  9. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{3} \cdot \frac{-1}{9}}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    2. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{3} \cdot \frac{-1}{9}}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    3. cube-multN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot \left(x \cdot x\right)\right)} \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    4. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot \color{blue}{{x}^{2}}\right) \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    5. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot {x}^{2}\right)} \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot \color{blue}{\left(x \cdot x\right)}\right) \cdot \frac{-1}{9}, \frac{1}{\mathsf{fma}\left(x \cdot x, \frac{2}{15}, \frac{1}{3}\right)}, x\right) \]
    7. lower-*.f6497.5

      \[\leadsto \mathsf{fma}\left(\left(x \cdot \color{blue}{\left(x \cdot x\right)}\right) \cdot -0.1111111111111111, \frac{1}{\mathsf{fma}\left(x \cdot x, 0.13333333333333333, 0.3333333333333333\right)}, x\right) \]
  10. Simplified97.5%

    \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot \left(x \cdot x\right)\right) \cdot -0.1111111111111111}, \frac{1}{\mathsf{fma}\left(x \cdot x, 0.13333333333333333, 0.3333333333333333\right)}, x\right) \]
  11. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{1}{6} \cdot x} \]
  12. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{x \cdot \frac{1}{6}} \]
    2. lower-*.f6416.5

      \[\leadsto \color{blue}{x \cdot 0.16666666666666666} \]
  13. Simplified16.5%

    \[\leadsto \color{blue}{x \cdot 0.16666666666666666} \]
  14. Add Preprocessing

Reproduce

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