Kahan p9 Example

Percentage Accurate: 68.4% → 100.0%
Time: 7.9s
Alternatives: 10
Speedup: 4.9×

Specification

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

\\
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\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: 68.4% accurate, 1.0× speedup?

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

\\
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\end{array}

Alternative 1: 100.0% accurate, 0.1× speedup?

\[\begin{array}{l} y = |y|\\ \\ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x + y}} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (/ (/ (- x y) (hypot x y)) (/ (hypot x y) (+ x y))))
y = abs(y);
double code(double x, double y) {
	return ((x - y) / hypot(x, y)) / (hypot(x, y) / (x + y));
}
y = Math.abs(y);
public static double code(double x, double y) {
	return ((x - y) / Math.hypot(x, y)) / (Math.hypot(x, y) / (x + y));
}
y = abs(y)
def code(x, y):
	return ((x - y) / math.hypot(x, y)) / (math.hypot(x, y) / (x + y))
y = abs(y)
function code(x, y)
	return Float64(Float64(Float64(x - y) / hypot(x, y)) / Float64(hypot(x, y) / Float64(x + y)))
end
y = abs(y)
function tmp = code(x, y)
	tmp = ((x - y) / hypot(x, y)) / (hypot(x, y) / (x + y));
end
NOTE: y should be positive before calling this function
code[x_, y_] := N[(N[(N[(x - y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y = |y|\\
\\
\frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x + y}}
\end{array}
Derivation
  1. Initial program 70.9%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Step-by-step derivation
    1. fma-def70.9%

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \]
    2. associate-/l*70.5%

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    3. *-un-lft-identity70.5%

      \[\leadsto \frac{\color{blue}{1 \cdot \left(x - y\right)}}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}} \]
    4. add-sqr-sqrt40.9%

      \[\leadsto \frac{1 \cdot \left(x - y\right)}{\color{blue}{\sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}} \cdot \sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}}} \]
    5. times-frac40.9%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \cdot \frac{x - y}{\sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}}} \]
    6. sqrt-div41.1%

      \[\leadsto \frac{1}{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}{\sqrt{x + y}}}} \cdot \frac{x - y}{\sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    7. fma-def41.1%

      \[\leadsto \frac{1}{\frac{\sqrt{\color{blue}{x \cdot x + y \cdot y}}}{\sqrt{x + y}}} \cdot \frac{x - y}{\sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    8. hypot-def41.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{hypot}\left(x, y\right)}}{\sqrt{x + y}}} \cdot \frac{x - y}{\sqrt{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    9. sqrt-div40.9%

      \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}} \cdot \frac{x - y}{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}{\sqrt{x + y}}}} \]
    10. fma-def40.9%

      \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}} \cdot \frac{x - y}{\frac{\sqrt{\color{blue}{x \cdot x + y \cdot y}}}{\sqrt{x + y}}} \]
    11. hypot-def51.1%

      \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}} \cdot \frac{x - y}{\frac{\color{blue}{\mathsf{hypot}\left(x, y\right)}}{\sqrt{x + y}}} \]
  3. Applied egg-rr51.1%

    \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}} \cdot \frac{x - y}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}}} \]
  4. Step-by-step derivation
    1. frac-times51.0%

      \[\leadsto \color{blue}{\frac{1 \cdot \left(x - y\right)}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}} \cdot \frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}}} \]
    2. *-un-lft-identity51.0%

      \[\leadsto \frac{\color{blue}{x - y}}{\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}} \cdot \frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}} \]
    3. pow251.0%

      \[\leadsto \frac{x - y}{\color{blue}{{\left(\frac{\mathsf{hypot}\left(x, y\right)}{\sqrt{x + y}}\right)}^{2}}} \]
  5. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x + y}}} \]
  6. Final simplification99.9%

    \[\leadsto \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x + y}} \]

Alternative 2: 99.7% accurate, 0.1× speedup?

\[\begin{array}{l} y = |y|\\ \\ \left(x + y\right) \cdot \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\mathsf{hypot}\left(x, y\right)} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (* (+ x y) (/ (/ (- x y) (hypot x y)) (hypot x y))))
y = abs(y);
double code(double x, double y) {
	return (x + y) * (((x - y) / hypot(x, y)) / hypot(x, y));
}
y = Math.abs(y);
public static double code(double x, double y) {
	return (x + y) * (((x - y) / Math.hypot(x, y)) / Math.hypot(x, y));
}
y = abs(y)
def code(x, y):
	return (x + y) * (((x - y) / math.hypot(x, y)) / math.hypot(x, y))
y = abs(y)
function code(x, y)
	return Float64(Float64(x + y) * Float64(Float64(Float64(x - y) / hypot(x, y)) / hypot(x, y)))
end
y = abs(y)
function tmp = code(x, y)
	tmp = (x + y) * (((x - y) / hypot(x, y)) / hypot(x, y));
end
NOTE: y should be positive before calling this function
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * N[(N[(N[(x - y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y = |y|\\
\\
\left(x + y\right) \cdot \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\mathsf{hypot}\left(x, y\right)}
\end{array}
Derivation
  1. Initial program 70.9%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Step-by-step derivation
    1. +-commutative70.9%

      \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
    2. +-commutative70.9%

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
    3. associate-*l/70.4%

      \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
    4. +-commutative70.4%

      \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
    5. +-commutative70.4%

      \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
    6. fma-def70.4%

      \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
  3. Simplified70.4%

    \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
  4. Step-by-step derivation
    1. *-un-lft-identity70.4%

      \[\leadsto \frac{\color{blue}{1 \cdot \left(x - y\right)}}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right) \]
    2. add-sqr-sqrt70.4%

      \[\leadsto \frac{1 \cdot \left(x - y\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}} \cdot \left(x + y\right) \]
    3. times-frac70.5%

      \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \frac{x - y}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right)} \cdot \left(x + y\right) \]
    4. fma-def70.5%

      \[\leadsto \left(\frac{1}{\sqrt{\color{blue}{x \cdot x + y \cdot y}}} \cdot \frac{x - y}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \left(x + y\right) \]
    5. hypot-def70.5%

      \[\leadsto \left(\frac{1}{\color{blue}{\mathsf{hypot}\left(x, y\right)}} \cdot \frac{x - y}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \left(x + y\right) \]
    6. fma-def70.5%

      \[\leadsto \left(\frac{1}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x - y}{\sqrt{\color{blue}{x \cdot x + y \cdot y}}}\right) \cdot \left(x + y\right) \]
    7. hypot-def99.7%

      \[\leadsto \left(\frac{1}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x - y}{\color{blue}{\mathsf{hypot}\left(x, y\right)}}\right) \cdot \left(x + y\right) \]
  5. Applied egg-rr99.7%

    \[\leadsto \color{blue}{\left(\frac{1}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x - y}{\mathsf{hypot}\left(x, y\right)}\right)} \cdot \left(x + y\right) \]
  6. Step-by-step derivation
    1. associate-*l/99.7%

      \[\leadsto \color{blue}{\frac{1 \cdot \frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\mathsf{hypot}\left(x, y\right)}} \cdot \left(x + y\right) \]
    2. *-lft-identity99.7%

      \[\leadsto \frac{\color{blue}{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}}{\mathsf{hypot}\left(x, y\right)} \cdot \left(x + y\right) \]
  7. Simplified99.7%

    \[\leadsto \color{blue}{\frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\mathsf{hypot}\left(x, y\right)}} \cdot \left(x + y\right) \]
  8. Final simplification99.7%

    \[\leadsto \left(x + y\right) \cdot \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\mathsf{hypot}\left(x, y\right)} \]

Alternative 3: 92.8% accurate, 0.5× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y + x \cdot \left(\frac{x}{y} + -1\right)}\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))))
   (if (<= t_0 2.0) t_0 (/ (- x y) (+ y (* x (+ (/ x y) -1.0)))))))
y = abs(y);
double code(double x, double y) {
	double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)));
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y))
    if (t_0 <= 2.0d0) then
        tmp = t_0
    else
        tmp = (x - y) / (y + (x * ((x / y) + (-1.0d0))))
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)));
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y))
	tmp = 0
	if t_0 <= 2.0:
		tmp = t_0
	else:
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)))
	return tmp
y = abs(y)
function code(x, y)
	t_0 = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)))
	tmp = 0.0
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = Float64(Float64(x - y) / Float64(y + Float64(x * Float64(Float64(x / y) + -1.0))));
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	tmp = 0.0;
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)));
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(N[(x - y), $MachinePrecision] / N[(y + N[(x * N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{y + x \cdot \left(\frac{x}{y} + -1\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2

    1. Initial program 99.8%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]

    if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y)))

    1. Initial program 0.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. associate-/l*3.1%

        \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}} \]
      2. +-commutative3.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y + x \cdot x}}{x + y}} \]
      3. remove-double-neg3.1%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{x + \color{blue}{\left(-\left(-y\right)\right)}}} \]
      4. sub-neg3.1%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{\color{blue}{x - \left(-y\right)}}} \]
      5. +-commutative3.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{x \cdot x + y \cdot y}}{x - \left(-y\right)}} \]
      6. fma-def3.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x - \left(-y\right)}} \]
      7. sub-neg3.1%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\color{blue}{x + \left(-\left(-y\right)\right)}}} \]
      8. remove-double-neg3.1%

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    4. Taylor expanded in x around 0 3.1%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{{y}^{2}}}{x + y}} \]
    5. Step-by-step derivation
      1. unpow23.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    6. Simplified3.1%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    7. Taylor expanded in y around inf 78.2%

      \[\leadsto \frac{x - y}{\color{blue}{y + \left(-1 \cdot x + \frac{{x}^{2}}{y}\right)}} \]
    8. Step-by-step derivation
      1. unpow278.2%

        \[\leadsto \frac{x - y}{y + \left(-1 \cdot x + \frac{\color{blue}{x \cdot x}}{y}\right)} \]
      2. associate-*l/79.3%

        \[\leadsto \frac{x - y}{y + \left(-1 \cdot x + \color{blue}{\frac{x}{y} \cdot x}\right)} \]
      3. distribute-rgt-out79.3%

        \[\leadsto \frac{x - y}{y + \color{blue}{x \cdot \left(-1 + \frac{x}{y}\right)}} \]
    9. Simplified79.3%

      \[\leadsto \frac{x - y}{\color{blue}{y + x \cdot \left(-1 + \frac{x}{y}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification93.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \leq 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y + x \cdot \left(\frac{x}{y} + -1\right)}\\ \end{array} \]

Alternative 4: 92.9% accurate, 0.5× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\left(x \cdot 2\right) \cdot \frac{x}{y} + \left(y - x\right)}\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))))
   (if (<= t_0 2.0) t_0 (/ (- x y) (+ (* (* x 2.0) (/ x y)) (- y x))))))
y = abs(y);
double code(double x, double y) {
	double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = (x - y) / (((x * 2.0) * (x / y)) + (y - x));
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y))
    if (t_0 <= 2.0d0) then
        tmp = t_0
    else
        tmp = (x - y) / (((x * 2.0d0) * (x / y)) + (y - x))
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = (x - y) / (((x * 2.0) * (x / y)) + (y - x));
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y))
	tmp = 0
	if t_0 <= 2.0:
		tmp = t_0
	else:
		tmp = (x - y) / (((x * 2.0) * (x / y)) + (y - x))
	return tmp
y = abs(y)
function code(x, y)
	t_0 = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)))
	tmp = 0.0
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = Float64(Float64(x - y) / Float64(Float64(Float64(x * 2.0) * Float64(x / y)) + Float64(y - x)));
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	tmp = 0.0;
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = (x - y) / (((x * 2.0) * (x / y)) + (y - x));
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(N[(x - y), $MachinePrecision] / N[(N[(N[(x * 2.0), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{\left(x \cdot 2\right) \cdot \frac{x}{y} + \left(y - x\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2

    1. Initial program 99.8%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]

    if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y)))

    1. Initial program 0.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. associate-/l*3.1%

        \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}} \]
      2. +-commutative3.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y + x \cdot x}}{x + y}} \]
      3. remove-double-neg3.1%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{x + \color{blue}{\left(-\left(-y\right)\right)}}} \]
      4. sub-neg3.1%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{\color{blue}{x - \left(-y\right)}}} \]
      5. +-commutative3.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{x \cdot x + y \cdot y}}{x - \left(-y\right)}} \]
      6. fma-def3.1%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x - \left(-y\right)}} \]
      7. sub-neg3.1%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\color{blue}{x + \left(-\left(-y\right)\right)}}} \]
      8. remove-double-neg3.1%

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    4. Step-by-step derivation
      1. fma-def3.1%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      2. +-commutative3.1%

        \[\leadsto \frac{x - y}{\color{blue}{y \cdot y + x \cdot x}} \cdot \left(x + y\right) \]
    5. Applied egg-rr3.1%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y + x \cdot x}}{x + y}} \]
    6. Taylor expanded in y around -inf 78.2%

      \[\leadsto \frac{x - y}{\color{blue}{y + \left(-1 \cdot x + -1 \cdot \frac{-1 \cdot {x}^{2} - {x}^{2}}{y}\right)}} \]
    7. Step-by-step derivation
      1. +-commutative78.2%

        \[\leadsto \frac{x - y}{\color{blue}{\left(-1 \cdot x + -1 \cdot \frac{-1 \cdot {x}^{2} - {x}^{2}}{y}\right) + y}} \]
      2. neg-mul-178.2%

        \[\leadsto \frac{x - y}{\left(\color{blue}{\left(-x\right)} + -1 \cdot \frac{-1 \cdot {x}^{2} - {x}^{2}}{y}\right) + y} \]
      3. +-commutative78.2%

        \[\leadsto \frac{x - y}{\color{blue}{\left(-1 \cdot \frac{-1 \cdot {x}^{2} - {x}^{2}}{y} + \left(-x\right)\right)} + y} \]
      4. associate-+l+78.2%

        \[\leadsto \frac{x - y}{\color{blue}{-1 \cdot \frac{-1 \cdot {x}^{2} - {x}^{2}}{y} + \left(\left(-x\right) + y\right)}} \]
    8. Simplified80.0%

      \[\leadsto \frac{x - y}{\color{blue}{\left(2 \cdot x\right) \cdot \frac{x}{y} + \left(y - x\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification94.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \leq 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\left(x \cdot 2\right) \cdot \frac{x}{y} + \left(y - x\right)}\\ \end{array} \]

Alternative 5: 92.0% accurate, 0.8× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 5.5 \cdot 10^{-167}:\\ \;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\ \mathbf{elif}\;y \leq 4 \cdot 10^{-18}:\\ \;\;\;\;\left(x + y\right) \cdot \frac{x - y}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= y 5.5e-167)
   (+ 1.0 (* (/ y x) (/ (* y -2.0) x)))
   (if (<= y 4e-18) (* (+ x y) (/ (- x y) (+ (* x x) (* y y)))) -1.0)))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 5.5e-167) {
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	} else if (y <= 4e-18) {
		tmp = (x + y) * ((x - y) / ((x * x) + (y * y)));
	} else {
		tmp = -1.0;
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= 5.5d-167) then
        tmp = 1.0d0 + ((y / x) * ((y * (-2.0d0)) / x))
    else if (y <= 4d-18) then
        tmp = (x + y) * ((x - y) / ((x * x) + (y * y)))
    else
        tmp = -1.0d0
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double tmp;
	if (y <= 5.5e-167) {
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	} else if (y <= 4e-18) {
		tmp = (x + y) * ((x - y) / ((x * x) + (y * y)));
	} else {
		tmp = -1.0;
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 5.5e-167:
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x))
	elif y <= 4e-18:
		tmp = (x + y) * ((x - y) / ((x * x) + (y * y)))
	else:
		tmp = -1.0
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 5.5e-167)
		tmp = Float64(1.0 + Float64(Float64(y / x) * Float64(Float64(y * -2.0) / x)));
	elseif (y <= 4e-18)
		tmp = Float64(Float64(x + y) * Float64(Float64(x - y) / Float64(Float64(x * x) + Float64(y * y))));
	else
		tmp = -1.0;
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= 5.5e-167)
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	elseif (y <= 4e-18)
		tmp = (x + y) * ((x - y) / ((x * x) + (y * y)));
	else
		tmp = -1.0;
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := If[LessEqual[y, 5.5e-167], N[(1.0 + N[(N[(y / x), $MachinePrecision] * N[(N[(y * -2.0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 4e-18], N[(N[(x + y), $MachinePrecision] * N[(N[(x - y), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 5.5 \cdot 10^{-167}:\\
\;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\

\mathbf{elif}\;y \leq 4 \cdot 10^{-18}:\\
\;\;\;\;\left(x + y\right) \cdot \frac{x - y}{x \cdot x + y \cdot y}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < 5.5000000000000003e-167

    1. Initial program 64.6%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative64.6%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative64.6%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/64.8%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative64.8%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative64.8%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def64.8%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified64.8%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around -inf 30.6%

      \[\leadsto \color{blue}{1 + \left(-1 \cdot \frac{y}{x} + \left(-1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} + \frac{y}{x}\right)\right)} \]
    5. Step-by-step derivation
      1. associate-+r+30.6%

        \[\leadsto \color{blue}{\left(1 + -1 \cdot \frac{y}{x}\right) + \left(-1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} + \frac{y}{x}\right)} \]
      2. +-commutative30.6%

        \[\leadsto \left(1 + -1 \cdot \frac{y}{x}\right) + \color{blue}{\left(\frac{y}{x} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}}\right)} \]
      3. associate-+r+30.6%

        \[\leadsto \color{blue}{\left(\left(1 + -1 \cdot \frac{y}{x}\right) + \frac{y}{x}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}}} \]
      4. associate-+r+30.6%

        \[\leadsto \color{blue}{\left(1 + \left(-1 \cdot \frac{y}{x} + \frac{y}{x}\right)\right)} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      5. distribute-lft1-in30.6%

        \[\leadsto \left(1 + \color{blue}{\left(-1 + 1\right) \cdot \frac{y}{x}}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      6. metadata-eval30.6%

        \[\leadsto \left(1 + \color{blue}{0} \cdot \frac{y}{x}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      7. mul0-lft31.2%

        \[\leadsto \left(1 + \color{blue}{0}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      8. metadata-eval31.2%

        \[\leadsto \color{blue}{1} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      9. unpow231.2%

        \[\leadsto 1 + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{\color{blue}{x \cdot x}} \]
      10. associate-*r/31.2%

        \[\leadsto 1 + \color{blue}{\frac{-1 \cdot \left({y}^{2} - -1 \cdot {y}^{2}\right)}{x \cdot x}} \]
    6. Simplified31.2%

      \[\leadsto \color{blue}{1 + \frac{\left(y \cdot y\right) \cdot -2}{x \cdot x}} \]
    7. Taylor expanded in y around 0 31.2%

      \[\leadsto 1 + \color{blue}{-2 \cdot \frac{{y}^{2}}{{x}^{2}}} \]
    8. Step-by-step derivation
      1. associate-*r/31.2%

        \[\leadsto 1 + \color{blue}{\frac{-2 \cdot {y}^{2}}{{x}^{2}}} \]
      2. *-commutative31.2%

        \[\leadsto 1 + \frac{\color{blue}{{y}^{2} \cdot -2}}{{x}^{2}} \]
      3. unpow231.2%

        \[\leadsto 1 + \frac{\color{blue}{\left(y \cdot y\right)} \cdot -2}{{x}^{2}} \]
      4. associate-*r*31.2%

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

        \[\leadsto 1 + \frac{y \cdot \left(y \cdot -2\right)}{\color{blue}{x \cdot x}} \]
      6. times-frac38.7%

        \[\leadsto 1 + \color{blue}{\frac{y}{x} \cdot \frac{y \cdot -2}{x}} \]
    9. Simplified38.7%

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

    if 5.5000000000000003e-167 < y < 4.0000000000000003e-18

    1. Initial program 99.9%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative99.9%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative99.9%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/95.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative95.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative95.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def95.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified95.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Step-by-step derivation
      1. fma-def95.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      2. +-commutative95.5%

        \[\leadsto \frac{x - y}{\color{blue}{y \cdot y + x \cdot x}} \cdot \left(x + y\right) \]
    5. Applied egg-rr95.5%

      \[\leadsto \frac{x - y}{\color{blue}{y \cdot y + x \cdot x}} \cdot \left(x + y\right) \]

    if 4.0000000000000003e-18 < y

    1. Initial program 100.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative100.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/99.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative99.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative99.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def99.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified99.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{-1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification49.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 5.5 \cdot 10^{-167}:\\ \;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\ \mathbf{elif}\;y \leq 4 \cdot 10^{-18}:\\ \;\;\;\;\left(x + y\right) \cdot \frac{x - y}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Alternative 6: 83.7% accurate, 1.0× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 2.5 \cdot 10^{-137}:\\ \;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y + x \cdot \left(\frac{x}{y} + -1\right)}\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= y 2.5e-137)
   (+ 1.0 (* (/ y x) (/ (* y -2.0) x)))
   (/ (- x y) (+ y (* x (+ (/ x y) -1.0))))))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 2.5e-137) {
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	} else {
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)));
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= 2.5d-137) then
        tmp = 1.0d0 + ((y / x) * ((y * (-2.0d0)) / x))
    else
        tmp = (x - y) / (y + (x * ((x / y) + (-1.0d0))))
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double tmp;
	if (y <= 2.5e-137) {
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	} else {
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)));
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 2.5e-137:
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x))
	else:
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)))
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 2.5e-137)
		tmp = Float64(1.0 + Float64(Float64(y / x) * Float64(Float64(y * -2.0) / x)));
	else
		tmp = Float64(Float64(x - y) / Float64(y + Float64(x * Float64(Float64(x / y) + -1.0))));
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= 2.5e-137)
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	else
		tmp = (x - y) / (y + (x * ((x / y) + -1.0)));
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := If[LessEqual[y, 2.5e-137], N[(1.0 + N[(N[(y / x), $MachinePrecision] * N[(N[(y * -2.0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x - y), $MachinePrecision] / N[(y + N[(x * N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.5 \cdot 10^{-137}:\\
\;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{y + x \cdot \left(\frac{x}{y} + -1\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 2.5e-137

    1. Initial program 66.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/65.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative65.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative65.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def65.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified65.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around -inf 31.2%

      \[\leadsto \color{blue}{1 + \left(-1 \cdot \frac{y}{x} + \left(-1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} + \frac{y}{x}\right)\right)} \]
    5. Step-by-step derivation
      1. associate-+r+31.2%

        \[\leadsto \color{blue}{\left(1 + -1 \cdot \frac{y}{x}\right) + \left(-1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} + \frac{y}{x}\right)} \]
      2. +-commutative31.2%

        \[\leadsto \left(1 + -1 \cdot \frac{y}{x}\right) + \color{blue}{\left(\frac{y}{x} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}}\right)} \]
      3. associate-+r+31.2%

        \[\leadsto \color{blue}{\left(\left(1 + -1 \cdot \frac{y}{x}\right) + \frac{y}{x}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}}} \]
      4. associate-+r+31.2%

        \[\leadsto \color{blue}{\left(1 + \left(-1 \cdot \frac{y}{x} + \frac{y}{x}\right)\right)} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      5. distribute-lft1-in31.2%

        \[\leadsto \left(1 + \color{blue}{\left(-1 + 1\right) \cdot \frac{y}{x}}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      6. metadata-eval31.2%

        \[\leadsto \left(1 + \color{blue}{0} \cdot \frac{y}{x}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      7. mul0-lft31.8%

        \[\leadsto \left(1 + \color{blue}{0}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      8. metadata-eval31.8%

        \[\leadsto \color{blue}{1} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      9. unpow231.8%

        \[\leadsto 1 + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{\color{blue}{x \cdot x}} \]
      10. associate-*r/31.8%

        \[\leadsto 1 + \color{blue}{\frac{-1 \cdot \left({y}^{2} - -1 \cdot {y}^{2}\right)}{x \cdot x}} \]
    6. Simplified31.8%

      \[\leadsto \color{blue}{1 + \frac{\left(y \cdot y\right) \cdot -2}{x \cdot x}} \]
    7. Taylor expanded in y around 0 31.8%

      \[\leadsto 1 + \color{blue}{-2 \cdot \frac{{y}^{2}}{{x}^{2}}} \]
    8. Step-by-step derivation
      1. associate-*r/31.8%

        \[\leadsto 1 + \color{blue}{\frac{-2 \cdot {y}^{2}}{{x}^{2}}} \]
      2. *-commutative31.8%

        \[\leadsto 1 + \frac{\color{blue}{{y}^{2} \cdot -2}}{{x}^{2}} \]
      3. unpow231.8%

        \[\leadsto 1 + \frac{\color{blue}{\left(y \cdot y\right)} \cdot -2}{{x}^{2}} \]
      4. associate-*r*31.8%

        \[\leadsto 1 + \frac{\color{blue}{y \cdot \left(y \cdot -2\right)}}{{x}^{2}} \]
      5. unpow231.8%

        \[\leadsto 1 + \frac{y \cdot \left(y \cdot -2\right)}{\color{blue}{x \cdot x}} \]
      6. times-frac39.1%

        \[\leadsto 1 + \color{blue}{\frac{y}{x} \cdot \frac{y \cdot -2}{x}} \]
    9. Simplified39.1%

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

    if 2.5e-137 < y

    1. Initial program 99.9%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. associate-/l*99.8%

        \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}} \]
      2. +-commutative99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y + x \cdot x}}{x + y}} \]
      3. remove-double-neg99.8%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{x + \color{blue}{\left(-\left(-y\right)\right)}}} \]
      4. sub-neg99.8%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{\color{blue}{x - \left(-y\right)}}} \]
      5. +-commutative99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{x \cdot x + y \cdot y}}{x - \left(-y\right)}} \]
      6. fma-def99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x - \left(-y\right)}} \]
      7. sub-neg99.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\color{blue}{x + \left(-\left(-y\right)\right)}}} \]
      8. remove-double-neg99.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + \color{blue}{y}}} \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    4. Taylor expanded in x around 0 86.7%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{{y}^{2}}}{x + y}} \]
    5. Step-by-step derivation
      1. unpow286.7%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    6. Simplified86.7%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    7. Taylor expanded in y around inf 87.0%

      \[\leadsto \frac{x - y}{\color{blue}{y + \left(-1 \cdot x + \frac{{x}^{2}}{y}\right)}} \]
    8. Step-by-step derivation
      1. unpow287.0%

        \[\leadsto \frac{x - y}{y + \left(-1 \cdot x + \frac{\color{blue}{x \cdot x}}{y}\right)} \]
      2. associate-*l/87.0%

        \[\leadsto \frac{x - y}{y + \left(-1 \cdot x + \color{blue}{\frac{x}{y} \cdot x}\right)} \]
      3. distribute-rgt-out87.0%

        \[\leadsto \frac{x - y}{y + \color{blue}{x \cdot \left(-1 + \frac{x}{y}\right)}} \]
    9. Simplified87.0%

      \[\leadsto \frac{x - y}{\color{blue}{y + x \cdot \left(-1 + \frac{x}{y}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification46.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 2.5 \cdot 10^{-137}:\\ \;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y + x \cdot \left(\frac{x}{y} + -1\right)}\\ \end{array} \]

Alternative 7: 83.6% accurate, 1.1× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 3.45 \cdot 10^{-137}:\\ \;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= y 3.45e-137)
   (+ 1.0 (* (/ y x) (/ (* y -2.0) x)))
   (+ -1.0 (* (/ x y) (/ x y)))))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 3.45e-137) {
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	} else {
		tmp = -1.0 + ((x / y) * (x / y));
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= 3.45d-137) then
        tmp = 1.0d0 + ((y / x) * ((y * (-2.0d0)) / x))
    else
        tmp = (-1.0d0) + ((x / y) * (x / y))
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double tmp;
	if (y <= 3.45e-137) {
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	} else {
		tmp = -1.0 + ((x / y) * (x / y));
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 3.45e-137:
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x))
	else:
		tmp = -1.0 + ((x / y) * (x / y))
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 3.45e-137)
		tmp = Float64(1.0 + Float64(Float64(y / x) * Float64(Float64(y * -2.0) / x)));
	else
		tmp = Float64(-1.0 + Float64(Float64(x / y) * Float64(x / y)));
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= 3.45e-137)
		tmp = 1.0 + ((y / x) * ((y * -2.0) / x));
	else
		tmp = -1.0 + ((x / y) * (x / y));
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := If[LessEqual[y, 3.45e-137], N[(1.0 + N[(N[(y / x), $MachinePrecision] * N[(N[(y * -2.0), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 + N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.45 \cdot 10^{-137}:\\
\;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\

\mathbf{else}:\\
\;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 3.44999999999999988e-137

    1. Initial program 66.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/65.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative65.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative65.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def65.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified65.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around -inf 31.2%

      \[\leadsto \color{blue}{1 + \left(-1 \cdot \frac{y}{x} + \left(-1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} + \frac{y}{x}\right)\right)} \]
    5. Step-by-step derivation
      1. associate-+r+31.2%

        \[\leadsto \color{blue}{\left(1 + -1 \cdot \frac{y}{x}\right) + \left(-1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} + \frac{y}{x}\right)} \]
      2. +-commutative31.2%

        \[\leadsto \left(1 + -1 \cdot \frac{y}{x}\right) + \color{blue}{\left(\frac{y}{x} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}}\right)} \]
      3. associate-+r+31.2%

        \[\leadsto \color{blue}{\left(\left(1 + -1 \cdot \frac{y}{x}\right) + \frac{y}{x}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}}} \]
      4. associate-+r+31.2%

        \[\leadsto \color{blue}{\left(1 + \left(-1 \cdot \frac{y}{x} + \frac{y}{x}\right)\right)} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      5. distribute-lft1-in31.2%

        \[\leadsto \left(1 + \color{blue}{\left(-1 + 1\right) \cdot \frac{y}{x}}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      6. metadata-eval31.2%

        \[\leadsto \left(1 + \color{blue}{0} \cdot \frac{y}{x}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      7. mul0-lft31.8%

        \[\leadsto \left(1 + \color{blue}{0}\right) + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      8. metadata-eval31.8%

        \[\leadsto \color{blue}{1} + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{{x}^{2}} \]
      9. unpow231.8%

        \[\leadsto 1 + -1 \cdot \frac{{y}^{2} - -1 \cdot {y}^{2}}{\color{blue}{x \cdot x}} \]
      10. associate-*r/31.8%

        \[\leadsto 1 + \color{blue}{\frac{-1 \cdot \left({y}^{2} - -1 \cdot {y}^{2}\right)}{x \cdot x}} \]
    6. Simplified31.8%

      \[\leadsto \color{blue}{1 + \frac{\left(y \cdot y\right) \cdot -2}{x \cdot x}} \]
    7. Taylor expanded in y around 0 31.8%

      \[\leadsto 1 + \color{blue}{-2 \cdot \frac{{y}^{2}}{{x}^{2}}} \]
    8. Step-by-step derivation
      1. associate-*r/31.8%

        \[\leadsto 1 + \color{blue}{\frac{-2 \cdot {y}^{2}}{{x}^{2}}} \]
      2. *-commutative31.8%

        \[\leadsto 1 + \frac{\color{blue}{{y}^{2} \cdot -2}}{{x}^{2}} \]
      3. unpow231.8%

        \[\leadsto 1 + \frac{\color{blue}{\left(y \cdot y\right)} \cdot -2}{{x}^{2}} \]
      4. associate-*r*31.8%

        \[\leadsto 1 + \frac{\color{blue}{y \cdot \left(y \cdot -2\right)}}{{x}^{2}} \]
      5. unpow231.8%

        \[\leadsto 1 + \frac{y \cdot \left(y \cdot -2\right)}{\color{blue}{x \cdot x}} \]
      6. times-frac39.1%

        \[\leadsto 1 + \color{blue}{\frac{y}{x} \cdot \frac{y \cdot -2}{x}} \]
    9. Simplified39.1%

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

    if 3.44999999999999988e-137 < y

    1. Initial program 99.9%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. associate-/l*99.8%

        \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}} \]
      2. +-commutative99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y + x \cdot x}}{x + y}} \]
      3. remove-double-neg99.8%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{x + \color{blue}{\left(-\left(-y\right)\right)}}} \]
      4. sub-neg99.8%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{\color{blue}{x - \left(-y\right)}}} \]
      5. +-commutative99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{x \cdot x + y \cdot y}}{x - \left(-y\right)}} \]
      6. fma-def99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x - \left(-y\right)}} \]
      7. sub-neg99.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\color{blue}{x + \left(-\left(-y\right)\right)}}} \]
      8. remove-double-neg99.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + \color{blue}{y}}} \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    4. Taylor expanded in x around 0 86.7%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{{y}^{2}}}{x + y}} \]
    5. Step-by-step derivation
      1. unpow286.7%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    6. Simplified86.7%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    7. Taylor expanded in x around 0 86.8%

      \[\leadsto \color{blue}{\frac{{x}^{2}}{{y}^{2}} - 1} \]
    8. Step-by-step derivation
      1. sub-neg86.8%

        \[\leadsto \color{blue}{\frac{{x}^{2}}{{y}^{2}} + \left(-1\right)} \]
      2. remove-double-neg86.8%

        \[\leadsto \color{blue}{\left(-\left(-\frac{{x}^{2}}{{y}^{2}}\right)\right)} + \left(-1\right) \]
      3. mul-1-neg86.8%

        \[\leadsto \left(-\color{blue}{-1 \cdot \frac{{x}^{2}}{{y}^{2}}}\right) + \left(-1\right) \]
      4. distribute-neg-in86.8%

        \[\leadsto \color{blue}{-\left(-1 \cdot \frac{{x}^{2}}{{y}^{2}} + 1\right)} \]
      5. +-commutative86.8%

        \[\leadsto -\color{blue}{\left(1 + -1 \cdot \frac{{x}^{2}}{{y}^{2}}\right)} \]
      6. distribute-neg-in86.8%

        \[\leadsto \color{blue}{\left(-1\right) + \left(--1 \cdot \frac{{x}^{2}}{{y}^{2}}\right)} \]
      7. metadata-eval86.8%

        \[\leadsto \color{blue}{-1} + \left(--1 \cdot \frac{{x}^{2}}{{y}^{2}}\right) \]
      8. mul-1-neg86.8%

        \[\leadsto -1 + \left(-\color{blue}{\left(-\frac{{x}^{2}}{{y}^{2}}\right)}\right) \]
      9. remove-double-neg86.8%

        \[\leadsto -1 + \color{blue}{\frac{{x}^{2}}{{y}^{2}}} \]
      10. unpow286.8%

        \[\leadsto -1 + \frac{\color{blue}{x \cdot x}}{{y}^{2}} \]
      11. unpow286.8%

        \[\leadsto -1 + \frac{x \cdot x}{\color{blue}{y \cdot y}} \]
      12. times-frac86.8%

        \[\leadsto -1 + \color{blue}{\frac{x}{y} \cdot \frac{x}{y}} \]
    9. Simplified86.8%

      \[\leadsto \color{blue}{-1 + \frac{x}{y} \cdot \frac{x}{y}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification46.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 3.45 \cdot 10^{-137}:\\ \;\;\;\;1 + \frac{y}{x} \cdot \frac{y \cdot -2}{x}\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\ \end{array} \]

Alternative 8: 82.9% accurate, 1.4× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 1.55 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= y 1.55e-137) 1.0 (+ -1.0 (* (/ x y) (/ x y)))))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 1.55e-137) {
		tmp = 1.0;
	} else {
		tmp = -1.0 + ((x / y) * (x / y));
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= 1.55d-137) then
        tmp = 1.0d0
    else
        tmp = (-1.0d0) + ((x / y) * (x / y))
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double tmp;
	if (y <= 1.55e-137) {
		tmp = 1.0;
	} else {
		tmp = -1.0 + ((x / y) * (x / y));
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 1.55e-137:
		tmp = 1.0
	else:
		tmp = -1.0 + ((x / y) * (x / y))
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 1.55e-137)
		tmp = 1.0;
	else
		tmp = Float64(-1.0 + Float64(Float64(x / y) * Float64(x / y)));
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= 1.55e-137)
		tmp = 1.0;
	else
		tmp = -1.0 + ((x / y) * (x / y));
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := If[LessEqual[y, 1.55e-137], 1.0, N[(-1.0 + N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.55 \cdot 10^{-137}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.54999999999999989e-137

    1. Initial program 66.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/65.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative65.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative65.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def65.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified65.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around inf 36.7%

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

    if 1.54999999999999989e-137 < y

    1. Initial program 99.9%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. associate-/l*99.8%

        \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}} \]
      2. +-commutative99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y + x \cdot x}}{x + y}} \]
      3. remove-double-neg99.8%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{x + \color{blue}{\left(-\left(-y\right)\right)}}} \]
      4. sub-neg99.8%

        \[\leadsto \frac{x - y}{\frac{y \cdot y + x \cdot x}{\color{blue}{x - \left(-y\right)}}} \]
      5. +-commutative99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{x \cdot x + y \cdot y}}{x - \left(-y\right)}} \]
      6. fma-def99.8%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x - \left(-y\right)}} \]
      7. sub-neg99.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\color{blue}{x + \left(-\left(-y\right)\right)}}} \]
      8. remove-double-neg99.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + \color{blue}{y}}} \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
    4. Taylor expanded in x around 0 86.7%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{{y}^{2}}}{x + y}} \]
    5. Step-by-step derivation
      1. unpow286.7%

        \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    6. Simplified86.7%

      \[\leadsto \frac{x - y}{\frac{\color{blue}{y \cdot y}}{x + y}} \]
    7. Taylor expanded in x around 0 86.8%

      \[\leadsto \color{blue}{\frac{{x}^{2}}{{y}^{2}} - 1} \]
    8. Step-by-step derivation
      1. sub-neg86.8%

        \[\leadsto \color{blue}{\frac{{x}^{2}}{{y}^{2}} + \left(-1\right)} \]
      2. remove-double-neg86.8%

        \[\leadsto \color{blue}{\left(-\left(-\frac{{x}^{2}}{{y}^{2}}\right)\right)} + \left(-1\right) \]
      3. mul-1-neg86.8%

        \[\leadsto \left(-\color{blue}{-1 \cdot \frac{{x}^{2}}{{y}^{2}}}\right) + \left(-1\right) \]
      4. distribute-neg-in86.8%

        \[\leadsto \color{blue}{-\left(-1 \cdot \frac{{x}^{2}}{{y}^{2}} + 1\right)} \]
      5. +-commutative86.8%

        \[\leadsto -\color{blue}{\left(1 + -1 \cdot \frac{{x}^{2}}{{y}^{2}}\right)} \]
      6. distribute-neg-in86.8%

        \[\leadsto \color{blue}{\left(-1\right) + \left(--1 \cdot \frac{{x}^{2}}{{y}^{2}}\right)} \]
      7. metadata-eval86.8%

        \[\leadsto \color{blue}{-1} + \left(--1 \cdot \frac{{x}^{2}}{{y}^{2}}\right) \]
      8. mul-1-neg86.8%

        \[\leadsto -1 + \left(-\color{blue}{\left(-\frac{{x}^{2}}{{y}^{2}}\right)}\right) \]
      9. remove-double-neg86.8%

        \[\leadsto -1 + \color{blue}{\frac{{x}^{2}}{{y}^{2}}} \]
      10. unpow286.8%

        \[\leadsto -1 + \frac{\color{blue}{x \cdot x}}{{y}^{2}} \]
      11. unpow286.8%

        \[\leadsto -1 + \frac{x \cdot x}{\color{blue}{y \cdot y}} \]
      12. times-frac86.8%

        \[\leadsto -1 + \color{blue}{\frac{x}{y} \cdot \frac{x}{y}} \]
    9. Simplified86.8%

      \[\leadsto \color{blue}{-1 + \frac{x}{y} \cdot \frac{x}{y}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification44.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 1.55 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\ \end{array} \]

Alternative 9: 82.5% accurate, 4.9× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 2 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y) :precision binary64 (if (<= y 2e-137) 1.0 -1.0))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 2e-137) {
		tmp = 1.0;
	} else {
		tmp = -1.0;
	}
	return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (y <= 2d-137) then
        tmp = 1.0d0
    else
        tmp = -1.0d0
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double tmp;
	if (y <= 2e-137) {
		tmp = 1.0;
	} else {
		tmp = -1.0;
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 2e-137:
		tmp = 1.0
	else:
		tmp = -1.0
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 2e-137)
		tmp = 1.0;
	else
		tmp = -1.0;
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= 2e-137)
		tmp = 1.0;
	else
		tmp = -1.0;
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := If[LessEqual[y, 2e-137], 1.0, -1.0]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2 \cdot 10^{-137}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.99999999999999996e-137

    1. Initial program 66.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative66.0%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/65.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative65.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative65.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def65.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified65.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around inf 36.7%

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

    if 1.99999999999999996e-137 < y

    1. Initial program 99.9%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. +-commutative99.9%

        \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
      2. +-commutative99.9%

        \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
      3. associate-*l/99.5%

        \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
      4. +-commutative99.5%

        \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
      5. +-commutative99.5%

        \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
      6. fma-def99.5%

        \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
    3. Simplified99.5%

      \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
    4. Taylor expanded in x around 0 85.9%

      \[\leadsto \color{blue}{-1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification43.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 2 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Alternative 10: 66.3% accurate, 15.0× speedup?

\[\begin{array}{l} y = |y|\\ \\ -1 \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y) :precision binary64 -1.0)
y = abs(y);
double code(double x, double y) {
	return -1.0;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = -1.0d0
end function
y = Math.abs(y);
public static double code(double x, double y) {
	return -1.0;
}
y = abs(y)
def code(x, y):
	return -1.0
y = abs(y)
function code(x, y)
	return -1.0
end
y = abs(y)
function tmp = code(x, y)
	tmp = -1.0;
end
NOTE: y should be positive before calling this function
code[x_, y_] := -1.0
\begin{array}{l}
y = |y|\\
\\
-1
\end{array}
Derivation
  1. Initial program 70.9%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Step-by-step derivation
    1. +-commutative70.9%

      \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}}{x \cdot x + y \cdot y} \]
    2. +-commutative70.9%

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{y \cdot y + x \cdot x}} \]
    3. associate-*l/70.4%

      \[\leadsto \color{blue}{\frac{x - y}{y \cdot y + x \cdot x} \cdot \left(y + x\right)} \]
    4. +-commutative70.4%

      \[\leadsto \frac{x - y}{y \cdot y + x \cdot x} \cdot \color{blue}{\left(x + y\right)} \]
    5. +-commutative70.4%

      \[\leadsto \frac{x - y}{\color{blue}{x \cdot x + y \cdot y}} \cdot \left(x + y\right) \]
    6. fma-def70.4%

      \[\leadsto \frac{x - y}{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \left(x + y\right) \]
  3. Simplified70.4%

    \[\leadsto \color{blue}{\frac{x - y}{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \left(x + y\right)} \]
  4. Taylor expanded in x around 0 66.1%

    \[\leadsto \color{blue}{-1} \]
  5. Final simplification66.1%

    \[\leadsto -1 \]

Developer target: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|\frac{x}{y}\right|\\ \mathbf{if}\;0.5 < t_0 \land t_0 < 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;1 - \frac{2}{1 + \frac{x}{y} \cdot \frac{x}{y}}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (fabs (/ x y))))
   (if (and (< 0.5 t_0) (< t_0 2.0))
     (/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))
     (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))))
double code(double x, double y) {
	double t_0 = fabs((x / y));
	double tmp;
	if ((0.5 < t_0) && (t_0 < 2.0)) {
		tmp = ((x - y) * (x + y)) / ((x * x) + (y * y));
	} else {
		tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y))));
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs((x / y))
    if ((0.5d0 < t_0) .and. (t_0 < 2.0d0)) then
        tmp = ((x - y) * (x + y)) / ((x * x) + (y * y))
    else
        tmp = 1.0d0 - (2.0d0 / (1.0d0 + ((x / y) * (x / y))))
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = Math.abs((x / y));
	double tmp;
	if ((0.5 < t_0) && (t_0 < 2.0)) {
		tmp = ((x - y) * (x + y)) / ((x * x) + (y * y));
	} else {
		tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y))));
	}
	return tmp;
}
def code(x, y):
	t_0 = math.fabs((x / y))
	tmp = 0
	if (0.5 < t_0) and (t_0 < 2.0):
		tmp = ((x - y) * (x + y)) / ((x * x) + (y * y))
	else:
		tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y))))
	return tmp
function code(x, y)
	t_0 = abs(Float64(x / y))
	tmp = 0.0
	if ((0.5 < t_0) && (t_0 < 2.0))
		tmp = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)));
	else
		tmp = Float64(1.0 - Float64(2.0 / Float64(1.0 + Float64(Float64(x / y) * Float64(x / y)))));
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = abs((x / y));
	tmp = 0.0;
	if ((0.5 < t_0) && (t_0 < 2.0))
		tmp = ((x - y) * (x + y)) / ((x * x) + (y * y));
	else
		tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y))));
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision]}, If[And[Less[0.5, t$95$0], Less[t$95$0, 2.0]], N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 - N[(2.0 / N[(1.0 + N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|\frac{x}{y}\right|\\
\mathbf{if}\;0.5 < t_0 \land t_0 < 2:\\
\;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\

\mathbf{else}:\\
\;\;\;\;1 - \frac{2}{1 + \frac{x}{y} \cdot \frac{x}{y}}\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2023293 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (and (< 0.0 x) (< x 1.0)) (< y 1.0))

  :herbie-target
  (if (and (< 0.5 (fabs (/ x y))) (< (fabs (/ x y)) 2.0)) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))