Hyperbolic secant

Percentage Accurate: 100.0% → 99.4%
Time: 2.5s
Alternatives: 5
Speedup: 1.0×

Specification

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

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

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

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

Alternative 1: 99.4% accurate, 13.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 340:\\ \;\;\;\;1 + \left(x \cdot x\right) \cdot \left(x \cdot \left(x \cdot 0.20833333333333334\right) + -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x)
 :precision binary64
 (if (<= x 340.0)
   (+ 1.0 (* (* x x) (+ (* x (* x 0.20833333333333334)) -0.5)))
   0.0))
x = abs(x);
double code(double x) {
	double tmp;
	if (x <= 340.0) {
		tmp = 1.0 + ((x * x) * ((x * (x * 0.20833333333333334)) + -0.5));
	} else {
		tmp = 0.0;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 340.0d0) then
        tmp = 1.0d0 + ((x * x) * ((x * (x * 0.20833333333333334d0)) + (-0.5d0)))
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
	double tmp;
	if (x <= 340.0) {
		tmp = 1.0 + ((x * x) * ((x * (x * 0.20833333333333334)) + -0.5));
	} else {
		tmp = 0.0;
	}
	return tmp;
}
x = abs(x)
def code(x):
	tmp = 0
	if x <= 340.0:
		tmp = 1.0 + ((x * x) * ((x * (x * 0.20833333333333334)) + -0.5))
	else:
		tmp = 0.0
	return tmp
x = abs(x)
function code(x)
	tmp = 0.0
	if (x <= 340.0)
		tmp = Float64(1.0 + Float64(Float64(x * x) * Float64(Float64(x * Float64(x * 0.20833333333333334)) + -0.5)));
	else
		tmp = 0.0;
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 340.0)
		tmp = 1.0 + ((x * x) * ((x * (x * 0.20833333333333334)) + -0.5));
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[x, 340.0], N[(1.0 + N[(N[(x * x), $MachinePrecision] * N[(N[(x * N[(x * 0.20833333333333334), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 340:\\
\;\;\;\;1 + \left(x \cdot x\right) \cdot \left(x \cdot \left(x \cdot 0.20833333333333334\right) + -0.5\right)\\

\mathbf{else}:\\
\;\;\;\;0\\


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

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{1 + \left(-0.5 \cdot {x}^{2} + 0.20833333333333334 \cdot {x}^{4}\right)} \]
    3. Step-by-step derivation
      1. fma-def65.1%

        \[\leadsto 1 + \color{blue}{\mathsf{fma}\left(-0.5, {x}^{2}, 0.20833333333333334 \cdot {x}^{4}\right)} \]
      2. unpow265.1%

        \[\leadsto 1 + \mathsf{fma}\left(-0.5, \color{blue}{x \cdot x}, 0.20833333333333334 \cdot {x}^{4}\right) \]
    4. Simplified65.1%

      \[\leadsto \color{blue}{1 + \mathsf{fma}\left(-0.5, x \cdot x, 0.20833333333333334 \cdot {x}^{4}\right)} \]
    5. Step-by-step derivation
      1. fma-udef65.1%

        \[\leadsto 1 + \color{blue}{\left(-0.5 \cdot \left(x \cdot x\right) + 0.20833333333333334 \cdot {x}^{4}\right)} \]
      2. +-commutative65.1%

        \[\leadsto 1 + \color{blue}{\left(0.20833333333333334 \cdot {x}^{4} + -0.5 \cdot \left(x \cdot x\right)\right)} \]
    6. Applied egg-rr65.1%

      \[\leadsto 1 + \color{blue}{\left(0.20833333333333334 \cdot {x}^{4} + -0.5 \cdot \left(x \cdot x\right)\right)} \]
    7. Taylor expanded in x around 0 65.1%

      \[\leadsto 1 + \color{blue}{\left(-0.5 \cdot {x}^{2} + 0.20833333333333334 \cdot {x}^{4}\right)} \]
    8. Step-by-step derivation
      1. +-commutative65.1%

        \[\leadsto 1 + \color{blue}{\left(0.20833333333333334 \cdot {x}^{4} + -0.5 \cdot {x}^{2}\right)} \]
      2. metadata-eval65.1%

        \[\leadsto 1 + \left(0.20833333333333334 \cdot {x}^{\color{blue}{\left(2 \cdot 2\right)}} + -0.5 \cdot {x}^{2}\right) \]
      3. pow-sqr65.1%

        \[\leadsto 1 + \left(0.20833333333333334 \cdot \color{blue}{\left({x}^{2} \cdot {x}^{2}\right)} + -0.5 \cdot {x}^{2}\right) \]
      4. unpow265.1%

        \[\leadsto 1 + \left(0.20833333333333334 \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot {x}^{2}\right) + -0.5 \cdot {x}^{2}\right) \]
      5. unpow265.1%

        \[\leadsto 1 + \left(0.20833333333333334 \cdot \left(\left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot x\right)}\right) + -0.5 \cdot {x}^{2}\right) \]
      6. associate-*r*65.1%

        \[\leadsto 1 + \left(\color{blue}{\left(0.20833333333333334 \cdot \left(x \cdot x\right)\right) \cdot \left(x \cdot x\right)} + -0.5 \cdot {x}^{2}\right) \]
      7. *-commutative65.1%

        \[\leadsto 1 + \left(\color{blue}{\left(x \cdot x\right) \cdot \left(0.20833333333333334 \cdot \left(x \cdot x\right)\right)} + -0.5 \cdot {x}^{2}\right) \]
      8. unpow265.1%

        \[\leadsto 1 + \left(\left(x \cdot x\right) \cdot \left(0.20833333333333334 \cdot \left(x \cdot x\right)\right) + -0.5 \cdot \color{blue}{\left(x \cdot x\right)}\right) \]
      9. *-commutative65.1%

        \[\leadsto 1 + \left(\left(x \cdot x\right) \cdot \left(0.20833333333333334 \cdot \left(x \cdot x\right)\right) + \color{blue}{\left(x \cdot x\right) \cdot -0.5}\right) \]
      10. distribute-lft-out65.3%

        \[\leadsto 1 + \color{blue}{\left(x \cdot x\right) \cdot \left(0.20833333333333334 \cdot \left(x \cdot x\right) + -0.5\right)} \]
      11. *-commutative65.3%

        \[\leadsto 1 + \left(x \cdot x\right) \cdot \left(\color{blue}{\left(x \cdot x\right) \cdot 0.20833333333333334} + -0.5\right) \]
      12. associate-*l*65.3%

        \[\leadsto 1 + \left(x \cdot x\right) \cdot \left(\color{blue}{x \cdot \left(x \cdot 0.20833333333333334\right)} + -0.5\right) \]
      13. fma-def65.3%

        \[\leadsto 1 + \left(x \cdot x\right) \cdot \color{blue}{\mathsf{fma}\left(x, x \cdot 0.20833333333333334, -0.5\right)} \]
    9. Simplified65.3%

      \[\leadsto 1 + \color{blue}{\left(x \cdot x\right) \cdot \mathsf{fma}\left(x, x \cdot 0.20833333333333334, -0.5\right)} \]
    10. Step-by-step derivation
      1. fma-udef65.3%

        \[\leadsto 1 + \left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot \left(x \cdot 0.20833333333333334\right) + -0.5\right)} \]
    11. Applied egg-rr65.3%

      \[\leadsto 1 + \left(x \cdot x\right) \cdot \color{blue}{\left(x \cdot \left(x \cdot 0.20833333333333334\right) + -0.5\right)} \]

    if 340 < x

    1. Initial program 100.0%

      \[\frac{2}{e^{x} + e^{-x}} \]
    2. Applied egg-rr100.0%

      \[\leadsto \color{blue}{0} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 340:\\ \;\;\;\;1 + \left(x \cdot x\right) \cdot \left(x \cdot \left(x \cdot 0.20833333333333334\right) + -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \frac{2}{e^{x} + e^{-x}} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 (/ 2.0 (+ (exp x) (exp (- x)))))
x = abs(x);
double code(double x) {
	return 2.0 / (exp(x) + exp(-x));
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    code = 2.0d0 / (exp(x) + exp(-x))
end function
x = Math.abs(x);
public static double code(double x) {
	return 2.0 / (Math.exp(x) + Math.exp(-x));
}
x = abs(x)
def code(x):
	return 2.0 / (math.exp(x) + math.exp(-x))
x = abs(x)
function code(x)
	return Float64(2.0 / Float64(exp(x) + exp(Float64(-x))))
end
x = abs(x)
function tmp = code(x)
	tmp = 2.0 / (exp(x) + exp(-x));
end
NOTE: x should be positive before calling this function
code[x_] := N[(2.0 / N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
\frac{2}{e^{x} + e^{-x}}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{2}{e^{x} + e^{-x}} \]
  2. Final simplification100.0%

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

Alternative 3: 99.2% accurate, 22.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.45:\\ \;\;\;\;1 + \left(x \cdot x\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 (if (<= x 1.45) (+ 1.0 (* (* x x) -0.5)) 0.0))
x = abs(x);
double code(double x) {
	double tmp;
	if (x <= 1.45) {
		tmp = 1.0 + ((x * x) * -0.5);
	} else {
		tmp = 0.0;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 1.45d0) then
        tmp = 1.0d0 + ((x * x) * (-0.5d0))
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
	double tmp;
	if (x <= 1.45) {
		tmp = 1.0 + ((x * x) * -0.5);
	} else {
		tmp = 0.0;
	}
	return tmp;
}
x = abs(x)
def code(x):
	tmp = 0
	if x <= 1.45:
		tmp = 1.0 + ((x * x) * -0.5)
	else:
		tmp = 0.0
	return tmp
x = abs(x)
function code(x)
	tmp = 0.0
	if (x <= 1.45)
		tmp = Float64(1.0 + Float64(Float64(x * x) * -0.5));
	else
		tmp = 0.0;
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 1.45)
		tmp = 1.0 + ((x * x) * -0.5);
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[x, 1.45], N[(1.0 + N[(N[(x * x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], 0.0]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.45:\\
\;\;\;\;1 + \left(x \cdot x\right) \cdot -0.5\\

\mathbf{else}:\\
\;\;\;\;0\\


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

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot {x}^{2}} \]
    3. Step-by-step derivation
      1. unpow265.2%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(x \cdot x\right)} \]
    4. Simplified65.2%

      \[\leadsto \color{blue}{1 + -0.5 \cdot \left(x \cdot x\right)} \]

    if 1.44999999999999996 < x

    1. Initial program 100.0%

      \[\frac{2}{e^{x} + e^{-x}} \]
    2. Applied egg-rr100.0%

      \[\leadsto \color{blue}{0} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.45:\\ \;\;\;\;1 + \left(x \cdot x\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]

Alternative 4: 99.0% accurate, 67.3× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 350:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 (if (<= x 350.0) 1.0 0.0))
x = abs(x);
double code(double x) {
	double tmp;
	if (x <= 350.0) {
		tmp = 1.0;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 350.0d0) then
        tmp = 1.0d0
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
	double tmp;
	if (x <= 350.0) {
		tmp = 1.0;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
x = abs(x)
def code(x):
	tmp = 0
	if x <= 350.0:
		tmp = 1.0
	else:
		tmp = 0.0
	return tmp
x = abs(x)
function code(x)
	tmp = 0.0
	if (x <= 350.0)
		tmp = 1.0;
	else
		tmp = 0.0;
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 350.0)
		tmp = 1.0;
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[x, 350.0], 1.0, 0.0]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 350:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;0\\


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

    1. Initial program 100.0%

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

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

    if 350 < x

    1. Initial program 100.0%

      \[\frac{2}{e^{x} + e^{-x}} \]
    2. Applied egg-rr100.0%

      \[\leadsto \color{blue}{0} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 350:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]

Alternative 5: 51.3% accurate, 206.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ 0 \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 0.0)
x = abs(x);
double code(double x) {
	return 0.0;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.0d0
end function
x = Math.abs(x);
public static double code(double x) {
	return 0.0;
}
x = abs(x)
def code(x):
	return 0.0
x = abs(x)
function code(x)
	return 0.0
end
x = abs(x)
function tmp = code(x)
	tmp = 0.0;
end
NOTE: x should be positive before calling this function
code[x_] := 0.0
\begin{array}{l}
x = |x|\\
\\
0
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{2}{e^{x} + e^{-x}} \]
  2. Applied egg-rr55.7%

    \[\leadsto \color{blue}{0} \]
  3. Final simplification55.7%

    \[\leadsto 0 \]

Reproduce

?
herbie shell --seed 2023263 
(FPCore (x)
  :name "Hyperbolic secant"
  :precision binary64
  (/ 2.0 (+ (exp x) (exp (- x)))))