
(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 10 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 (exp (fma (log (+ (exp a) (exp b))) -1.0 a)))
double code(double a, double b) {
return exp(fma(log((exp(a) + exp(b))), -1.0, a));
}
function code(a, b) return exp(fma(log(Float64(exp(a) + exp(b))), -1.0, a)) end
code[a_, b_] := N[Exp[N[(N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * -1.0 + a), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
e^{\mathsf{fma}\left(\log \left(e^{a} + e^{b}\right), -1, a\right)}
\end{array}
Initial program 98.8%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
clear-numN/A
associate-/r/N/A
inv-powN/A
pow-to-expN/A
lift-exp.f64N/A
prod-expN/A
lower-exp.f64N/A
lower-fma.f64N/A
lower-log.f6498.8
Applied rewrites98.8%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.02) (/ 1.0 (+ 1.0 (exp (- a)))) (exp (- (log1p (exp b))))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.02) {
tmp = 1.0 / (1.0 + exp(-a));
} else {
tmp = exp(-log1p(exp(b)));
}
return tmp;
}
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 0.02) {
tmp = 1.0 / (1.0 + Math.exp(-a));
} else {
tmp = Math.exp(-Math.log1p(Math.exp(b)));
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.02: tmp = 1.0 / (1.0 + math.exp(-a)) else: tmp = math.exp(-math.log1p(math.exp(b))) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.02) tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-a)))); else tmp = exp(Float64(-log1p(exp(b)))); end return tmp end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.02], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Exp[(-N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision])], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.02:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.0200000000000000004Initial program 96.5%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
clear-numN/A
lower-/.f64N/A
div-invN/A
lower-*.f64N/A
lift-exp.f64N/A
rec-expN/A
lower-exp.f64N/A
lower-neg.f6496.6
Applied rewrites96.6%
Taylor expanded in b around 0
+-commutativeN/A
distribute-lft-inN/A
exp-negN/A
lft-mult-inverseN/A
*-rgt-identityN/A
lower-+.f64N/A
neg-mul-1N/A
lower-exp.f64N/A
neg-mul-1N/A
lower-neg.f6496.7
Applied rewrites96.7%
if 0.0200000000000000004 < (exp.f64 a) Initial program 99.4%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
clear-numN/A
associate-/r/N/A
inv-powN/A
pow-to-expN/A
lift-exp.f64N/A
prod-expN/A
lower-exp.f64N/A
lower-fma.f64N/A
lower-log.f6499.5
Applied rewrites99.5%
Taylor expanded in a around 0
mul-1-negN/A
lower-neg.f64N/A
lower-log1p.f64N/A
lower-exp.f6499.1
Applied rewrites99.1%
(FPCore (a b) :precision binary64 (* (exp a) (/ 1.0 (+ (exp a) (exp b)))))
double code(double a, double b) {
return exp(a) * (1.0 / (exp(a) + exp(b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) * (1.0d0 / (exp(a) + exp(b)))
end function
public static double code(double a, double b) {
return Math.exp(a) * (1.0 / (Math.exp(a) + Math.exp(b)));
}
def code(a, b): return math.exp(a) * (1.0 / (math.exp(a) + math.exp(b)))
function code(a, b) return Float64(exp(a) * Float64(1.0 / Float64(exp(a) + exp(b)))) end
function tmp = code(a, b) tmp = exp(a) * (1.0 / (exp(a) + exp(b))); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] * N[(1.0 / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
e^{a} \cdot \frac{1}{e^{a} + e^{b}}
\end{array}
Initial program 98.8%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
div-invN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites98.8%
Final simplification98.8%
(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}
Initial program 98.8%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.02) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.02) {
tmp = 1.0 / (1.0 + exp(-a));
} else {
tmp = 1.0 / (exp(b) + 1.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (exp(a) <= 0.02d0) then
tmp = 1.0d0 / (1.0d0 + exp(-a))
else
tmp = 1.0d0 / (exp(b) + 1.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 0.02) {
tmp = 1.0 / (1.0 + Math.exp(-a));
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.02: tmp = 1.0 / (1.0 + math.exp(-a)) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.02) tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-a)))); else tmp = Float64(1.0 / Float64(exp(b) + 1.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 0.02) tmp = 1.0 / (1.0 + exp(-a)); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.02], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.02:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.0200000000000000004Initial program 96.5%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
clear-numN/A
lower-/.f64N/A
div-invN/A
lower-*.f64N/A
lift-exp.f64N/A
rec-expN/A
lower-exp.f64N/A
lower-neg.f6496.6
Applied rewrites96.6%
Taylor expanded in b around 0
+-commutativeN/A
distribute-lft-inN/A
exp-negN/A
lft-mult-inverseN/A
*-rgt-identityN/A
lower-+.f64N/A
neg-mul-1N/A
lower-exp.f64N/A
neg-mul-1N/A
lower-neg.f6496.7
Applied rewrites96.7%
if 0.0200000000000000004 < (exp.f64 a) Initial program 99.4%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6499.1
Applied rewrites99.1%
Final simplification98.5%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.02) (/ (exp a) (+ 1.0 1.0)) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.02) {
tmp = exp(a) / (1.0 + 1.0);
} else {
tmp = 1.0 / (exp(b) + 1.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (exp(a) <= 0.02d0) then
tmp = exp(a) / (1.0d0 + 1.0d0)
else
tmp = 1.0d0 / (exp(b) + 1.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 0.02) {
tmp = Math.exp(a) / (1.0 + 1.0);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.02: tmp = math.exp(a) / (1.0 + 1.0) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.02) tmp = Float64(exp(a) / Float64(1.0 + 1.0)); else tmp = Float64(1.0 / Float64(exp(b) + 1.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 0.02) tmp = exp(a) / (1.0 + 1.0); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.02], N[(N[Exp[a], $MachinePrecision] / N[(1.0 + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.02:\\
\;\;\;\;\frac{e^{a}}{1 + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.0200000000000000004Initial program 96.5%
Taylor expanded in b around 0
Applied rewrites96.6%
Taylor expanded in a around 0
Applied rewrites93.9%
if 0.0200000000000000004 < (exp.f64 a) Initial program 99.4%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6499.1
Applied rewrites99.1%
Final simplification97.9%
(FPCore (a b) :precision binary64 (/ 1.0 (+ (exp b) 1.0)))
double code(double a, double b) {
return 1.0 / (exp(b) + 1.0);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (exp(b) + 1.0d0)
end function
public static double code(double a, double b) {
return 1.0 / (Math.exp(b) + 1.0);
}
def code(a, b): return 1.0 / (math.exp(b) + 1.0)
function code(a, b) return Float64(1.0 / Float64(exp(b) + 1.0)) end
function tmp = code(a, b) tmp = 1.0 / (exp(b) + 1.0); end
code[a_, b_] := N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{e^{b} + 1}
\end{array}
Initial program 98.8%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6483.9
Applied rewrites83.9%
Final simplification83.9%
(FPCore (a b) :precision binary64 (* (/ 1.0 (+ 1.0 (fma a (fma a (fma a 0.16666666666666666 0.5) 1.0) 1.0))) (fma a (fma a 0.5 1.0) 1.0)))
double code(double a, double b) {
return (1.0 / (1.0 + fma(a, fma(a, fma(a, 0.16666666666666666, 0.5), 1.0), 1.0))) * fma(a, fma(a, 0.5, 1.0), 1.0);
}
function code(a, b) return Float64(Float64(1.0 / Float64(1.0 + fma(a, fma(a, fma(a, 0.16666666666666666, 0.5), 1.0), 1.0))) * fma(a, fma(a, 0.5, 1.0), 1.0)) end
code[a_, b_] := N[(N[(1.0 / N[(1.0 + N[(a * N[(a * N[(a * 0.16666666666666666 + 0.5), $MachinePrecision] + 1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(a * N[(a * 0.5 + 1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + \mathsf{fma}\left(a, \mathsf{fma}\left(a, \mathsf{fma}\left(a, 0.16666666666666666, 0.5\right), 1\right), 1\right)} \cdot \mathsf{fma}\left(a, \mathsf{fma}\left(a, 0.5, 1\right), 1\right)
\end{array}
Initial program 98.8%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
div-invN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites98.8%
Taylor expanded in b around 0
Applied rewrites61.6%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6440.3
Applied rewrites40.3%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6443.1
Applied rewrites43.1%
Final simplification43.1%
(FPCore (a b) :precision binary64 (let* ((t_0 (fma a (fma a 0.5 1.0) 1.0))) (* t_0 (/ 1.0 (+ 1.0 t_0)))))
double code(double a, double b) {
double t_0 = fma(a, fma(a, 0.5, 1.0), 1.0);
return t_0 * (1.0 / (1.0 + t_0));
}
function code(a, b) t_0 = fma(a, fma(a, 0.5, 1.0), 1.0) return Float64(t_0 * Float64(1.0 / Float64(1.0 + t_0))) end
code[a_, b_] := Block[{t$95$0 = N[(a * N[(a * 0.5 + 1.0), $MachinePrecision] + 1.0), $MachinePrecision]}, N[(t$95$0 * N[(1.0 / N[(1.0 + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(a, \mathsf{fma}\left(a, 0.5, 1\right), 1\right)\\
t\_0 \cdot \frac{1}{1 + t\_0}
\end{array}
\end{array}
Initial program 98.8%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
div-invN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites98.8%
Taylor expanded in b around 0
Applied rewrites61.6%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6440.3
Applied rewrites40.3%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6441.2
Applied rewrites41.2%
Final simplification41.2%
(FPCore (a b) :precision binary64 (/ 1.0 (+ 1.0 1.0)))
double code(double a, double b) {
return 1.0 / (1.0 + 1.0);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (1.0d0 + 1.0d0)
end function
public static double code(double a, double b) {
return 1.0 / (1.0 + 1.0);
}
def code(a, b): return 1.0 / (1.0 + 1.0)
function code(a, b) return Float64(1.0 / Float64(1.0 + 1.0)) end
function tmp = code(a, b) tmp = 1.0 / (1.0 + 1.0); end
code[a_, b_] := N[(1.0 / N[(1.0 + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + 1}
\end{array}
Initial program 98.8%
Taylor expanded in b around 0
Applied rewrites61.6%
Taylor expanded in a around 0
Applied rewrites60.6%
Taylor expanded in a around 0
Applied rewrites40.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 2024212
(FPCore (a b)
:name "Quotient of sum of exps"
:precision binary64
:alt
(! :herbie-platform default (/ 1 (+ 1 (exp (- b a)))))
(/ (exp a) (+ (exp a) (exp b))))