?

Average Error: 23.17% → 0.02%
Time: 2.9s
Precision: binary64
Cost: 7112

?

\[\frac{x}{x \cdot x + 1} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -10000000000:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 1000:\\ \;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(x, x, 1\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - {x}^{-3}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ x (+ (* x x) 1.0)))
(FPCore (x)
 :precision binary64
 (if (<= x -10000000000.0)
   (/ 1.0 x)
   (if (<= x 1000.0) (* x (/ 1.0 (fma x x 1.0))) (- (/ 1.0 x) (pow x -3.0)))))
double code(double x) {
	return x / ((x * x) + 1.0);
}
double code(double x) {
	double tmp;
	if (x <= -10000000000.0) {
		tmp = 1.0 / x;
	} else if (x <= 1000.0) {
		tmp = x * (1.0 / fma(x, x, 1.0));
	} else {
		tmp = (1.0 / x) - pow(x, -3.0);
	}
	return tmp;
}
function code(x)
	return Float64(x / Float64(Float64(x * x) + 1.0))
end
function code(x)
	tmp = 0.0
	if (x <= -10000000000.0)
		tmp = Float64(1.0 / x);
	elseif (x <= 1000.0)
		tmp = Float64(x * Float64(1.0 / fma(x, x, 1.0)));
	else
		tmp = Float64(Float64(1.0 / x) - (x ^ -3.0));
	end
	return tmp
end
code[x_] := N[(x / N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[x, -10000000000.0], N[(1.0 / x), $MachinePrecision], If[LessEqual[x, 1000.0], N[(x * N[(1.0 / N[(x * x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] - N[Power[x, -3.0], $MachinePrecision]), $MachinePrecision]]]
\frac{x}{x \cdot x + 1}
\begin{array}{l}
\mathbf{if}\;x \leq -10000000000:\\
\;\;\;\;\frac{1}{x}\\

\mathbf{elif}\;x \leq 1000:\\
\;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(x, x, 1\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{x} - {x}^{-3}\\


\end{array}

Error?

Target

Original23.17%
Target0.14%
Herbie0.02%
\[\frac{1}{x + \frac{1}{x}} \]

Derivation?

  1. Split input into 3 regimes
  2. if x < -1e10

    1. Initial program 47.64

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

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

    if -1e10 < x < 1e3

    1. Initial program 0.02

      \[\frac{x}{x \cdot x + 1} \]
    2. Applied egg-rr0.03

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

    if 1e3 < x

    1. Initial program 47.59

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

      \[\leadsto \color{blue}{\frac{1}{x} - \frac{1}{{x}^{3}}} \]
    3. Applied egg-rr0.27

      \[\leadsto \frac{1}{x} - \color{blue}{\left(e^{\mathsf{log1p}\left({x}^{-3}\right)} - 1\right)} \]
    4. Simplified0.04

      \[\leadsto \frac{1}{x} - \color{blue}{{x}^{-3}} \]
      Proof

      [Start]0.27

      \[ \frac{1}{x} - \left(e^{\mathsf{log1p}\left({x}^{-3}\right)} - 1\right) \]

      expm1-def [=>]0.04

      \[ \frac{1}{x} - \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({x}^{-3}\right)\right)} \]

      expm1-log1p [=>]0.04

      \[ \frac{1}{x} - \color{blue}{{x}^{-3}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.02

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

Alternatives

Alternative 1
Error0.02%
Cost7049
\[\begin{array}{l} \mathbf{if}\;x \leq -20000000 \lor \neg \left(x \leq 1000\right):\\ \;\;\;\;\frac{1}{x} - {x}^{-3}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{1 + x \cdot x}\\ \end{array} \]
Alternative 2
Error0.75%
Cost712
\[\begin{array}{l} \mathbf{if}\;x \leq -0.86:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 0.85:\\ \;\;\;\;x \cdot \left(1 - x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Alternative 3
Error0.01%
Cost712
\[\begin{array}{l} \mathbf{if}\;x \leq -10000000000:\\ \;\;\;\;\frac{1}{x}\\ \mathbf{elif}\;x \leq 100000000:\\ \;\;\;\;\frac{x}{1 + x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x}\\ \end{array} \]
Alternative 4
Error1.01%
Cost456
\[\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 5
Error48.33%
Cost64
\[x \]

Error

Reproduce?

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

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

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