Quotient of sum of exps

?

Percentage Accurate: 68.9% → 83.8%
Time: 11.5s
Precision: binary64
Cost: 39108

?

\[\frac{e^{a}}{e^{a} + e^{b}} \]
\[\begin{array}{l} t_0 := \frac{e^{a}}{e^{a} + e^{b}}\\ \mathbf{if}\;t_0 \leq 0.6:\\ \;\;\;\;t_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
 (let* ((t_0 (/ (exp a) (+ (exp a) (exp b)))))
   (if (<= t_0 0.6) t_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 t_0 = exp(a) / (exp(a) + exp(b));
	double tmp;
	if (t_0 <= 0.6) {
		tmp = t_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 t_0 = Math.exp(a) / (Math.exp(a) + Math.exp(b));
	double tmp;
	if (t_0 <= 0.6) {
		tmp = t_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):
	t_0 = math.exp(a) / (math.exp(a) + math.exp(b))
	tmp = 0
	if t_0 <= 0.6:
		tmp = t_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)
	t_0 = Float64(exp(a) / Float64(exp(a) + exp(b)))
	tmp = 0.0
	if (t_0 <= 0.6)
		tmp = t_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_] := 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, 0.6], t$95$0, N[Exp[(-N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision])], $MachinePrecision]]]
\frac{e^{a}}{e^{a} + e^{b}}
\begin{array}{l}
t_0 := \frac{e^{a}}{e^{a} + e^{b}}\\
\mathbf{if}\;t_0 \leq 0.6:\\
\;\;\;\;t_0\\

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


\end{array}

Local Percentage Accuracy vs ?

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.

Herbie found 9 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original68.9%
Target100.0%
Herbie83.8%
\[\frac{1}{1 + e^{b - a}} \]

Derivation?

  1. Split input into 2 regimes
  2. if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.599999999999999978

    1. Initial program 100.0%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]

    if 0.599999999999999978 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b)))

    1. Initial program 30.9%

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

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

      \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
    4. Simplified69.9%

      \[\leadsto \color{blue}{e^{-\mathsf{log1p}\left(e^{b}\right)}} \]
      Step-by-step derivation

      [Start]69.9%

      \[ \frac{1}{1 + e^{b}} \]

      rem-exp-log [<=]69.9%

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

      log1p-def [=>]69.9%

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

      exp-neg [<=]69.9%

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

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

Alternatives

Alternative 1
Accuracy83.8%
Cost39108
\[\begin{array}{l} t_0 := \frac{e^{a}}{e^{a} + e^{b}}\\ \mathbf{if}\;t_0 \leq 0.6:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\ \end{array} \]
Alternative 2
Accuracy59.2%
Cost7124
\[\begin{array}{l} \mathbf{if}\;b \leq -4.8 \cdot 10^{-17}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;b \leq -5.7 \cdot 10^{-167}:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq -3.1 \cdot 10^{-258}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;b \leq 0.015:\\ \;\;\;\;0.5 + b \cdot -0.25\\ \mathbf{elif}\;b \leq 1.5 \cdot 10^{+58}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;b \leq 1.34 \cdot 10^{+154}:\\ \;\;\;\;\frac{2 - b}{\frac{16 - \left(b \cdot b\right) \cdot \left(b \cdot b\right)}{4 + b \cdot b}}\\ \mathbf{else}:\\ \;\;\;\;\frac{2 - b}{-b \cdot b}\\ \end{array} \]
Alternative 3
Accuracy65.8%
Cost7124
\[\begin{array}{l} t_0 := 0.5 + b \cdot -0.25\\ \mathbf{if}\;b \leq -2.4:\\ \;\;\;\;e^{b} + 1\\ \mathbf{elif}\;b \leq -5.7 \cdot 10^{-167}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq -1 \cdot 10^{-259}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;b \leq 0.0019:\\ \;\;\;\;t_0\\ \mathbf{elif}\;b \leq 1.5 \cdot 10^{+58}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;b \leq 1.34 \cdot 10^{+154}:\\ \;\;\;\;\frac{2 - b}{\frac{16 - \left(b \cdot b\right) \cdot \left(b \cdot b\right)}{4 + b \cdot b}}\\ \mathbf{else}:\\ \;\;\;\;\frac{2 - b}{-b \cdot b}\\ \end{array} \]
Alternative 4
Accuracy80.1%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -9.5 \cdot 10^{+118}:\\ \;\;\;\;e^{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \end{array} \]
Alternative 5
Accuracy48.6%
Cost1480
\[\begin{array}{l} \mathbf{if}\;b \leq -1.35:\\ \;\;\;\;0.5\\ \mathbf{elif}\;b \leq 1.34 \cdot 10^{+154}:\\ \;\;\;\;\frac{2 - b}{\frac{16 - \left(b \cdot b\right) \cdot \left(b \cdot b\right)}{4 + b \cdot b}}\\ \mathbf{else}:\\ \;\;\;\;\frac{2 - b}{-b \cdot b}\\ \end{array} \]
Alternative 6
Accuracy43.6%
Cost836
\[\begin{array}{l} \mathbf{if}\;b \leq -2:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2 + \left(b + 0.5 \cdot \left(b \cdot b\right)\right)}\\ \end{array} \]
Alternative 7
Accuracy43.4%
Cost708
\[\begin{array}{l} \mathbf{if}\;b \leq -1.25:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{2 - b}{4 - b \cdot b}\\ \end{array} \]
Alternative 8
Accuracy43.2%
Cost644
\[\begin{array}{l} \mathbf{if}\;b \leq 6.4 \cdot 10^{+89}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{2 - b}{-b \cdot b}\\ \end{array} \]
Alternative 9
Accuracy32.3%
Cost64
\[0.5 \]

Reproduce?

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