
(FPCore (x) :precision binary64 (let* ((t_0 (exp (- x)))) (/ (- (exp x) t_0) (+ (exp x) t_0))))
double code(double x) {
double t_0 = exp(-x);
return (exp(x) - t_0) / (exp(x) + t_0);
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = exp(-x)
code = (exp(x) - t_0) / (exp(x) + t_0)
end function
public static double code(double x) {
double t_0 = Math.exp(-x);
return (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
def code(x): t_0 = math.exp(-x) return (math.exp(x) - t_0) / (math.exp(x) + t_0)
function code(x) t_0 = exp(Float64(-x)) return Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0)) end
function tmp = code(x) t_0 = exp(-x); tmp = (exp(x) - t_0) / (exp(x) + t_0); end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t_0}{e^{x} + t_0}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (let* ((t_0 (exp (- x)))) (/ (- (exp x) t_0) (+ (exp x) t_0))))
double code(double x) {
double t_0 = exp(-x);
return (exp(x) - t_0) / (exp(x) + t_0);
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = exp(-x)
code = (exp(x) - t_0) / (exp(x) + t_0)
end function
public static double code(double x) {
double t_0 = Math.exp(-x);
return (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
def code(x): t_0 = math.exp(-x) return (math.exp(x) - t_0) / (math.exp(x) + t_0)
function code(x) t_0 = exp(Float64(-x)) return Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0)) end
function tmp = code(x) t_0 = exp(-x); tmp = (exp(x) - t_0) / (exp(x) + t_0); end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\frac{e^{x} - t_0}{e^{x} + t_0}
\end{array}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= x 0.00092)
(/ (+ (* x 2.0) (* 0.3333333333333333 (pow x 3.0))) (+ 2.0 (* x x)))
(/ (- (exp x) t_0) (+ (exp x) t_0)))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if (x <= 0.00092) {
tmp = ((x * 2.0) + (0.3333333333333333 * pow(x, 3.0))) / (2.0 + (x * x));
} else {
tmp = (exp(x) - t_0) / (exp(x) + t_0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = exp(-x)
if (x <= 0.00092d0) then
tmp = ((x * 2.0d0) + (0.3333333333333333d0 * (x ** 3.0d0))) / (2.0d0 + (x * x))
else
tmp = (exp(x) - t_0) / (exp(x) + t_0)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.exp(-x);
double tmp;
if (x <= 0.00092) {
tmp = ((x * 2.0) + (0.3333333333333333 * Math.pow(x, 3.0))) / (2.0 + (x * x));
} else {
tmp = (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
}
return tmp;
}
def code(x): t_0 = math.exp(-x) tmp = 0 if x <= 0.00092: tmp = ((x * 2.0) + (0.3333333333333333 * math.pow(x, 3.0))) / (2.0 + (x * x)) else: tmp = (math.exp(x) - t_0) / (math.exp(x) + t_0) return tmp
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (x <= 0.00092) tmp = Float64(Float64(Float64(x * 2.0) + Float64(0.3333333333333333 * (x ^ 3.0))) / Float64(2.0 + Float64(x * x))); else tmp = Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0)); end return tmp end
function tmp_2 = code(x) t_0 = exp(-x); tmp = 0.0; if (x <= 0.00092) tmp = ((x * 2.0) + (0.3333333333333333 * (x ^ 3.0))) / (2.0 + (x * x)); else tmp = (exp(x) - t_0) / (exp(x) + t_0); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[x, 0.00092], N[(N[(N[(x * 2.0), $MachinePrecision] + N[(0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;x \leq 0.00092:\\
\;\;\;\;\frac{x \cdot 2 + 0.3333333333333333 \cdot {x}^{3}}{2 + x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{x} - t_0}{e^{x} + t_0}\\
\end{array}
\end{array}
if x < 9.2000000000000003e-4Initial program 8.0%
Taylor expanded in x around 0 7.8%
unpow27.8%
Simplified7.8%
Taylor expanded in x around 0 98.3%
if 9.2000000000000003e-4 < x Initial program 74.4%
Final simplification97.5%
(FPCore (x) :precision binary64 (/ (+ (* x 2.0) (* 0.3333333333333333 (pow x 3.0))) (+ 2.0 (* x x))))
double code(double x) {
return ((x * 2.0) + (0.3333333333333333 * pow(x, 3.0))) / (2.0 + (x * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x * 2.0d0) + (0.3333333333333333d0 * (x ** 3.0d0))) / (2.0d0 + (x * x))
end function
public static double code(double x) {
return ((x * 2.0) + (0.3333333333333333 * Math.pow(x, 3.0))) / (2.0 + (x * x));
}
def code(x): return ((x * 2.0) + (0.3333333333333333 * math.pow(x, 3.0))) / (2.0 + (x * x))
function code(x) return Float64(Float64(Float64(x * 2.0) + Float64(0.3333333333333333 * (x ^ 3.0))) / Float64(2.0 + Float64(x * x))) end
function tmp = code(x) tmp = ((x * 2.0) + (0.3333333333333333 * (x ^ 3.0))) / (2.0 + (x * x)); end
code[x_] := N[(N[(N[(x * 2.0), $MachinePrecision] + N[(0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot 2 + 0.3333333333333333 \cdot {x}^{3}}{2 + x \cdot x}
\end{array}
Initial program 10.1%
Taylor expanded in x around 0 7.8%
unpow27.8%
Simplified7.8%
Taylor expanded in x around 0 95.7%
Final simplification95.7%
(FPCore (x) :precision binary64 (/ (+ x x) (+ 2.0 (* x x))))
double code(double x) {
return (x + x) / (2.0 + (x * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x + x) / (2.0d0 + (x * x))
end function
public static double code(double x) {
return (x + x) / (2.0 + (x * x));
}
def code(x): return (x + x) / (2.0 + (x * x))
function code(x) return Float64(Float64(x + x) / Float64(2.0 + Float64(x * x))) end
function tmp = code(x) tmp = (x + x) / (2.0 + (x * x)); end
code[x_] := N[(N[(x + x), $MachinePrecision] / N[(2.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + x}{2 + x \cdot x}
\end{array}
Initial program 10.1%
Taylor expanded in x around 0 95.2%
count-295.2%
Simplified95.2%
Taylor expanded in x around 0 95.5%
unpow27.8%
Simplified95.5%
Final simplification95.5%
(FPCore (x) :precision binary64 2.25)
double code(double x) {
return 2.25;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.25d0
end function
public static double code(double x) {
return 2.25;
}
def code(x): return 2.25
function code(x) return 2.25 end
function tmp = code(x) tmp = 2.25; end
code[x_] := 2.25
\begin{array}{l}
\\
2.25
\end{array}
Initial program 10.1%
Taylor expanded in x around 0 95.5%
Taylor expanded in x around inf 5.6%
Applied egg-rr4.0%
Taylor expanded in x around 0 4.1%
Final simplification4.1%
(FPCore (x) :precision binary64 x)
double code(double x) {
return x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x
end function
public static double code(double x) {
return x;
}
def code(x): return x
function code(x) return x end
function tmp = code(x) tmp = x; end
code[x_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 10.1%
Taylor expanded in x around 0 95.5%
Final simplification95.5%
herbie shell --seed 2023240
(FPCore (x)
:name "Hyperbolic tangent"
:precision binary64
(/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))