
(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 6 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 2e+39) (/ 2.0 (fma x x 2.0)) (/ 2.0 (cbrt (pow x 6.0)))))
double code(double x) {
double tmp;
if (x <= 2e+39) {
tmp = 2.0 / fma(x, x, 2.0);
} else {
tmp = 2.0 / cbrt(pow(x, 6.0));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 2e+39) tmp = Float64(2.0 / fma(x, x, 2.0)); else tmp = Float64(2.0 / cbrt((x ^ 6.0))); end return tmp end
code[x_] := If[LessEqual[x, 2e+39], N[(2.0 / N[(x * x + 2.0), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[Power[N[Power[x, 6.0], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2 \cdot 10^{+39}:\\
\;\;\;\;\frac{2}{\mathsf{fma}\left(x, x, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{\sqrt[3]{{x}^{6}}}\\
\end{array}
\end{array}
if x < 1.99999999999999988e39Initial program 100.0%
Taylor expanded in x around 0 78.5%
+-commutative78.5%
unpow278.5%
fma-define78.5%
Simplified78.5%
if 1.99999999999999988e39 < x Initial program 100.0%
Taylor expanded in x around 0 70.7%
+-commutative70.7%
unpow270.7%
fma-define70.7%
Simplified70.7%
Taylor expanded in x around inf 70.7%
add-cbrt-cube96.9%
pow396.9%
pow-pow96.9%
metadata-eval96.9%
Applied egg-rr96.9%
Final simplification82.9%
(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.6%
+-commutative76.6%
unpow276.6%
fma-define76.6%
Simplified76.6%
Final simplification76.6%
(FPCore (x) :precision binary64 (if (<= x 1.25) (+ 1.0 (* (* x x) -0.5)) (/ 2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 1.25) {
tmp = 1.0 + ((x * x) * -0.5);
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.25d0) then
tmp = 1.0d0 + ((x * x) * (-0.5d0))
else
tmp = 2.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.25) {
tmp = 1.0 + ((x * x) * -0.5);
} else {
tmp = 2.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.25: tmp = 1.0 + ((x * x) * -0.5) else: tmp = 2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 1.25) tmp = Float64(1.0 + Float64(Float64(x * x) * -0.5)); else tmp = Float64(2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.25) tmp = 1.0 + ((x * x) * -0.5); else tmp = 2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.25], N[(1.0 + N[(N[(x * x), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.25:\\
\;\;\;\;1 + \left(x \cdot x\right) \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x \cdot x}\\
\end{array}
\end{array}
if x < 1.25Initial program 100.0%
Taylor expanded in x around 0 68.8%
*-commutative68.8%
Simplified68.8%
unpow218.1%
Applied egg-rr68.8%
if 1.25 < x Initial program 100.0%
Taylor expanded in x around 0 59.9%
+-commutative59.9%
unpow259.9%
fma-define59.9%
Simplified59.9%
Taylor expanded in x around inf 59.8%
unpow259.8%
Applied egg-rr59.8%
Final simplification66.2%
(FPCore (x) :precision binary64 (if (<= x 1.4) 1.0 (/ 2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 1.4) {
tmp = 1.0;
} else {
tmp = 2.0 / (x * 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 = 2.0d0 / (x * x)
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 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.4: tmp = 1.0 else: tmp = 2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 1.4) tmp = 1.0; else tmp = Float64(2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.4) tmp = 1.0; else tmp = 2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.4], 1.0, N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.4:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x \cdot x}\\
\end{array}
\end{array}
if x < 1.3999999999999999Initial program 100.0%
Taylor expanded in x around 0 68.9%
if 1.3999999999999999 < x Initial program 100.0%
Taylor expanded in x around 0 59.9%
+-commutative59.9%
unpow259.9%
fma-define59.9%
Simplified59.9%
Taylor expanded in x around inf 59.8%
unpow259.8%
Applied egg-rr59.8%
Final simplification66.3%
(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.2%
Final simplification50.2%
herbie shell --seed 2024077
(FPCore (x)
:name "Hyperbolic secant"
:precision binary64
(/ 2.0 (+ (exp x) (exp (- x)))))