Asymptote C

Percentage Accurate: 53.7% → 99.8%
Time: 2.3min
Alternatives: 10
Speedup: 0.7×

Specification

?
\[\frac{x}{x + 1} - \frac{x + 1}{x - 1} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))
double code(double x) {
	return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (x / (x + 1.0d0)) - ((x + 1.0d0) / (x - 1.0d0))
end function
public static double code(double x) {
	return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
def code(x):
	return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))
function code(x)
	return Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0)))
end
function tmp = code(x)
	tmp = (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
end
code[x_] := N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(x / (x + (1))) - ((x + (1)) / (x - (1)))
END code
\frac{x}{x + 1} - \frac{x + 1}{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 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: 53.7% accurate, 1.0× speedup?

\[\frac{x}{x + 1} - \frac{x + 1}{x - 1} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))
double code(double x) {
	return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (x / (x + 1.0d0)) - ((x + 1.0d0) / (x - 1.0d0))
end function
public static double code(double x) {
	return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
def code(x):
	return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))
function code(x)
	return Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0)))
end
function tmp = code(x)
	tmp = (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
end
code[x_] := N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	(x / (x + (1))) - ((x + (1)) / (x - (1)))
END code
\frac{x}{x + 1} - \frac{x + 1}{x - 1}

Alternative 1: 99.8% accurate, 0.4× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\ \;\;\;\;\frac{-1 \cdot \frac{1 + 3 \cdot \frac{1}{x}}{x} - 3}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{\mathsf{fma}\left(-3, x, -1\right)}}\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.0)
  (/ (- (* -1.0 (/ (+ 1.0 (* 3.0 (/ 1.0 x))) x)) 3.0) x)
  (/ 1.0 (/ (fma x x -1.0) (fma -3.0 x -1.0)))))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.0) {
		tmp = ((-1.0 * ((1.0 + (3.0 * (1.0 / x))) / x)) - 3.0) / x;
	} else {
		tmp = 1.0 / (fma(x, x, -1.0) / fma(-3.0, x, -1.0));
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.0)
		tmp = Float64(Float64(Float64(-1.0 * Float64(Float64(1.0 + Float64(3.0 * Float64(1.0 / x))) / x)) - 3.0) / x);
	else
		tmp = Float64(1.0 / Float64(fma(x, x, -1.0) / fma(-3.0, x, -1.0)));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(-1.0 * N[(N[(1.0 + N[(3.0 * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision] - 3.0), $MachinePrecision] / x), $MachinePrecision], N[(1.0 / N[(N[(x * x + -1.0), $MachinePrecision] / N[(-3.0 * x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (0)) THEN ((((-1) * (((1) + ((3) * ((1) / x))) / x)) - (3)) / x) ELSE ((1) / (((x * x) + (-1)) / (((-3) * x) + (-1)))) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\
\;\;\;\;\frac{-1 \cdot \frac{1 + 3 \cdot \frac{1}{x}}{x} - 3}{x}\\

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


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

    1. Initial program 53.7%

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

      \[\leadsto \frac{-1 \cdot \frac{1 + 3 \cdot \frac{1}{x}}{x} - 3}{x} \]
    3. Applied rewrites51.4%

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

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

    1. Initial program 53.7%

      \[\frac{x}{x + 1} - \frac{x + 1}{x - 1} \]
    2. Applied rewrites57.7%

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

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{-3 \cdot x - 1}} \]
    4. Applied rewrites76.7%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{-3 \cdot x - 1}} \]
    5. Applied rewrites76.7%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{\mathsf{fma}\left(-3, x, -1\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 99.7% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\ \;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{\mathsf{fma}\left(-3, x, -1\right)}}\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.0)
  (/ (- (/ -1.0 x) 3.0) x)
  (/ 1.0 (/ (fma x x -1.0) (fma -3.0 x -1.0)))))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.0) {
		tmp = ((-1.0 / x) - 3.0) / x;
	} else {
		tmp = 1.0 / (fma(x, x, -1.0) / fma(-3.0, x, -1.0));
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.0)
		tmp = Float64(Float64(Float64(-1.0 / x) - 3.0) / x);
	else
		tmp = Float64(1.0 / Float64(fma(x, x, -1.0) / fma(-3.0, x, -1.0)));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(-1.0 / x), $MachinePrecision] - 3.0), $MachinePrecision] / x), $MachinePrecision], N[(1.0 / N[(N[(x * x + -1.0), $MachinePrecision] / N[(-3.0 * x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (0)) THEN ((((-1) / x) - (3)) / x) ELSE ((1) / (((x * x) + (-1)) / (((-3) * x) + (-1)))) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\
\;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\

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


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

    1. Initial program 53.7%

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

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    3. Applied rewrites50.4%

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    4. Applied rewrites50.4%

      \[\leadsto \frac{\frac{-1}{x} - 3}{x} \]

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

    1. Initial program 53.7%

      \[\frac{x}{x + 1} - \frac{x + 1}{x - 1} \]
    2. Applied rewrites57.7%

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

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{-3 \cdot x - 1}} \]
    4. Applied rewrites76.7%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{-3 \cdot x - 1}} \]
    5. Applied rewrites76.7%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(x, x, -1\right)}{\mathsf{fma}\left(-3, x, -1\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.7% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\ \;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-3, x, -1\right)}{\mathsf{fma}\left(x, x, -1\right)}\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.0)
  (/ (- (/ -1.0 x) 3.0) x)
  (/ (fma -3.0 x -1.0) (fma x x -1.0))))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.0) {
		tmp = ((-1.0 / x) - 3.0) / x;
	} else {
		tmp = fma(-3.0, x, -1.0) / fma(x, x, -1.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.0)
		tmp = Float64(Float64(Float64(-1.0 / x) - 3.0) / x);
	else
		tmp = Float64(fma(-3.0, x, -1.0) / fma(x, x, -1.0));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(-1.0 / x), $MachinePrecision] - 3.0), $MachinePrecision] / x), $MachinePrecision], N[(N[(-3.0 * x + -1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (0)) THEN ((((-1) / x) - (3)) / x) ELSE ((((-3) * x) + (-1)) / ((x * x) + (-1))) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\
\;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\

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


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

    1. Initial program 53.7%

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

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    3. Applied rewrites50.4%

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    4. Applied rewrites50.4%

      \[\leadsto \frac{\frac{-1}{x} - 3}{x} \]

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

    1. Initial program 53.7%

      \[\frac{x}{x + 1} - \frac{x + 1}{x - 1} \]
    2. Applied rewrites57.8%

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

      \[\leadsto \frac{-3 \cdot x - 1}{\mathsf{fma}\left(x, x, -1\right)} \]
    4. Applied rewrites76.8%

      \[\leadsto \frac{-3 \cdot x - 1}{\mathsf{fma}\left(x, x, -1\right)} \]
    5. Applied rewrites76.8%

      \[\leadsto \frac{\mathsf{fma}\left(-3, x, -1\right)}{\mathsf{fma}\left(x, x, -1\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 99.2% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\ \;\;\;\;\frac{-3}{x} + \frac{-1}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(x, 3, 1\right)\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.5)
  (+ (/ -3.0 x) (/ -1.0 (* x x)))
  (* (fma x x 1.0) (fma x 3.0 1.0))))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = (-3.0 / x) + (-1.0 / (x * x));
	} else {
		tmp = fma(x, x, 1.0) * fma(x, 3.0, 1.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.5)
		tmp = Float64(Float64(-3.0 / x) + Float64(-1.0 / Float64(x * x)));
	else
		tmp = Float64(fma(x, x, 1.0) * fma(x, 3.0, 1.0));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(N[(-3.0 / x), $MachinePrecision] + N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * x + 1.0), $MachinePrecision] * N[(x * 3.0 + 1.0), $MachinePrecision]), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (5e-1)) THEN (((-3) / x) + ((-1) / (x * x))) ELSE (((x * x) + (1)) * ((x * (3)) + (1))) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{-3}{x} + \frac{-1}{x \cdot x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(x, 3, 1\right)\\


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

    1. Initial program 53.7%

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

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    3. Applied rewrites50.4%

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    4. Applied rewrites50.4%

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

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

    1. Initial program 53.7%

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

      \[\leadsto 1 + x \cdot \left(3 + x \cdot \left(1 + 3 \cdot x\right)\right) \]
    3. Applied rewrites49.8%

      \[\leadsto 1 + x \cdot \left(3 + x \cdot \left(1 + 3 \cdot x\right)\right) \]
    4. Applied rewrites49.8%

      \[\leadsto \mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(x, 3, 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 99.2% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\ \;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(x, 3, 1\right)\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.5)
  (/ (- (/ -1.0 x) 3.0) x)
  (* (fma x x 1.0) (fma x 3.0 1.0))))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = ((-1.0 / x) - 3.0) / x;
	} else {
		tmp = fma(x, x, 1.0) * fma(x, 3.0, 1.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.5)
		tmp = Float64(Float64(Float64(-1.0 / x) - 3.0) / x);
	else
		tmp = Float64(fma(x, x, 1.0) * fma(x, 3.0, 1.0));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(N[(N[(-1.0 / x), $MachinePrecision] - 3.0), $MachinePrecision] / x), $MachinePrecision], N[(N[(x * x + 1.0), $MachinePrecision] * N[(x * 3.0 + 1.0), $MachinePrecision]), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (5e-1)) THEN ((((-1) / x) - (3)) / x) ELSE (((x * x) + (1)) * ((x * (3)) + (1))) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(x, 3, 1\right)\\


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

    1. Initial program 53.7%

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

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    3. Applied rewrites50.4%

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    4. Applied rewrites50.4%

      \[\leadsto \frac{\frac{-1}{x} - 3}{x} \]

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

    1. Initial program 53.7%

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

      \[\leadsto 1 + x \cdot \left(3 + x \cdot \left(1 + 3 \cdot x\right)\right) \]
    3. Applied rewrites49.8%

      \[\leadsto 1 + x \cdot \left(3 + x \cdot \left(1 + 3 \cdot x\right)\right) \]
    4. Applied rewrites49.8%

      \[\leadsto \mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(x, 3, 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 99.1% accurate, 0.6× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\ \;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, x - -3, 1\right)\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.5)
  (/ (- (/ -1.0 x) 3.0) x)
  (fma x (- x -3.0) 1.0)))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = ((-1.0 / x) - 3.0) / x;
	} else {
		tmp = fma(x, (x - -3.0), 1.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.5)
		tmp = Float64(Float64(Float64(-1.0 / x) - 3.0) / x);
	else
		tmp = fma(x, Float64(x - -3.0), 1.0);
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(N[(N[(-1.0 / x), $MachinePrecision] - 3.0), $MachinePrecision] / x), $MachinePrecision], N[(x * N[(x - -3.0), $MachinePrecision] + 1.0), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (5e-1)) THEN ((((-1) / x) - (3)) / x) ELSE ((x * (x - (-3))) + (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{\frac{-1}{x} - 3}{x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x - -3, 1\right)\\


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

    1. Initial program 53.7%

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

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    3. Applied rewrites50.4%

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    4. Applied rewrites50.4%

      \[\leadsto \frac{\frac{-1}{x} - 3}{x} \]

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

    1. Initial program 53.7%

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

      \[\leadsto 1 + x \cdot \left(3 + x\right) \]
    3. Applied rewrites50.2%

      \[\leadsto 1 + x \cdot \left(3 + x\right) \]
    4. Applied rewrites50.2%

      \[\leadsto \mathsf{fma}\left(x, x - -3, 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 98.6% accurate, 0.6× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\ \;\;\;\;\frac{-3}{x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, x - -3, 1\right)\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.5)
  (/ -3.0 x)
  (fma x (- x -3.0) 1.0)))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = -3.0 / x;
	} else {
		tmp = fma(x, (x - -3.0), 1.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.5)
		tmp = Float64(-3.0 / x);
	else
		tmp = fma(x, Float64(x - -3.0), 1.0);
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(-3.0 / x), $MachinePrecision], N[(x * N[(x - -3.0), $MachinePrecision] + 1.0), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (5e-1)) THEN ((-3) / x) ELSE ((x * (x - (-3))) + (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{-3}{x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x - -3, 1\right)\\


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

    1. Initial program 53.7%

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

      \[\leadsto \frac{-3}{x} \]
    3. Applied rewrites51.1%

      \[\leadsto \frac{-3}{x} \]

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

    1. Initial program 53.7%

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

      \[\leadsto 1 + x \cdot \left(3 + x\right) \]
    3. Applied rewrites50.2%

      \[\leadsto 1 + x \cdot \left(3 + x\right) \]
    4. Applied rewrites50.2%

      \[\leadsto \mathsf{fma}\left(x, x - -3, 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 8: 98.4% accurate, 0.7× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\ \;\;\;\;\frac{-3}{x}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(3, x, 1\right)\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.5)
  (/ -3.0 x)
  (fma 3.0 x 1.0)))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = -3.0 / x;
	} else {
		tmp = fma(3.0, x, 1.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.5)
		tmp = Float64(-3.0 / x);
	else
		tmp = fma(3.0, x, 1.0);
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(-3.0 / x), $MachinePrecision], N[(3.0 * x + 1.0), $MachinePrecision]]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (5e-1)) THEN ((-3) / x) ELSE (((3) * x) + (1)) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{-3}{x}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(3, x, 1\right)\\


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

    1. Initial program 53.7%

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

      \[\leadsto \frac{-3}{x} \]
    3. Applied rewrites51.1%

      \[\leadsto \frac{-3}{x} \]

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

    1. Initial program 53.7%

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

      \[\leadsto 1 + 3 \cdot x \]
    3. Applied rewrites49.7%

      \[\leadsto 1 + 3 \cdot x \]
    4. Applied rewrites49.7%

      \[\leadsto \mathsf{fma}\left(3, x, 1\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 9: 97.8% accurate, 0.7× speedup?

\[\begin{array}{l} \mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\ \;\;\;\;\frac{-3}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.5)
  (/ -3.0 x)
  1.0))
double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = -3.0 / x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8) :: tmp
    if (((x / (x + 1.0d0)) - ((x + 1.0d0) / (x - 1.0d0))) <= 0.5d0) then
        tmp = (-3.0d0) / x
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5) {
		tmp = -3.0 / x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if ((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5:
		tmp = -3.0 / x
	else:
		tmp = 1.0
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.5)
		tmp = Float64(-3.0 / x);
	else
		tmp = 1.0;
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.5)
		tmp = -3.0 / x;
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(-3.0 / x), $MachinePrecision], 1.0]
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	LET tmp = IF (((x / (x + (1))) - ((x + (1)) / (x - (1)))) <= (5e-1)) THEN ((-3) / x) ELSE (1) ENDIF IN
	tmp
END code
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{-3}{x}\\

\mathbf{else}:\\
\;\;\;\;1\\


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

    1. Initial program 53.7%

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

      \[\leadsto \frac{-3}{x} \]
    3. Applied rewrites51.1%

      \[\leadsto \frac{-3}{x} \]

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

    1. Initial program 53.7%

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

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    3. Applied rewrites50.4%

      \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
    4. Applied rewrites50.4%

      \[\leadsto \frac{\frac{-1}{x} - 3}{x} \]
    5. Applied rewrites27.2%

      \[\leadsto \frac{-0.3333333333333333 - x}{\left(0.3333333333333333 \cdot x\right) \cdot x} \]
    6. Taylor expanded in x around 0

      \[\leadsto 1 \]
    7. Applied rewrites50.2%

      \[\leadsto 1 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 10: 50.2% accurate, 19.0× speedup?

\[1 \]
(FPCore (x)
  :precision binary64
  :pre TRUE
  1.0)
double code(double x) {
	return 1.0;
}
real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = 1.0d0
end function
public static double code(double x) {
	return 1.0;
}
def code(x):
	return 1.0
function code(x)
	return 1.0
end
function tmp = code(x)
	tmp = 1.0;
end
code[x_] := 1.0
f(x):
	x in [-inf, +inf]
code: THEORY
BEGIN
f(x: real): real =
	1
END code
1
Derivation
  1. Initial program 53.7%

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

    \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
  3. Applied rewrites50.4%

    \[\leadsto -1 \cdot \frac{3 + \frac{1}{x}}{x} \]
  4. Applied rewrites50.4%

    \[\leadsto \frac{\frac{-1}{x} - 3}{x} \]
  5. Applied rewrites27.2%

    \[\leadsto \frac{-0.3333333333333333 - x}{\left(0.3333333333333333 \cdot x\right) \cdot x} \]
  6. Taylor expanded in x around 0

    \[\leadsto 1 \]
  7. Applied rewrites50.2%

    \[\leadsto 1 \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2026074 +o generate:egglog
(FPCore (x)
  :name "Asymptote C"
  :precision binary64
  (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))