
(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 7 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))) (t_1 (/ (- (exp x) t_0) (+ (exp x) t_0))))
(if (<= t_1 -0.02)
t_1
(/
(* x (+ (* x (* x 0.3333333333333333)) 2.0))
(/ (- 4.0 (* (* x x) (* x x))) (- 2.0 (* x x)))))))
double code(double x) {
double t_0 = exp(-x);
double t_1 = (exp(x) - t_0) / (exp(x) + t_0);
double tmp;
if (t_1 <= -0.02) {
tmp = t_1;
} else {
tmp = (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x)));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = exp(-x)
t_1 = (exp(x) - t_0) / (exp(x) + t_0)
if (t_1 <= (-0.02d0)) then
tmp = t_1
else
tmp = (x * ((x * (x * 0.3333333333333333d0)) + 2.0d0)) / ((4.0d0 - ((x * x) * (x * x))) / (2.0d0 - (x * x)))
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.exp(-x);
double t_1 = (Math.exp(x) - t_0) / (Math.exp(x) + t_0);
double tmp;
if (t_1 <= -0.02) {
tmp = t_1;
} else {
tmp = (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x)));
}
return tmp;
}
def code(x): t_0 = math.exp(-x) t_1 = (math.exp(x) - t_0) / (math.exp(x) + t_0) tmp = 0 if t_1 <= -0.02: tmp = t_1 else: tmp = (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x))) return tmp
function code(x) t_0 = exp(Float64(-x)) t_1 = Float64(Float64(exp(x) - t_0) / Float64(exp(x) + t_0)) tmp = 0.0 if (t_1 <= -0.02) tmp = t_1; else tmp = Float64(Float64(x * Float64(Float64(x * Float64(x * 0.3333333333333333)) + 2.0)) / Float64(Float64(4.0 - Float64(Float64(x * x) * Float64(x * x))) / Float64(2.0 - Float64(x * x)))); end return tmp end
function tmp_2 = code(x) t_0 = exp(-x); t_1 = (exp(x) - t_0) / (exp(x) + t_0); tmp = 0.0; if (t_1 <= -0.02) tmp = t_1; else tmp = (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x))); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[Exp[x], $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.02], t$95$1, N[(N[(x * N[(N[(x * N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision] / N[(N[(4.0 - N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 - N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
t_1 := \frac{e^{x} - t_0}{e^{x} + t_0}\\
\mathbf{if}\;t_1 \leq -0.02:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;\frac{x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333\right) + 2\right)}{\frac{4 - \left(x \cdot x\right) \cdot \left(x \cdot x\right)}{2 - x \cdot x}}\\
\end{array}
\end{array}
if (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) < -0.0200000000000000004Initial program 99.1%
if -0.0200000000000000004 < (/.f64 (-.f64 (exp.f64 x) (exp.f64 (neg.f64 x))) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))) Initial program 7.0%
Taylor expanded in x around 0 6.6%
unpow26.6%
Simplified6.6%
Taylor expanded in x around 0 97.8%
unpow397.8%
unpow297.8%
associate-*r*97.8%
distribute-rgt-out97.8%
*-commutative97.8%
unpow297.8%
associate-*l*97.8%
fma-def97.8%
Simplified97.8%
fma-udef97.8%
Applied egg-rr97.8%
flip-+97.8%
metadata-eval97.8%
Applied egg-rr97.8%
Final simplification97.8%
(FPCore (x) :precision binary64 (* (+ (* 0.016666666666666666 (pow x 5.0)) (+ (* 0.3333333333333333 (pow x 3.0)) (* x 2.0))) (/ 0.5 (cosh x))))
double code(double x) {
return ((0.016666666666666666 * pow(x, 5.0)) + ((0.3333333333333333 * pow(x, 3.0)) + (x * 2.0))) * (0.5 / cosh(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((0.016666666666666666d0 * (x ** 5.0d0)) + ((0.3333333333333333d0 * (x ** 3.0d0)) + (x * 2.0d0))) * (0.5d0 / cosh(x))
end function
public static double code(double x) {
return ((0.016666666666666666 * Math.pow(x, 5.0)) + ((0.3333333333333333 * Math.pow(x, 3.0)) + (x * 2.0))) * (0.5 / Math.cosh(x));
}
def code(x): return ((0.016666666666666666 * math.pow(x, 5.0)) + ((0.3333333333333333 * math.pow(x, 3.0)) + (x * 2.0))) * (0.5 / math.cosh(x))
function code(x) return Float64(Float64(Float64(0.016666666666666666 * (x ^ 5.0)) + Float64(Float64(0.3333333333333333 * (x ^ 3.0)) + Float64(x * 2.0))) * Float64(0.5 / cosh(x))) end
function tmp = code(x) tmp = ((0.016666666666666666 * (x ^ 5.0)) + ((0.3333333333333333 * (x ^ 3.0)) + (x * 2.0))) * (0.5 / cosh(x)); end
code[x_] := N[(N[(N[(0.016666666666666666 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(0.5 / N[Cosh[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.016666666666666666 \cdot {x}^{5} + \left(0.3333333333333333 \cdot {x}^{3} + x \cdot 2\right)\right) \cdot \frac{0.5}{\cosh x}
\end{array}
Initial program 9.1%
Taylor expanded in x around 0 96.2%
div-inv96.2%
fma-def96.2%
fma-def96.2%
fma-def96.2%
*-commutative96.2%
cosh-undef96.2%
Applied egg-rr96.2%
fma-udef96.2%
*-commutative96.2%
fma-def96.2%
fma-udef96.2%
*-commutative96.2%
fma-def96.2%
fma-udef96.2%
*-commutative96.2%
*-commutative96.2%
fma-def96.2%
*-commutative96.2%
associate-/r*96.2%
metadata-eval96.2%
Simplified96.2%
Taylor expanded in x around 0 96.1%
Final simplification96.1%
(FPCore (x) :precision binary64 (/ (* x (+ (* x (* x 0.3333333333333333)) 2.0)) (/ (- 4.0 (* (* x x) (* x x))) (- 2.0 (* x x)))))
double code(double x) {
return (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * ((x * (x * 0.3333333333333333d0)) + 2.0d0)) / ((4.0d0 - ((x * x) * (x * x))) / (2.0d0 - (x * x)))
end function
public static double code(double x) {
return (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x)));
}
def code(x): return (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x)))
function code(x) return Float64(Float64(x * Float64(Float64(x * Float64(x * 0.3333333333333333)) + 2.0)) / Float64(Float64(4.0 - Float64(Float64(x * x) * Float64(x * x))) / Float64(2.0 - Float64(x * x)))) end
function tmp = code(x) tmp = (x * ((x * (x * 0.3333333333333333)) + 2.0)) / ((4.0 - ((x * x) * (x * x))) / (2.0 - (x * x))); end
code[x_] := N[(N[(x * N[(N[(x * N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision] / N[(N[(4.0 - N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 - N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333\right) + 2\right)}{\frac{4 - \left(x \cdot x\right) \cdot \left(x \cdot x\right)}{2 - x \cdot x}}
\end{array}
Initial program 9.1%
Taylor expanded in x around 0 6.9%
unpow26.9%
Simplified6.9%
Taylor expanded in x around 0 96.1%
unpow396.1%
unpow296.1%
associate-*r*96.1%
distribute-rgt-out96.1%
*-commutative96.1%
unpow296.1%
associate-*l*96.1%
fma-def96.1%
Simplified96.1%
fma-udef96.1%
Applied egg-rr96.1%
flip-+96.1%
metadata-eval96.1%
Applied egg-rr96.1%
Final simplification96.1%
(FPCore (x) :precision binary64 (/ (* x (+ (* x (* x 0.3333333333333333)) 2.0)) (+ 2.0 (* x x))))
double code(double x) {
return (x * ((x * (x * 0.3333333333333333)) + 2.0)) / (2.0 + (x * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * ((x * (x * 0.3333333333333333d0)) + 2.0d0)) / (2.0d0 + (x * x))
end function
public static double code(double x) {
return (x * ((x * (x * 0.3333333333333333)) + 2.0)) / (2.0 + (x * x));
}
def code(x): return (x * ((x * (x * 0.3333333333333333)) + 2.0)) / (2.0 + (x * x))
function code(x) return Float64(Float64(x * Float64(Float64(x * Float64(x * 0.3333333333333333)) + 2.0)) / Float64(2.0 + Float64(x * x))) end
function tmp = code(x) tmp = (x * ((x * (x * 0.3333333333333333)) + 2.0)) / (2.0 + (x * x)); end
code[x_] := N[(N[(x * N[(N[(x * N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision] / N[(2.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(x \cdot \left(x \cdot 0.3333333333333333\right) + 2\right)}{2 + x \cdot x}
\end{array}
Initial program 9.1%
Taylor expanded in x around 0 6.9%
unpow26.9%
Simplified6.9%
Taylor expanded in x around 0 96.1%
unpow396.1%
unpow296.1%
associate-*r*96.1%
distribute-rgt-out96.1%
*-commutative96.1%
unpow296.1%
associate-*l*96.1%
fma-def96.1%
Simplified96.1%
fma-udef96.1%
Applied egg-rr96.1%
Final simplification96.1%
(FPCore (x) :precision binary64 (/ (* x 2.0) (+ 2.0 (* x x))))
double code(double x) {
return (x * 2.0) / (2.0 + (x * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * 2.0d0) / (2.0d0 + (x * x))
end function
public static double code(double x) {
return (x * 2.0) / (2.0 + (x * x));
}
def code(x): return (x * 2.0) / (2.0 + (x * x))
function code(x) return Float64(Float64(x * 2.0) / Float64(2.0 + Float64(x * x))) end
function tmp = code(x) tmp = (x * 2.0) / (2.0 + (x * x)); end
code[x_] := N[(N[(x * 2.0), $MachinePrecision] / N[(2.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot 2}{2 + x \cdot x}
\end{array}
Initial program 9.1%
Taylor expanded in x around 0 6.9%
unpow26.9%
Simplified6.9%
Taylor expanded in x around 0 95.8%
Final simplification95.8%
(FPCore (x) :precision binary64 13.5)
double code(double x) {
return 13.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 13.5d0
end function
public static double code(double x) {
return 13.5;
}
def code(x): return 13.5
function code(x) return 13.5 end
function tmp = code(x) tmp = 13.5; end
code[x_] := 13.5
\begin{array}{l}
\\
13.5
\end{array}
Initial program 9.1%
Taylor expanded in x around 0 6.9%
unpow26.9%
Simplified6.9%
Applied egg-rr3.8%
Taylor expanded in x around 0 3.8%
Final simplification3.8%
(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 9.1%
Taylor expanded in x around 0 95.7%
Final simplification95.7%
herbie shell --seed 2023285
(FPCore (x)
:name "Hyperbolic tangent"
:precision binary64
(/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))