2frac (problem 3.3.1)

Percentage Accurate: 78.0% → 99.0%
Time: 7.9s
Alternatives: 10
Speedup: 0.3×

Specification

?
\[\begin{array}{l} \\ \frac{1}{x + 1} - \frac{1}{x} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x):
	return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x)
	return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x))
end
function tmp = code(x)
	tmp = (1.0 / (x + 1.0)) - (1.0 / x);
end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}

Sampling outcomes in binary64 precision:

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.

Initial Program: 78.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{x + 1} - \frac{1}{x} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x):
	return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x)
	return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x))
end
function tmp = code(x)
	tmp = (1.0 / (x + 1.0)) - (1.0 / x);
end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}

Alternative 1: 99.0% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\ \mathbf{if}\;t\_0 \leq -2:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, 1\right)} + \frac{-1}{x}\\ \mathbf{elif}\;t\_0 \leq 0:\\ \;\;\;\;\frac{1}{x} \cdot \frac{-1 + \frac{x + -1}{x \cdot x}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 x))))
   (if (<= t_0 -2.0)
     (+ (/ (fma x x 1.0) (* (+ 1.0 x) (fma x x 1.0))) (/ -1.0 x))
     (if (<= t_0 0.0)
       (* (/ 1.0 x) (/ (+ -1.0 (/ (+ x -1.0) (* x x))) x))
       (/ -1.0 x)))))
double code(double x) {
	double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
	double tmp;
	if (t_0 <= -2.0) {
		tmp = (fma(x, x, 1.0) / ((1.0 + x) * fma(x, x, 1.0))) + (-1.0 / x);
	} else if (t_0 <= 0.0) {
		tmp = (1.0 / x) * ((-1.0 + ((x + -1.0) / (x * x))) / x);
	} else {
		tmp = -1.0 / x;
	}
	return tmp;
}
function code(x)
	t_0 = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / x))
	tmp = 0.0
	if (t_0 <= -2.0)
		tmp = Float64(Float64(fma(x, x, 1.0) / Float64(Float64(1.0 + x) * fma(x, x, 1.0))) + Float64(-1.0 / x));
	elseif (t_0 <= 0.0)
		tmp = Float64(Float64(1.0 / x) * Float64(Float64(-1.0 + Float64(Float64(x + -1.0) / Float64(x * x))) / x));
	else
		tmp = Float64(-1.0 / x);
	end
	return tmp
end
code[x_] := Block[{t$95$0 = N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2.0], N[(N[(N[(x * x + 1.0), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] * N[(x * x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.0], N[(N[(1.0 / x), $MachinePrecision] * N[(N[(-1.0 + N[(N[(x + -1.0), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision], N[(-1.0 / x), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\
\mathbf{if}\;t\_0 \leq -2:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, 1\right)} + \frac{-1}{x}\\

\mathbf{elif}\;t\_0 \leq 0:\\
\;\;\;\;\frac{1}{x} \cdot \frac{-1 + \frac{x + -1}{x \cdot x}}{x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < -2

    1. Initial program 100.0%

      \[\frac{1}{x + 1} - \frac{1}{x} \]
    2. Add Preprocessing
    3. Applied rewrites100.0%

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

    if -2 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < 0.0

    1. Initial program 58.6%

      \[\frac{1}{x + 1} - \frac{1}{x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{\frac{1}{x} - \left(1 + \frac{1}{{x}^{2}}\right)}{{x}^{2}}} \]
    4. Step-by-step derivation
      1. associate--r+N/A

        \[\leadsto \frac{\color{blue}{\left(\frac{1}{x} - 1\right) - \frac{1}{{x}^{2}}}}{{x}^{2}} \]
      2. sub-negN/A

        \[\leadsto \frac{\color{blue}{\left(\frac{1}{x} - 1\right) + \left(\mathsf{neg}\left(\frac{1}{{x}^{2}}\right)\right)}}{{x}^{2}} \]
      3. +-commutativeN/A

        \[\leadsto \frac{\color{blue}{\left(\mathsf{neg}\left(\frac{1}{{x}^{2}}\right)\right) + \left(\frac{1}{x} - 1\right)}}{{x}^{2}} \]
      4. associate-+r-N/A

        \[\leadsto \frac{\color{blue}{\left(\left(\mathsf{neg}\left(\frac{1}{{x}^{2}}\right)\right) + \frac{1}{x}\right) - 1}}{{x}^{2}} \]
      5. neg-sub0N/A

        \[\leadsto \frac{\left(\color{blue}{\left(0 - \frac{1}{{x}^{2}}\right)} + \frac{1}{x}\right) - 1}{{x}^{2}} \]
      6. associate--r-N/A

        \[\leadsto \frac{\color{blue}{\left(0 - \left(\frac{1}{{x}^{2}} - \frac{1}{x}\right)\right)} - 1}{{x}^{2}} \]
      7. unpow2N/A

        \[\leadsto \frac{\left(0 - \left(\frac{1}{\color{blue}{x \cdot x}} - \frac{1}{x}\right)\right) - 1}{{x}^{2}} \]
      8. associate-/r*N/A

        \[\leadsto \frac{\left(0 - \left(\color{blue}{\frac{\frac{1}{x}}{x}} - \frac{1}{x}\right)\right) - 1}{{x}^{2}} \]
      9. div-subN/A

        \[\leadsto \frac{\left(0 - \color{blue}{\frac{\frac{1}{x} - 1}{x}}\right) - 1}{{x}^{2}} \]
      10. neg-sub0N/A

        \[\leadsto \frac{\color{blue}{\left(\mathsf{neg}\left(\frac{\frac{1}{x} - 1}{x}\right)\right)} - 1}{{x}^{2}} \]
      11. mul-1-negN/A

        \[\leadsto \frac{\color{blue}{-1 \cdot \frac{\frac{1}{x} - 1}{x}} - 1}{{x}^{2}} \]
      12. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{{x}^{2}}} \]
    5. Applied rewrites98.1%

      \[\leadsto \color{blue}{\frac{-1 + \frac{-1 + x}{x \cdot x}}{x \cdot x}} \]
    6. Step-by-step derivation
      1. Applied rewrites99.7%

        \[\leadsto \frac{-1 + \frac{x + -1}{x \cdot x}}{x} \cdot \color{blue}{\frac{1}{x}} \]

      if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x))

      1. Initial program 100.0%

        \[\frac{1}{x + 1} - \frac{1}{x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{-1}{x}} \]
      4. Step-by-step derivation
        1. lower-/.f64100.0

          \[\leadsto \color{blue}{\frac{-1}{x}} \]
      5. Applied rewrites100.0%

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1}{1 + x} + \frac{-1}{x} \leq -2:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, 1\right)} + \frac{-1}{x}\\ \mathbf{elif}\;\frac{1}{1 + x} + \frac{-1}{x} \leq 0:\\ \;\;\;\;\frac{1}{x} \cdot \frac{-1 + \frac{x + -1}{x \cdot x}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x}\\ \end{array} \]
    9. Add Preprocessing

    Alternative 2: 98.8% accurate, 0.3× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\ \mathbf{if}\;t\_0 \leq -2 \cdot 10^{-10}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, 1\right)} + \frac{-1}{x}\\ \mathbf{elif}\;t\_0 \leq 0:\\ \;\;\;\;\frac{\frac{-1}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x}\\ \end{array} \end{array} \]
    (FPCore (x)
     :precision binary64
     (let* ((t_0 (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 x))))
       (if (<= t_0 -2e-10)
         (+ (/ (fma x x 1.0) (* (+ 1.0 x) (fma x x 1.0))) (/ -1.0 x))
         (if (<= t_0 0.0) (/ (/ -1.0 x) x) (/ -1.0 x)))))
    double code(double x) {
    	double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
    	double tmp;
    	if (t_0 <= -2e-10) {
    		tmp = (fma(x, x, 1.0) / ((1.0 + x) * fma(x, x, 1.0))) + (-1.0 / x);
    	} else if (t_0 <= 0.0) {
    		tmp = (-1.0 / x) / x;
    	} else {
    		tmp = -1.0 / x;
    	}
    	return tmp;
    }
    
    function code(x)
    	t_0 = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / x))
    	tmp = 0.0
    	if (t_0 <= -2e-10)
    		tmp = Float64(Float64(fma(x, x, 1.0) / Float64(Float64(1.0 + x) * fma(x, x, 1.0))) + Float64(-1.0 / x));
    	elseif (t_0 <= 0.0)
    		tmp = Float64(Float64(-1.0 / x) / x);
    	else
    		tmp = Float64(-1.0 / x);
    	end
    	return tmp
    end
    
    code[x_] := Block[{t$95$0 = N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -2e-10], N[(N[(N[(x * x + 1.0), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] * N[(x * x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.0], N[(N[(-1.0 / x), $MachinePrecision] / x), $MachinePrecision], N[(-1.0 / x), $MachinePrecision]]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\
    \mathbf{if}\;t\_0 \leq -2 \cdot 10^{-10}:\\
    \;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, 1\right)} + \frac{-1}{x}\\
    
    \mathbf{elif}\;t\_0 \leq 0:\\
    \;\;\;\;\frac{\frac{-1}{x}}{x}\\
    
    \mathbf{else}:\\
    \;\;\;\;\frac{-1}{x}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < -2.00000000000000007e-10

      1. Initial program 99.7%

        \[\frac{1}{x + 1} - \frac{1}{x} \]
      2. Add Preprocessing
      3. Applied rewrites99.6%

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

      if -2.00000000000000007e-10 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < 0.0

      1. Initial program 55.9%

        \[\frac{1}{x + 1} - \frac{1}{x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{\frac{-1}{{x}^{2}}} \]
      4. Step-by-step derivation
        1. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{-1}{{x}^{2}}} \]
        2. unpow2N/A

          \[\leadsto \frac{-1}{\color{blue}{x \cdot x}} \]
        3. lower-*.f6497.9

          \[\leadsto \frac{-1}{\color{blue}{x \cdot x}} \]
      5. Applied rewrites97.9%

        \[\leadsto \color{blue}{\frac{-1}{x \cdot x}} \]
      6. Step-by-step derivation
        1. Applied rewrites98.8%

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

        if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x))

        1. Initial program 100.0%

          \[\frac{1}{x + 1} - \frac{1}{x} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{\frac{-1}{x}} \]
        4. Step-by-step derivation
          1. lower-/.f6498.1

            \[\leadsto \color{blue}{\frac{-1}{x}} \]
        5. Applied rewrites98.1%

          \[\leadsto \color{blue}{\frac{-1}{x}} \]
      7. Recombined 3 regimes into one program.
      8. Final simplification98.8%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1}{1 + x} + \frac{-1}{x} \leq -2 \cdot 10^{-10}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\left(1 + x\right) \cdot \mathsf{fma}\left(x, x, 1\right)} + \frac{-1}{x}\\ \mathbf{elif}\;\frac{1}{1 + x} + \frac{-1}{x} \leq 0:\\ \;\;\;\;\frac{\frac{-1}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-1}{x}\\ \end{array} \]
      9. Add Preprocessing

      Developer Target 1: 99.9% accurate, 1.1× speedup?

      \[\begin{array}{l} \\ \frac{\frac{-1}{x}}{x + 1} \end{array} \]
      (FPCore (x) :precision binary64 (/ (/ -1.0 x) (+ x 1.0)))
      double code(double x) {
      	return (-1.0 / x) / (x + 1.0);
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = ((-1.0d0) / x) / (x + 1.0d0)
      end function
      
      public static double code(double x) {
      	return (-1.0 / x) / (x + 1.0);
      }
      
      def code(x):
      	return (-1.0 / x) / (x + 1.0)
      
      function code(x)
      	return Float64(Float64(-1.0 / x) / Float64(x + 1.0))
      end
      
      function tmp = code(x)
      	tmp = (-1.0 / x) / (x + 1.0);
      end
      
      code[x_] := N[(N[(-1.0 / x), $MachinePrecision] / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \frac{\frac{-1}{x}}{x + 1}
      \end{array}
      

      Developer Target 2: 99.4% accurate, 1.5× speedup?

      \[\begin{array}{l} \\ \frac{1}{x \cdot \left(-1 - x\right)} \end{array} \]
      (FPCore (x) :precision binary64 (/ 1.0 (* x (- -1.0 x))))
      double code(double x) {
      	return 1.0 / (x * (-1.0 - x));
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = 1.0d0 / (x * ((-1.0d0) - x))
      end function
      
      public static double code(double x) {
      	return 1.0 / (x * (-1.0 - x));
      }
      
      def code(x):
      	return 1.0 / (x * (-1.0 - x))
      
      function code(x)
      	return Float64(1.0 / Float64(x * Float64(-1.0 - x)))
      end
      
      function tmp = code(x)
      	tmp = 1.0 / (x * (-1.0 - x));
      end
      
      code[x_] := N[(1.0 / N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \frac{1}{x \cdot \left(-1 - x\right)}
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (x)
        :name "2frac (problem 3.3.1)"
        :precision binary64
      
        :alt
        (! :herbie-platform default (/ (/ -1 x) (+ x 1)))
      
        :alt
        (! :herbie-platform default (/ 1 (* x (- -1 x))))
      
        (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))