Quotient of sum of exps

?

Percentage Accurate: 99.0% → 98.6%
Time: 5.8s
Precision: binary64
Cost: 26184

?

\[\frac{e^{a}}{e^{a} + e^{b}} \]
\[\begin{array}{l} \mathbf{if}\;e^{b} \leq 0.999998:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \mathbf{elif}\;e^{b} \leq 1.0002:\\ \;\;\;\;\frac{e^{a}}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (if (<= (exp b) 0.999998)
   (/ 1.0 (+ (exp b) 1.0))
   (if (<= (exp b) 1.0002) (/ (exp a) (+ (exp a) 1.0)) 0.0)))
double code(double a, double b) {
	return exp(a) / (exp(a) + exp(b));
}
double code(double a, double b) {
	double tmp;
	if (exp(b) <= 0.999998) {
		tmp = 1.0 / (exp(b) + 1.0);
	} else if (exp(b) <= 1.0002) {
		tmp = exp(a) / (exp(a) + 1.0);
	} else {
		tmp = 0.0;
	}
	return tmp;
}
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
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if (exp(b) <= 0.999998d0) then
        tmp = 1.0d0 / (exp(b) + 1.0d0)
    else if (exp(b) <= 1.0002d0) then
        tmp = exp(a) / (exp(a) + 1.0d0)
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
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(b) <= 0.999998) {
		tmp = 1.0 / (Math.exp(b) + 1.0);
	} else if (Math.exp(b) <= 1.0002) {
		tmp = Math.exp(a) / (Math.exp(a) + 1.0);
	} else {
		tmp = 0.0;
	}
	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(b) <= 0.999998:
		tmp = 1.0 / (math.exp(b) + 1.0)
	elif math.exp(b) <= 1.0002:
		tmp = math.exp(a) / (math.exp(a) + 1.0)
	else:
		tmp = 0.0
	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(b) <= 0.999998)
		tmp = Float64(1.0 / Float64(exp(b) + 1.0));
	elseif (exp(b) <= 1.0002)
		tmp = Float64(exp(a) / Float64(exp(a) + 1.0));
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp = code(a, b)
	tmp = exp(a) / (exp(a) + exp(b));
end
function tmp_2 = code(a, b)
	tmp = 0.0;
	if (exp(b) <= 0.999998)
		tmp = 1.0 / (exp(b) + 1.0);
	elseif (exp(b) <= 1.0002)
		tmp = exp(a) / (exp(a) + 1.0);
	else
		tmp = 0.0;
	end
	tmp_2 = 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[b], $MachinePrecision], 0.999998], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Exp[b], $MachinePrecision], 1.0002], N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], 0.0]]
\frac{e^{a}}{e^{a} + e^{b}}
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.999998:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\

\mathbf{elif}\;e^{b} \leq 1.0002:\\
\;\;\;\;\frac{e^{a}}{e^{a} + 1}\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original99.0%
Target100.0%
Herbie98.6%
\[\frac{1}{1 + e^{b - a}} \]

Derivation?

  1. Split input into 3 regimes
  2. if (exp.f64 b) < 0.999998000000000054

    1. Initial program 98.1%

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

      \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]

    if 0.999998000000000054 < (exp.f64 b) < 1.0002

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{\frac{e^{a}}{1 + e^{a}}} \]

    if 1.0002 < (exp.f64 b)

    1. Initial program 98.7%

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

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

      \[\leadsto \frac{1}{\color{blue}{2 + b}} \]
    4. Simplified5.3%

      \[\leadsto \frac{1}{\color{blue}{b + 2}} \]
      Step-by-step derivation

      [Start]5.3

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

      +-commutative [=>]5.3

      \[ \frac{1}{\color{blue}{b + 2}} \]
    5. Applied egg-rr91.3%

      \[\leadsto \color{blue}{\left(1 + \frac{1}{b + 2}\right) - 1} \]
      Step-by-step derivation

      [Start]5.3

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

      expm1-log1p-u [=>]5.3

      \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{b + 2}\right)\right)} \]

      expm1-udef [=>]91.3

      \[ \color{blue}{e^{\mathsf{log1p}\left(\frac{1}{b + 2}\right)} - 1} \]

      log1p-udef [=>]91.3

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

      add-exp-log [<=]91.3

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

      \[\leadsto \color{blue}{1} - 1 \]
  3. Recombined 3 regimes into one program.
  4. Final simplification99.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{b} \leq 0.999998:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \mathbf{elif}\;e^{b} \leq 1.0002:\\ \;\;\;\;\frac{e^{a}}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.3%
Cost25920
\[e^{a - \log \left(e^{a} + e^{b}\right)} \]
Alternative 2
Accuracy99.0%
Cost19520
\[\frac{e^{a}}{e^{a} + e^{b}} \]
Alternative 3
Accuracy98.7%
Cost13252
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \end{array} \]
Alternative 4
Accuracy79.5%
Cost6596
\[\begin{array}{l} \mathbf{if}\;b \leq -3.9 \cdot 10^{-54}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;b \leq 0.00031:\\ \;\;\;\;0.5 + a \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 5
Accuracy79.3%
Cost708
\[\begin{array}{l} \mathbf{if}\;a \leq -5.5:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;\left(1 + \frac{1}{b + 2}\right) + -1\\ \end{array} \]
Alternative 6
Accuracy65.1%
Cost452
\[\begin{array}{l} \mathbf{if}\;b \leq 0.000226:\\ \;\;\;\;0.5 + a \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 7
Accuracy64.8%
Cost196
\[\begin{array}{l} \mathbf{if}\;b \leq 0.00014:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 8
Accuracy39.0%
Cost64
\[0.5 \]

Error

Reproduce?

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