?

Average Error: 1.03% → 1.5%
Time: 8.1s
Precision: binary64
Cost: 25860

?

\[\frac{e^{a}}{e^{a} + e^{b}} \]
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\ \end{array} \]
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (if (<= (exp a) 0.0) 0.0 (exp (- (log1p (exp b))))))
double code(double a, double b) {
	return exp(a) / (exp(a) + exp(b));
}
double code(double a, double b) {
	double tmp;
	if (exp(a) <= 0.0) {
		tmp = 0.0;
	} else {
		tmp = exp(-log1p(exp(b)));
	}
	return tmp;
}
public static double code(double a, double b) {
	return Math.exp(a) / (Math.exp(a) + Math.exp(b));
}
public static double code(double a, double b) {
	double tmp;
	if (Math.exp(a) <= 0.0) {
		tmp = 0.0;
	} else {
		tmp = Math.exp(-Math.log1p(Math.exp(b)));
	}
	return tmp;
}
def code(a, b):
	return math.exp(a) / (math.exp(a) + math.exp(b))
def code(a, b):
	tmp = 0
	if math.exp(a) <= 0.0:
		tmp = 0.0
	else:
		tmp = math.exp(-math.log1p(math.exp(b)))
	return tmp
function code(a, b)
	return Float64(exp(a) / Float64(exp(a) + exp(b)))
end
function code(a, b)
	tmp = 0.0
	if (exp(a) <= 0.0)
		tmp = 0.0;
	else
		tmp = exp(Float64(-log1p(exp(b))));
	end
	return tmp
end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.0], 0.0, N[Exp[(-N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision])], $MachinePrecision]]
\frac{e^{a}}{e^{a} + e^{b}}
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;0\\

\mathbf{else}:\\
\;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original1.03%
Target0.02%
Herbie1.5%
\[\frac{1}{1 + e^{b - a}} \]

Derivation?

  1. Split input into 2 regimes
  2. if (exp.f64 a) < 0.0

    1. Initial program 1.41

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Taylor expanded in a around 0 64.88

      \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
    3. Taylor expanded in b around 0 96.13

      \[\leadsto \frac{1}{\color{blue}{2 + b}} \]
    4. Simplified96.13

      \[\leadsto \frac{1}{\color{blue}{b + 2}} \]
      Proof

      [Start]96.13

      \[ \frac{1}{2 + b} \]

      +-commutative [=>]96.13

      \[ \frac{1}{\color{blue}{b + 2}} \]
    5. Applied egg-rr66.98

      \[\leadsto \color{blue}{\left(1 + \frac{1}{b + 2}\right) - 1} \]
    6. Taylor expanded in b around inf 0.59

      \[\leadsto \color{blue}{1} - 1 \]

    if 0.0 < (exp.f64 a)

    1. Initial program 0.89

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Applied egg-rr0.68

      \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Taylor expanded in a around 0 1.83

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(1 + e^{b}\right)}} \]
    4. Simplified1.83

      \[\leadsto e^{\color{blue}{-\mathsf{log1p}\left(e^{b}\right)}} \]
      Proof

      [Start]1.83

      \[ e^{-1 \cdot \log \left(1 + e^{b}\right)} \]

      mul-1-neg [=>]1.83

      \[ e^{\color{blue}{-\log \left(1 + e^{b}\right)}} \]

      log1p-def [=>]1.83

      \[ e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error0.88%
Cost25920
\[e^{a - \log \left(e^{a} + e^{b}\right)} \]
Alternative 2
Error1.03%
Cost19520
\[\frac{e^{a}}{e^{a} + e^{b}} \]
Alternative 3
Error1.5%
Cost13252
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \end{array} \]
Alternative 4
Error19.87%
Cost7108
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;\left(1 + \frac{1}{b + 2}\right) + -1\\ \end{array} \]
Alternative 5
Error35.23%
Cost1244
\[\begin{array}{l} \mathbf{if}\;b \leq -750000000:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -2.1 \cdot 10^{-29}:\\ \;\;\;\;0\\ \mathbf{elif}\;b \leq -9 \cdot 10^{-57}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -1.6 \cdot 10^{-92}:\\ \;\;\;\;0\\ \mathbf{elif}\;b \leq -8 \cdot 10^{-267}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -8.2 \cdot 10^{-286}:\\ \;\;\;\;0\\ \mathbf{elif}\;b \leq 1.2:\\ \;\;\;\;0.5 + b \cdot -0.25\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 6
Error35.43%
Cost988
\[\begin{array}{l} \mathbf{if}\;b \leq -470000000:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -2 \cdot 10^{-28}:\\ \;\;\;\;0\\ \mathbf{elif}\;b \leq -7.5 \cdot 10^{-57}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -1.6 \cdot 10^{-92}:\\ \;\;\;\;0\\ \mathbf{elif}\;b \leq -1.15 \cdot 10^{-266}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -4.5 \cdot 10^{-286}:\\ \;\;\;\;0\\ \mathbf{elif}\;b \leq 3.7 \cdot 10^{-12}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 7
Error60.22%
Cost64
\[0.5 \]

Error

Reproduce?

herbie shell --seed 2023115 
(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))))