Hyperbolic secant

Percentage Accurate: 100.0% → 100.0%
Time: 1.2min
Alternatives: 6
Speedup: 1.7×

Specification

?
\[\frac{2}{e^{x} + e^{-x}} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (/ 2.0 (+ (exp x) (exp (- x)))))
double code(double x) {
	return 2.0 / (exp(x) + exp(-x));
}
real(8) function code(x)
use fmin_fmax_functions
    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]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(2) / ((exp(x)) + (exp((- x))))
END code
\frac{2}{e^{x} + e^{-x}}

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 6 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?

\[\frac{2}{e^{x} + e^{-x}} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (/ 2.0 (+ (exp x) (exp (- x)))))
double code(double x) {
	return 2.0 / (exp(x) + exp(-x));
}
real(8) function code(x)
use fmin_fmax_functions
    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]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(2) / ((exp(x)) + (exp((- x))))
END code
\frac{2}{e^{x} + e^{-x}}

Alternative 1: 100.0% accurate, 1.7× speedup?

\[\frac{1}{\cosh x} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (/ 1.0 (cosh x)))
double code(double x) {
	return 1.0 / cosh(x);
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = 1.0d0 / cosh(x)
end function
public static double code(double x) {
	return 1.0 / Math.cosh(x);
}
def code(x):
	return 1.0 / math.cosh(x)
function code(x)
	return Float64(1.0 / cosh(x))
end
function tmp = code(x)
	tmp = 1.0 / cosh(x);
end
code[x_] := N[(1.0 / N[Cosh[x], $MachinePrecision]), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(1) / (((1) / (2)) * ((exp(x)) + ((1) / (exp(x)))))
END code
\frac{1}{\cosh x}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \frac{1}{\cosh x} \]
  3. Add Preprocessing

Alternative 2: 99.3% accurate, 0.7× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{2}{e^{x} + e^{-x}} \leq 2 \cdot 10^{-6}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \mathsf{fma}\left(x, x, -2\right)\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (/ 2.0 (+ (exp x) (exp (- x)))) 2e-6)
  0.0
  (* -0.5 (fma x x -2.0))))
double code(double x) {
	double tmp;
	if ((2.0 / (exp(x) + exp(-x))) <= 2e-6) {
		tmp = 0.0;
	} else {
		tmp = -0.5 * fma(x, x, -2.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(2.0 / Float64(exp(x) + exp(Float64(-x)))) <= 2e-6)
		tmp = 0.0;
	else
		tmp = Float64(-0.5 * fma(x, x, -2.0));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(2.0 / N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-6], 0.0, N[(-0.5 * N[(x * x + -2.0), $MachinePrecision]), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((2) / ((exp(x)) + (exp((- x))))) <= (199999999999999990949622365177251737122787744738161563873291015625e-71)) THEN (0) ELSE ((-5e-1) * ((x * x) + (-2))) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{2}{e^{x} + e^{-x}} \leq 2 \cdot 10^{-6}:\\
\;\;\;\;0\\

\mathbf{else}:\\
\;\;\;\;-0.5 \cdot \mathsf{fma}\left(x, x, -2\right)\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 #s(literal 2 binary64) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < 1.9999999999999999e-6

    1. Initial program 100.0%

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

      \[\leadsto 1 + \frac{-1}{2} \cdot {x}^{2} \]
    3. Applied rewrites49.7%

      \[\leadsto 1 + -0.5 \cdot {x}^{2} \]
    4. Applied rewrites49.7%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(x, x, -2\right), -0.5, 1\right) - 1 \]
    5. Applied rewrites52.1%

      \[\leadsto 0 \]

    if 1.9999999999999999e-6 < (/.f64 #s(literal 2 binary64) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x))))

    1. Initial program 100.0%

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

      \[\leadsto 1 + \frac{-1}{2} \cdot {x}^{2} \]
    3. Applied rewrites49.7%

      \[\leadsto 1 + -0.5 \cdot {x}^{2} \]
    4. Applied rewrites49.7%

      \[\leadsto -0.5 \cdot \mathsf{fma}\left(x, x, -2\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} \mathbf{if}\;e^{x} + e^{-x} \leq 1.693928135454124 \cdot 10^{+129}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (+ (exp x) (exp (- x))) 1.693928135454124e+129) 1.0 0.0))
double code(double x) {
	double tmp;
	if ((exp(x) + exp(-x)) <= 1.693928135454124e+129) {
		tmp = 1.0;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((exp(x) + exp(-x)) <= 1.693928135454124d+129) then
        tmp = 1.0d0
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((Math.exp(x) + Math.exp(-x)) <= 1.693928135454124e+129) {
		tmp = 1.0;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (math.exp(x) + math.exp(-x)) <= 1.693928135454124e+129:
		tmp = 1.0
	else:
		tmp = 0.0
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(exp(x) + exp(Float64(-x))) <= 1.693928135454124e+129)
		tmp = 1.0;
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((exp(x) + exp(-x)) <= 1.693928135454124e+129)
		tmp = 1.0;
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(N[Exp[x], $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 1.693928135454124e+129], 1.0, 0.0]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((exp(x)) + (exp((- x)))) <= (1693928135454124127948598477649350140655226641029623547788001815593093350879842447114418741200561560606239272507046029518643920896)) THEN (1) ELSE (0) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;e^{x} + e^{-x} \leq 1.693928135454124 \cdot 10^{+129}:\\
\;\;\;\;1\\

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


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

    1. Initial program 100.0%

      \[\frac{2}{e^{x} + e^{-x}} \]
    2. Applied rewrites74.3%

      \[\leadsto \frac{e^{x} \cdot 2}{e^{x + x} - -1} \]
    3. Applied rewrites50.1%

      \[\leadsto 1 \]

    if 1.6939281354541241e129 < (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))

    1. Initial program 100.0%

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

      \[\leadsto 1 + \frac{-1}{2} \cdot {x}^{2} \]
    3. Applied rewrites49.7%

      \[\leadsto 1 + -0.5 \cdot {x}^{2} \]
    4. Applied rewrites49.7%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(x, x, -2\right), -0.5, 1\right) - 1 \]
    5. Applied rewrites52.1%

      \[\leadsto 0 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 98.6% accurate, 2.2× speedup?

\[e^{-\left|x\right|} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (exp (- (fabs x))))
double code(double x) {
	return exp(-fabs(x));
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = exp(-abs(x))
end function
public static double code(double x) {
	return Math.exp(-Math.abs(x));
}
def code(x):
	return math.exp(-math.fabs(x))
function code(x)
	return exp(Float64(-abs(x)))
end
function tmp = code(x)
	tmp = exp(-abs(x));
end
code[x_] := N[Exp[(-N[Abs[x], $MachinePrecision])], $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	exp((- (abs(x))))
END code
e^{-\left|x\right|}
Derivation
  1. Initial program 100.0%

    \[\frac{2}{e^{x} + e^{-x}} \]
  2. Applied rewrites74.3%

    \[\leadsto \frac{e^{x} \cdot 2}{e^{x + x} - -1} \]
  3. Applied rewrites74.1%

    \[\leadsto e^{-x} \]
  4. Add Preprocessing

Alternative 5: 50.1% accurate, 30.4× speedup?

\[1 \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  1.0)
double code(double x) {
	return 1.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = 1.0d0
end function
public static double code(double x) {
	return 1.0;
}
def code(x):
	return 1.0
function code(x)
	return 1.0
end
function tmp = code(x)
	tmp = 1.0;
end
code[x_] := 1.0
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	1
END code
1
Derivation
  1. Initial program 100.0%

    \[\frac{2}{e^{x} + e^{-x}} \]
  2. Applied rewrites74.3%

    \[\leadsto \frac{e^{x} \cdot 2}{e^{x + x} - -1} \]
  3. Applied rewrites50.1%

    \[\leadsto 1 \]
  4. Add Preprocessing

Alternative 6: 8.1% accurate, 30.4× speedup?

\[0.00033546262790251185 \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  0.00033546262790251185)
double code(double x) {
	return 0.00033546262790251185;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = 0.00033546262790251185d0
end function
public static double code(double x) {
	return 0.00033546262790251185;
}
def code(x):
	return 0.00033546262790251185
function code(x)
	return 0.00033546262790251185
end
function tmp = code(x)
	tmp = 0.00033546262790251185;
end
code[x_] := 0.00033546262790251185
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	3354626279025118532235716362066568763111717998981475830078125e-64
END code
0.00033546262790251185
Derivation
  1. Initial program 100.0%

    \[\frac{2}{e^{x} + e^{-x}} \]
  2. Applied rewrites74.3%

    \[\leadsto \frac{e^{x} \cdot 2}{e^{x + x} - -1} \]
  3. Applied rewrites73.3%

    \[\leadsto e^{x} \]
  4. Applied rewrites8.1%

    \[\leadsto e^{-8} \]
  5. Evaluated real constant8.1%

    \[\leadsto 0.00033546262790251185 \]
  6. Add Preprocessing

Reproduce

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