
(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 9 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 (let* ((t_0 (/ (exp a) (+ (exp a) (exp b))))) (if (<= t_0 1.0) t_0 1.0)))
double code(double a, double b) {
double t_0 = exp(a) / (exp(a) + exp(b));
double tmp;
if (t_0 <= 1.0) {
tmp = t_0;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: t_0
real(8) :: tmp
t_0 = exp(a) / (exp(a) + exp(b))
if (t_0 <= 1.0d0) then
tmp = t_0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double t_0 = Math.exp(a) / (Math.exp(a) + Math.exp(b));
double tmp;
if (t_0 <= 1.0) {
tmp = t_0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(a, b): t_0 = math.exp(a) / (math.exp(a) + math.exp(b)) tmp = 0 if t_0 <= 1.0: tmp = t_0 else: tmp = 1.0 return tmp
function code(a, b) t_0 = Float64(exp(a) / Float64(exp(a) + exp(b))) tmp = 0.0 if (t_0 <= 1.0) tmp = t_0; else tmp = 1.0; end return tmp end
function tmp_2 = code(a, b) t_0 = exp(a) / (exp(a) + exp(b)); tmp = 0.0; if (t_0 <= 1.0) tmp = t_0; else tmp = 1.0; end tmp_2 = tmp; end
code[a_, b_] := Block[{t$95$0 = N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 1.0], t$95$0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{e^{a}}{e^{a} + e^{b}}\\
\mathbf{if}\;t_0 \leq 1:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 1Initial program 100.0%
if 1 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 0.0%
add-cube-cbrt0.0%
pow30.0%
pow-to-exp0.0%
pow1/30.0%
log-pow0.0%
log-div0.0%
add-log-exp19.3%
Applied egg-rr19.3%
Taylor expanded in a around 0 59.4%
add-cube-cbrt59.4%
pow359.4%
*-commutative59.4%
associate-*l*59.4%
log1p-def59.4%
metadata-eval59.4%
Applied egg-rr59.4%
Applied egg-rr83.9%
Final simplification99.6%
(FPCore (a b) :precision binary64 (exp (* (* 0.3333333333333333 (- a (log (+ (exp a) (exp b))))) 3.0)))
double code(double a, double b) {
return exp(((0.3333333333333333 * (a - log((exp(a) + exp(b))))) * 3.0));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(((0.3333333333333333d0 * (a - log((exp(a) + exp(b))))) * 3.0d0))
end function
public static double code(double a, double b) {
return Math.exp(((0.3333333333333333 * (a - Math.log((Math.exp(a) + Math.exp(b))))) * 3.0));
}
def code(a, b): return math.exp(((0.3333333333333333 * (a - math.log((math.exp(a) + math.exp(b))))) * 3.0))
function code(a, b) return exp(Float64(Float64(0.3333333333333333 * Float64(a - log(Float64(exp(a) + exp(b))))) * 3.0)) end
function tmp = code(a, b) tmp = exp(((0.3333333333333333 * (a - log((exp(a) + exp(b))))) * 3.0)); end
code[a_, b_] := N[Exp[N[(N[(0.3333333333333333 * N[(a - N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 3.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
e^{\left(0.3333333333333333 \cdot \left(a - \log \left(e^{a} + e^{b}\right)\right)\right) \cdot 3}
\end{array}
Initial program 97.6%
add-cube-cbrt97.1%
pow397.1%
pow-to-exp97.1%
pow1/397.2%
log-pow97.6%
log-div97.6%
add-log-exp98.1%
Applied egg-rr98.1%
Final simplification98.1%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.0) (exp a) (if (<= (exp a) 1.00000000002) (+ 0.5 (* a 0.25)) 1.0)))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.0) {
tmp = exp(a);
} else if (exp(a) <= 1.00000000002) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 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.0d0) then
tmp = exp(a)
else if (exp(a) <= 1.00000000002d0) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 1.0d0
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);
} else if (Math.exp(a) <= 1.00000000002) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0;
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.0: tmp = math.exp(a) elif math.exp(a) <= 1.00000000002: tmp = 0.5 + (a * 0.25) else: tmp = 1.0 return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.0) tmp = exp(a); elseif (exp(a) <= 1.00000000002) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = 1.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 0.0) tmp = exp(a); elseif (exp(a) <= 1.00000000002) tmp = 0.5 + (a * 0.25); else tmp = 1.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.0], N[Exp[a], $MachinePrecision], If[LessEqual[N[Exp[a], $MachinePrecision], 1.00000000002], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;e^{a}\\
\mathbf{elif}\;e^{a} \leq 1.00000000002:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (exp.f64 a) < 0.0Initial program 98.6%
add-cube-cbrt98.6%
pow398.6%
pow-to-exp98.6%
pow1/398.6%
log-pow98.6%
log-div98.6%
add-log-exp98.7%
Applied egg-rr98.7%
Taylor expanded in a around inf 98.7%
if 0.0 < (exp.f64 a) < 1.00000000002Initial program 99.9%
Taylor expanded in b around 0 49.2%
Taylor expanded in a around 0 49.2%
*-commutative49.2%
Simplified49.2%
if 1.00000000002 < (exp.f64 a) Initial program 54.5%
add-cube-cbrt54.3%
pow354.4%
pow-to-exp54.3%
pow1/354.5%
log-pow54.5%
log-div54.4%
add-log-exp64.5%
Applied egg-rr64.5%
Taylor expanded in a around 0 72.7%
add-cube-cbrt72.7%
pow372.7%
*-commutative72.7%
associate-*l*72.7%
log1p-def72.7%
metadata-eval72.7%
Applied egg-rr72.7%
Applied egg-rr57.4%
Final simplification63.5%
(FPCore (a b) :precision binary64 (if (<= a -10000.0) (exp a) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (a <= -10000.0) {
tmp = 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 (a <= (-10000.0d0)) then
tmp = 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 (a <= -10000.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -10000.0: tmp = math.exp(a) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (a <= -10000.0) tmp = exp(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 (a <= -10000.0) tmp = exp(a); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -10000.0], N[Exp[a], $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -10000:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if a < -1e4Initial program 100.0%
add-cube-cbrt100.0%
pow3100.0%
pow-to-exp100.0%
pow1/3100.0%
log-pow100.0%
log-div100.0%
add-log-exp100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
if -1e4 < a Initial program 96.7%
Taylor expanded in a around 0 97.9%
Final simplification98.5%
(FPCore (a b) :precision binary64 (if (<= b -0.86) 1.0 (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (b <= -0.86) {
tmp = 1.0;
} 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 (b <= (-0.86d0)) then
tmp = 1.0d0
else
tmp = 0.5d0 + (a * 0.25d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -0.86) {
tmp = 1.0;
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.86: tmp = 1.0 else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (b <= -0.86) tmp = 1.0; else tmp = Float64(0.5 + Float64(a * 0.25)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.86) tmp = 1.0; else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.86], 1.0, N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.86:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if b < -0.859999999999999987Initial program 96.3%
add-cube-cbrt96.3%
pow396.3%
pow-to-exp96.3%
pow1/396.3%
log-pow96.3%
log-div96.3%
add-log-exp96.4%
Applied egg-rr96.4%
Taylor expanded in a around 0 100.0%
add-cube-cbrt100.0%
pow3100.0%
*-commutative100.0%
associate-*l*100.0%
log1p-def100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Applied egg-rr98.6%
if -0.859999999999999987 < b Initial program 98.0%
Taylor expanded in b around 0 73.2%
Taylor expanded in a around 0 39.0%
*-commutative39.0%
Simplified39.0%
Final simplification51.6%
(FPCore (a b) :precision binary64 (if (<= b -1.25) 1.0 (+ 0.5 (* b -0.25))))
double code(double a, double b) {
double tmp;
if (b <= -1.25) {
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.25d0)) 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.25) {
tmp = 1.0;
} else {
tmp = 0.5 + (b * -0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.25: tmp = 1.0 else: tmp = 0.5 + (b * -0.25) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.25) 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.25) tmp = 1.0; else tmp = 0.5 + (b * -0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.25], 1.0, N[(0.5 + N[(b * -0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.25:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5 + b \cdot -0.25\\
\end{array}
\end{array}
if b < -1.25Initial program 96.3%
add-cube-cbrt96.3%
pow396.3%
pow-to-exp96.3%
pow1/396.3%
log-pow96.3%
log-div96.3%
add-log-exp96.4%
Applied egg-rr96.4%
Taylor expanded in a around 0 100.0%
add-cube-cbrt100.0%
pow3100.0%
*-commutative100.0%
associate-*l*100.0%
log1p-def100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Applied egg-rr98.6%
if -1.25 < b Initial program 98.0%
Taylor expanded in a around 0 74.5%
Taylor expanded in b around 0 39.6%
*-commutative39.6%
Simplified39.6%
Final simplification52.0%
(FPCore (a b) :precision binary64 (if (<= b -0.42) 1.0 (/ 1.0 (+ b 2.0))))
double code(double a, double b) {
double tmp;
if (b <= -0.42) {
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 <= (-0.42d0)) 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 <= -0.42) {
tmp = 1.0;
} else {
tmp = 1.0 / (b + 2.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.42: tmp = 1.0 else: tmp = 1.0 / (b + 2.0) return tmp
function code(a, b) tmp = 0.0 if (b <= -0.42) 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 <= -0.42) tmp = 1.0; else tmp = 1.0 / (b + 2.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.42], 1.0, N[(1.0 / N[(b + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.42:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{b + 2}\\
\end{array}
\end{array}
if b < -0.419999999999999984Initial program 96.3%
add-cube-cbrt96.3%
pow396.3%
pow-to-exp96.3%
pow1/396.3%
log-pow96.3%
log-div96.3%
add-log-exp96.4%
Applied egg-rr96.4%
Taylor expanded in a around 0 100.0%
add-cube-cbrt100.0%
pow3100.0%
*-commutative100.0%
associate-*l*100.0%
log1p-def100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Applied egg-rr98.6%
if -0.419999999999999984 < b Initial program 98.0%
Taylor expanded in a around 0 74.5%
Taylor expanded in b around 0 40.3%
+-commutative40.3%
Simplified40.3%
Final simplification52.6%
(FPCore (a b) :precision binary64 (if (<= b -0.75) 1.0 0.5))
double code(double a, double b) {
double tmp;
if (b <= -0.75) {
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.75d0)) 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.75) {
tmp = 1.0;
} else {
tmp = 0.5;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.75: tmp = 1.0 else: tmp = 0.5 return tmp
function code(a, b) tmp = 0.0 if (b <= -0.75) tmp = 1.0; else tmp = 0.5; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.75) tmp = 1.0; else tmp = 0.5; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.75], 1.0, 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.75:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if b < -0.75Initial program 96.3%
add-cube-cbrt96.3%
pow396.3%
pow-to-exp96.3%
pow1/396.3%
log-pow96.3%
log-div96.3%
add-log-exp96.4%
Applied egg-rr96.4%
Taylor expanded in a around 0 100.0%
add-cube-cbrt100.0%
pow3100.0%
*-commutative100.0%
associate-*l*100.0%
log1p-def100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Applied egg-rr98.6%
if -0.75 < b Initial program 98.0%
Taylor expanded in a around 0 74.5%
Taylor expanded in b around 0 38.7%
Final simplification51.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 97.6%
Taylor expanded in a around 0 79.9%
Taylor expanded in b around 0 34.5%
Final simplification34.5%
(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 2024016
(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))))