
(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 99.2%
clear-num99.2%
inv-pow99.2%
Applied egg-rr99.2%
Taylor expanded in a around inf 99.2%
remove-double-div99.2%
rec-exp99.2%
+-commutative99.2%
associate-/r*99.2%
+-commutative99.2%
distribute-rgt-in71.9%
rec-exp71.9%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/100.0%
*-rgt-identity100.0%
div-exp100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (exp a) 5e-88) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 5e-88) {
tmp = 1.0 / (1.0 + 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) <= 5d-88) then
tmp = 1.0d0 / (1.0d0 + 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) <= 5e-88) {
tmp = 1.0 / (1.0 + Math.exp(-a));
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 5e-88: tmp = 1.0 / (1.0 + math.exp(-a)) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 5e-88) tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-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) <= 5e-88) tmp = 1.0 / (1.0 + exp(-a)); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 5e-88], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 5 \cdot 10^{-88}:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if (exp.f64 a) < 5.00000000000000009e-88Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
remove-double-div100.0%
rec-exp100.0%
+-commutative100.0%
associate-/r*100.0%
+-commutative100.0%
distribute-rgt-in2.8%
rec-exp2.8%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/100.0%
*-rgt-identity100.0%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 98.8%
if 5.00000000000000009e-88 < (exp.f64 a) Initial program 98.9%
Taylor expanded in a around 0 98.5%
Final simplification98.6%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.0) (/ (exp a) b) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.0) {
tmp = exp(a) / b;
} 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) <= 0.0d0) then
tmp = exp(a) / b
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) <= 0.0) {
tmp = Math.exp(a) / b;
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.0: tmp = math.exp(a) / b else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.0) tmp = Float64(exp(a) / b); 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) <= 0.0) tmp = exp(a) / b; else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.0], N[(N[Exp[a], $MachinePrecision] / b), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{e^{a}}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.0Initial program 100.0%
Taylor expanded in b around 0 100.0%
associate-+r+100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in b around inf 100.0%
if 0.0 < (exp.f64 a) Initial program 98.9%
Taylor expanded in a around 0 98.0%
Final simplification98.6%
(FPCore (a b) :precision binary64 (if (<= a -205.0) (/ (exp a) b) (if (<= a -1.55e-172) (/ 1.0 (- 2.0 a)) (if (<= a -2.3e-223) 1.0 0.5))))
double code(double a, double b) {
double tmp;
if (a <= -205.0) {
tmp = exp(a) / b;
} else if (a <= -1.55e-172) {
tmp = 1.0 / (2.0 - a);
} else if (a <= -2.3e-223) {
tmp = 1.0;
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-205.0d0)) then
tmp = exp(a) / b
else if (a <= (-1.55d-172)) then
tmp = 1.0d0 / (2.0d0 - a)
else if (a <= (-2.3d-223)) then
tmp = 1.0d0
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -205.0) {
tmp = Math.exp(a) / b;
} else if (a <= -1.55e-172) {
tmp = 1.0 / (2.0 - a);
} else if (a <= -2.3e-223) {
tmp = 1.0;
} else {
tmp = 0.5;
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -205.0: tmp = math.exp(a) / b elif a <= -1.55e-172: tmp = 1.0 / (2.0 - a) elif a <= -2.3e-223: tmp = 1.0 else: tmp = 0.5 return tmp
function code(a, b) tmp = 0.0 if (a <= -205.0) tmp = Float64(exp(a) / b); elseif (a <= -1.55e-172) tmp = Float64(1.0 / Float64(2.0 - a)); elseif (a <= -2.3e-223) tmp = 1.0; else tmp = 0.5; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -205.0) tmp = exp(a) / b; elseif (a <= -1.55e-172) tmp = 1.0 / (2.0 - a); elseif (a <= -2.3e-223) tmp = 1.0; else tmp = 0.5; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -205.0], N[(N[Exp[a], $MachinePrecision] / b), $MachinePrecision], If[LessEqual[a, -1.55e-172], N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision], If[LessEqual[a, -2.3e-223], 1.0, 0.5]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -205:\\
\;\;\;\;\frac{e^{a}}{b}\\
\mathbf{elif}\;a \leq -1.55 \cdot 10^{-172}:\\
\;\;\;\;\frac{1}{2 - a}\\
\mathbf{elif}\;a \leq -2.3 \cdot 10^{-223}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if a < -205Initial program 100.0%
Taylor expanded in b around 0 100.0%
associate-+r+100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in b around inf 100.0%
if -205 < a < -1.5500000000000001e-172Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
remove-double-div100.0%
rec-exp99.9%
+-commutative99.9%
associate-/r*99.9%
+-commutative99.9%
distribute-rgt-in99.9%
rec-exp100.0%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/99.9%
*-rgt-identity99.9%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 58.7%
Taylor expanded in a around 0 55.4%
mul-1-neg55.4%
unsub-neg55.4%
Simplified55.4%
if -1.5500000000000001e-172 < a < -2.3e-223Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
remove-double-div100.0%
rec-exp100.0%
+-commutative100.0%
associate-/r*100.0%
+-commutative100.0%
distribute-rgt-in100.0%
rec-exp100.0%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/100.0%
*-rgt-identity100.0%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 23.2%
Applied egg-rr66.0%
if -2.3e-223 < a Initial program 98.3%
Taylor expanded in b around 0 52.4%
Taylor expanded in a around 0 52.7%
Final simplification67.3%
(FPCore (a b) :precision binary64 (if (<= b -1.7) 1.0 (+ 0.5 (* b -0.25))))
double code(double a, double b) {
double tmp;
if (b <= -1.7) {
tmp = 1.0;
} else {
tmp = 0.5 + (b * -0.25);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-1.7d0)) then
tmp = 1.0d0
else
tmp = 0.5d0 + (b * (-0.25d0))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.7) {
tmp = 1.0;
} else {
tmp = 0.5 + (b * -0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.7: tmp = 1.0 else: tmp = 0.5 + (b * -0.25) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.7) tmp = 1.0; else tmp = Float64(0.5 + Float64(b * -0.25)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.7) tmp = 1.0; else tmp = 0.5 + (b * -0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.7], 1.0, N[(0.5 + N[(b * -0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.7:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5 + b \cdot -0.25\\
\end{array}
\end{array}
if b < -1.69999999999999996Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
remove-double-div100.0%
rec-exp99.9%
+-commutative99.9%
associate-/r*99.9%
+-commutative99.9%
distribute-rgt-in99.9%
rec-exp100.0%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/100.0%
*-rgt-identity100.0%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 18.5%
Applied egg-rr100.0%
if -1.69999999999999996 < b Initial program 99.0%
Taylor expanded in a around 0 77.7%
Taylor expanded in b around 0 42.9%
Taylor expanded in b around 0 41.5%
*-commutative41.5%
Simplified41.5%
Final simplification52.5%
(FPCore (a b) :precision binary64 (if (<= b -1.0) 1.0 (/ 1.0 (+ b 2.0))))
double code(double a, double b) {
double tmp;
if (b <= -1.0) {
tmp = 1.0;
} else {
tmp = 1.0 / (b + 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 <= (-1.0d0)) then
tmp = 1.0d0
else
tmp = 1.0d0 / (b + 2.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.0) {
tmp = 1.0;
} else {
tmp = 1.0 / (b + 2.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.0: tmp = 1.0 else: tmp = 1.0 / (b + 2.0) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.0) tmp = 1.0; else tmp = Float64(1.0 / Float64(b + 2.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.0) tmp = 1.0; else tmp = 1.0 / (b + 2.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.0], 1.0, N[(1.0 / N[(b + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{b + 2}\\
\end{array}
\end{array}
if b < -1Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
remove-double-div100.0%
rec-exp99.9%
+-commutative99.9%
associate-/r*99.9%
+-commutative99.9%
distribute-rgt-in99.9%
rec-exp100.0%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/100.0%
*-rgt-identity100.0%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 18.5%
Applied egg-rr100.0%
if -1 < b Initial program 99.0%
Taylor expanded in a around 0 77.7%
Taylor expanded in b around 0 42.9%
Final simplification53.6%
(FPCore (a b) :precision binary64 (if (<= b -0.96) 1.0 0.5))
double code(double a, double b) {
double tmp;
if (b <= -0.96) {
tmp = 1.0;
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-0.96d0)) then
tmp = 1.0d0
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -0.96) {
tmp = 1.0;
} else {
tmp = 0.5;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.96: tmp = 1.0 else: tmp = 0.5 return tmp
function code(a, b) tmp = 0.0 if (b <= -0.96) tmp = 1.0; else tmp = 0.5; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.96) tmp = 1.0; else tmp = 0.5; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.96], 1.0, 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.96:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if b < -0.95999999999999996Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
remove-double-div100.0%
rec-exp99.9%
+-commutative99.9%
associate-/r*99.9%
+-commutative99.9%
distribute-rgt-in99.9%
rec-exp100.0%
rgt-mult-inverse100.0%
rec-exp100.0%
associate-*r/100.0%
*-rgt-identity100.0%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 18.5%
Applied egg-rr100.0%
if -0.95999999999999996 < b Initial program 99.0%
Taylor expanded in b around 0 74.9%
Taylor expanded in a around 0 41.5%
Final simplification52.4%
(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 99.2%
Taylor expanded in b around 0 64.3%
Taylor expanded in a around 0 37.2%
Final simplification37.2%
(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 2024027
(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))))