
(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 11 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 (- (log1p (exp (- b a))))))
double code(double a, double b) {
return exp(-log1p(exp((b - a))));
}
public static double code(double a, double b) {
return Math.exp(-Math.log1p(Math.exp((b - a))));
}
def code(a, b): return math.exp(-math.log1p(math.exp((b - a))))
function code(a, b) return exp(Float64(-log1p(exp(Float64(b - a))))) end
code[a_, b_] := N[Exp[(-N[Log[1 + N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision]], $MachinePrecision])], $MachinePrecision]
\begin{array}{l}
\\
e^{-\mathsf{log1p}\left(e^{b - a}\right)}
\end{array}
Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in70.3%
exp-neg70.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b)
:precision binary64
(let* ((t_0 (* (* a a) -0.5)))
(if (<= a -1.35e+154)
(/ (- -2.0 a) (* a a))
(if (<= a -7.2e+102) (/ (- a t_0) (* a t_0)) (/ 1.0 (+ 1.0 (exp b)))))))
double code(double a, double b) {
double t_0 = (a * a) * -0.5;
double tmp;
if (a <= -1.35e+154) {
tmp = (-2.0 - a) / (a * a);
} else if (a <= -7.2e+102) {
tmp = (a - t_0) / (a * t_0);
} 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) :: t_0
real(8) :: tmp
t_0 = (a * a) * (-0.5d0)
if (a <= (-1.35d+154)) then
tmp = ((-2.0d0) - a) / (a * a)
else if (a <= (-7.2d+102)) then
tmp = (a - t_0) / (a * t_0)
else
tmp = 1.0d0 / (1.0d0 + exp(b))
end if
code = tmp
end function
public static double code(double a, double b) {
double t_0 = (a * a) * -0.5;
double tmp;
if (a <= -1.35e+154) {
tmp = (-2.0 - a) / (a * a);
} else if (a <= -7.2e+102) {
tmp = (a - t_0) / (a * t_0);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): t_0 = (a * a) * -0.5 tmp = 0 if a <= -1.35e+154: tmp = (-2.0 - a) / (a * a) elif a <= -7.2e+102: tmp = (a - t_0) / (a * t_0) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) t_0 = Float64(Float64(a * a) * -0.5) tmp = 0.0 if (a <= -1.35e+154) tmp = Float64(Float64(-2.0 - a) / Float64(a * a)); elseif (a <= -7.2e+102) tmp = Float64(Float64(a - t_0) / Float64(a * t_0)); else tmp = Float64(1.0 / Float64(1.0 + exp(b))); end return tmp end
function tmp_2 = code(a, b) t_0 = (a * a) * -0.5; tmp = 0.0; if (a <= -1.35e+154) tmp = (-2.0 - a) / (a * a); elseif (a <= -7.2e+102) tmp = (a - t_0) / (a * t_0); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := Block[{t$95$0 = N[(N[(a * a), $MachinePrecision] * -0.5), $MachinePrecision]}, If[LessEqual[a, -1.35e+154], N[(N[(-2.0 - a), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision], If[LessEqual[a, -7.2e+102], N[(N[(a - t$95$0), $MachinePrecision] / N[(a * t$95$0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(a \cdot a\right) \cdot -0.5\\
\mathbf{if}\;a \leq -1.35 \cdot 10^{+154}:\\
\;\;\;\;\frac{-2 - a}{a \cdot a}\\
\mathbf{elif}\;a \leq -7.2 \cdot 10^{+102}:\\
\;\;\;\;\frac{a - t_0}{a \cdot t_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -1.35000000000000003e154Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in0.0%
exp-neg0.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 100.0%
Taylor expanded in a around 0 6.4%
neg-mul-16.4%
unsub-neg6.4%
Simplified6.4%
Taylor expanded in a around inf 6.4%
distribute-neg-in6.4%
associate-*r/6.4%
metadata-eval6.4%
distribute-neg-frac6.4%
metadata-eval6.4%
unpow26.4%
distribute-neg-frac6.4%
metadata-eval6.4%
Simplified6.4%
associate-/r*6.4%
frac-add100.0%
metadata-eval100.0%
div-inv100.0%
fma-def100.0%
div-inv100.0%
metadata-eval100.0%
Applied egg-rr100.0%
fma-udef100.0%
*-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
+-commutative100.0%
remove-double-neg100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-in100.0%
mul-1-neg100.0%
distribute-lft-neg-in100.0%
associate-*l*100.0%
unsub-neg100.0%
associate-*r/100.0%
associate-*l*100.0%
metadata-eval100.0%
*-commutative100.0%
associate-/l*100.0%
*-inverses100.0%
metadata-eval100.0%
sub-neg100.0%
metadata-eval100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
if -1.35000000000000003e154 < a < -7.2000000000000003e102Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in0.0%
exp-neg0.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 100.0%
Taylor expanded in a around 0 4.3%
neg-mul-14.3%
unsub-neg4.3%
Simplified4.3%
Taylor expanded in a around inf 4.3%
distribute-neg-in4.3%
associate-*r/4.3%
metadata-eval4.3%
distribute-neg-frac4.3%
metadata-eval4.3%
unpow24.3%
distribute-neg-frac4.3%
metadata-eval4.3%
Simplified4.3%
clear-num4.3%
frac-add100.0%
*-un-lft-identity100.0%
div-inv100.0%
metadata-eval100.0%
div-inv100.0%
metadata-eval100.0%
Applied egg-rr100.0%
if -7.2000000000000003e102 < a Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in86.9%
exp-neg86.9%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 88.8%
Final simplification91.0%
(FPCore (a b)
:precision binary64
(if (<= b -9.5)
1.0
(if (<= b 2.4e+40)
(* (/ 1.0 (+ (* a a) 4.0)) (+ a 2.0))
(* (pow a 3.0) -0.020833333333333332))))
double code(double a, double b) {
double tmp;
if (b <= -9.5) {
tmp = 1.0;
} else if (b <= 2.4e+40) {
tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0);
} else {
tmp = pow(a, 3.0) * -0.020833333333333332;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-9.5d0)) then
tmp = 1.0d0
else if (b <= 2.4d+40) then
tmp = (1.0d0 / ((a * a) + 4.0d0)) * (a + 2.0d0)
else
tmp = (a ** 3.0d0) * (-0.020833333333333332d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -9.5) {
tmp = 1.0;
} else if (b <= 2.4e+40) {
tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0);
} else {
tmp = Math.pow(a, 3.0) * -0.020833333333333332;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -9.5: tmp = 1.0 elif b <= 2.4e+40: tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0) else: tmp = math.pow(a, 3.0) * -0.020833333333333332 return tmp
function code(a, b) tmp = 0.0 if (b <= -9.5) tmp = 1.0; elseif (b <= 2.4e+40) tmp = Float64(Float64(1.0 / Float64(Float64(a * a) + 4.0)) * Float64(a + 2.0)); else tmp = Float64((a ^ 3.0) * -0.020833333333333332); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -9.5) tmp = 1.0; elseif (b <= 2.4e+40) tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0); else tmp = (a ^ 3.0) * -0.020833333333333332; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -9.5], 1.0, If[LessEqual[b, 2.4e+40], N[(N[(1.0 / N[(N[(a * a), $MachinePrecision] + 4.0), $MachinePrecision]), $MachinePrecision] * N[(a + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[Power[a, 3.0], $MachinePrecision] * -0.020833333333333332), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.5:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 2.4 \cdot 10^{+40}:\\
\;\;\;\;\frac{1}{a \cdot a + 4} \cdot \left(a + 2\right)\\
\mathbf{else}:\\
\;\;\;\;{a}^{3} \cdot -0.020833333333333332\\
\end{array}
\end{array}
if b < -9.5Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in100.0%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -9.5 < b < 2.4e40Initial program 99.9%
*-lft-identity99.9%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in63.3%
exp-neg63.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 96.1%
Taylor expanded in a around 0 59.0%
neg-mul-159.0%
unsub-neg59.0%
Simplified59.0%
flip--74.2%
associate-/r/74.2%
sub-neg74.2%
metadata-eval74.2%
add-sqr-sqrt25.7%
sqrt-unprod74.2%
sqr-neg74.2%
sqrt-unprod48.5%
add-sqr-sqrt74.2%
distribute-lft-neg-out74.2%
sqr-neg74.2%
+-commutative74.2%
Applied egg-rr74.2%
if 2.4e40 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in66.2%
exp-neg66.2%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 35.9%
Taylor expanded in a around 0 2.7%
Taylor expanded in a around inf 41.3%
*-commutative41.3%
Simplified41.3%
Final simplification69.4%
(FPCore (a b) :precision binary64 (if (<= a -8.6e-5) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -8.6e-5) {
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 (a <= (-8.6d-5)) 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 (a <= -8.6e-5) {
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 a <= -8.6e-5: 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 (a <= -8.6e-5) 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 (a <= -8.6e-5) 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[a, -8.6e-5], 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}\;a \leq -8.6 \cdot 10^{-5}:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -8.6000000000000003e-5Initial program 99.9%
*-lft-identity99.9%
associate-/l*99.9%
remove-double-div99.9%
exp-neg99.9%
associate-/r/99.9%
/-rgt-identity99.9%
*-commutative99.9%
distribute-rgt-in6.1%
exp-neg6.1%
rgt-mult-inverse99.9%
prod-exp99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in b around 0 99.9%
if -8.6000000000000003e-5 < a Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in100.0%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 99.3%
Final simplification99.5%
(FPCore (a b) :precision binary64 (/ 1.0 (+ (exp (- b a)) 1.0)))
double code(double a, double b) {
return 1.0 / (exp((b - a)) + 1.0);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (exp((b - a)) + 1.0d0)
end function
public static double code(double a, double b) {
return 1.0 / (Math.exp((b - a)) + 1.0);
}
def code(a, b): return 1.0 / (math.exp((b - a)) + 1.0)
function code(a, b) return Float64(1.0 / Float64(exp(Float64(b - a)) + 1.0)) end
function tmp = code(a, b) tmp = 1.0 / (exp((b - a)) + 1.0); end
code[a_, b_] := N[(1.0 / N[(N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{e^{b - a} + 1}
\end{array}
Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in70.3%
exp-neg70.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= b -5.6) 1.0 (* (/ 1.0 (+ (* a a) 4.0)) (+ a 2.0))))
double code(double a, double b) {
double tmp;
if (b <= -5.6) {
tmp = 1.0;
} else {
tmp = (1.0 / ((a * a) + 4.0)) * (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 <= (-5.6d0)) then
tmp = 1.0d0
else
tmp = (1.0d0 / ((a * a) + 4.0d0)) * (a + 2.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -5.6) {
tmp = 1.0;
} else {
tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -5.6: tmp = 1.0 else: tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0) return tmp
function code(a, b) tmp = 0.0 if (b <= -5.6) tmp = 1.0; else tmp = Float64(Float64(1.0 / Float64(Float64(a * a) + 4.0)) * Float64(a + 2.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -5.6) tmp = 1.0; else tmp = (1.0 / ((a * a) + 4.0)) * (a + 2.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -5.6], 1.0, N[(N[(1.0 / N[(N[(a * a), $MachinePrecision] + 4.0), $MachinePrecision]), $MachinePrecision] * N[(a + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5.6:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{a \cdot a + 4} \cdot \left(a + 2\right)\\
\end{array}
\end{array}
if b < -5.5999999999999996Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in100.0%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -5.5999999999999996 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in64.3%
exp-neg64.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 76.0%
Taylor expanded in a around 0 40.6%
neg-mul-140.6%
unsub-neg40.6%
Simplified40.6%
flip--56.0%
associate-/r/56.0%
sub-neg56.0%
metadata-eval56.0%
add-sqr-sqrt17.5%
sqrt-unprod56.0%
sqr-neg56.0%
sqrt-unprod38.5%
add-sqr-sqrt56.0%
distribute-lft-neg-out56.0%
sqr-neg56.0%
+-commutative56.0%
Applied egg-rr56.0%
Final simplification63.4%
(FPCore (a b) :precision binary64 (if (<= b -1.4) 1.0 (if (<= b 1e+15) (/ 1.0 (- 2.0 a)) (/ (- -2.0 a) (* a a)))))
double code(double a, double b) {
double tmp;
if (b <= -1.4) {
tmp = 1.0;
} else if (b <= 1e+15) {
tmp = 1.0 / (2.0 - a);
} else {
tmp = (-2.0 - a) / (a * a);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-1.4d0)) then
tmp = 1.0d0
else if (b <= 1d+15) then
tmp = 1.0d0 / (2.0d0 - a)
else
tmp = ((-2.0d0) - a) / (a * a)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.4) {
tmp = 1.0;
} else if (b <= 1e+15) {
tmp = 1.0 / (2.0 - a);
} else {
tmp = (-2.0 - a) / (a * a);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.4: tmp = 1.0 elif b <= 1e+15: tmp = 1.0 / (2.0 - a) else: tmp = (-2.0 - a) / (a * a) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.4) tmp = 1.0; elseif (b <= 1e+15) tmp = Float64(1.0 / Float64(2.0 - a)); else tmp = Float64(Float64(-2.0 - a) / Float64(a * a)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.4) tmp = 1.0; elseif (b <= 1e+15) tmp = 1.0 / (2.0 - a); else tmp = (-2.0 - a) / (a * a); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.4], 1.0, If[LessEqual[b, 1e+15], N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 - a), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.4:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 10^{+15}:\\
\;\;\;\;\frac{1}{2 - a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2 - a}{a \cdot a}\\
\end{array}
\end{array}
if b < -1.3999999999999999Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in100.0%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -1.3999999999999999 < b < 1e15Initial program 99.9%
*-lft-identity99.9%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in65.4%
exp-neg65.4%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 96.7%
Taylor expanded in a around 0 61.4%
neg-mul-161.4%
unsub-neg61.4%
Simplified61.4%
if 1e15 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in62.3%
exp-neg62.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 39.6%
Taylor expanded in a around 0 4.0%
neg-mul-14.0%
unsub-neg4.0%
Simplified4.0%
Taylor expanded in a around inf 3.3%
distribute-neg-in3.3%
associate-*r/3.3%
metadata-eval3.3%
distribute-neg-frac3.3%
metadata-eval3.3%
unpow23.3%
distribute-neg-frac3.3%
metadata-eval3.3%
Simplified3.3%
associate-/r*3.3%
frac-add22.7%
metadata-eval22.7%
div-inv22.7%
fma-def22.7%
div-inv22.7%
metadata-eval22.7%
Applied egg-rr22.7%
fma-udef22.7%
*-commutative22.7%
*-commutative22.7%
neg-mul-122.7%
+-commutative22.7%
remove-double-neg22.7%
distribute-lft-neg-out22.7%
distribute-rgt-neg-in22.7%
mul-1-neg22.7%
distribute-lft-neg-in22.7%
associate-*l*22.7%
unsub-neg22.7%
associate-*r/22.7%
associate-*l*22.7%
metadata-eval22.7%
*-commutative22.7%
associate-/l*22.7%
*-inverses22.7%
metadata-eval22.7%
sub-neg22.7%
metadata-eval22.7%
+-commutative22.7%
sub-neg22.7%
Simplified22.7%
Final simplification56.2%
(FPCore (a b) :precision binary64 (if (<= b -0.00019) 1.0 (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (b <= -0.00019) {
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.00019d0)) 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.00019) {
tmp = 1.0;
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.00019: tmp = 1.0 else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (b <= -0.00019) 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.00019) tmp = 1.0; else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.00019], 1.0, N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.00019:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if b < -1.9000000000000001e-4Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in97.7%
exp-neg97.7%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Applied egg-rr97.8%
*-inverses97.8%
Simplified97.8%
if -1.9000000000000001e-4 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in64.6%
exp-neg64.6%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 75.9%
Taylor expanded in a around 0 40.3%
*-commutative40.3%
Simplified40.3%
Final simplification50.2%
(FPCore (a b) :precision binary64 (if (<= b -1.4) 1.0 (/ 1.0 (- 2.0 a))))
double code(double a, double b) {
double tmp;
if (b <= -1.4) {
tmp = 1.0;
} else {
tmp = 1.0 / (2.0 - a);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-1.4d0)) then
tmp = 1.0d0
else
tmp = 1.0d0 / (2.0d0 - a)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.4) {
tmp = 1.0;
} else {
tmp = 1.0 / (2.0 - a);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.4: tmp = 1.0 else: tmp = 1.0 / (2.0 - a) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.4) tmp = 1.0; else tmp = Float64(1.0 / Float64(2.0 - a)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.4) tmp = 1.0; else tmp = 1.0 / (2.0 - a); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.4], 1.0, N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.4:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 - a}\\
\end{array}
\end{array}
if b < -1.3999999999999999Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in100.0%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -1.3999999999999999 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in64.3%
exp-neg64.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 76.0%
Taylor expanded in a around 0 40.6%
neg-mul-140.6%
unsub-neg40.6%
Simplified40.6%
Final simplification50.6%
(FPCore (a b) :precision binary64 (if (<= b -1.1) 1.0 0.5))
double code(double a, double b) {
double tmp;
if (b <= -1.1) {
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 <= (-1.1d0)) 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 <= -1.1) {
tmp = 1.0;
} else {
tmp = 0.5;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.1: tmp = 1.0 else: tmp = 0.5 return tmp
function code(a, b) tmp = 0.0 if (b <= -1.1) tmp = 1.0; else tmp = 0.5; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.1) tmp = 1.0; else tmp = 0.5; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.1], 1.0, 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.1:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if b < -1.1000000000000001Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in100.0%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -1.1000000000000001 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in64.3%
exp-neg64.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 76.0%
Taylor expanded in a around 0 39.3%
Final simplification49.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 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in70.3%
exp-neg70.3%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 66.4%
Taylor expanded in a around 0 35.8%
Final simplification35.8%
(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 2023272
(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))))