Hyperbolic sine

Percentage Accurate: 54.0% → 100.0%
Time: 6.6s
Alternatives: 7
Speedup: 29.4×

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 7 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.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.0005:\\ \;\;\;\;x\_m + 0.16666666666666666 \cdot {x\_m}^{3}\\ \mathbf{else}:\\ \;\;\;\;\frac{t\_0}{2}\\ \end{array} \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
 :precision binary64
 (let* ((t_0 (- (exp x_m) (exp (- x_m)))))
   (*
    x_s
    (if (<= t_0 0.0005)
      (+ x_m (* 0.16666666666666666 (pow x_m 3.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.0005) {
		tmp = x_m + (0.16666666666666666 * pow(x_m, 3.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.0005d0) then
        tmp = x_m + (0.16666666666666666d0 * (x_m ** 3.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.0005) {
		tmp = x_m + (0.16666666666666666 * Math.pow(x_m, 3.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.0005:
		tmp = x_m + (0.16666666666666666 * math.pow(x_m, 3.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.0005)
		tmp = Float64(x_m + Float64(0.16666666666666666 * (x_m ^ 3.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.0005)
		tmp = x_m + (0.16666666666666666 * (x_m ^ 3.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.0005], N[(x$95$m + N[(0.16666666666666666 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $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.0005:\\
\;\;\;\;x\_m + 0.16666666666666666 \cdot {x\_m}^{3}\\

\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))) < 5.0000000000000001e-4

    1. Initial program 40.1%

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

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

        \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + 2\right)}}{2} \]
      2. distribute-rgt-in87.0%

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

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

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

        \[\leadsto \frac{\mathsf{fma}\left(0.3333333333333333, \color{blue}{{x}^{\left(2 + 1\right)}}, 2 \cdot x\right)}{2} \]
      6. metadata-eval87.0%

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, 2 \cdot x\right)}}{2} \]
    6. Taylor expanded in x around 0 87.0%

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

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

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

        \[\leadsto x + \color{blue}{0.16666666666666666 \cdot \left({x}^{2} \cdot x\right)} \]
      4. unpow287.0%

        \[\leadsto x + 0.16666666666666666 \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot x\right) \]
      5. unpow387.0%

        \[\leadsto x + 0.16666666666666666 \cdot \color{blue}{{x}^{3}} \]
    8. Simplified87.0%

      \[\leadsto \color{blue}{x + 0.16666666666666666 \cdot {x}^{3}} \]

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

    1. Initial program 100.0%

      \[\frac{e^{x} - e^{-x}}{2} \]
    2. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 99.7% 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 2.1:\\ \;\;\;\;x\_m + 0.16666666666666666 \cdot {x\_m}^{3}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{expm1}\left(x\_m\right)}{2}\\ \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (if (<= x_m 2.1)
    (+ x_m (* 0.16666666666666666 (pow x_m 3.0)))
    (/ (expm1 x_m) 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 <= 2.1) {
		tmp = x_m + (0.16666666666666666 * pow(x_m, 3.0));
	} else {
		tmp = expm1(x_m) / 2.0;
	}
	return x_s * tmp;
}
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 <= 2.1) {
		tmp = x_m + (0.16666666666666666 * Math.pow(x_m, 3.0));
	} else {
		tmp = Math.expm1(x_m) / 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 <= 2.1:
		tmp = x_m + (0.16666666666666666 * math.pow(x_m, 3.0))
	else:
		tmp = math.expm1(x_m) / 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 <= 2.1)
		tmp = Float64(x_m + Float64(0.16666666666666666 * (x_m ^ 3.0)));
	else
		tmp = Float64(expm1(x_m) / 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_] := N[(x$95$s * If[LessEqual[x$95$m, 2.1], N[(x$95$m + N[(0.16666666666666666 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(Exp[x$95$m] - 1), $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 2.1:\\
\;\;\;\;x\_m + 0.16666666666666666 \cdot {x\_m}^{3}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{expm1}\left(x\_m\right)}{2}\\


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

    1. Initial program 40.1%

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

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

        \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + 2\right)}}{2} \]
      2. distribute-rgt-in87.0%

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

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

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

        \[\leadsto \frac{\mathsf{fma}\left(0.3333333333333333, \color{blue}{{x}^{\left(2 + 1\right)}}, 2 \cdot x\right)}{2} \]
      6. metadata-eval87.0%

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, 2 \cdot x\right)}}{2} \]
    6. Taylor expanded in x around 0 87.0%

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

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

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

        \[\leadsto x + \color{blue}{0.16666666666666666 \cdot \left({x}^{2} \cdot x\right)} \]
      4. unpow287.0%

        \[\leadsto x + 0.16666666666666666 \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot x\right) \]
      5. unpow387.0%

        \[\leadsto x + 0.16666666666666666 \cdot \color{blue}{{x}^{3}} \]
    8. Simplified87.0%

      \[\leadsto \color{blue}{x + 0.16666666666666666 \cdot {x}^{3}} \]

    if 2.10000000000000009 < x

    1. Initial program 100.0%

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

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

        \[\leadsto \frac{e^{x} - \left(1 + \color{blue}{\left(-x\right)}\right)}{2} \]
      2. unsub-neg100.0%

        \[\leadsto \frac{e^{x} - \color{blue}{\left(1 - x\right)}}{2} \]
    5. Simplified100.0%

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

      \[\leadsto \frac{e^{x} - \color{blue}{1}}{2} \]
    7. Step-by-step derivation
      1. expm1-define100.0%

        \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(x\right)}}{2} \]
    8. Applied egg-rr100.0%

      \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(x\right)}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.5% 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 1.25:\\ \;\;\;\;x\_m\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{expm1}\left(x\_m\right)}{2}\\ \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
 :precision binary64
 (* x_s (if (<= x_m 1.25) x_m (/ (expm1 x_m) 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 <= 1.25) {
		tmp = x_m;
	} else {
		tmp = expm1(x_m) / 2.0;
	}
	return x_s * tmp;
}
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 <= 1.25) {
		tmp = x_m;
	} else {
		tmp = Math.expm1(x_m) / 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 <= 1.25:
		tmp = x_m
	else:
		tmp = math.expm1(x_m) / 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 <= 1.25)
		tmp = x_m;
	else
		tmp = Float64(expm1(x_m) / 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_] := N[(x$95$s * If[LessEqual[x$95$m, 1.25], x$95$m, N[(N[(Exp[x$95$m] - 1), $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 1.25:\\
\;\;\;\;x\_m\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{expm1}\left(x\_m\right)}{2}\\


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

    1. Initial program 40.1%

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

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

        \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + 2\right)}}{2} \]
      2. distribute-rgt-in87.0%

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

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

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

        \[\leadsto \frac{\mathsf{fma}\left(0.3333333333333333, \color{blue}{{x}^{\left(2 + 1\right)}}, 2 \cdot x\right)}{2} \]
      6. metadata-eval87.0%

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, 2 \cdot x\right)}}{2} \]
    6. Taylor expanded in x around 0 66.6%

      \[\leadsto \color{blue}{x} \]

    if 1.25 < x

    1. Initial program 100.0%

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

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

        \[\leadsto \frac{e^{x} - \left(1 + \color{blue}{\left(-x\right)}\right)}{2} \]
      2. unsub-neg100.0%

        \[\leadsto \frac{e^{x} - \color{blue}{\left(1 - x\right)}}{2} \]
    5. Simplified100.0%

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

      \[\leadsto \frac{e^{x} - \color{blue}{1}}{2} \]
    7. Step-by-step derivation
      1. expm1-define100.0%

        \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(x\right)}}{2} \]
    8. Applied egg-rr100.0%

      \[\leadsto \frac{\color{blue}{\mathsf{expm1}\left(x\right)}}{2} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 87.8% accurate, 13.7× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \left(x\_m \cdot \left(1 + x\_m \cdot \left(0.25 + x\_m \cdot \left(0.08333333333333333 + x\_m \cdot 0.020833333333333332\right)\right)\right)\right) \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
 :precision binary64
 (*
  x_s
  (*
   x_m
   (+
    1.0
    (*
     x_m
     (+ 0.25 (* x_m (+ 0.08333333333333333 (* x_m 0.020833333333333332)))))))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (x_m * (1.0 + (x_m * (0.25 + (x_m * (0.08333333333333333 + (x_m * 0.020833333333333332)))))));
}
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 * (1.0d0 + (x_m * (0.25d0 + (x_m * (0.08333333333333333d0 + (x_m * 0.020833333333333332d0)))))))
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 * (1.0 + (x_m * (0.25 + (x_m * (0.08333333333333333 + (x_m * 0.020833333333333332)))))));
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (x_m * (1.0 + (x_m * (0.25 + (x_m * (0.08333333333333333 + (x_m * 0.020833333333333332)))))))
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(x_m * Float64(1.0 + Float64(x_m * Float64(0.25 + Float64(x_m * Float64(0.08333333333333333 + Float64(x_m * 0.020833333333333332))))))))
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * (x_m * (1.0 + (x_m * (0.25 + (x_m * (0.08333333333333333 + (x_m * 0.020833333333333332)))))));
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[(x$95$m * N[(1.0 + N[(x$95$m * N[(0.25 + N[(x$95$m * N[(0.08333333333333333 + N[(x$95$m * 0.020833333333333332), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot \left(x\_m \cdot \left(1 + x\_m \cdot \left(0.25 + x\_m \cdot \left(0.08333333333333333 + x\_m \cdot 0.020833333333333332\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 56.7%

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

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

      \[\leadsto \frac{e^{x} - \left(1 + \color{blue}{\left(-x\right)}\right)}{2} \]
    2. unsub-neg32.5%

      \[\leadsto \frac{e^{x} - \color{blue}{\left(1 - x\right)}}{2} \]
  5. Simplified32.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + x \cdot \left(0.25 + x \cdot \left(0.08333333333333333 + 0.020833333333333332 \cdot x\right)\right)\right)} \]
  7. Step-by-step derivation
    1. *-commutative65.7%

      \[\leadsto x \cdot \left(1 + x \cdot \left(0.25 + x \cdot \left(0.08333333333333333 + \color{blue}{x \cdot 0.020833333333333332}\right)\right)\right) \]
  8. Simplified65.7%

    \[\leadsto \color{blue}{x \cdot \left(1 + x \cdot \left(0.25 + x \cdot \left(0.08333333333333333 + x \cdot 0.020833333333333332\right)\right)\right)} \]
  9. Add Preprocessing

Alternative 5: 83.7% accurate, 18.7× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \left(x\_m \cdot \left(1 + x\_m \cdot \left(0.25 + x\_m \cdot 0.08333333333333333\right)\right)\right) \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m)
 :precision binary64
 (* x_s (* x_m (+ 1.0 (* x_m (+ 0.25 (* x_m 0.08333333333333333)))))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (x_m * (1.0 + (x_m * (0.25 + (x_m * 0.08333333333333333)))));
}
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 * (1.0d0 + (x_m * (0.25d0 + (x_m * 0.08333333333333333d0)))))
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 * (1.0 + (x_m * (0.25 + (x_m * 0.08333333333333333)))));
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (x_m * (1.0 + (x_m * (0.25 + (x_m * 0.08333333333333333)))))
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(x_m * Float64(1.0 + Float64(x_m * Float64(0.25 + Float64(x_m * 0.08333333333333333))))))
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * (x_m * (1.0 + (x_m * (0.25 + (x_m * 0.08333333333333333)))));
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[(x$95$m * N[(1.0 + N[(x$95$m * N[(0.25 + N[(x$95$m * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot \left(x\_m \cdot \left(1 + x\_m \cdot \left(0.25 + x\_m \cdot 0.08333333333333333\right)\right)\right)
\end{array}
Derivation
  1. Initial program 56.7%

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

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

      \[\leadsto \frac{e^{x} - \left(1 + \color{blue}{\left(-x\right)}\right)}{2} \]
    2. unsub-neg32.5%

      \[\leadsto \frac{e^{x} - \color{blue}{\left(1 - x\right)}}{2} \]
  5. Simplified32.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + x \cdot \left(0.25 + 0.08333333333333333 \cdot x\right)\right)} \]
  7. Step-by-step derivation
    1. *-commutative78.5%

      \[\leadsto x \cdot \left(1 + x \cdot \left(0.25 + \color{blue}{x \cdot 0.08333333333333333}\right)\right) \]
  8. Simplified78.5%

    \[\leadsto \color{blue}{x \cdot \left(1 + x \cdot \left(0.25 + x \cdot 0.08333333333333333\right)\right)} \]
  9. Add Preprocessing

Alternative 6: 75.8% accurate, 29.4× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \left(x\_m \cdot \left(1 + x\_m \cdot 0.25\right)\right) \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m) :precision binary64 (* x_s (* x_m (+ 1.0 (* x_m 0.25)))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * (x_m * (1.0 + (x_m * 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 * (x_m * (1.0d0 + (x_m * 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 * (x_m * (1.0 + (x_m * 0.25)));
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * (x_m * (1.0 + (x_m * 0.25)))
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * Float64(x_m * Float64(1.0 + Float64(x_m * 0.25))))
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * (x_m * (1.0 + (x_m * 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 * N[(x$95$m * N[(1.0 + N[(x$95$m * 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot \left(x\_m \cdot \left(1 + x\_m \cdot 0.25\right)\right)
\end{array}
Derivation
  1. Initial program 56.7%

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

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

      \[\leadsto \frac{e^{x} - \left(1 + \color{blue}{\left(-x\right)}\right)}{2} \]
    2. unsub-neg32.5%

      \[\leadsto \frac{e^{x} - \color{blue}{\left(1 - x\right)}}{2} \]
  5. Simplified32.5%

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

    \[\leadsto \color{blue}{x \cdot \left(1 + 0.25 \cdot x\right)} \]
  7. Step-by-step derivation
    1. *-commutative59.1%

      \[\leadsto x \cdot \left(1 + \color{blue}{x \cdot 0.25}\right) \]
  8. Simplified59.1%

    \[\leadsto \color{blue}{x \cdot \left(1 + x \cdot 0.25\right)} \]
  9. Add Preprocessing

Alternative 7: 52.5% accurate, 206.0× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot x\_m \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m) :precision binary64 (* x_s x_m))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * x_m;
}
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
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;
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * x_m
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * x_m)
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m)
	tmp = x_s * x_m;
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 * x$95$m), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot x\_m
\end{array}
Derivation
  1. Initial program 56.7%

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

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

      \[\leadsto \frac{x \cdot \color{blue}{\left(0.3333333333333333 \cdot {x}^{2} + 2\right)}}{2} \]
    2. distribute-rgt-in79.2%

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

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

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

      \[\leadsto \frac{\mathsf{fma}\left(0.3333333333333333, \color{blue}{{x}^{\left(2 + 1\right)}}, 2 \cdot x\right)}{2} \]
    6. metadata-eval79.2%

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

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, {x}^{3}, 2 \cdot x\right)}}{2} \]
  6. Taylor expanded in x around 0 49.6%

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

Reproduce

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