Asymptote A

Percentage Accurate: 77.9% → 99.9%
Time: 5.8s
Alternatives: 4
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ \frac{1}{x + 1} - \frac{1}{x - 1} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))
double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
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 - 1.0d0))
end function
public static double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
def code(x):
	return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0))
function code(x)
	return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / Float64(x - 1.0)))
end
function tmp = code(x)
	tmp = (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x + 1} - \frac{1}{x - 1}
\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 4 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.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{x + 1} - \frac{1}{x - 1} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))
double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
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 - 1.0d0))
end function
public static double code(double x) {
	return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
def code(x):
	return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0))
function code(x)
	return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / Float64(x - 1.0)))
end
function tmp = code(x)
	tmp = (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 99.9% accurate, N/A× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} t_0 := \left(-1 \cdot x\_m\right) \cdot x\_m\\ t_1 := {\left(-1 \cdot t\_0\right)}^{2}\\ \mathbf{if}\;x\_m \leq 200000000:\\ \;\;\;\;\frac{\mathsf{fma}\left(x\_m - 1, 1, \mathsf{fma}\left(-1 \cdot x\_m, 1, -1\right)\right)}{\mathsf{fma}\left(t\_0, -1, -1\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_1}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_1}\right), 1, -2\right)}{x\_m}}{x\_m}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (let* ((t_0 (* (* -1.0 x_m) x_m)) (t_1 (pow (* -1.0 t_0) 2.0)))
   (if (<= x_m 200000000.0)
     (/ (fma (- x_m 1.0) 1.0 (fma (* -1.0 x_m) 1.0 -1.0)) (fma t_0 -1.0 -1.0))
     (/
      (/
       (fma (fma (* (pow t_1 -0.5) (- -2.0)) -1.0 (/ -2.0 t_1)) 1.0 -2.0)
       x_m)
      x_m))))
x_m = fabs(x);
double code(double x_m) {
	double t_0 = (-1.0 * x_m) * x_m;
	double t_1 = pow((-1.0 * t_0), 2.0);
	double tmp;
	if (x_m <= 200000000.0) {
		tmp = fma((x_m - 1.0), 1.0, fma((-1.0 * x_m), 1.0, -1.0)) / fma(t_0, -1.0, -1.0);
	} else {
		tmp = (fma(fma((pow(t_1, -0.5) * -(-2.0)), -1.0, (-2.0 / t_1)), 1.0, -2.0) / x_m) / x_m;
	}
	return tmp;
}
x_m = abs(x)
function code(x_m)
	t_0 = Float64(Float64(-1.0 * x_m) * x_m)
	t_1 = Float64(-1.0 * t_0) ^ 2.0
	tmp = 0.0
	if (x_m <= 200000000.0)
		tmp = Float64(fma(Float64(x_m - 1.0), 1.0, fma(Float64(-1.0 * x_m), 1.0, -1.0)) / fma(t_0, -1.0, -1.0));
	else
		tmp = Float64(Float64(fma(fma(Float64((t_1 ^ -0.5) * Float64(-(-2.0))), -1.0, Float64(-2.0 / t_1)), 1.0, -2.0) / x_m) / x_m);
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := Block[{t$95$0 = N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]}, Block[{t$95$1 = N[Power[N[(-1.0 * t$95$0), $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[x$95$m, 200000000.0], N[(N[(N[(x$95$m - 1.0), $MachinePrecision] * 1.0 + N[(N[(-1.0 * x$95$m), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * -1.0 + -1.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[(N[Power[t$95$1, -0.5], $MachinePrecision] * (--2.0)), $MachinePrecision] * -1.0 + N[(-2.0 / t$95$1), $MachinePrecision]), $MachinePrecision] * 1.0 + -2.0), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]]]
\begin{array}{l}
x_m = \left|x\right|

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

\mathbf{else}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_1}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_1}\right), 1, -2\right)}{x\_m}}{x\_m}\\


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

    1. Initial program 86.1%

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

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

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

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

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

        \[\leadsto \frac{1}{x + 1} - \color{blue}{\frac{1}{x - 1}} \]
      6. frac-subN/A

        \[\leadsto \color{blue}{\frac{1 \cdot \left(x - 1\right) - \left(x + 1\right) \cdot 1}{\left(x + 1\right) \cdot \left(x - 1\right)}} \]
      7. difference-of-squares-revN/A

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

        \[\leadsto \color{blue}{\frac{1 \cdot \left(x - 1\right) - \left(x + 1\right) \cdot 1}{x \cdot x - 1 \cdot 1}} \]
      9. fp-cancel-sub-sign-invN/A

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \frac{\mathsf{fma}\left(x - 1, 1, \mathsf{fma}\left(\color{blue}{-1 \cdot x}, 1, -1\right) \cdot 1\right)}{x \cdot x - 1 \cdot 1} \]
      20. difference-of-squares-revN/A

        \[\leadsto \frac{\mathsf{fma}\left(x - 1, 1, \mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot 1\right)}{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}} \]
      21. difference-of-sqr--1-revN/A

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

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

    if 2e8 < x

    1. Initial program 49.6%

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

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

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

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{\color{blue}{{x}^{2} \cdot {x}^{2}}} \]
      3. pow-prod-upN/A

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{{x}^{\color{blue}{\left(2 + 2\right)}}} \]
      4. metadata-evalN/A

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{{x}^{4}} \]
      5. lower-/.f64N/A

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{\color{blue}{{x}^{4}}} \]
    5. Applied rewrites35.3%

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

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

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

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

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} t_0 := {\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{2}\\ \mathbf{if}\;x\_m \leq 230:\\ \;\;\;\;\frac{1}{x\_m - -1} - \frac{1}{x\_m - 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_0}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_0}\right), 1, -2\right)}{x\_m}}{x\_m}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (let* ((t_0 (pow (* -1.0 (* (* -1.0 x_m) x_m)) 2.0)))
   (if (<= x_m 230.0)
     (- (/ 1.0 (- x_m -1.0)) (/ 1.0 (- x_m 1.0)))
     (/
      (/
       (fma (fma (* (pow t_0 -0.5) (- -2.0)) -1.0 (/ -2.0 t_0)) 1.0 -2.0)
       x_m)
      x_m))))
x_m = fabs(x);
double code(double x_m) {
	double t_0 = pow((-1.0 * ((-1.0 * x_m) * x_m)), 2.0);
	double tmp;
	if (x_m <= 230.0) {
		tmp = (1.0 / (x_m - -1.0)) - (1.0 / (x_m - 1.0));
	} else {
		tmp = (fma(fma((pow(t_0, -0.5) * -(-2.0)), -1.0, (-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m;
	}
	return tmp;
}
x_m = abs(x)
function code(x_m)
	t_0 = Float64(-1.0 * Float64(Float64(-1.0 * x_m) * x_m)) ^ 2.0
	tmp = 0.0
	if (x_m <= 230.0)
		tmp = Float64(Float64(1.0 / Float64(x_m - -1.0)) - Float64(1.0 / Float64(x_m - 1.0)));
	else
		tmp = Float64(Float64(fma(fma(Float64((t_0 ^ -0.5) * Float64(-(-2.0))), -1.0, Float64(-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m);
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := Block[{t$95$0 = N[Power[N[(-1.0 * N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[x$95$m, 230.0], N[(N[(1.0 / N[(x$95$m - -1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x$95$m - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[(N[Power[t$95$0, -0.5], $MachinePrecision] * (--2.0)), $MachinePrecision] * -1.0 + N[(-2.0 / t$95$0), $MachinePrecision]), $MachinePrecision] * 1.0 + -2.0), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
t_0 := {\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{2}\\
\mathbf{if}\;x\_m \leq 230:\\
\;\;\;\;\frac{1}{x\_m - -1} - \frac{1}{x\_m - 1}\\

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


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

    1. Initial program 86.3%

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

    if 230 < x

    1. Initial program 50.1%

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

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

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

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{\color{blue}{{x}^{2} \cdot {x}^{2}}} \]
      3. pow-prod-upN/A

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{{x}^{\color{blue}{\left(2 + 2\right)}}} \]
      4. metadata-evalN/A

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{{x}^{4}} \]
      5. lower-/.f64N/A

        \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{\color{blue}{{x}^{4}}} \]
    5. Applied rewrites37.1%

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

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

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

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

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} t_0 := {\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{2}\\ \frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_0}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_0}\right), 1, -2\right)}{x\_m}}{x\_m} \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (let* ((t_0 (pow (* -1.0 (* (* -1.0 x_m) x_m)) 2.0)))
   (/
    (/ (fma (fma (* (pow t_0 -0.5) (- -2.0)) -1.0 (/ -2.0 t_0)) 1.0 -2.0) x_m)
    x_m)))
x_m = fabs(x);
double code(double x_m) {
	double t_0 = pow((-1.0 * ((-1.0 * x_m) * x_m)), 2.0);
	return (fma(fma((pow(t_0, -0.5) * -(-2.0)), -1.0, (-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m;
}
x_m = abs(x)
function code(x_m)
	t_0 = Float64(-1.0 * Float64(Float64(-1.0 * x_m) * x_m)) ^ 2.0
	return Float64(Float64(fma(fma(Float64((t_0 ^ -0.5) * Float64(-(-2.0))), -1.0, Float64(-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m)
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := Block[{t$95$0 = N[Power[N[(-1.0 * N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]}, N[(N[(N[(N[(N[(N[Power[t$95$0, -0.5], $MachinePrecision] * (--2.0)), $MachinePrecision] * -1.0 + N[(-2.0 / t$95$0), $MachinePrecision]), $MachinePrecision] * 1.0 + -2.0), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
t_0 := {\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{2}\\
\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_0}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_0}\right), 1, -2\right)}{x\_m}}{x\_m}
\end{array}
\end{array}
Derivation
  1. Initial program 76.0%

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

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

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

      \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{\color{blue}{{x}^{2} \cdot {x}^{2}}} \]
    3. pow-prod-upN/A

      \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{{x}^{\color{blue}{\left(2 + 2\right)}}} \]
    4. metadata-evalN/A

      \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{{x}^{4}} \]
    5. lower-/.f64N/A

      \[\leadsto \frac{\left(-1 \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{{x}^{2}}\right) \cdot {x}^{2} - {x}^{2} \cdot 2}{\color{blue}{{x}^{4}}} \]
  5. Applied rewrites17.0%

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

    \[\leadsto \frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\left({\left({\left(x \cdot x\right)}^{2}\right)}^{-0.5} \cdot -1\right) \cdot -2, -1, \frac{-2}{{\left(x \cdot x\right)}^{2}}\right), 1, -2\right)}{-1 \cdot x}}{\color{blue}{-1 \cdot x}} \]
  7. Final simplification52.4%

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

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

\[\begin{array}{l} x_m = \left|x\right| \\ \frac{-1}{x\_m} \cdot \frac{\mathsf{fma}\left({\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{-1} \cdot 2, 1, 2\right)}{x\_m} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (*
  (/ -1.0 x_m)
  (/ (fma (* (pow (* -1.0 (* (* -1.0 x_m) x_m)) -1.0) 2.0) 1.0 2.0) x_m)))
x_m = fabs(x);
double code(double x_m) {
	return (-1.0 / x_m) * (fma((pow((-1.0 * ((-1.0 * x_m) * x_m)), -1.0) * 2.0), 1.0, 2.0) / x_m);
}
x_m = abs(x)
function code(x_m)
	return Float64(Float64(-1.0 / x_m) * Float64(fma(Float64((Float64(-1.0 * Float64(Float64(-1.0 * x_m) * x_m)) ^ -1.0) * 2.0), 1.0, 2.0) / x_m))
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(N[(-1.0 / x$95$m), $MachinePrecision] * N[(N[(N[(N[Power[N[(-1.0 * N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision] * 2.0), $MachinePrecision] * 1.0 + 2.0), $MachinePrecision] / x$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

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

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

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

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

      \[\leadsto \frac{-1 \cdot \left(2 + 2 \cdot \frac{1}{{x}^{2}}\right)}{x \cdot \color{blue}{x}} \]
    3. times-fracN/A

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

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

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

      \[\leadsto \frac{-1}{x} \cdot \frac{2 + 2 \cdot \frac{1}{{x}^{2}}}{\color{blue}{x}} \]
    7. +-commutativeN/A

      \[\leadsto \frac{-1}{x} \cdot \frac{2 \cdot \frac{1}{{x}^{2}} + 2}{x} \]
    8. *-commutativeN/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\frac{1}{{x}^{2}} \cdot 2 + 2}{x} \]
    9. metadata-evalN/A

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

      \[\leadsto \frac{-1}{x} \cdot \frac{\left(\frac{1}{{x}^{2}} \cdot 2\right) \cdot 1 + 2}{x} \]
    11. *-commutativeN/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\left(2 \cdot \frac{1}{{x}^{2}}\right) \cdot 1 + 2}{x} \]
    12. lower-fma.f64N/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left(2 \cdot \frac{1}{{x}^{2}}, 1, 2\right)}{x} \]
    13. *-commutativeN/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left(\frac{1}{{x}^{2}} \cdot 2, 1, 2\right)}{x} \]
    14. lower-*.f64N/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left(\frac{1}{{x}^{2}} \cdot 2, 1, 2\right)}{x} \]
    15. inv-powN/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left({\left({x}^{2}\right)}^{-1} \cdot 2, 1, 2\right)}{x} \]
    16. lower-pow.f64N/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left({\left({x}^{2}\right)}^{-1} \cdot 2, 1, 2\right)}{x} \]
    17. unpow2N/A

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left({\left(x \cdot x\right)}^{-1} \cdot 2, 1, 2\right)}{x} \]
    18. lower-*.f6452.2

      \[\leadsto \frac{-1}{x} \cdot \frac{\mathsf{fma}\left({\left(x \cdot x\right)}^{-1} \cdot 2, 1, 2\right)}{x} \]
  5. Applied rewrites52.2%

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

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

Reproduce

?
herbie shell --seed 2025065 
(FPCore (x)
  :name "Asymptote A"
  :precision binary64
  (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))