Kahan p9 Example

Percentage Accurate: 69.1% → 99.9%
Time: 7.4s
Alternatives: 8
Speedup: 2.1×

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 8 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: 69.1% 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: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} y = |y|\\ \\ \frac{x + y}{\mathsf{hypot}\left(x, y\right) \cdot \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(x + y) / Float64(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[(x + y), $MachinePrecision] / N[(N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision] * N[(N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y = |y|\\
\\
\frac{x + y}{\mathsf{hypot}\left(x, y\right) \cdot \frac{\mathsf{hypot}\left(x, y\right)}{x - y}}
\end{array}
Derivation
  1. Initial program 67.2%

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}} \]
    2. times-frac67.3%

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}} \]
    3. hypot-def67.3%

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\frac{x + y}{1}}}{\mathsf{hypot}\left(x, y\right) \cdot \frac{\mathsf{hypot}\left(x, y\right)}{x - y}} \]
    6. /-rgt-identity99.9%

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

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

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

Alternative 2: 99.9% accurate, 0.1× speedup?

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

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}} \]
    2. times-frac67.3%

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}} \]
    3. hypot-def67.3%

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

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

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

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

Alternative 3: 93.0% 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}\\ t_1 := \frac{x}{\frac{y}{\frac{x}{y}}}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1 + \left(t_1 + -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))))
        (t_1 (/ x (/ y (/ x y)))))
   (if (<= t_0 2.0) t_0 (+ t_1 (+ t_1 -1.0)))))
y = abs(y);
double code(double x, double y) {
	double t_0 = ((x + y) * (x - y)) / ((x * x) + (y * y));
	double t_1 = x / (y / (x / y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = t_1 + (t_1 + -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) :: t_1
    real(8) :: tmp
    t_0 = ((x + y) * (x - y)) / ((x * x) + (y * y))
    t_1 = x / (y / (x / y))
    if (t_0 <= 2.0d0) then
        tmp = t_0
    else
        tmp = t_1 + (t_1 + (-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 t_1 = x / (y / (x / y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = t_1 + (t_1 + -1.0);
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	t_0 = ((x + y) * (x - y)) / ((x * x) + (y * y))
	t_1 = x / (y / (x / y))
	tmp = 0
	if t_0 <= 2.0:
		tmp = t_0
	else:
		tmp = t_1 + (t_1 + -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)))
	t_1 = Float64(x / Float64(y / Float64(x / y)))
	tmp = 0.0
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = Float64(t_1 + Float64(t_1 + -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));
	t_1 = x / (y / (x / y));
	tmp = 0.0;
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = t_1 + (t_1 + -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]}, Block[{t$95$1 = N[(x / N[(y / N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(t$95$1 + N[(t$95$1 + -1.0), $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}\\
t_1 := \frac{x}{\frac{y}{\frac{x}{y}}}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1 + \left(t_1 + -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 100.0%

      \[\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. add-sqr-sqrt0.0%

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

        \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}} \]
      3. hypot-def3.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(0 + \frac{x}{y \cdot \frac{y}{x}}\right) + -1\right) + \left(-\color{blue}{\left(-\frac{{x}^{2}}{{y}^{2}}\right)}\right) \]
      14. remove-double-neg46.4%

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

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

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

      \[\leadsto \color{blue}{\left(\left(0 + \frac{x}{y \cdot \frac{y}{x}}\right) + -1\right) + \frac{x}{y \cdot \frac{y}{x}}} \]
    7. Step-by-step derivation
      1. expm1-log1p-u73.3%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(\left(0 + \frac{x}{y \cdot \frac{y}{x}}\right) + -1\right) + \frac{x}{y \cdot \frac{y}{x}}\right)\right)} \]
      2. expm1-udef73.3%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\left(\left(0 + \frac{x}{y \cdot \frac{y}{x}}\right) + -1\right) + \frac{x}{y \cdot \frac{y}{x}}\right)} - 1} \]
      3. +-commutative73.3%

        \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\frac{x}{y \cdot \frac{y}{x}} + \left(\left(0 + \frac{x}{y \cdot \frac{y}{x}}\right) + -1\right)}\right)} - 1 \]
      4. +-lft-identity73.3%

        \[\leadsto e^{\mathsf{log1p}\left(\frac{x}{y \cdot \frac{y}{x}} + \left(\color{blue}{\frac{x}{y \cdot \frac{y}{x}}} + -1\right)\right)} - 1 \]
    8. Applied egg-rr73.3%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{x}{y \cdot \frac{y}{x}} + \left(\frac{x}{y \cdot \frac{y}{x}} + -1\right)\right)} - 1} \]
    9. Step-by-step derivation
      1. expm1-def73.3%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x}{y \cdot \frac{y}{x}} + \left(\frac{x}{y \cdot \frac{y}{x}} + -1\right)\right)\right)} \]
      2. expm1-log1p73.3%

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

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

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

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

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

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

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

    \[\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}{\frac{y}{\frac{x}{y}}} + \left(\frac{x}{\frac{y}{\frac{x}{y}}} + -1\right)\\ \end{array} \]

Alternative 4: 92.7% 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}\\ \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))))
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;
	}
	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
    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;
	}
	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
	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) / y);
	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;
	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] / y), $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}\\


\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 100.0%

      \[\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 71.2%

      \[\leadsto \frac{x - y}{\color{blue}{y}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.5%

    \[\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}\\ \end{array} \]

Alternative 5: 82.7% accurate, 0.9× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 6 \cdot 10^{-189} \lor \neg \left(y \leq 1.2 \cdot 10^{-135}\right) \land y \leq 3.5 \cdot 10^{-122}:\\ \;\;\;\;1 + \frac{y}{\frac{x}{y}} \cdot \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y}\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (or (<= y 6e-189) (and (not (<= y 1.2e-135)) (<= y 3.5e-122)))
   (+ 1.0 (* (/ y (/ x y)) (/ -2.0 x)))
   (/ (- x y) y)))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if ((y <= 6e-189) || (!(y <= 1.2e-135) && (y <= 3.5e-122))) {
		tmp = 1.0 + ((y / (x / y)) * (-2.0 / x));
	} else {
		tmp = (x - y) / 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 <= 6d-189) .or. (.not. (y <= 1.2d-135)) .and. (y <= 3.5d-122)) then
        tmp = 1.0d0 + ((y / (x / y)) * ((-2.0d0) / x))
    else
        tmp = (x - y) / y
    end if
    code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
	double tmp;
	if ((y <= 6e-189) || (!(y <= 1.2e-135) && (y <= 3.5e-122))) {
		tmp = 1.0 + ((y / (x / y)) * (-2.0 / x));
	} else {
		tmp = (x - y) / y;
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if (y <= 6e-189) or (not (y <= 1.2e-135) and (y <= 3.5e-122)):
		tmp = 1.0 + ((y / (x / y)) * (-2.0 / x))
	else:
		tmp = (x - y) / y
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if ((y <= 6e-189) || (!(y <= 1.2e-135) && (y <= 3.5e-122)))
		tmp = Float64(1.0 + Float64(Float64(y / Float64(x / y)) * Float64(-2.0 / x)));
	else
		tmp = Float64(Float64(x - y) / y);
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= 6e-189) || (~((y <= 1.2e-135)) && (y <= 3.5e-122)))
		tmp = 1.0 + ((y / (x / y)) * (-2.0 / x));
	else
		tmp = (x - y) / y;
	end
	tmp_2 = tmp;
end
NOTE: y should be positive before calling this function
code[x_, y_] := If[Or[LessEqual[y, 6e-189], And[N[Not[LessEqual[y, 1.2e-135]], $MachinePrecision], LessEqual[y, 3.5e-122]]], N[(1.0 + N[(N[(y / N[(x / y), $MachinePrecision]), $MachinePrecision] * N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6 \cdot 10^{-189} \lor \neg \left(y \leq 1.2 \cdot 10^{-135}\right) \land y \leq 3.5 \cdot 10^{-122}:\\
\;\;\;\;1 + \frac{y}{\frac{x}{y}} \cdot \frac{-2}{x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 6e-189 or 1.1999999999999999e-135 < y < 3.5000000000000001e-122

    1. Initial program 62.5%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Step-by-step derivation
      1. add-sqr-sqrt62.5%

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

        \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}} \]
      3. hypot-def62.9%

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

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

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

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

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

        \[\leadsto \color{blue}{\frac{\left(x + y\right) \cdot 1}{\mathsf{hypot}\left(x, y\right) \cdot \frac{\mathsf{hypot}\left(x, y\right)}{x - y}}} \]
      4. metadata-eval100.0%

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

        \[\leadsto \frac{\color{blue}{\frac{x + y}{1}}}{\mathsf{hypot}\left(x, y\right) \cdot \frac{\mathsf{hypot}\left(x, y\right)}{x - y}} \]
      6. /-rgt-identity100.0%

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

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

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

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

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

        \[\leadsto 1 + \frac{{y}^{2} \cdot -2}{\color{blue}{x \cdot x}} \]
      4. times-frac39.7%

        \[\leadsto 1 + \color{blue}{\frac{{y}^{2}}{x} \cdot \frac{-2}{x}} \]
      5. unpow239.7%

        \[\leadsto 1 + \frac{\color{blue}{y \cdot y}}{x} \cdot \frac{-2}{x} \]
      6. associate-/l*40.3%

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

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

    if 6e-189 < y < 1.1999999999999999e-135 or 3.5000000000000001e-122 < y

    1. Initial program 87.5%

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

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

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

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

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

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

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

        \[\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-neg86.5%

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

      \[\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 68.8%

      \[\leadsto \frac{x - y}{\color{blue}{y}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 6 \cdot 10^{-189} \lor \neg \left(y \leq 1.2 \cdot 10^{-135}\right) \land y \leq 3.5 \cdot 10^{-122}:\\ \;\;\;\;1 + \frac{y}{\frac{x}{y}} \cdot \frac{-2}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y}\\ \end{array} \]

Alternative 6: 82.1% accurate, 1.3× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 1.5 \cdot 10^{-190}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.66 \cdot 10^{-134} \lor \neg \left(y \leq 1.75 \cdot 10^{-122}\right):\\ \;\;\;\;\frac{x - y}{y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= y 1.5e-190)
   1.0
   (if (or (<= y 1.66e-134) (not (<= y 1.75e-122))) (/ (- x y) y) 1.0)))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 1.5e-190) {
		tmp = 1.0;
	} else if ((y <= 1.66e-134) || !(y <= 1.75e-122)) {
		tmp = (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 <= 1.5d-190) then
        tmp = 1.0d0
    else if ((y <= 1.66d-134) .or. (.not. (y <= 1.75d-122))) then
        tmp = (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 <= 1.5e-190) {
		tmp = 1.0;
	} else if ((y <= 1.66e-134) || !(y <= 1.75e-122)) {
		tmp = (x - y) / y;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 1.5e-190:
		tmp = 1.0
	elif (y <= 1.66e-134) or not (y <= 1.75e-122):
		tmp = (x - y) / y
	else:
		tmp = 1.0
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 1.5e-190)
		tmp = 1.0;
	elseif ((y <= 1.66e-134) || !(y <= 1.75e-122))
		tmp = Float64(Float64(x - y) / y);
	else
		tmp = 1.0;
	end
	return tmp
end
y = abs(y)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (y <= 1.5e-190)
		tmp = 1.0;
	elseif ((y <= 1.66e-134) || ~((y <= 1.75e-122)))
		tmp = (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, 1.5e-190], 1.0, If[Or[LessEqual[y, 1.66e-134], N[Not[LessEqual[y, 1.75e-122]], $MachinePrecision]], N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision], 1.0]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.5 \cdot 10^{-190}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 1.66 \cdot 10^{-134} \lor \neg \left(y \leq 1.75 \cdot 10^{-122}\right):\\
\;\;\;\;\frac{x - y}{y}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.4999999999999999e-190 or 1.6599999999999999e-134 < y < 1.7500000000000001e-122

    1. Initial program 62.3%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Taylor expanded in x around inf 38.1%

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

    if 1.4999999999999999e-190 < y < 1.6599999999999999e-134 or 1.7500000000000001e-122 < y

    1. Initial program 87.8%

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

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

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

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

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

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

        \[\leadsto \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x - \left(-y\right)}} \]
      7. sub-neg86.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-neg86.8%

        \[\leadsto \frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + \color{blue}{y}}} \]
    3. Simplified86.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 67.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 1.5 \cdot 10^{-190}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.66 \cdot 10^{-134} \lor \neg \left(y \leq 1.75 \cdot 10^{-122}\right):\\ \;\;\;\;\frac{x - y}{y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 7: 81.5% accurate, 2.1× speedup?

\[\begin{array}{l} y = |y|\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 2.2 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 10^{-156}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 8 \cdot 10^{-121}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \end{array} \]
NOTE: y should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= y 2.2e-189) 1.0 (if (<= y 1e-156) -1.0 (if (<= y 8e-121) 1.0 -1.0))))
y = abs(y);
double code(double x, double y) {
	double tmp;
	if (y <= 2.2e-189) {
		tmp = 1.0;
	} else if (y <= 1e-156) {
		tmp = -1.0;
	} else if (y <= 8e-121) {
		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 <= 2.2d-189) then
        tmp = 1.0d0
    else if (y <= 1d-156) then
        tmp = -1.0d0
    else if (y <= 8d-121) 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 <= 2.2e-189) {
		tmp = 1.0;
	} else if (y <= 1e-156) {
		tmp = -1.0;
	} else if (y <= 8e-121) {
		tmp = 1.0;
	} else {
		tmp = -1.0;
	}
	return tmp;
}
y = abs(y)
def code(x, y):
	tmp = 0
	if y <= 2.2e-189:
		tmp = 1.0
	elif y <= 1e-156:
		tmp = -1.0
	elif y <= 8e-121:
		tmp = 1.0
	else:
		tmp = -1.0
	return tmp
y = abs(y)
function code(x, y)
	tmp = 0.0
	if (y <= 2.2e-189)
		tmp = 1.0;
	elseif (y <= 1e-156)
		tmp = -1.0;
	elseif (y <= 8e-121)
		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 <= 2.2e-189)
		tmp = 1.0;
	elseif (y <= 1e-156)
		tmp = -1.0;
	elseif (y <= 8e-121)
		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, 2.2e-189], 1.0, If[LessEqual[y, 1e-156], -1.0, If[LessEqual[y, 8e-121], 1.0, -1.0]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.2 \cdot 10^{-189}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 10^{-156}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq 8 \cdot 10^{-121}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 2.20000000000000019e-189 or 1.00000000000000004e-156 < y < 7.9999999999999998e-121

    1. Initial program 63.4%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Taylor expanded in x around inf 39.4%

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

    if 2.20000000000000019e-189 < y < 1.00000000000000004e-156 or 7.9999999999999998e-121 < y

    1. Initial program 86.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Taylor expanded in x around 0 72.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 2.2 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 10^{-156}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 8 \cdot 10^{-121}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Alternative 8: 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 67.2%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Taylor expanded in x around 0 62.7%

    \[\leadsto \color{blue}{-1} \]
  3. Final simplification62.7%

    \[\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 2023297 
(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))))