x / (x^2 + 1)

Percentage Accurate: 76.4% → 100.0%
Time: 2.4s
Alternatives: 6
Speedup: TODO×

Specification

?
\[\frac{x}{x \cdot x + 1} \]

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 6 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

Original76.4%
Target99.8%
Herbie100.0%
\[\frac{1}{x + \frac{1}{x}} \]

Alternative 1?

\[\begin{array}{l} \mathbf{if}\;x \leq -20000000000:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 10000:\\ \;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(x, x, 1\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - {x}^{-3}\\ \end{array} \]
Derivation
  1. Split input into 3 regimes
  2. if x < -2e10

    1. Initial program 50.7%

      \[\frac{x}{x \cdot x + 1} \]
    2. Taylor expanded in x around inf 100.0%

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

    if -2e10 < x < 1e4

    1. Initial program 100.0%

      \[\frac{x}{x \cdot x + 1} \]
    2. Step-by-step derivation
      1. clear-num99.8%

        \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + 1}{x}}} \]
      2. associate-/r/100.0%

        \[\leadsto \color{blue}{\frac{1}{x \cdot x + 1} \cdot x} \]
      3. fma-def100.0%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, x, 1\right)}} \cdot x \]
    3. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(x, x, 1\right)} \cdot x} \]

    if 1e4 < x

    1. Initial program 45.8%

      \[\frac{x}{x \cdot x + 1} \]
    2. Step-by-step derivation
      1. clear-num45.9%

        \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + 1}{x}}} \]
      2. associate-/r/45.8%

        \[\leadsto \color{blue}{\frac{1}{x \cdot x + 1} \cdot x} \]
      3. fma-def45.8%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, x, 1\right)}} \cdot x \]
    3. Applied egg-rr45.8%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(x, x, 1\right)} \cdot x} \]
    4. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{\frac{1}{x} - \frac{1}{{x}^{3}}} \]
    5. Step-by-step derivation
      1. exp-to-pow100.0%

        \[\leadsto \frac{1}{x} - \frac{1}{\color{blue}{e^{\log x \cdot 3}}} \]
      2. *-commutative100.0%

        \[\leadsto \frac{1}{x} - \frac{1}{e^{\color{blue}{3 \cdot \log x}}} \]
      3. exp-neg100.0%

        \[\leadsto \frac{1}{x} - \color{blue}{e^{-3 \cdot \log x}} \]
      4. distribute-lft-neg-in100.0%

        \[\leadsto \frac{1}{x} - e^{\color{blue}{\left(-3\right) \cdot \log x}} \]
      5. metadata-eval100.0%

        \[\leadsto \frac{1}{x} - e^{\color{blue}{-3} \cdot \log x} \]
      6. *-commutative100.0%

        \[\leadsto \frac{1}{x} - e^{\color{blue}{\log x \cdot -3}} \]
      7. exp-to-pow100.0%

        \[\leadsto \frac{1}{x} - \color{blue}{{x}^{-3}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\frac{1}{x} - {x}^{-3}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -20000000000:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 10000:\\ \;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(x, x, 1\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - {x}^{-3}\\ \end{array} \]

Alternative 2?

\[\begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+30}:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 10000:\\ \;\;\;\;\frac{x}{1 + x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - {x}^{-3}\\ \end{array} \]
Derivation
  1. Split input into 3 regimes
  2. if x < -1e30

    1. Initial program 49.9%

      \[\frac{x}{x \cdot x + 1} \]
    2. Taylor expanded in x around inf 100.0%

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

    if -1e30 < x < 1e4

    1. Initial program 100.0%

      \[\frac{x}{x \cdot x + 1} \]

    if 1e4 < x

    1. Initial program 45.8%

      \[\frac{x}{x \cdot x + 1} \]
    2. Step-by-step derivation
      1. clear-num45.9%

        \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + 1}{x}}} \]
      2. associate-/r/45.8%

        \[\leadsto \color{blue}{\frac{1}{x \cdot x + 1} \cdot x} \]
      3. fma-def45.8%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, x, 1\right)}} \cdot x \]
    3. Applied egg-rr45.8%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(x, x, 1\right)} \cdot x} \]
    4. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{\frac{1}{x} - \frac{1}{{x}^{3}}} \]
    5. Step-by-step derivation
      1. exp-to-pow100.0%

        \[\leadsto \frac{1}{x} - \frac{1}{\color{blue}{e^{\log x \cdot 3}}} \]
      2. *-commutative100.0%

        \[\leadsto \frac{1}{x} - \frac{1}{e^{\color{blue}{3 \cdot \log x}}} \]
      3. exp-neg100.0%

        \[\leadsto \frac{1}{x} - \color{blue}{e^{-3 \cdot \log x}} \]
      4. distribute-lft-neg-in100.0%

        \[\leadsto \frac{1}{x} - e^{\color{blue}{\left(-3\right) \cdot \log x}} \]
      5. metadata-eval100.0%

        \[\leadsto \frac{1}{x} - e^{\color{blue}{-3} \cdot \log x} \]
      6. *-commutative100.0%

        \[\leadsto \frac{1}{x} - e^{\color{blue}{\log x \cdot -3}} \]
      7. exp-to-pow100.0%

        \[\leadsto \frac{1}{x} - \color{blue}{{x}^{-3}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\frac{1}{x} - {x}^{-3}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+30}:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 10000:\\ \;\;\;\;\frac{x}{1 + x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - {x}^{-3}\\ \end{array} \]

Alternative 3?

\[\begin{array}{l} \mathbf{if}\;x \leq -0.86:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 0.86:\\ \;\;\;\;x \cdot \left(1 - x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if x < -0.859999999999999987 or 0.859999999999999987 < x

    1. Initial program 48.8%

      \[\frac{x}{x \cdot x + 1} \]
    2. Taylor expanded in x around inf 99.2%

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

    if -0.859999999999999987 < x < 0.859999999999999987

    1. Initial program 100.0%

      \[\frac{x}{x \cdot x + 1} \]
    2. Step-by-step derivation
      1. clear-num99.8%

        \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + 1}{x}}} \]
      2. associate-/r/100.0%

        \[\leadsto \color{blue}{\frac{1}{x \cdot x + 1} \cdot x} \]
      3. fma-def100.0%

        \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, x, 1\right)}} \cdot x \]
    3. Applied egg-rr100.0%

      \[\leadsto \color{blue}{\frac{1}{\mathsf{fma}\left(x, x, 1\right)} \cdot x} \]
    4. Taylor expanded in x around 0 98.4%

      \[\leadsto \color{blue}{\left(1 + -1 \cdot {x}^{2}\right)} \cdot x \]
    5. Step-by-step derivation
      1. unpow298.4%

        \[\leadsto \left(1 + -1 \cdot \color{blue}{\left(x \cdot x\right)}\right) \cdot x \]
      2. neg-mul-198.4%

        \[\leadsto \left(1 + \color{blue}{\left(-x \cdot x\right)}\right) \cdot x \]
      3. sub-neg98.4%

        \[\leadsto \color{blue}{\left(1 - x \cdot x\right)} \cdot x \]
    6. Simplified98.4%

      \[\leadsto \color{blue}{\left(1 - x \cdot x\right)} \cdot x \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.86:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 0.86:\\ \;\;\;\;x \cdot \left(1 - x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]

Alternative 4?

\[\begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+30}:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 100000000:\\ \;\;\;\;\frac{x}{1 + x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if x < -1e30 or 1e8 < x

    1. Initial program 47.3%

      \[\frac{x}{x \cdot x + 1} \]
    2. Taylor expanded in x around inf 100.0%

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

    if -1e30 < x < 1e8

    1. Initial program 100.0%

      \[\frac{x}{x \cdot x + 1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+30}:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 100000000:\\ \;\;\;\;\frac{x}{1 + x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]

Alternative 5?

\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if x < -1 or 1 < x

    1. Initial program 48.8%

      \[\frac{x}{x \cdot x + 1} \]
    2. Taylor expanded in x around inf 99.2%

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

    if -1 < x < 1

    1. Initial program 100.0%

      \[\frac{x}{x \cdot x + 1} \]
    2. Taylor expanded in x around 0 97.7%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]

Alternative 6?

\[x \]
Derivation
  1. Initial program 73.2%

    \[\frac{x}{x \cdot x + 1} \]
  2. Taylor expanded in x around 0 48.4%

    \[\leadsto \color{blue}{x} \]
  3. Final simplification48.4%

    \[\leadsto x \]

Reproduce

?
herbie shell --seed 2023166 
(FPCore (x)
  :name "x / (x^2 + 1)"
  :precision binary64

  :herbie-target
  (/ 1.0 (+ x (/ 1.0 x)))

  (/ x (+ (* x x) 1.0)))