
(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 135000000.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 <= 135000000.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 <= 135000000.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, 135000000.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 135000000:\\
\;\;\;\;\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 < 1.35e8Initial program 100.0%
Taylor expanded in x around 0 78.2%
+-commutative78.2%
unpow278.2%
fma-define78.2%
Simplified78.2%
if 1.35e8 < x Initial program 100.0%
Taylor expanded in x around 0 55.8%
+-commutative55.8%
unpow255.8%
fma-define55.8%
Simplified55.8%
Taylor expanded in x around inf 55.8%
add-sqr-sqrt55.8%
sqrt-unprod79.9%
frac-times79.9%
metadata-eval79.9%
pow-sqr79.9%
metadata-eval79.9%
Applied egg-rr79.9%
pow1/279.9%
div-inv79.9%
unpow-prod-down79.9%
pow-flip79.9%
pow-pow53.6%
metadata-eval53.6%
metadata-eval53.6%
unpow1/253.6%
metadata-eval53.6%
expm1-log1p-u53.6%
*-commutative53.6%
expm1-undefine100.0%
Applied egg-rr100.0%
Final simplification83.5%
(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 66.5%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 54.9%
+-commutative54.9%
unpow254.9%
fma-define54.9%
Simplified54.9%
Taylor expanded in x around inf 54.9%
clear-num54.9%
associate-/r/54.9%
pow-flip52.8%
metadata-eval52.8%
Applied egg-rr52.8%
Final simplification63.1%
(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 66.5%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 54.9%
+-commutative54.9%
unpow254.9%
fma-define54.9%
Simplified54.9%
Taylor expanded in x around inf 54.9%
Final simplification63.6%
(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 72.8%
+-commutative72.8%
unpow272.8%
fma-define72.8%
Simplified72.8%
Final simplification72.8%
(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 66.5%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 54.9%
+-commutative54.9%
unpow254.9%
fma-define54.9%
Simplified54.9%
Taylor expanded in x around inf 54.9%
add-sqr-sqrt54.9%
sqrt-unprod78.7%
frac-times78.7%
metadata-eval78.7%
pow-sqr78.7%
metadata-eval78.7%
Applied egg-rr78.7%
pow1/278.7%
div-inv78.7%
unpow-prod-down78.7%
pow-flip78.7%
pow-pow52.8%
metadata-eval52.8%
metadata-eval52.8%
unpow1/252.8%
metadata-eval52.8%
*-commutative52.8%
sqr-pow52.8%
associate-*l*52.8%
metadata-eval52.8%
unpow-152.8%
metadata-eval52.8%
unpow-152.8%
Applied egg-rr52.8%
Final simplification63.1%
(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.9%
Final simplification50.9%
herbie shell --seed 2024059
(FPCore (x)
:name "Hyperbolic secant"
:precision binary64
(/ 2.0 (+ (exp x) (exp (- x)))))