2frac (problem 3.3.1)

Percentage Accurate: 77.7% → 99.6%
Time: 4.1s
Alternatives: 6
Speedup: N/A×

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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    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 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.

Initial Program: 77.7% 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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    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.6% accurate, N/A× speedup?

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

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

\mathbf{elif}\;x \leq 1700:\\
\;\;\;\;\frac{1}{x - -1} - \frac{1}{x}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(t\_0 \cdot -1, 1, -1\right)}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -13500

    1. Initial program 52.2%

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

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

        \[\leadsto \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x \cdot \color{blue}{x}} \]
      2. associate-/r*N/A

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

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

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

    if -13500 < x < 1700

    1. Initial program 100.0%

      \[\frac{1}{x + 1} - \frac{1}{x} \]
    2. Add Preprocessing

    if 1700 < x

    1. Initial program 62.1%

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

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

        \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
      2. lower-/.f64N/A

        \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
    5. Applied rewrites99.9%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right) \cdot -1}{\left(-1 \cdot x\right) \cdot x}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification99.9%

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

Alternative 2: 99.4% accurate, N/A× speedup?

\[\begin{array}{l} \\ \frac{-1 \cdot -1}{\mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot x} \end{array} \]
(FPCore (x)
 :precision binary64
 (/ (* -1.0 -1.0) (* (fma (* -1.0 x) 1.0 -1.0) x)))
double code(double x) {
	return (-1.0 * -1.0) / (fma((-1.0 * x), 1.0, -1.0) * x);
}
function code(x)
	return Float64(Float64(-1.0 * -1.0) / Float64(fma(Float64(-1.0 * x), 1.0, -1.0) * x))
end
code[x_] := N[(N[(-1.0 * -1.0), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{-1 \cdot -1}{\mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot x}
\end{array}
Derivation
  1. Initial program 79.4%

    \[\frac{1}{x + 1} - \frac{1}{x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\frac{1}{x + 1} - \frac{1}{x}} \]
    2. lift-+.f64N/A

      \[\leadsto \frac{1}{\color{blue}{x + 1}} - \frac{1}{x} \]
    3. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{1}{x + 1}} - \frac{1}{x} \]
    4. frac-2negN/A

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

      \[\leadsto \frac{\color{blue}{-1}}{\mathsf{neg}\left(\left(x + 1\right)\right)} - \frac{1}{x} \]
    6. lift-/.f64N/A

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

      \[\leadsto \frac{-1}{\mathsf{neg}\left(\left(x + 1\right)\right)} - \color{blue}{\frac{\mathsf{neg}\left(1\right)}{\mathsf{neg}\left(x\right)}} \]
    8. metadata-evalN/A

      \[\leadsto \frac{-1}{\mathsf{neg}\left(\left(x + 1\right)\right)} - \frac{\color{blue}{-1}}{\mathsf{neg}\left(x\right)} \]
    9. frac-subN/A

      \[\leadsto \color{blue}{\frac{-1 \cdot \left(\mathsf{neg}\left(x\right)\right) - \left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)}} \]
    10. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot \left(\mathsf{neg}\left(x\right)\right) - \left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)}} \]
    11. lower--.f64N/A

      \[\leadsto \frac{\color{blue}{-1 \cdot \left(\mathsf{neg}\left(x\right)\right) - \left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    12. lower-*.f64N/A

      \[\leadsto \frac{\color{blue}{-1 \cdot \left(\mathsf{neg}\left(x\right)\right)} - \left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    13. mul-1-negN/A

      \[\leadsto \frac{-1 \cdot \color{blue}{\left(-1 \cdot x\right)} - \left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    14. lower-*.f64N/A

      \[\leadsto \frac{-1 \cdot \color{blue}{\left(-1 \cdot x\right)} - \left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    15. lower-*.f64N/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \color{blue}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot -1}}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    16. distribute-neg-inN/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \color{blue}{\left(\left(\mathsf{neg}\left(x\right)\right) + \left(\mathsf{neg}\left(1\right)\right)\right)} \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    17. *-lft-identityN/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \left(\color{blue}{1 \cdot \left(\mathsf{neg}\left(x\right)\right)} + \left(\mathsf{neg}\left(1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    18. *-commutativeN/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot 1} + \left(\mathsf{neg}\left(1\right)\right)\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    19. metadata-evalN/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \left(\left(\mathsf{neg}\left(x\right)\right) \cdot 1 + \color{blue}{-1}\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    20. lower-fma.f64N/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \color{blue}{\mathsf{fma}\left(\mathsf{neg}\left(x\right), 1, -1\right)} \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    21. mul-1-negN/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \mathsf{fma}\left(\color{blue}{-1 \cdot x}, 1, -1\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
    22. lower-*.f64N/A

      \[\leadsto \frac{-1 \cdot \left(-1 \cdot x\right) - \mathsf{fma}\left(\color{blue}{-1 \cdot x}, 1, -1\right) \cdot -1}{\left(\mathsf{neg}\left(\left(x + 1\right)\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \]
  4. Applied rewrites79.9%

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

    \[\leadsto \frac{\color{blue}{-1}}{\mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot \left(-1 \cdot x\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites99.9%

      \[\leadsto \frac{\color{blue}{-1}}{\mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot \left(-1 \cdot x\right)} \]
    2. Final simplification99.9%

      \[\leadsto \frac{-1 \cdot -1}{\mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot x} \]
    3. Add Preprocessing

    Alternative 3: 51.0% accurate, N/A× speedup?

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

      1. Initial program 100.0%

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

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

          \[\leadsto \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x \cdot \color{blue}{x}} \]
        2. associate-/r*N/A

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

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

        \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x}}{x}} \]
      6. Applied rewrites6.5%

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

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

      1. Initial program 70.6%

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

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

          \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
        2. lower-/.f64N/A

          \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
      5. Applied rewrites68.1%

        \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right) \cdot -1}{\left(-1 \cdot x\right) \cdot x}} \]
    3. Recombined 2 regimes into one program.
    4. Final simplification49.6%

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

    Alternative 4: 50.5% accurate, N/A× speedup?

    \[\begin{array}{l} \\ \frac{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x}}{x} \end{array} \]
    (FPCore (x)
     :precision binary64
     (/ (/ (fma (* (/ (fma (pow x -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) x) x))
    double code(double x) {
    	return (fma(((fma(pow(x, -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) / x;
    }
    
    function code(x)
    	return Float64(Float64(fma(Float64(Float64(fma((x ^ -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) / x)
    end
    
    code[x_] := N[(N[(N[(N[(N[(N[(N[Power[x, -1.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \frac{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x}}{x}
    \end{array}
    
    Derivation
    1. Initial program 79.4%

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

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

        \[\leadsto \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x \cdot \color{blue}{x}} \]
      2. associate-/r*N/A

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

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

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x}}{x}} \]
    6. Add Preprocessing

    Alternative 5: 49.1% accurate, N/A× speedup?

    \[\begin{array}{l} \\ \frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1} \end{array} \]
    (FPCore (x)
     :precision binary64
     (/
      (fma
       (* (/ (fma (* (/ (fma (pow x -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) x) -1.0)
       1.0
       -1.0)
      (* (* (* -1.0 x) x) -1.0)))
    double code(double x) {
    	return fma(((fma(((fma(pow(x, -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / (((-1.0 * x) * x) * -1.0);
    }
    
    function code(x)
    	return Float64(fma(Float64(Float64(fma(Float64(Float64(fma((x ^ -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / Float64(Float64(Float64(-1.0 * x) * x) * -1.0))
    end
    
    code[x_] := N[(N[(N[(N[(N[(N[(N[(N[(N[Power[x, -1.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * x), $MachinePrecision] * -1.0), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1}
    \end{array}
    
    Derivation
    1. Initial program 79.4%

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

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

        \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
      2. lower-/.f64N/A

        \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
    5. Applied rewrites47.8%

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right) \cdot -1}{\left(-1 \cdot x\right) \cdot x}} \]
    6. Final simplification47.8%

      \[\leadsto \frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1} \]
    7. Add Preprocessing

    Alternative 6: 15.7% accurate, N/A× speedup?

    \[\begin{array}{l} \\ \frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\left(-1 \cdot \left(x - 1\right)\right) \cdot x, 1, -1\right) \cdot x, 1, 1\right)}{{x}^{3}}}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1} \end{array} \]
    (FPCore (x)
     :precision binary64
     (/
      (/ (fma (* (fma (* (* -1.0 (- x 1.0)) x) 1.0 -1.0) x) 1.0 1.0) (pow x 3.0))
      (* (* (* -1.0 x) x) -1.0)))
    double code(double x) {
    	return (fma((fma(((-1.0 * (x - 1.0)) * x), 1.0, -1.0) * x), 1.0, 1.0) / pow(x, 3.0)) / (((-1.0 * x) * x) * -1.0);
    }
    
    function code(x)
    	return Float64(Float64(fma(Float64(fma(Float64(Float64(-1.0 * Float64(x - 1.0)) * x), 1.0, -1.0) * x), 1.0, 1.0) / (x ^ 3.0)) / Float64(Float64(Float64(-1.0 * x) * x) * -1.0))
    end
    
    code[x_] := N[(N[(N[(N[(N[(N[(N[(-1.0 * N[(x - 1.0), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] * x), $MachinePrecision] * 1.0 + 1.0), $MachinePrecision] / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * x), $MachinePrecision] * -1.0), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\left(-1 \cdot \left(x - 1\right)\right) \cdot x, 1, -1\right) \cdot x, 1, 1\right)}{{x}^{3}}}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1}
    \end{array}
    
    Derivation
    1. Initial program 79.4%

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

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

        \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
      2. lower-/.f64N/A

        \[\leadsto \frac{\mathsf{neg}\left(\left(-1 \cdot \frac{-1 \cdot \frac{\frac{1}{x} - 1}{x} - 1}{x} - 1\right)\right)}{\color{blue}{\mathsf{neg}\left({x}^{2}\right)}} \]
    5. Applied rewrites47.8%

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

      \[\leadsto \frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\left(-1 \cdot \left(x - 1\right)\right) \cdot x, 1, -1\right) \cdot x, 1, 1\right)}{{x}^{3}} \cdot -1}{\left(\color{blue}{-1} \cdot x\right) \cdot x} \]
    7. Final simplification13.0%

      \[\leadsto \frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\left(-1 \cdot \left(x - 1\right)\right) \cdot x, 1, -1\right) \cdot x, 1, 1\right)}{{x}^{3}}}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1} \]
    8. Add Preprocessing

    Reproduce

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