Hyperbolic sine

Percentage Accurate: 54.9% → 99.1%
Time: 6.1s
Alternatives: 4
Speedup: 15.8×

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 4 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.9% 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: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(x_m\right)\right) \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m) :precision binary64 (* x_s (log1p (expm1 x_m))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
	return x_s * log1p(expm1(x_m));
}
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 * Math.log1p(Math.expm1(x_m));
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
def code(x_s, x_m):
	return x_s * math.log1p(math.expm1(x_m))
x_m = abs(x)
x_s = copysign(1.0, x)
function code(x_s, x_m)
	return Float64(x_s * log1p(expm1(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 * N[Log[1 + N[(Exp[x$95$m] - 1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)

\\
x_s \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(x_m\right)\right)
\end{array}
Derivation
  1. Initial program 60.6%

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

    \[\leadsto \frac{\color{blue}{2 \cdot x}}{2} \]
  3. Step-by-step derivation
    1. associate-/l*46.1%

      \[\leadsto \color{blue}{\frac{2}{\frac{2}{x}}} \]
    2. associate-/r/46.2%

      \[\leadsto \color{blue}{\frac{2}{2} \cdot x} \]
    3. metadata-eval46.2%

      \[\leadsto \color{blue}{1} \cdot x \]
    4. *-un-lft-identity46.2%

      \[\leadsto \color{blue}{x} \]
    5. log1p-expm1-u99.3%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(x\right)\right)} \]
  4. Applied egg-rr99.3%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(x\right)\right)} \]
  5. Final simplification99.3%

    \[\leadsto \mathsf{log1p}\left(\mathsf{expm1}\left(x\right)\right) \]

Alternative 2: 83.7% accurate, 15.8× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ x_s \cdot \frac{\left(x_m \cdot 0.3333333333333333\right) \cdot \left(x_m \cdot x_m\right) + 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 0.3333333333333333) (* x_m x_m)) (* 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 * 0.3333333333333333) * (x_m * x_m)) + (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 * 0.3333333333333333d0) * (x_m * x_m)) + (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 * 0.3333333333333333) * (x_m * x_m)) + (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 * 0.3333333333333333) * (x_m * x_m)) + (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(Float64(x_m * 0.3333333333333333) * Float64(x_m * x_m)) + 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 * 0.3333333333333333) * (x_m * x_m)) + (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[(N[(x$95$m * 0.3333333333333333), $MachinePrecision] * N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision] + N[(x$95$m * 2.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 \frac{\left(x_m \cdot 0.3333333333333333\right) \cdot \left(x_m \cdot x_m\right) + x_m \cdot 2}{2}
\end{array}
Derivation
  1. Initial program 60.6%

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

    \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{2} \]
  3. Step-by-step derivation
    1. cube-mult82.8%

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

      \[\leadsto \frac{\color{blue}{\left(0.3333333333333333 \cdot x\right) \cdot \left(x \cdot x\right)} + 2 \cdot x}{2} \]
    3. *-un-lft-identity82.8%

      \[\leadsto \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(x \cdot \color{blue}{\left(1 \cdot x\right)}\right) + 2 \cdot x}{2} \]
    4. metadata-eval82.8%

      \[\leadsto \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(x \cdot \left(\color{blue}{\frac{2}{2}} \cdot x\right)\right) + 2 \cdot x}{2} \]
    5. associate-/r/82.8%

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

      \[\leadsto \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(x \cdot \color{blue}{\frac{2 \cdot x}{2}}\right) + 2 \cdot x}{2} \]
    7. clear-num82.8%

      \[\leadsto \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(x \cdot \color{blue}{\frac{1}{\frac{2}{2 \cdot x}}}\right) + 2 \cdot x}{2} \]
    8. un-div-inv82.8%

      \[\leadsto \frac{\left(0.3333333333333333 \cdot x\right) \cdot \color{blue}{\frac{x}{\frac{2}{2 \cdot x}}} + 2 \cdot x}{2} \]
    9. associate-*r/82.8%

      \[\leadsto \frac{\color{blue}{\frac{\left(0.3333333333333333 \cdot x\right) \cdot x}{\frac{2}{2 \cdot x}}} + 2 \cdot x}{2} \]
    10. associate-/r*82.8%

      \[\leadsto \frac{\frac{\left(0.3333333333333333 \cdot x\right) \cdot x}{\color{blue}{\frac{\frac{2}{2}}{x}}} + 2 \cdot x}{2} \]
    11. metadata-eval82.8%

      \[\leadsto \frac{\frac{\left(0.3333333333333333 \cdot x\right) \cdot x}{\frac{\color{blue}{1}}{x}} + 2 \cdot x}{2} \]
  4. Applied egg-rr82.8%

    \[\leadsto \frac{\color{blue}{\frac{\left(0.3333333333333333 \cdot x\right) \cdot x}{\frac{1}{x}}} + 2 \cdot x}{2} \]
  5. Step-by-step derivation
    1. associate-/l*82.8%

      \[\leadsto \frac{\color{blue}{\frac{0.3333333333333333 \cdot x}{\frac{\frac{1}{x}}{x}}} + 2 \cdot x}{2} \]
    2. div-inv82.8%

      \[\leadsto \frac{\frac{0.3333333333333333 \cdot x}{\color{blue}{\frac{1}{x} \cdot \frac{1}{x}}} + 2 \cdot x}{2} \]
    3. div-inv82.8%

      \[\leadsto \frac{\color{blue}{\left(0.3333333333333333 \cdot x\right) \cdot \frac{1}{\frac{1}{x} \cdot \frac{1}{x}}} + 2 \cdot x}{2} \]
    4. *-commutative82.8%

      \[\leadsto \frac{\color{blue}{\left(x \cdot 0.3333333333333333\right)} \cdot \frac{1}{\frac{1}{x} \cdot \frac{1}{x}} + 2 \cdot x}{2} \]
    5. inv-pow82.8%

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

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \frac{1}{{x}^{\color{blue}{\left(-1\right)}} \cdot \frac{1}{x}} + 2 \cdot x}{2} \]
    7. inv-pow82.8%

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \frac{1}{{x}^{\left(-1\right)} \cdot \color{blue}{{x}^{-1}}} + 2 \cdot x}{2} \]
    8. metadata-eval82.8%

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \frac{1}{{x}^{\left(-1\right)} \cdot {x}^{\color{blue}{\left(-1\right)}}} + 2 \cdot x}{2} \]
    9. pow-sqr82.8%

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \frac{1}{\color{blue}{{x}^{\left(2 \cdot \left(-1\right)\right)}}} + 2 \cdot x}{2} \]
    10. metadata-eval82.8%

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \frac{1}{{x}^{\left(2 \cdot \color{blue}{-1}\right)}} + 2 \cdot x}{2} \]
    11. metadata-eval82.8%

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \frac{1}{{x}^{\color{blue}{-2}}} + 2 \cdot x}{2} \]
  6. Applied egg-rr82.8%

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

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \color{blue}{{x}^{\left(--2\right)}} + 2 \cdot x}{2} \]
    2. metadata-eval82.8%

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

      \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \color{blue}{\left(x \cdot x\right)} + 2 \cdot x}{2} \]
  8. Applied egg-rr82.8%

    \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \color{blue}{\left(x \cdot x\right)} + 2 \cdot x}{2} \]
  9. Final simplification82.8%

    \[\leadsto \frac{\left(x \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right) + x \cdot 2}{2} \]

Alternative 3: 51.7% 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 60.6%

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

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

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

Alternative 4: 51.7% 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 1 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 60.6%

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

    \[\leadsto \frac{\color{blue}{2 \cdot x}}{2} \]
  3. Taylor expanded in x around 0 46.2%

    \[\leadsto \color{blue}{x} \]
  4. Final simplification46.2%

    \[\leadsto x \]

Reproduce

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