Quotient of sum of exps

Percentage Accurate: 98.8% → 98.6%
Time: 7.3s
Alternatives: 10
Speedup: TODO×

Specification

?
\[\begin{array}{l} t_0 := e^{a}\\ \frac{t_0}{t_0 + e^{b}} \end{array} \]

Your Program's Arguments

Results

Enter valid numbers for all inputs

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.

Accuracy vs Speed?

Herbie found 10 alternatives:

AlternativeAccuracySpeedup
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.

Target

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

Alternative 1?

\[e^{a - \left(\mathsf{log1p}\left(e^{b}\right) + \frac{a}{e^{b} + 1}\right)} \]
Derivation
  1. Initial program 99.2%

    \[\frac{e^{a}}{e^{a} + e^{b}} \]
  2. Step-by-step derivation
    1. add-exp-log99.2%

      \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
    2. div-exp99.2%

      \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
  3. Applied egg-rr99.2%

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

    \[\leadsto e^{a - \color{blue}{\left(\frac{a}{1 + e^{b}} + \log \left(1 + e^{b}\right)\right)}} \]
  5. Step-by-step derivation
    1. +-commutative99.3%

      \[\leadsto e^{a - \color{blue}{\left(\log \left(1 + e^{b}\right) + \frac{a}{1 + e^{b}}\right)}} \]
    2. log1p-def99.3%

      \[\leadsto e^{a - \left(\color{blue}{\mathsf{log1p}\left(e^{b}\right)} + \frac{a}{1 + e^{b}}\right)} \]
  6. Simplified99.3%

    \[\leadsto e^{a - \color{blue}{\left(\mathsf{log1p}\left(e^{b}\right) + \frac{a}{1 + e^{b}}\right)}} \]
  7. Final simplification99.3%

    \[\leadsto e^{a - \left(\mathsf{log1p}\left(e^{b}\right) + \frac{a}{e^{b} + 1}\right)} \]

Alternative 2?

\[e^{a - \log \left(e^{b} + e^{a}\right)} \]
Derivation
  1. Initial program 99.2%

    \[\frac{e^{a}}{e^{a} + e^{b}} \]
  2. Step-by-step derivation
    1. add-exp-log99.2%

      \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
    2. div-exp99.2%

      \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
  3. Applied egg-rr99.2%

    \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
  4. Final simplification99.2%

    \[\leadsto e^{a - \log \left(e^{b} + e^{a}\right)} \]

Alternative 3?

\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 5 \cdot 10^{-41}:\\ \;\;\;\;e^{a}\\ \mathbf{else}:\\ \;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 a) < 4.9999999999999996e-41

    1. Initial program 98.6%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log98.6%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp98.7%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr98.7%

      \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    4. Taylor expanded in a around inf 98.7%

      \[\leadsto e^{\color{blue}{a}} \]

    if 4.9999999999999996e-41 < (exp.f64 a)

    1. Initial program 99.4%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log99.4%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp99.4%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr99.4%

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

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(1 + e^{b}\right)}} \]
    5. Step-by-step derivation
      1. neg-mul-199.4%

        \[\leadsto e^{\color{blue}{-\log \left(1 + e^{b}\right)}} \]
      2. log1p-def99.4%

        \[\leadsto e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}} \]
    6. Simplified99.4%

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

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

Alternative 4?

\[\frac{e^{a}}{e^{b} + e^{a}} \]
Derivation
  1. Initial program 99.2%

    \[\frac{e^{a}}{e^{a} + e^{b}} \]
  2. Final simplification99.2%

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

Alternative 5?

\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 5 \cdot 10^{-41}:\\ \;\;\;\;e^{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 a) < 4.9999999999999996e-41

    1. Initial program 98.6%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log98.6%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp98.7%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr98.7%

      \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    4. Taylor expanded in a around inf 98.7%

      \[\leadsto e^{\color{blue}{a}} \]

    if 4.9999999999999996e-41 < (exp.f64 a)

    1. Initial program 99.4%

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

      \[\leadsto \color{blue}{\frac{1}{1 + e^{b}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{a} \leq 5 \cdot 10^{-41}:\\ \;\;\;\;e^{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{b} + 1}\\ \end{array} \]

Alternative 6?

\[\begin{array}{l} \mathbf{if}\;b \leq -12:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{a}}{2}\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if b < -12

    1. Initial program 97.9%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log97.9%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp97.9%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr97.9%

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

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(1 + e^{b}\right)}} \]
    5. Step-by-step derivation
      1. neg-mul-199.6%

        \[\leadsto e^{\color{blue}{-\log \left(1 + e^{b}\right)}} \]
      2. log1p-def99.6%

        \[\leadsto e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}} \]
    6. Simplified99.6%

      \[\leadsto e^{\color{blue}{-\mathsf{log1p}\left(e^{b}\right)}} \]
    7. Step-by-step derivation
      1. exp-neg99.6%

        \[\leadsto \color{blue}{\frac{1}{e^{\mathsf{log1p}\left(e^{b}\right)}}} \]
      2. log1p-udef99.6%

        \[\leadsto \frac{1}{e^{\color{blue}{\log \left(1 + e^{b}\right)}}} \]
      3. add-exp-log99.6%

        \[\leadsto \frac{1}{\color{blue}{1 + e^{b}}} \]
      4. add-sqr-sqrt99.6%

        \[\leadsto \frac{1}{\color{blue}{\sqrt{1 + e^{b}} \cdot \sqrt{1 + e^{b}}}} \]
      5. associate-/r*99.6%

        \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1 + e^{b}}}}{\sqrt{1 + e^{b}}}} \]
    8. Applied egg-rr98.9%

      \[\leadsto \color{blue}{\frac{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}} \]
    9. Step-by-step derivation
      1. *-inverses98.9%

        \[\leadsto \color{blue}{1} \]
    10. Simplified98.9%

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

    if -12 < b

    1. Initial program 99.5%

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

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

      \[\leadsto \frac{e^{a}}{\color{blue}{2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification77.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -12:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{e^{a}}{2}\\ \end{array} \]

Alternative 7?

\[\begin{array}{l} \mathbf{if}\;a \leq -1.35 \cdot 10^{-45}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;a \leq 4.4 \cdot 10^{-44}:\\ \;\;\;\;0.5 + a \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Derivation
  1. Split input into 3 regimes
  2. if a < -1.34999999999999992e-45

    1. Initial program 98.8%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log98.8%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp98.8%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr98.8%

      \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    4. Taylor expanded in a around inf 90.6%

      \[\leadsto e^{\color{blue}{a}} \]

    if -1.34999999999999992e-45 < a < 4.40000000000000024e-44

    1. Initial program 100.0%

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

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

      \[\leadsto \color{blue}{0.5 + 0.25 \cdot a} \]
    4. Step-by-step derivation
      1. *-commutative53.0%

        \[\leadsto 0.5 + \color{blue}{a \cdot 0.25} \]
    5. Simplified53.0%

      \[\leadsto \color{blue}{0.5 + a \cdot 0.25} \]

    if 4.40000000000000024e-44 < a

    1. Initial program 95.3%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log95.3%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp95.4%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr95.4%

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

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(1 + e^{b}\right)}} \]
    5. Step-by-step derivation
      1. neg-mul-195.3%

        \[\leadsto e^{\color{blue}{-\log \left(1 + e^{b}\right)}} \]
      2. log1p-def95.3%

        \[\leadsto e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}} \]
    6. Simplified95.3%

      \[\leadsto e^{\color{blue}{-\mathsf{log1p}\left(e^{b}\right)}} \]
    7. Step-by-step derivation
      1. exp-neg95.3%

        \[\leadsto \color{blue}{\frac{1}{e^{\mathsf{log1p}\left(e^{b}\right)}}} \]
      2. log1p-udef95.3%

        \[\leadsto \frac{1}{e^{\color{blue}{\log \left(1 + e^{b}\right)}}} \]
      3. add-exp-log95.3%

        \[\leadsto \frac{1}{\color{blue}{1 + e^{b}}} \]
      4. add-sqr-sqrt94.7%

        \[\leadsto \frac{1}{\color{blue}{\sqrt{1 + e^{b}} \cdot \sqrt{1 + e^{b}}}} \]
      5. associate-/r*94.9%

        \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1 + e^{b}}}}{\sqrt{1 + e^{b}}}} \]
    8. Applied egg-rr56.7%

      \[\leadsto \color{blue}{\frac{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}} \]
    9. Step-by-step derivation
      1. *-inverses57.2%

        \[\leadsto \color{blue}{1} \]
    10. Simplified57.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \leq -1.35 \cdot 10^{-45}:\\ \;\;\;\;e^{a}\\ \mathbf{elif}\;a \leq 4.4 \cdot 10^{-44}:\\ \;\;\;\;0.5 + a \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 8?

\[\begin{array}{l} \mathbf{if}\;b \leq -1.6:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0.5 + b \cdot -0.25\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if b < -1.6000000000000001

    1. Initial program 97.9%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log97.9%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp97.9%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr97.9%

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

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(1 + e^{b}\right)}} \]
    5. Step-by-step derivation
      1. neg-mul-199.6%

        \[\leadsto e^{\color{blue}{-\log \left(1 + e^{b}\right)}} \]
      2. log1p-def99.6%

        \[\leadsto e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}} \]
    6. Simplified99.6%

      \[\leadsto e^{\color{blue}{-\mathsf{log1p}\left(e^{b}\right)}} \]
    7. Step-by-step derivation
      1. exp-neg99.6%

        \[\leadsto \color{blue}{\frac{1}{e^{\mathsf{log1p}\left(e^{b}\right)}}} \]
      2. log1p-udef99.6%

        \[\leadsto \frac{1}{e^{\color{blue}{\log \left(1 + e^{b}\right)}}} \]
      3. add-exp-log99.6%

        \[\leadsto \frac{1}{\color{blue}{1 + e^{b}}} \]
      4. add-sqr-sqrt99.6%

        \[\leadsto \frac{1}{\color{blue}{\sqrt{1 + e^{b}} \cdot \sqrt{1 + e^{b}}}} \]
      5. associate-/r*99.6%

        \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1 + e^{b}}}}{\sqrt{1 + e^{b}}}} \]
    8. Applied egg-rr98.9%

      \[\leadsto \color{blue}{\frac{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}} \]
    9. Step-by-step derivation
      1. *-inverses98.9%

        \[\leadsto \color{blue}{1} \]
    10. Simplified98.9%

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

    if -1.6000000000000001 < b

    1. Initial program 99.5%

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

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

      \[\leadsto \color{blue}{0.5 + -0.25 \cdot b} \]
    4. Step-by-step derivation
      1. *-commutative40.4%

        \[\leadsto 0.5 + \color{blue}{b \cdot -0.25} \]
    5. Simplified40.4%

      \[\leadsto \color{blue}{0.5 + b \cdot -0.25} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification51.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -1.6:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0.5 + b \cdot -0.25\\ \end{array} \]

Alternative 9?

\[\begin{array}{l} \mathbf{if}\;b \leq -1.1:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if b < -1.1000000000000001

    1. Initial program 97.9%

      \[\frac{e^{a}}{e^{a} + e^{b}} \]
    2. Step-by-step derivation
      1. add-exp-log97.9%

        \[\leadsto \frac{e^{a}}{\color{blue}{e^{\log \left(e^{a} + e^{b}\right)}}} \]
      2. div-exp97.9%

        \[\leadsto \color{blue}{e^{a - \log \left(e^{a} + e^{b}\right)}} \]
    3. Applied egg-rr97.9%

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

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(1 + e^{b}\right)}} \]
    5. Step-by-step derivation
      1. neg-mul-199.6%

        \[\leadsto e^{\color{blue}{-\log \left(1 + e^{b}\right)}} \]
      2. log1p-def99.6%

        \[\leadsto e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}} \]
    6. Simplified99.6%

      \[\leadsto e^{\color{blue}{-\mathsf{log1p}\left(e^{b}\right)}} \]
    7. Step-by-step derivation
      1. exp-neg99.6%

        \[\leadsto \color{blue}{\frac{1}{e^{\mathsf{log1p}\left(e^{b}\right)}}} \]
      2. log1p-udef99.6%

        \[\leadsto \frac{1}{e^{\color{blue}{\log \left(1 + e^{b}\right)}}} \]
      3. add-exp-log99.6%

        \[\leadsto \frac{1}{\color{blue}{1 + e^{b}}} \]
      4. add-sqr-sqrt99.6%

        \[\leadsto \frac{1}{\color{blue}{\sqrt{1 + e^{b}} \cdot \sqrt{1 + e^{b}}}} \]
      5. associate-/r*99.6%

        \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{1 + e^{b}}}}{\sqrt{1 + e^{b}}}} \]
    8. Applied egg-rr98.9%

      \[\leadsto \color{blue}{\frac{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}{\mathsf{hypot}\left(1, \sqrt{e^{b}}\right)}} \]
    9. Step-by-step derivation
      1. *-inverses98.9%

        \[\leadsto \color{blue}{1} \]
    10. Simplified98.9%

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

    if -1.1000000000000001 < b

    1. Initial program 99.5%

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

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

      \[\leadsto \color{blue}{0.5} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification51.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -1.1:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]

Alternative 10?

\[0.5 \]
Derivation
  1. Initial program 99.2%

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

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

    \[\leadsto \color{blue}{0.5} \]
  4. Final simplification36.1%

    \[\leadsto 0.5 \]

Reproduce

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