Hyperbolic sine

?

Percentage Accurate: 98.0% → 99.9%
Time: 1.0min
Precision: binary64
Cost: 59273

?

\[\frac{e^{x} - e^{-x}}{2} \]
\[\begin{array}{l} t_0 := e^{x} - e^{-x}\\ \mathbf{if}\;t_0 \leq -0.5 \lor \neg \left(t_0 \leq 0.05\right):\\ \;\;\;\;\frac{t_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \left(x + x\right) + 0.016666666666666666 \cdot {x}^{5}\right)\right)}{2}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (- (exp x) (exp (- x))) 2.0))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (- (exp x) (exp (- x)))))
   (if (or (<= t_0 -0.5) (not (<= t_0 0.05)))
     (/ t_0 2.0)
     (/
      (fma
       0.0003968253968253968
       (pow x 7.0)
       (fma
        0.3333333333333333
        (pow x 3.0)
        (+ (+ x x) (* 0.016666666666666666 (pow x 5.0)))))
      2.0))))
double code(double x) {
	return (exp(x) - exp(-x)) / 2.0;
}
double code(double x) {
	double t_0 = exp(x) - exp(-x);
	double tmp;
	if ((t_0 <= -0.5) || !(t_0 <= 0.05)) {
		tmp = t_0 / 2.0;
	} else {
		tmp = fma(0.0003968253968253968, pow(x, 7.0), fma(0.3333333333333333, pow(x, 3.0), ((x + x) + (0.016666666666666666 * pow(x, 5.0))))) / 2.0;
	}
	return tmp;
}
function code(x)
	return Float64(Float64(exp(x) - exp(Float64(-x))) / 2.0)
end
function code(x)
	t_0 = Float64(exp(x) - exp(Float64(-x)))
	tmp = 0.0
	if ((t_0 <= -0.5) || !(t_0 <= 0.05))
		tmp = Float64(t_0 / 2.0);
	else
		tmp = Float64(fma(0.0003968253968253968, (x ^ 7.0), fma(0.3333333333333333, (x ^ 3.0), Float64(Float64(x + x) + Float64(0.016666666666666666 * (x ^ 5.0))))) / 2.0);
	end
	return tmp
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(N[Exp[x], $MachinePrecision] - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -0.5], N[Not[LessEqual[t$95$0, 0.05]], $MachinePrecision]], N[(t$95$0 / 2.0), $MachinePrecision], N[(N[(0.0003968253968253968 * N[Power[x, 7.0], $MachinePrecision] + N[(0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision] + N[(N[(x + x), $MachinePrecision] + N[(0.016666666666666666 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]]
\frac{e^{x} - e^{-x}}{2}
\begin{array}{l}
t_0 := e^{x} - e^{-x}\\
\mathbf{if}\;t_0 \leq -0.5 \lor \neg \left(t_0 \leq 0.05\right):\\
\;\;\;\;\frac{t_0}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \left(x + x\right) + 0.016666666666666666 \cdot {x}^{5}\right)\right)}{2}\\


\end{array}

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.

Herbie found 10 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) < -0.5 or 0.050000000000000003 < (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))

    1. Initial program 100.0%

      \[\frac{e^{x} - e^{-x}}{2} \]

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

    1. Initial program 80.7%

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

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

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

      [Start]99.9%

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

      +-commutative [=>]99.9%

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

      associate-+r+ [=>]99.9%

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

      associate-+r+ [=>]99.9%

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

      +-commutative [=>]99.9%

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

      fma-def [=>]99.9%

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

      +-commutative [=>]99.9%

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

      associate-+l+ [=>]100.0%

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

      fma-def [=>]100.0%

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

      fma-def [=>]100.0%

      \[ \frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \color{blue}{\mathsf{fma}\left(0.016666666666666666, {x}^{5}, 2 \cdot x\right)}\right)\right)}{2} \]
    4. Applied egg-rr100.0%

      \[\leadsto \frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \color{blue}{\left(x + x\right) + 0.016666666666666666 \cdot {x}^{5}}\right)\right)}{2} \]
      Step-by-step derivation

      [Start]100.0%

      \[ \frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \mathsf{fma}\left(0.016666666666666666, {x}^{5}, 2 \cdot x\right)\right)\right)}{2} \]

      fma-udef [=>]100.0%

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

      +-commutative [=>]100.0%

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

      add-log-exp [=>]81.6%

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

      *-commutative [=>]81.6%

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

      exp-lft-sqr [=>]80.2%

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

      log-prod [=>]80.2%

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

      add-log-exp [<=]81.7%

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

      add-log-exp [<=]100.0%

      \[ \frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \left(x + \color{blue}{x}\right) + 0.016666666666666666 \cdot {x}^{5}\right)\right)}{2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

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

Alternatives

Alternative 1
Accuracy99.9%
Cost59273
\[\begin{array}{l} t_0 := e^{x} - e^{-x}\\ \mathbf{if}\;t_0 \leq -0.5 \lor \neg \left(t_0 \leq 0.05\right):\\ \;\;\;\;\frac{t_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(0.0003968253968253968, {x}^{7}, \mathsf{fma}\left(0.3333333333333333, {x}^{3}, \left(x + x\right) + 0.016666666666666666 \cdot {x}^{5}\right)\right)}{2}\\ \end{array} \]
Alternative 2
Accuracy99.9%
Cost46729
\[\begin{array}{l} t_0 := e^{x} - e^{-x}\\ \mathbf{if}\;t_0 \leq -0.5 \lor \neg \left(t_0 \leq 0.05\right):\\ \;\;\;\;\frac{t_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2 + \left(0.3333333333333333 \cdot {x}^{3} + \left(0.016666666666666666 \cdot {x}^{5} + 0.0003968253968253968 \cdot {x}^{7}\right)\right)}{2}\\ \end{array} \]
Alternative 3
Accuracy99.9%
Cost40009
\[\begin{array}{l} t_0 := e^{x} - e^{-x}\\ \mathbf{if}\;t_0 \leq -0.05 \lor \neg \left(t_0 \leq 0.02\right):\\ \;\;\;\;\frac{t_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2 + \left(0.016666666666666666 \cdot {x}^{5} + 0.3333333333333333 \cdot {x}^{3}\right)}{2}\\ \end{array} \]
Alternative 4
Accuracy99.5%
Cost39433
\[\begin{array}{l} t_0 := e^{x} - e^{-x}\\ \mathbf{if}\;t_0 \leq -0.05 \lor \neg \left(t_0 \leq 0.005\right):\\ \;\;\;\;\frac{t_0}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2 + x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333\right)\right)}{2}\\ \end{array} \]
Alternative 5
Accuracy23.2%
Cost7049
\[\begin{array}{l} \mathbf{if}\;x \leq -5.5 \lor \neg \left(x \leq 5.5\right):\\ \;\;\;\;\frac{0.0003968253968253968 \cdot {x}^{7}}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot 2 + x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333\right)\right)}{2}\\ \end{array} \]
Alternative 6
Accuracy14.7%
Cost832
\[\frac{x \cdot 2 + x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333\right)\right)}{2} \]
Alternative 7
Accuracy14.6%
Cost704
\[\frac{x \cdot \left(2 + x \cdot \left(x \cdot 0.3333333333333333\right)\right)}{2} \]
Alternative 8
Accuracy11.1%
Cost320
\[\frac{x \cdot 2}{2} \]
Alternative 9
Accuracy5.0%
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-8}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;0.25\\ \end{array} \]
Alternative 10
Accuracy2.9%
Cost64
\[-1 \]

Reproduce?

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