
(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:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(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}
Initial program 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 40000000.0) (/ 2.0 (fma x x 2.0)) (+ (+ 1.0 (* 2.0 (pow x -2.0))) -1.0)))
double code(double x) {
double tmp;
if (x <= 40000000.0) {
tmp = 2.0 / fma(x, x, 2.0);
} else {
tmp = (1.0 + (2.0 * pow(x, -2.0))) + -1.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 40000000.0) tmp = Float64(2.0 / fma(x, x, 2.0)); else tmp = Float64(Float64(1.0 + Float64(2.0 * (x ^ -2.0))) + -1.0); end return tmp end
code[x_] := If[LessEqual[x, 40000000.0], N[(2.0 / N[(x * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 + N[(2.0 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 40000000:\\
\;\;\;\;\frac{2}{\mathsf{fma}\left(x, x, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(1 + 2 \cdot {x}^{-2}\right) + -1\\
\end{array}
\end{array}
if x < 4e7Initial program 100.0%
Taylor expanded in x around 0 82.6%
+-commutative82.6%
unpow282.6%
fma-define82.6%
Simplified82.6%
if 4e7 < x Initial program 100.0%
Taylor expanded in x around 0 55.6%
+-commutative55.6%
unpow255.6%
fma-define55.6%
Simplified55.6%
Taylor expanded in x around inf 55.6%
add-sqr-sqrt55.6%
sqrt-unprod73.7%
frac-times78.3%
metadata-eval78.3%
pow-sqr78.3%
metadata-eval78.3%
Applied egg-rr78.3%
pow1/278.3%
div-inv78.3%
unpow-prod-down78.3%
pow-flip73.7%
pow-pow55.6%
metadata-eval55.6%
metadata-eval55.6%
unpow1/255.6%
metadata-eval55.6%
expm1-log1p-u55.6%
*-commutative55.6%
expm1-undefine100.0%
Applied egg-rr100.0%
Final simplification86.8%
(FPCore (x) :precision binary64 (if (<= x 1.4) 1.0 (* 2.0 (pow x -2.0))))
double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = 2.0 * pow(x, -2.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.4d0) then
tmp = 1.0d0
else
tmp = 2.0d0 * (x ** (-2.0d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = 2.0 * Math.pow(x, -2.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4: tmp = 1.0 else: tmp = 2.0 * math.pow(x, -2.0) return tmp
function code(x) tmp = 0.0 if (x <= 1.4) tmp = 1.0; else tmp = Float64(2.0 * (x ^ -2.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4) tmp = 1.0; else tmp = 2.0 * (x ^ -2.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4], 1.0, N[(2.0 * N[Power[x, -2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;2 \cdot {x}^{-2}\\
\end{array}
\end{array}
if x < 1.3999999999999999Initial program 100.0%
Taylor expanded in x around 0 65.3%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 54.7%
+-commutative54.7%
unpow254.7%
fma-define54.7%
Simplified54.7%
Taylor expanded in x around inf 54.7%
clear-num54.7%
associate-/r/54.7%
pow-flip54.7%
metadata-eval54.7%
Applied egg-rr54.7%
Final simplification62.7%
(FPCore (x) :precision binary64 (if (<= x 1.4) 1.0 (/ 2.0 (pow x 2.0))))
double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = 2.0 / pow(x, 2.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.4d0) then
tmp = 1.0d0
else
tmp = 2.0d0 / (x ** 2.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = 2.0 / Math.pow(x, 2.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4: tmp = 1.0 else: tmp = 2.0 / math.pow(x, 2.0) return tmp
function code(x) tmp = 0.0 if (x <= 1.4) tmp = 1.0; else tmp = Float64(2.0 / (x ^ 2.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4) tmp = 1.0; else tmp = 2.0 / (x ^ 2.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4], 1.0, N[(2.0 / N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{{x}^{2}}\\
\end{array}
\end{array}
if x < 1.3999999999999999Initial program 100.0%
Taylor expanded in x around 0 65.3%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 54.7%
+-commutative54.7%
unpow254.7%
fma-define54.7%
Simplified54.7%
Taylor expanded in x around inf 54.7%
Final simplification62.7%
(FPCore (x) :precision binary64 (/ 2.0 (fma x x 2.0)))
double code(double x) {
return 2.0 / fma(x, x, 2.0);
}
function code(x) return Float64(2.0 / fma(x, x, 2.0)) end
code[x_] := N[(2.0 / N[(x * x + 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\mathsf{fma}\left(x, x, 2\right)}
\end{array}
Initial program 100.0%
Taylor expanded in x around 0 76.0%
+-commutative76.0%
unpow276.0%
fma-define76.0%
Simplified76.0%
Final simplification76.0%
(FPCore (x) :precision binary64 (if (<= x 1.4) 1.0 (* (/ 1.0 x) (* 2.0 (/ 1.0 x)))))
double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = (1.0 / x) * (2.0 * (1.0 / x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.4d0) then
tmp = 1.0d0
else
tmp = (1.0d0 / x) * (2.0d0 * (1.0d0 / x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = (1.0 / x) * (2.0 * (1.0 / x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4: tmp = 1.0 else: tmp = (1.0 / x) * (2.0 * (1.0 / x)) return tmp
function code(x) tmp = 0.0 if (x <= 1.4) tmp = 1.0; else tmp = Float64(Float64(1.0 / x) * Float64(2.0 * Float64(1.0 / x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4) tmp = 1.0; else tmp = (1.0 / x) * (2.0 * (1.0 / x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4], 1.0, N[(N[(1.0 / x), $MachinePrecision] * N[(2.0 * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x} \cdot \left(2 \cdot \frac{1}{x}\right)\\
\end{array}
\end{array}
if x < 1.3999999999999999Initial program 100.0%
Taylor expanded in x around 0 65.3%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 54.7%
+-commutative54.7%
unpow254.7%
fma-define54.7%
Simplified54.7%
Taylor expanded in x around inf 54.7%
add-sqr-sqrt54.7%
sqrt-unprod72.6%
frac-times77.1%
metadata-eval77.1%
pow-sqr77.1%
metadata-eval77.1%
Applied egg-rr77.1%
pow1/277.1%
div-inv77.1%
unpow-prod-down77.1%
pow-flip72.6%
pow-pow54.7%
metadata-eval54.7%
metadata-eval54.7%
unpow1/254.7%
metadata-eval54.7%
*-commutative54.7%
sqr-pow54.7%
associate-*l*54.7%
metadata-eval54.7%
unpow-154.7%
metadata-eval54.7%
unpow-154.7%
Applied egg-rr54.7%
Final simplification62.7%
(FPCore (x) :precision binary64 1.0)
double code(double x) {
return 1.0;
}
real(8) function code(x)
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
\begin{array}{l}
\\
1
\end{array}
Initial program 100.0%
Taylor expanded in x around 0 50.0%
Final simplification50.0%
herbie shell --seed 2024067
(FPCore (x)
:name "Hyperbolic secant"
:precision binary64
(/ 2.0 (+ (exp x) (exp (- x)))))