?

Average Error: 58.0 → 0.9
Time: 37.8s
Precision: binary64
Cost: 46468

?

\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
\[\begin{array}{l} t_0 := e^{-x}\\ t_1 := e^{x} + t_0\\ \mathbf{if}\;\frac{e^{x} - t_0}{t_1} \leq 0.0005:\\ \;\;\;\;\left(x + {x}^{5} \cdot 0.13333333333333333\right) + \left({x}^{3} \cdot -0.3333333333333333 + {x}^{7} \cdot -0.05396825396825397\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \frac{e^{x}}{t_1} - 1\\ \end{array} \]
(FPCore (x)
 :precision binary64
 (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (exp (- x))) (t_1 (+ (exp x) t_0)))
   (if (<= (/ (- (exp x) t_0) t_1) 0.0005)
     (+
      (+ x (* (pow x 5.0) 0.13333333333333333))
      (+
       (* (pow x 3.0) -0.3333333333333333)
       (* (pow x 7.0) -0.05396825396825397)))
     (- (* 2.0 (/ (exp x) t_1)) 1.0))))
double code(double x) {
	return (exp(x) - exp(-x)) / (exp(x) + exp(-x));
}
double code(double x) {
	double t_0 = exp(-x);
	double t_1 = exp(x) + t_0;
	double tmp;
	if (((exp(x) - t_0) / t_1) <= 0.0005) {
		tmp = (x + (pow(x, 5.0) * 0.13333333333333333)) + ((pow(x, 3.0) * -0.3333333333333333) + (pow(x, 7.0) * -0.05396825396825397));
	} else {
		tmp = (2.0 * (exp(x) / t_1)) - 1.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = exp(-x)
    t_1 = exp(x) + t_0
    if (((exp(x) - t_0) / t_1) <= 0.0005d0) then
        tmp = (x + ((x ** 5.0d0) * 0.13333333333333333d0)) + (((x ** 3.0d0) * (-0.3333333333333333d0)) + ((x ** 7.0d0) * (-0.05396825396825397d0)))
    else
        tmp = (2.0d0 * (exp(x) / t_1)) - 1.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
}
public static double code(double x) {
	double t_0 = Math.exp(-x);
	double t_1 = Math.exp(x) + t_0;
	double tmp;
	if (((Math.exp(x) - t_0) / t_1) <= 0.0005) {
		tmp = (x + (Math.pow(x, 5.0) * 0.13333333333333333)) + ((Math.pow(x, 3.0) * -0.3333333333333333) + (Math.pow(x, 7.0) * -0.05396825396825397));
	} else {
		tmp = (2.0 * (Math.exp(x) / t_1)) - 1.0;
	}
	return tmp;
}
def code(x):
	return (math.exp(x) - math.exp(-x)) / (math.exp(x) + math.exp(-x))
def code(x):
	t_0 = math.exp(-x)
	t_1 = math.exp(x) + t_0
	tmp = 0
	if ((math.exp(x) - t_0) / t_1) <= 0.0005:
		tmp = (x + (math.pow(x, 5.0) * 0.13333333333333333)) + ((math.pow(x, 3.0) * -0.3333333333333333) + (math.pow(x, 7.0) * -0.05396825396825397))
	else:
		tmp = (2.0 * (math.exp(x) / t_1)) - 1.0
	return tmp
function code(x)
	return Float64(Float64(exp(x) - exp(Float64(-x))) / Float64(exp(x) + exp(Float64(-x))))
end
function code(x)
	t_0 = exp(Float64(-x))
	t_1 = Float64(exp(x) + t_0)
	tmp = 0.0
	if (Float64(Float64(exp(x) - t_0) / t_1) <= 0.0005)
		tmp = Float64(Float64(x + Float64((x ^ 5.0) * 0.13333333333333333)) + Float64(Float64((x ^ 3.0) * -0.3333333333333333) + Float64((x ^ 7.0) * -0.05396825396825397)));
	else
		tmp = Float64(Float64(2.0 * Float64(exp(x) / t_1)) - 1.0);
	end
	return tmp
end
function tmp = code(x)
	tmp = (exp(x) - exp(-x)) / (exp(x) + exp(-x));
end
function tmp_2 = code(x)
	t_0 = exp(-x);
	t_1 = exp(x) + t_0;
	tmp = 0.0;
	if (((exp(x) - t_0) / t_1) <= 0.0005)
		tmp = (x + ((x ^ 5.0) * 0.13333333333333333)) + (((x ^ 3.0) * -0.3333333333333333) + ((x ^ 7.0) * -0.05396825396825397));
	else
		tmp = (2.0 * (exp(x) / t_1)) - 1.0;
	end
	tmp_2 = tmp;
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, Block[{t$95$1 = N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]}, If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / t$95$1), $MachinePrecision], 0.0005], N[(N[(x + N[(N[Power[x, 5.0], $MachinePrecision] * 0.13333333333333333), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Power[x, 3.0], $MachinePrecision] * -0.3333333333333333), $MachinePrecision] + N[(N[Power[x, 7.0], $MachinePrecision] * -0.05396825396825397), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[(N[Exp[x], $MachinePrecision] / t$95$1), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]]]]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\begin{array}{l}
t_0 := e^{-x}\\
t_1 := e^{x} + t_0\\
\mathbf{if}\;\frac{e^{x} - t_0}{t_1} \leq 0.0005:\\
\;\;\;\;\left(x + {x}^{5} \cdot 0.13333333333333333\right) + \left({x}^{3} \cdot -0.3333333333333333 + {x}^{7} \cdot -0.05396825396825397\right)\\

\mathbf{else}:\\
\;\;\;\;2 \cdot \frac{e^{x}}{t_1} - 1\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < 5.0000000000000001e-4

    1. Initial program 58.6

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

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

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

      [Start]0.3

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

      rational_best-simplify-47 [=>]0.3

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

      rational_best-simplify-3 [=>]0.3

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

      rational_best-simplify-1 [=>]0.3

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

      rational_best-simplify-1 [=>]0.3

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

      rational_best-simplify-1 [=>]0.3

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

    if 5.0000000000000001e-4 < (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x))))

    1. Initial program 40.7

      \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
    2. Applied egg-rr20.5

      \[\leadsto \color{blue}{\frac{e^{x}}{e^{x} + e^{-x}} \cdot 2 - \left(1 + \frac{0}{e^{x} + e^{-x}}\right)} \]
    3. Simplified20.5

      \[\leadsto \color{blue}{2 \cdot \frac{e^{x}}{e^{x} + e^{-x}} - 1} \]
      Proof

      [Start]20.5

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

      metadata-eval [<=]20.5

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

      rational_best-simplify-66 [<=]20.5

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

      rational_best-simplify-5 [=>]20.5

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

      metadata-eval [=>]20.5

      \[ \frac{e^{x}}{e^{x} + e^{-x}} \cdot 2 - \color{blue}{1} \]

      rational_best-simplify-1 [=>]20.5

      \[ \color{blue}{2 \cdot \frac{e^{x}}{e^{x} + e^{-x}}} - 1 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \leq 0.0005:\\ \;\;\;\;\left(x + {x}^{5} \cdot 0.13333333333333333\right) + \left({x}^{3} \cdot -0.3333333333333333 + {x}^{7} \cdot -0.05396825396825397\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \frac{e^{x}}{e^{x} + e^{-x}} - 1\\ \end{array} \]

Alternatives

Alternative 1
Error1.9
Cost13504
\[x + \left({x}^{5} \cdot 0.13333333333333333 + {x}^{3} \cdot -0.3333333333333333\right) \]
Alternative 2
Error2.1
Cost6784
\[-0.3333333333333333 \cdot {x}^{3} + x \]
Alternative 3
Error2.3
Cost64
\[x \]

Error

Reproduce?

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