
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
double code(double a, double b) {
return exp(a) / (exp(a) + exp(b));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / (exp(a) + exp(b))
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(a) + Math.exp(b));
}
def code(a, b): return math.exp(a) / (math.exp(a) + math.exp(b))
function code(a, b) return Float64(exp(a) / Float64(exp(a) + exp(b))) end
function tmp = code(a, b) tmp = exp(a) / (exp(a) + exp(b)); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{e^{a} + e^{b}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
double code(double a, double b) {
return exp(a) / (exp(a) + exp(b));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / (exp(a) + exp(b))
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(a) + Math.exp(b));
}
def code(a, b): return math.exp(a) / (math.exp(a) + math.exp(b))
function code(a, b) return Float64(exp(a) / Float64(exp(a) + exp(b))) end
function tmp = code(a, b) tmp = exp(a) / (exp(a) + exp(b)); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{e^{a} + e^{b}}
\end{array}
(FPCore (a b) :precision binary64 (/ -1.0 (- -1.0 (exp (- b a)))))
double code(double a, double b) {
return -1.0 / (-1.0 - exp((b - a)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (-1.0d0) / ((-1.0d0) - exp((b - a)))
end function
public static double code(double a, double b) {
return -1.0 / (-1.0 - Math.exp((b - a)));
}
def code(a, b): return -1.0 / (-1.0 - math.exp((b - a)))
function code(a, b) return Float64(-1.0 / Float64(-1.0 - exp(Float64(b - a)))) end
function tmp = code(a, b) tmp = -1.0 / (-1.0 - exp((b - a))); end
code[a_, b_] := N[(-1.0 / N[(-1.0 - N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{-1 - e^{b - a}}
\end{array}
Initial program 98.8%
add-exp-log98.8%
div-exp99.2%
Applied egg-rr99.2%
exp-diff98.8%
add-exp-log98.8%
clear-num98.8%
frac-2neg98.8%
metadata-eval98.8%
Applied egg-rr98.8%
rem-exp-log98.8%
exp-diff99.2%
sub-neg99.2%
+-commutative99.2%
prod-exp98.8%
rem-exp-log98.8%
distribute-lft-in71.5%
rec-exp71.5%
lft-mult-inverse99.2%
prod-exp100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (or (<= (exp b) 0.0) (not (<= (exp b) 1.0000000005))) (/ 1.0 (+ 1.0 (exp b))) (/ 1.0 (+ 1.0 (exp (- a))))))
double code(double a, double b) {
double tmp;
if ((exp(b) <= 0.0) || !(exp(b) <= 1.0000000005)) {
tmp = 1.0 / (1.0 + exp(b));
} else {
tmp = 1.0 / (1.0 + exp(-a));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((exp(b) <= 0.0d0) .or. (.not. (exp(b) <= 1.0000000005d0))) then
tmp = 1.0d0 / (1.0d0 + exp(b))
else
tmp = 1.0d0 / (1.0d0 + exp(-a))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((Math.exp(b) <= 0.0) || !(Math.exp(b) <= 1.0000000005)) {
tmp = 1.0 / (1.0 + Math.exp(b));
} else {
tmp = 1.0 / (1.0 + Math.exp(-a));
}
return tmp;
}
def code(a, b): tmp = 0 if (math.exp(b) <= 0.0) or not (math.exp(b) <= 1.0000000005): tmp = 1.0 / (1.0 + math.exp(b)) else: tmp = 1.0 / (1.0 + math.exp(-a)) return tmp
function code(a, b) tmp = 0.0 if ((exp(b) <= 0.0) || !(exp(b) <= 1.0000000005)) tmp = Float64(1.0 / Float64(1.0 + exp(b))); else tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-a)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((exp(b) <= 0.0) || ~((exp(b) <= 1.0000000005))) tmp = 1.0 / (1.0 + exp(b)); else tmp = 1.0 / (1.0 + exp(-a)); end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[N[Exp[b], $MachinePrecision], 0.0], N[Not[LessEqual[N[Exp[b], $MachinePrecision], 1.0000000005]], $MachinePrecision]], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0 \lor \neg \left(e^{b} \leq 1.0000000005\right):\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\end{array}
\end{array}
if (exp.f64 b) < 0.0 or 1.0000000005 < (exp.f64 b) Initial program 98.2%
Taylor expanded in a around 0 100.0%
if 0.0 < (exp.f64 b) < 1.0000000005Initial program 99.3%
add-exp-log99.3%
div-exp99.2%
Applied egg-rr99.2%
exp-diff99.3%
add-exp-log99.3%
clear-num99.3%
frac-2neg99.3%
metadata-eval99.3%
Applied egg-rr99.3%
rem-exp-log99.3%
exp-diff99.3%
sub-neg99.3%
+-commutative99.3%
prod-exp99.3%
rem-exp-log99.3%
distribute-lft-in65.5%
rec-exp65.5%
lft-mult-inverse100.0%
prod-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (exp a) 2e-8) (exp a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 2e-8) {
tmp = exp(a);
} else {
tmp = 1.0 / (1.0 + exp(b));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (exp(a) <= 2d-8) then
tmp = exp(a)
else
tmp = 1.0d0 / (1.0d0 + exp(b))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 2e-8) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 2e-8: tmp = math.exp(a) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 2e-8) tmp = exp(a); else tmp = Float64(1.0 / Float64(1.0 + exp(b))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 2e-8) tmp = exp(a); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 2e-8], N[Exp[a], $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 2 \cdot 10^{-8}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if (exp.f64 a) < 2e-8Initial program 98.6%
add-exp-log98.6%
div-exp98.6%
Applied egg-rr98.6%
Taylor expanded in a around inf 98.1%
if 2e-8 < (exp.f64 a) Initial program 98.9%
Taylor expanded in a around 0 96.7%
Final simplification97.1%
(FPCore (a b) :precision binary64 (if (<= (exp a) 5e-5) (exp a) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 5e-5) {
tmp = exp(a);
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (exp(a) <= 5d-5) then
tmp = exp(a)
else
tmp = 0.5d0 + (a * 0.25d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 5e-5) {
tmp = Math.exp(a);
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 5e-5: tmp = math.exp(a) else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 5e-5) tmp = exp(a); else tmp = Float64(0.5 + Float64(a * 0.25)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 5e-5) tmp = exp(a); else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 5e-5], N[Exp[a], $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 5 \cdot 10^{-5}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if (exp.f64 a) < 5.00000000000000024e-5Initial program 98.6%
add-exp-log98.6%
div-exp98.6%
Applied egg-rr98.6%
Taylor expanded in a around inf 96.8%
if 5.00000000000000024e-5 < (exp.f64 a) Initial program 98.9%
Taylor expanded in b around 0 56.3%
Taylor expanded in a around 0 55.2%
*-commutative55.2%
Simplified55.2%
Final simplification67.1%
(FPCore (a b) :precision binary64 (if (<= b -15.5) (exp a) (/ (exp a) 2.0)))
double code(double a, double b) {
double tmp;
if (b <= -15.5) {
tmp = exp(a);
} else {
tmp = exp(a) / 2.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-15.5d0)) then
tmp = exp(a)
else
tmp = exp(a) / 2.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -15.5) {
tmp = Math.exp(a);
} else {
tmp = Math.exp(a) / 2.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -15.5: tmp = math.exp(a) else: tmp = math.exp(a) / 2.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -15.5) tmp = exp(a); else tmp = Float64(exp(a) / 2.0); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -15.5) tmp = exp(a); else tmp = exp(a) / 2.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -15.5], N[Exp[a], $MachinePrecision], N[(N[Exp[a], $MachinePrecision] / 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -15.5:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{a}}{2}\\
\end{array}
\end{array}
if b < -15.5Initial program 97.3%
add-exp-log97.3%
div-exp97.4%
Applied egg-rr97.4%
Taylor expanded in a around inf 97.0%
if -15.5 < b Initial program 99.0%
Taylor expanded in b around 0 76.4%
Taylor expanded in a around 0 73.7%
Final simplification77.0%
(FPCore (a b) :precision binary64 (+ 0.5 (* a 0.25)))
double code(double a, double b) {
return 0.5 + (a * 0.25);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 0.5d0 + (a * 0.25d0)
end function
public static double code(double a, double b) {
return 0.5 + (a * 0.25);
}
def code(a, b): return 0.5 + (a * 0.25)
function code(a, b) return Float64(0.5 + Float64(a * 0.25)) end
function tmp = code(a, b) tmp = 0.5 + (a * 0.25); end
code[a_, b_] := N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + a \cdot 0.25
\end{array}
Initial program 98.8%
Taylor expanded in b around 0 68.0%
Taylor expanded in a around 0 40.1%
*-commutative40.1%
Simplified40.1%
Final simplification40.1%
(FPCore (a b) :precision binary64 (/ 1.0 (- 2.0 a)))
double code(double a, double b) {
return 1.0 / (2.0 - a);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (2.0d0 - a)
end function
public static double code(double a, double b) {
return 1.0 / (2.0 - a);
}
def code(a, b): return 1.0 / (2.0 - a)
function code(a, b) return Float64(1.0 / Float64(2.0 - a)) end
function tmp = code(a, b) tmp = 1.0 / (2.0 - a); end
code[a_, b_] := N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 - a}
\end{array}
Initial program 98.8%
add-exp-log98.8%
div-exp99.2%
Applied egg-rr99.2%
exp-diff98.8%
add-exp-log98.8%
clear-num98.8%
frac-2neg98.8%
metadata-eval98.8%
Applied egg-rr98.8%
rem-exp-log98.8%
exp-diff99.2%
sub-neg99.2%
+-commutative99.2%
prod-exp98.8%
rem-exp-log98.8%
distribute-lft-in71.5%
rec-exp71.5%
lft-mult-inverse99.2%
prod-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 68.4%
Taylor expanded in a around 0 40.5%
mul-1-neg40.5%
unsub-neg40.5%
Simplified40.5%
Final simplification40.5%
(FPCore (a b) :precision binary64 0.5)
double code(double a, double b) {
return 0.5;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 0.5d0
end function
public static double code(double a, double b) {
return 0.5;
}
def code(a, b): return 0.5
function code(a, b) return 0.5 end
function tmp = code(a, b) tmp = 0.5; end
code[a_, b_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 98.8%
Taylor expanded in a around 0 78.7%
Taylor expanded in b around 0 39.3%
Final simplification39.3%
(FPCore (a b) :precision binary64 (/ 1.0 (+ 1.0 (exp (- b a)))))
double code(double a, double b) {
return 1.0 / (1.0 + exp((b - a)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (1.0d0 + exp((b - a)))
end function
public static double code(double a, double b) {
return 1.0 / (1.0 + Math.exp((b - a)));
}
def code(a, b): return 1.0 / (1.0 + math.exp((b - a)))
function code(a, b) return Float64(1.0 / Float64(1.0 + exp(Float64(b - a)))) end
function tmp = code(a, b) tmp = 1.0 / (1.0 + exp((b - a))); end
code[a_, b_] := N[(1.0 / N[(1.0 + N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + e^{b - a}}
\end{array}
herbie shell --seed 2023185
(FPCore (a b)
:name "Quotient of sum of exps"
:precision binary64
:herbie-target
(/ 1.0 (+ 1.0 (exp (- b a))))
(/ (exp a) (+ (exp a) (exp b))))