Hyperbolic sine

Percentage Accurate: 54.0% → 100.0%
Time: 7.8s
Alternatives: 9
Speedup: 1.9×

Specification

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

\\
\frac{e^{x} - e^{-x}}{2}
\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 9 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: 54.0% accurate, 1.0× speedup?

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

\\
\frac{e^{x} - e^{-x}}{2}
\end{array}

Alternative 1: 100.0% accurate, 0.4× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ \begin{array}{l} t_0 := e^{x_m} - e^{-x_m}\\ x_s \cdot \begin{array}{l} \mathbf{if}\;t_0 \leq 0.05:\\ \;\;\;\;\frac{0.0003968253968253968 \cdot {x_m}^{7} + \left(0.016666666666666666 \cdot {x_m}^{5} + \left(0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2\right)\right)}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{2}\\ \end{array} \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (let* ((t_0 (- (exp x_m) (exp (- x_m)))))
   (*
    x_s
    (if (<= t_0 0.05)
      (/
       (+
        (* 0.0003968253968253968 (pow x_m 7.0))
        (+
         (* 0.016666666666666666 (pow x_m 5.0))
         (+ (* 0.3333333333333333 (pow x_m 3.0)) (* x_m 2.0))))
       2.0)
      (/ t_0 2.0)))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	double t_0 = exp(x_m) - exp(-x_m);
	double tmp;
	if (t_0 <= 0.05) {
		tmp = ((0.0003968253968253968 * pow(x_m, 7.0)) + ((0.016666666666666666 * pow(x_m, 5.0)) + ((0.3333333333333333 * pow(x_m, 3.0)) + (x_m * 2.0)))) / 2.0;
	} else {
		tmp = t_0 / 2.0;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = exp(x_m) - exp(-x_m)
    if (t_0 <= 0.05d0) then
        tmp = ((0.0003968253968253968d0 * (x_m ** 7.0d0)) + ((0.016666666666666666d0 * (x_m ** 5.0d0)) + ((0.3333333333333333d0 * (x_m ** 3.0d0)) + (x_m * 2.0d0)))) / 2.0d0
    else
        tmp = t_0 / 2.0d0
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	double t_0 = Math.exp(x_m) - Math.exp(-x_m);
	double tmp;
	if (t_0 <= 0.05) {
		tmp = ((0.0003968253968253968 * Math.pow(x_m, 7.0)) + ((0.016666666666666666 * Math.pow(x_m, 5.0)) + ((0.3333333333333333 * Math.pow(x_m, 3.0)) + (x_m * 2.0)))) / 2.0;
	} else {
		tmp = t_0 / 2.0;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	t_0 = math.exp(x_m) - math.exp(-x_m)
	tmp = 0
	if t_0 <= 0.05:
		tmp = ((0.0003968253968253968 * math.pow(x_m, 7.0)) + ((0.016666666666666666 * math.pow(x_m, 5.0)) + ((0.3333333333333333 * math.pow(x_m, 3.0)) + (x_m * 2.0)))) / 2.0
	else:
		tmp = t_0 / 2.0
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	t_0 = Float64(exp(x_m) - exp(Float64(-x_m)))
	tmp = 0.0
	if (t_0 <= 0.05)
		tmp = Float64(Float64(Float64(0.0003968253968253968 * (x_m ^ 7.0)) + Float64(Float64(0.016666666666666666 * (x_m ^ 5.0)) + Float64(Float64(0.3333333333333333 * (x_m ^ 3.0)) + Float64(x_m * 2.0)))) / 2.0);
	else
		tmp = Float64(t_0 / 2.0);
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m)
	t_0 = exp(x_m) - exp(-x_m);
	tmp = 0.0;
	if (t_0 <= 0.05)
		tmp = ((0.0003968253968253968 * (x_m ^ 7.0)) + ((0.016666666666666666 * (x_m ^ 5.0)) + ((0.3333333333333333 * (x_m ^ 3.0)) + (x_m * 2.0)))) / 2.0;
	else
		tmp = t_0 / 2.0;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[(N[Exp[x$95$m], $MachinePrecision] - N[Exp[(-x$95$m)], $MachinePrecision]), $MachinePrecision]}, N[(x$95$s * If[LessEqual[t$95$0, 0.05], N[(N[(N[(0.0003968253968253968 * N[Power[x$95$m, 7.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.016666666666666666 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(t$95$0 / 2.0), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
\begin{array}{l}
t_0 := e^{x_m} - e^{-x_m}\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;t_0 \leq 0.05:\\
\;\;\;\;\frac{0.0003968253968253968 \cdot {x_m}^{7} + \left(0.016666666666666666 \cdot {x_m}^{5} + \left(0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2\right)\right)}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{2}\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) < 0.050000000000000003

    1. Initial program 42.0%

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

      \[\leadsto \frac{\color{blue}{0.0003968253968253968 \cdot {x}^{7} + \left(0.016666666666666666 \cdot {x}^{5} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}}{2} \]

    if 0.050000000000000003 < (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))

    1. Initial program 100.0%

      \[\frac{e^{x} - e^{-x}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.6%

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

Alternative 2: 99.5% accurate, 0.4× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \frac{0.0003968253968253968 \cdot {x_m}^{7} + \left(\log \left(1 + \mathsf{expm1}\left(0.016666666666666666 \cdot {x_m}^{5}\right)\right) + \left(0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2\right)\right)}{2} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (/
   (+
    (* 0.0003968253968253968 (pow x_m 7.0))
    (+
     (log (+ 1.0 (expm1 (* 0.016666666666666666 (pow x_m 5.0)))))
     (+ (* 0.3333333333333333 (pow x_m 3.0)) (* x_m 2.0))))
   2.0)))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (((0.0003968253968253968 * pow(x_m, 7.0)) + (log((1.0 + expm1((0.016666666666666666 * pow(x_m, 5.0))))) + ((0.3333333333333333 * pow(x_m, 3.0)) + (x_m * 2.0)))) / 2.0);
}
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * (((0.0003968253968253968 * Math.pow(x_m, 7.0)) + (Math.log((1.0 + Math.expm1((0.016666666666666666 * Math.pow(x_m, 5.0))))) + ((0.3333333333333333 * Math.pow(x_m, 3.0)) + (x_m * 2.0)))) / 2.0);
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (((0.0003968253968253968 * math.pow(x_m, 7.0)) + (math.log((1.0 + math.expm1((0.016666666666666666 * math.pow(x_m, 5.0))))) + ((0.3333333333333333 * math.pow(x_m, 3.0)) + (x_m * 2.0)))) / 2.0)
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(Float64(Float64(0.0003968253968253968 * (x_m ^ 7.0)) + Float64(log(Float64(1.0 + expm1(Float64(0.016666666666666666 * (x_m ^ 5.0))))) + Float64(Float64(0.3333333333333333 * (x_m ^ 3.0)) + Float64(x_m * 2.0)))) / 2.0))
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(N[(0.0003968253968253968 * N[Power[x$95$m, 7.0], $MachinePrecision]), $MachinePrecision] + N[(N[Log[N[(1.0 + N[(Exp[N[(0.016666666666666666 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + N[(N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \frac{0.0003968253968253968 \cdot {x_m}^{7} + \left(\log \left(1 + \mathsf{expm1}\left(0.016666666666666666 \cdot {x_m}^{5}\right)\right) + \left(0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2\right)\right)}{2}
\end{array}
Derivation
  1. Initial program 55.6%

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

    \[\leadsto \frac{\color{blue}{0.0003968253968253968 \cdot {x}^{7} + \left(0.016666666666666666 \cdot {x}^{5} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}}{2} \]
  3. Step-by-step derivation
    1. log1p-expm1-u99.8%

      \[\leadsto \frac{0.0003968253968253968 \cdot {x}^{7} + \left(\color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(0.016666666666666666 \cdot {x}^{5}\right)\right)} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}{2} \]
    2. log1p-udef99.7%

      \[\leadsto \frac{0.0003968253968253968 \cdot {x}^{7} + \left(\color{blue}{\log \left(1 + \mathsf{expm1}\left(0.016666666666666666 \cdot {x}^{5}\right)\right)} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}{2} \]
  4. Applied egg-rr99.7%

    \[\leadsto \frac{0.0003968253968253968 \cdot {x}^{7} + \left(\color{blue}{\log \left(1 + \mathsf{expm1}\left(0.016666666666666666 \cdot {x}^{5}\right)\right)} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}{2} \]
  5. Final simplification99.7%

    \[\leadsto \frac{0.0003968253968253968 \cdot {x}^{7} + \left(\log \left(1 + \mathsf{expm1}\left(0.016666666666666666 \cdot {x}^{5}\right)\right) + \left(0.3333333333333333 \cdot {x}^{3} + x \cdot 2\right)\right)}{2} \]

Alternative 3: 100.0% accurate, 0.4× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ \begin{array}{l} t_0 := e^{x_m} - e^{-x_m}\\ x_s \cdot \begin{array}{l} \mathbf{if}\;t_0 \leq 0.001:\\ \;\;\;\;\frac{\mathsf{fma}\left(2, x_m, 0.016666666666666666 \cdot {x_m}^{5} + 0.3333333333333333 \cdot {x_m}^{3}\right)}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{2}\\ \end{array} \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (let* ((t_0 (- (exp x_m) (exp (- x_m)))))
   (*
    x_s
    (if (<= t_0 0.001)
      (/
       (fma
        2.0
        x_m
        (+
         (* 0.016666666666666666 (pow x_m 5.0))
         (* 0.3333333333333333 (pow x_m 3.0))))
       2.0)
      (/ t_0 2.0)))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	double t_0 = exp(x_m) - exp(-x_m);
	double tmp;
	if (t_0 <= 0.001) {
		tmp = fma(2.0, x_m, ((0.016666666666666666 * pow(x_m, 5.0)) + (0.3333333333333333 * pow(x_m, 3.0)))) / 2.0;
	} else {
		tmp = t_0 / 2.0;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	t_0 = Float64(exp(x_m) - exp(Float64(-x_m)))
	tmp = 0.0
	if (t_0 <= 0.001)
		tmp = Float64(fma(2.0, x_m, Float64(Float64(0.016666666666666666 * (x_m ^ 5.0)) + Float64(0.3333333333333333 * (x_m ^ 3.0)))) / 2.0);
	else
		tmp = Float64(t_0 / 2.0);
	end
	return Float64(x_s * tmp)
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[(N[Exp[x$95$m], $MachinePrecision] - N[Exp[(-x$95$m)], $MachinePrecision]), $MachinePrecision]}, N[(x$95$s * If[LessEqual[t$95$0, 0.001], N[(N[(2.0 * x$95$m + N[(N[(0.016666666666666666 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision] + N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(t$95$0 / 2.0), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
\begin{array}{l}
t_0 := e^{x_m} - e^{-x_m}\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;t_0 \leq 0.001:\\
\;\;\;\;\frac{\mathsf{fma}\left(2, x_m, 0.016666666666666666 \cdot {x_m}^{5} + 0.3333333333333333 \cdot {x_m}^{3}\right)}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{2}\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) < 1e-3

    1. Initial program 41.8%

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

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

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

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

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(2, x, 0.016666666666666666 \cdot {x}^{5} + 0.3333333333333333 \cdot {x}^{3}\right)}}{2} \]
      4. +-commutative93.0%

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

        \[\leadsto \frac{\mathsf{fma}\left(2, x, \color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, 0.016666666666666666 \cdot {x}^{5}\right)}\right)}{2} \]
    4. Simplified93.0%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(2, x, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, 0.016666666666666666 \cdot {x}^{5}\right)\right)}}{2} \]
    5. Step-by-step derivation
      1. fma-udef93.0%

        \[\leadsto \frac{\mathsf{fma}\left(2, x, \color{blue}{0.3333333333333333 \cdot {x}^{3} + 0.016666666666666666 \cdot {x}^{5}}\right)}{2} \]
    6. Applied egg-rr93.0%

      \[\leadsto \frac{\mathsf{fma}\left(2, x, \color{blue}{0.3333333333333333 \cdot {x}^{3} + 0.016666666666666666 \cdot {x}^{5}}\right)}{2} \]

    if 1e-3 < (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))

    1. Initial program 99.9%

      \[\frac{e^{x} - e^{-x}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification94.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{x} - e^{-x} \leq 0.001:\\ \;\;\;\;\frac{\mathsf{fma}\left(2, x, 0.016666666666666666 \cdot {x}^{5} + 0.3333333333333333 \cdot {x}^{3}\right)}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{x} - e^{-x}}{2}\\ \end{array} \]

Alternative 4: 100.0% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ \begin{array}{l} t_0 := e^{x_m} - e^{-x_m}\\ x_s \cdot \begin{array}{l} \mathbf{if}\;t_0 \leq 0.001:\\ \;\;\;\;\frac{0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{2}\\ \end{array} \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (let* ((t_0 (- (exp x_m) (exp (- x_m)))))
   (*
    x_s
    (if (<= t_0 0.001)
      (/ (+ (* 0.3333333333333333 (pow x_m 3.0)) (* x_m 2.0)) 2.0)
      (/ t_0 2.0)))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	double t_0 = exp(x_m) - exp(-x_m);
	double tmp;
	if (t_0 <= 0.001) {
		tmp = ((0.3333333333333333 * pow(x_m, 3.0)) + (x_m * 2.0)) / 2.0;
	} else {
		tmp = t_0 / 2.0;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8) :: t_0
    real(8) :: tmp
    t_0 = exp(x_m) - exp(-x_m)
    if (t_0 <= 0.001d0) then
        tmp = ((0.3333333333333333d0 * (x_m ** 3.0d0)) + (x_m * 2.0d0)) / 2.0d0
    else
        tmp = t_0 / 2.0d0
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	double t_0 = Math.exp(x_m) - Math.exp(-x_m);
	double tmp;
	if (t_0 <= 0.001) {
		tmp = ((0.3333333333333333 * Math.pow(x_m, 3.0)) + (x_m * 2.0)) / 2.0;
	} else {
		tmp = t_0 / 2.0;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	t_0 = math.exp(x_m) - math.exp(-x_m)
	tmp = 0
	if t_0 <= 0.001:
		tmp = ((0.3333333333333333 * math.pow(x_m, 3.0)) + (x_m * 2.0)) / 2.0
	else:
		tmp = t_0 / 2.0
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	t_0 = Float64(exp(x_m) - exp(Float64(-x_m)))
	tmp = 0.0
	if (t_0 <= 0.001)
		tmp = Float64(Float64(Float64(0.3333333333333333 * (x_m ^ 3.0)) + Float64(x_m * 2.0)) / 2.0);
	else
		tmp = Float64(t_0 / 2.0);
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m)
	t_0 = exp(x_m) - exp(-x_m);
	tmp = 0.0;
	if (t_0 <= 0.001)
		tmp = ((0.3333333333333333 * (x_m ^ 3.0)) + (x_m * 2.0)) / 2.0;
	else
		tmp = t_0 / 2.0;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := Block[{t$95$0 = N[(N[Exp[x$95$m], $MachinePrecision] - N[Exp[(-x$95$m)], $MachinePrecision]), $MachinePrecision]}, N[(x$95$s * If[LessEqual[t$95$0, 0.001], N[(N[(N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(t$95$0 / 2.0), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
\begin{array}{l}
t_0 := e^{x_m} - e^{-x_m}\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;t_0 \leq 0.001:\\
\;\;\;\;\frac{0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{2}\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) < 1e-3

    1. Initial program 41.8%

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

      \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{2} \]

    if 1e-3 < (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))

    1. Initial program 99.9%

      \[\frac{e^{x} - e^{-x}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{x} - e^{-x} \leq 0.001:\\ \;\;\;\;\frac{0.3333333333333333 \cdot {x}^{3} + x \cdot 2}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{x} - e^{-x}}{2}\\ \end{array} \]

Alternative 5: 93.1% accurate, 1.8× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \begin{array}{l} \mathbf{if}\;x_m \leq 5.5:\\ \;\;\;\;\frac{0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.0003968253968253968 \cdot {x_m}^{7}}{2}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (if (<= x_m 5.5)
    (/ (+ (* 0.3333333333333333 (pow x_m 3.0)) (* x_m 2.0)) 2.0)
    (/ (* 0.0003968253968253968 (pow x_m 7.0)) 2.0))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	double tmp;
	if (x_m <= 5.5) {
		tmp = ((0.3333333333333333 * pow(x_m, 3.0)) + (x_m * 2.0)) / 2.0;
	} else {
		tmp = (0.0003968253968253968 * pow(x_m, 7.0)) / 2.0;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8) :: tmp
    if (x_m <= 5.5d0) then
        tmp = ((0.3333333333333333d0 * (x_m ** 3.0d0)) + (x_m * 2.0d0)) / 2.0d0
    else
        tmp = (0.0003968253968253968d0 * (x_m ** 7.0d0)) / 2.0d0
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	double tmp;
	if (x_m <= 5.5) {
		tmp = ((0.3333333333333333 * Math.pow(x_m, 3.0)) + (x_m * 2.0)) / 2.0;
	} else {
		tmp = (0.0003968253968253968 * Math.pow(x_m, 7.0)) / 2.0;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	tmp = 0
	if x_m <= 5.5:
		tmp = ((0.3333333333333333 * math.pow(x_m, 3.0)) + (x_m * 2.0)) / 2.0
	else:
		tmp = (0.0003968253968253968 * math.pow(x_m, 7.0)) / 2.0
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	tmp = 0.0
	if (x_m <= 5.5)
		tmp = Float64(Float64(Float64(0.3333333333333333 * (x_m ^ 3.0)) + Float64(x_m * 2.0)) / 2.0);
	else
		tmp = Float64(Float64(0.0003968253968253968 * (x_m ^ 7.0)) / 2.0);
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m)
	tmp = 0.0;
	if (x_m <= 5.5)
		tmp = ((0.3333333333333333 * (x_m ^ 3.0)) + (x_m * 2.0)) / 2.0;
	else
		tmp = (0.0003968253968253968 * (x_m ^ 7.0)) / 2.0;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 5.5], N[(N[(N[(0.3333333333333333 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x$95$m * 2.0), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(0.0003968253968253968 * N[Power[x$95$m, 7.0], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;x_m \leq 5.5:\\
\;\;\;\;\frac{0.3333333333333333 \cdot {x_m}^{3} + x_m \cdot 2}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.0003968253968253968 \cdot {x_m}^{7}}{2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 5.5

    1. Initial program 42.3%

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

      \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{2} \]

    if 5.5 < x

    1. Initial program 100.0%

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

      \[\leadsto \frac{\color{blue}{0.0003968253968253968 \cdot {x}^{7} + \left(0.016666666666666666 \cdot {x}^{5} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}}{2} \]
    3. Taylor expanded in x around inf 87.1%

      \[\leadsto \frac{\color{blue}{0.0003968253968253968 \cdot {x}^{7}}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 5.5:\\ \;\;\;\;\frac{0.3333333333333333 \cdot {x}^{3} + x \cdot 2}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.0003968253968253968 \cdot {x}^{7}}{2}\\ \end{array} \]

Alternative 6: 92.8% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \begin{array}{l} \mathbf{if}\;x_m \leq 4.2:\\ \;\;\;\;\frac{x_m \cdot 2}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.0003968253968253968 \cdot {x_m}^{7}}{2}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (if (<= x_m 4.2)
    (/ (* x_m 2.0) 2.0)
    (/ (* 0.0003968253968253968 (pow x_m 7.0)) 2.0))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	double tmp;
	if (x_m <= 4.2) {
		tmp = (x_m * 2.0) / 2.0;
	} else {
		tmp = (0.0003968253968253968 * pow(x_m, 7.0)) / 2.0;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8) :: tmp
    if (x_m <= 4.2d0) then
        tmp = (x_m * 2.0d0) / 2.0d0
    else
        tmp = (0.0003968253968253968d0 * (x_m ** 7.0d0)) / 2.0d0
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	double tmp;
	if (x_m <= 4.2) {
		tmp = (x_m * 2.0) / 2.0;
	} else {
		tmp = (0.0003968253968253968 * Math.pow(x_m, 7.0)) / 2.0;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	tmp = 0
	if x_m <= 4.2:
		tmp = (x_m * 2.0) / 2.0
	else:
		tmp = (0.0003968253968253968 * math.pow(x_m, 7.0)) / 2.0
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	tmp = 0.0
	if (x_m <= 4.2)
		tmp = Float64(Float64(x_m * 2.0) / 2.0);
	else
		tmp = Float64(Float64(0.0003968253968253968 * (x_m ^ 7.0)) / 2.0);
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m)
	tmp = 0.0;
	if (x_m <= 4.2)
		tmp = (x_m * 2.0) / 2.0;
	else
		tmp = (0.0003968253968253968 * (x_m ^ 7.0)) / 2.0;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 4.2], N[(N[(x$95$m * 2.0), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[(0.0003968253968253968 * N[Power[x$95$m, 7.0], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;x_m \leq 4.2:\\
\;\;\;\;\frac{x_m \cdot 2}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.0003968253968253968 \cdot {x_m}^{7}}{2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 4.20000000000000018

    1. Initial program 42.3%

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

      \[\leadsto \frac{\color{blue}{2 \cdot x}}{2} \]

    if 4.20000000000000018 < x

    1. Initial program 100.0%

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

      \[\leadsto \frac{\color{blue}{0.0003968253968253968 \cdot {x}^{7} + \left(0.016666666666666666 \cdot {x}^{5} + \left(0.3333333333333333 \cdot {x}^{3} + 2 \cdot x\right)\right)}}{2} \]
    3. Taylor expanded in x around inf 87.1%

      \[\leadsto \frac{\color{blue}{0.0003968253968253968 \cdot {x}^{7}}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification69.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 4.2:\\ \;\;\;\;\frac{x \cdot 2}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.0003968253968253968 \cdot {x}^{7}}{2}\\ \end{array} \]

Alternative 7: 52.4% accurate, 41.2× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \frac{x_m \cdot 2}{2} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m) :precision binary64 (* x_s (/ (* x_m 2.0) 2.0)))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * ((x_m * 2.0) / 2.0);
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * ((x_m * 2.0d0) / 2.0d0)
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * ((x_m * 2.0) / 2.0);
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * ((x_m * 2.0) / 2.0)
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(Float64(x_m * 2.0) / 2.0))
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * ((x_m * 2.0) / 2.0);
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * N[(N[(x$95$m * 2.0), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \frac{x_m \cdot 2}{2}
\end{array}
Derivation
  1. Initial program 55.6%

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

    \[\leadsto \frac{\color{blue}{2 \cdot x}}{2} \]
  3. Final simplification50.6%

    \[\leadsto \frac{x \cdot 2}{2} \]

Alternative 8: 3.5% accurate, 206.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot 0 \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m) :precision binary64 (* x_s 0.0))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * 0.0;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * 0.0d0
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * 0.0;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * 0.0
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * 0.0)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * 0.0;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * 0.0), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot 0
\end{array}
Derivation
  1. Initial program 55.6%

    \[\frac{e^{x} - e^{-x}}{2} \]
  2. Applied egg-rr3.4%

    \[\leadsto \frac{\color{blue}{0}}{2} \]
  3. Final simplification3.4%

    \[\leadsto 0 \]

Alternative 9: 4.3% accurate, 206.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot 0.25 \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m) :precision binary64 (* x_s 0.25))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * 0.25;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    code = x_s * 0.25d0
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
	return x_s * 0.25;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * 0.25
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * 0.25)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * 0.25;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * 0.25), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot 0.25
\end{array}
Derivation
  1. Initial program 55.6%

    \[\frac{e^{x} - e^{-x}}{2} \]
  2. Applied egg-rr2.8%

    \[\leadsto \frac{\color{blue}{0.5}}{2} \]
  3. Final simplification2.8%

    \[\leadsto 0.25 \]

Reproduce

?
herbie shell --seed 2023331 
(FPCore (x)
  :name "Hyperbolic sine"
  :precision binary64
  (/ (- (exp x) (exp (- x))) 2.0))