
(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 (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 98.4%
add-cube-cbrt97.8%
pow397.8%
pow-to-exp97.8%
pow1/397.9%
log-pow98.4%
log-div98.4%
add-log-exp98.8%
Applied egg-rr98.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.4%
Final simplification98.4%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.05) (exp a) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.05) {
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) <= 0.05d0) 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) <= 0.05) {
tmp = Math.exp(a);
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.05: tmp = math.exp(a) else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.05) 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) <= 0.05) tmp = exp(a); else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.05], N[Exp[a], $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.05:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if (exp.f64 a) < 0.050000000000000003Initial program 98.6%
add-cube-cbrt98.5%
pow398.5%
pow-to-exp98.5%
pow1/398.5%
log-pow98.5%
log-div98.5%
add-log-exp98.6%
Applied egg-rr98.6%
Taylor expanded in a around inf 96.2%
if 0.050000000000000003 < (exp.f64 a) Initial program 98.4%
Taylor expanded in b around 0 57.5%
Taylor expanded in a around 0 57.1%
*-commutative57.1%
Simplified57.1%
Final simplification67.8%
(FPCore (a b) :precision binary64 (if (<= a -720000000000.0) (exp a) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (a <= -720000000000.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 <= (-720000000000.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 <= -720000000000.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -720000000000.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 <= -720000000000.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 <= -720000000000.0) tmp = exp(a); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -720000000000.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 -720000000000:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if a < -7.2e11Initial 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 -7.2e11 < a Initial program 97.9%
Taylor expanded in a around 0 97.0%
Final simplification97.7%
(FPCore (a b) :precision binary64 (if (<= b 260.0) (+ 0.5 (* a 0.25)) (if (<= b 1.35e+154) (/ a b) (/ (- (* a (- b)) b) (* b (- b))))))
double code(double a, double b) {
double tmp;
if (b <= 260.0) {
tmp = 0.5 + (a * 0.25);
} else if (b <= 1.35e+154) {
tmp = a / b;
} else {
tmp = ((a * -b) - b) / (b * -b);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 260.0d0) then
tmp = 0.5d0 + (a * 0.25d0)
else if (b <= 1.35d+154) then
tmp = a / b
else
tmp = ((a * -b) - b) / (b * -b)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 260.0) {
tmp = 0.5 + (a * 0.25);
} else if (b <= 1.35e+154) {
tmp = a / b;
} else {
tmp = ((a * -b) - b) / (b * -b);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 260.0: tmp = 0.5 + (a * 0.25) elif b <= 1.35e+154: tmp = a / b else: tmp = ((a * -b) - b) / (b * -b) return tmp
function code(a, b) tmp = 0.0 if (b <= 260.0) tmp = Float64(0.5 + Float64(a * 0.25)); elseif (b <= 1.35e+154) tmp = Float64(a / b); else tmp = Float64(Float64(Float64(a * Float64(-b)) - b) / Float64(b * Float64(-b))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 260.0) tmp = 0.5 + (a * 0.25); elseif (b <= 1.35e+154) tmp = a / b; else tmp = ((a * -b) - b) / (b * -b); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 260.0], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.35e+154], N[(a / b), $MachinePrecision], N[(N[(N[(a * (-b)), $MachinePrecision] - b), $MachinePrecision] / N[(b * (-b)), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 260:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{elif}\;b \leq 1.35 \cdot 10^{+154}:\\
\;\;\;\;\frac{a}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{a \cdot \left(-b\right) - b}{b \cdot \left(-b\right)}\\
\end{array}
\end{array}
if b < 260Initial program 98.4%
Taylor expanded in b around 0 79.1%
Taylor expanded in a around 0 54.9%
*-commutative54.9%
Simplified54.9%
if 260 < b < 1.35000000000000003e154Initial program 100.0%
Taylor expanded in b around 0 36.8%
associate-+r+36.8%
+-commutative36.8%
Simplified36.8%
Taylor expanded in b around inf 36.8%
Taylor expanded in a around 0 3.4%
Taylor expanded in a around inf 18.7%
if 1.35000000000000003e154 < b Initial program 96.4%
Taylor expanded in b around 0 36.8%
associate-+r+36.8%
+-commutative36.8%
Simplified36.8%
Taylor expanded in b around inf 36.9%
Taylor expanded in a around 0 6.1%
frac-2neg6.1%
metadata-eval6.1%
frac-add75.0%
neg-mul-175.0%
Applied egg-rr75.0%
Final simplification52.2%
(FPCore (a b) :precision binary64 (if (<= b 320.0) (+ 0.5 (* a 0.25)) (/ a b)))
double code(double a, double b) {
double tmp;
if (b <= 320.0) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = a / b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 320.0d0) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = a / b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 320.0) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = a / b;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 320.0: tmp = 0.5 + (a * 0.25) else: tmp = a / b return tmp
function code(a, b) tmp = 0.0 if (b <= 320.0) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(a / b); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 320.0) tmp = 0.5 + (a * 0.25); else tmp = a / b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 320.0], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(a / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 320:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{b}\\
\end{array}
\end{array}
if b < 320Initial program 98.4%
Taylor expanded in b around 0 79.1%
Taylor expanded in a around 0 54.9%
*-commutative54.9%
Simplified54.9%
if 320 < b Initial program 98.4%
Taylor expanded in b around 0 36.8%
associate-+r+36.8%
+-commutative36.8%
Simplified36.8%
Taylor expanded in b around inf 36.8%
Taylor expanded in a around 0 4.6%
Taylor expanded in a around inf 30.5%
Final simplification48.9%
(FPCore (a b) :precision binary64 (if (<= b 1.35e+28) 0.5 (/ a b)))
double code(double a, double b) {
double tmp;
if (b <= 1.35e+28) {
tmp = 0.5;
} else {
tmp = a / b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 1.35d+28) then
tmp = 0.5d0
else
tmp = a / b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 1.35e+28) {
tmp = 0.5;
} else {
tmp = a / b;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 1.35e+28: tmp = 0.5 else: tmp = a / b return tmp
function code(a, b) tmp = 0.0 if (b <= 1.35e+28) tmp = 0.5; else tmp = Float64(a / b); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 1.35e+28) tmp = 0.5; else tmp = a / b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 1.35e+28], 0.5, N[(a / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.35 \cdot 10^{+28}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{b}\\
\end{array}
\end{array}
if b < 1.3500000000000001e28Initial program 98.4%
Taylor expanded in a around 0 74.2%
Taylor expanded in b around 0 54.0%
if 1.3500000000000001e28 < b Initial program 98.4%
Taylor expanded in b around 0 34.7%
associate-+r+34.7%
+-commutative34.7%
Simplified34.7%
Taylor expanded in b around inf 34.8%
Taylor expanded in a around 0 4.7%
Taylor expanded in a around inf 31.4%
Final simplification48.6%
(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.4%
Taylor expanded in a around 0 80.3%
Taylor expanded in b around 0 41.9%
Final simplification41.9%
(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 2024017
(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))))