
(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}}
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}}
(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}
Initial program 100.0%
Applied rewrites100.0%
(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}
if (/.f64 #s(literal 2 binary64) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < 1.9999999999999999e-6Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites49.7%
Applied rewrites49.7%
Applied rewrites52.1%
if 1.9999999999999999e-6 < (/.f64 #s(literal 2 binary64) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites49.7%
Applied rewrites49.7%
(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}
if (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) < 1.6939281354541241e129Initial program 100.0%
Applied rewrites74.3%
Applied rewrites50.1%
if 1.6939281354541241e129 < (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites49.7%
Applied rewrites49.7%
Applied rewrites52.1%
(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|}
Initial program 100.0%
Applied rewrites74.3%
Applied rewrites74.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
Initial program 100.0%
Applied rewrites74.3%
Applied rewrites50.1%
(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
Initial program 100.0%
Applied rewrites74.3%
Applied rewrites73.3%
Applied rewrites8.1%
Evaluated real constant8.1%
herbie shell --seed 2026074 +o generate:egglog
(FPCore (x)
:name "Hyperbolic secant"
:precision binary64
(/ 2.0 (+ (exp x) (exp (- x)))))