Kahan p9 Example

Percentage Accurate: 68.5% → 100.0%
Time: 9.5s
Alternatives: 9
Speedup: 0.6×

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 9 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.5% 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_m = \left|y\right| \\ \frac{\left(x + y_m\right) \cdot \frac{y_m - x}{\mathsf{hypot}\left(x, y_m\right)}}{-\mathsf{hypot}\left(x, y_m\right)} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (/ (* (+ x y_m) (/ (- y_m x) (hypot x y_m))) (- (hypot x y_m))))
y_m = fabs(y);
double code(double x, double y_m) {
	return ((x + y_m) * ((y_m - x) / hypot(x, y_m))) / -hypot(x, y_m);
}
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	return ((x + y_m) * ((y_m - x) / Math.hypot(x, y_m))) / -Math.hypot(x, y_m);
}
y_m = math.fabs(y)
def code(x, y_m):
	return ((x + y_m) * ((y_m - x) / math.hypot(x, y_m))) / -math.hypot(x, y_m)
y_m = abs(y)
function code(x, y_m)
	return Float64(Float64(Float64(x + y_m) * Float64(Float64(y_m - x) / hypot(x, y_m))) / Float64(-hypot(x, y_m)))
end
y_m = abs(y);
function tmp = code(x, y_m)
	tmp = ((x + y_m) * ((y_m - x) / hypot(x, y_m))) / -hypot(x, y_m);
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := N[(N[(N[(x + y$95$m), $MachinePrecision] * N[(N[(y$95$m - x), $MachinePrecision] / N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / (-N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision])), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|

\\
\frac{\left(x + y_m\right) \cdot \frac{y_m - x}{\mathsf{hypot}\left(x, y_m\right)}}{-\mathsf{hypot}\left(x, y_m\right)}
\end{array}
Derivation
  1. Initial program 61.5%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt61.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.2%

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

      \[\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-def100.0%

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

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

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

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

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

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

Alternative 2: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \frac{x - y_m}{\mathsf{hypot}\left(x, y_m\right)} \cdot \frac{x + y_m}{\mathsf{hypot}\left(x, y_m\right)} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (* (/ (- x y_m) (hypot x y_m)) (/ (+ x y_m) (hypot x y_m))))
y_m = fabs(y);
double code(double x, double y_m) {
	return ((x - y_m) / hypot(x, y_m)) * ((x + y_m) / hypot(x, y_m));
}
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	return ((x - y_m) / Math.hypot(x, y_m)) * ((x + y_m) / Math.hypot(x, y_m));
}
y_m = math.fabs(y)
def code(x, y_m):
	return ((x - y_m) / math.hypot(x, y_m)) * ((x + y_m) / math.hypot(x, y_m))
y_m = abs(y)
function code(x, y_m)
	return Float64(Float64(Float64(x - y_m) / hypot(x, y_m)) * Float64(Float64(x + y_m) / hypot(x, y_m)))
end
y_m = abs(y);
function tmp = code(x, y_m)
	tmp = ((x - y_m) / hypot(x, y_m)) * ((x + y_m) / hypot(x, y_m));
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := N[(N[(N[(x - y$95$m), $MachinePrecision] / N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(x + y$95$m), $MachinePrecision] / N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|

\\
\frac{x - y_m}{\mathsf{hypot}\left(x, y_m\right)} \cdot \frac{x + y_m}{\mathsf{hypot}\left(x, y_m\right)}
\end{array}
Derivation
  1. Initial program 61.5%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt61.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.2%

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

      \[\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-def100.0%

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

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

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

Alternative 3: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \frac{x + y_m}{\mathsf{hypot}\left(x, y_m\right) \cdot \frac{\mathsf{hypot}\left(x, y_m\right)}{x - y_m}} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (/ (+ x y_m) (* (hypot x y_m) (/ (hypot x y_m) (- x y_m)))))
y_m = fabs(y);
double code(double x, double y_m) {
	return (x + y_m) / (hypot(x, y_m) * (hypot(x, y_m) / (x - y_m)));
}
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	return (x + y_m) / (Math.hypot(x, y_m) * (Math.hypot(x, y_m) / (x - y_m)));
}
y_m = math.fabs(y)
def code(x, y_m):
	return (x + y_m) / (math.hypot(x, y_m) * (math.hypot(x, y_m) / (x - y_m)))
y_m = abs(y)
function code(x, y_m)
	return Float64(Float64(x + y_m) / Float64(hypot(x, y_m) * Float64(hypot(x, y_m) / Float64(x - y_m))))
end
y_m = abs(y);
function tmp = code(x, y_m)
	tmp = (x + y_m) / (hypot(x, y_m) * (hypot(x, y_m) / (x - y_m)));
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := N[(N[(x + y$95$m), $MachinePrecision] / N[(N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision] * N[(N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision] / N[(x - y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|

\\
\frac{x + y_m}{\mathsf{hypot}\left(x, y_m\right) \cdot \frac{\mathsf{hypot}\left(x, y_m\right)}{x - y_m}}
\end{array}
Derivation
  1. Initial program 61.5%

    \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt61.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.2%

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

      \[\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-def100.0%

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

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

      \[\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. *-commutative100.0%

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

      \[\leadsto \frac{\color{blue}{x + y}}{\mathsf{hypot}\left(x, y\right) \cdot \frac{\mathsf{hypot}\left(x, y\right)}{x - y}} \]
  6. 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}}} \]
  7. Final simplification100.0%

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

Alternative 4: 93.1% accurate, 0.1× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \begin{array}{l} t_0 := \frac{\left(x - y_m\right) \cdot \left(x + y_m\right)}{x \cdot x + y_m \cdot y_m}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{x + y_m}{\mathsf{hypot}\left(x, y_m\right)} \cdot \left(\frac{x}{y_m} + -1\right)\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (let* ((t_0 (/ (* (- x y_m) (+ x y_m)) (+ (* x x) (* y_m y_m)))))
   (if (<= t_0 2.0) t_0 (* (/ (+ x y_m) (hypot x y_m)) (+ (/ x y_m) -1.0)))))
y_m = fabs(y);
double code(double x, double y_m) {
	double t_0 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = ((x + y_m) / hypot(x, y_m)) * ((x / y_m) + -1.0);
	}
	return tmp;
}
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	double t_0 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} else {
		tmp = ((x + y_m) / Math.hypot(x, y_m)) * ((x / y_m) + -1.0);
	}
	return tmp;
}
y_m = math.fabs(y)
def code(x, y_m):
	t_0 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m))
	tmp = 0
	if t_0 <= 2.0:
		tmp = t_0
	else:
		tmp = ((x + y_m) / math.hypot(x, y_m)) * ((x / y_m) + -1.0)
	return tmp
y_m = abs(y)
function code(x, y_m)
	t_0 = Float64(Float64(Float64(x - y_m) * Float64(x + y_m)) / Float64(Float64(x * x) + Float64(y_m * y_m)))
	tmp = 0.0
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = Float64(Float64(Float64(x + y_m) / hypot(x, y_m)) * Float64(Float64(x / y_m) + -1.0));
	end
	return tmp
end
y_m = abs(y);
function tmp_2 = code(x, y_m)
	t_0 = ((x - y_m) * (x + y_m)) / ((x * x) + (y_m * y_m));
	tmp = 0.0;
	if (t_0 <= 2.0)
		tmp = t_0;
	else
		tmp = ((x + y_m) / hypot(x, y_m)) * ((x / y_m) + -1.0);
	end
	tmp_2 = tmp;
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := Block[{t$95$0 = N[(N[(N[(x - y$95$m), $MachinePrecision] * N[(x + y$95$m), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(N[(N[(x + y$95$m), $MachinePrecision] / N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(x / y$95$m), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
y_m = \left|y\right|

\\
\begin{array}{l}
t_0 := \frac{\left(x - y_m\right) \cdot \left(x + y_m\right)}{x \cdot x + y_m \cdot y_m}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{x + y_m}{\mathsf{hypot}\left(x, y_m\right)} \cdot \left(\frac{x}{y_m} + -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.6%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Add Preprocessing

    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. Add Preprocessing
    3. 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-def100.0%

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

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

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

    \[\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}{\mathsf{hypot}\left(x, y\right)} \cdot \left(\frac{x}{y} + -1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 80.7% accurate, 0.4× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \begin{array}{l} \mathbf{if}\;y_m \leq 5.4 \cdot 10^{-197} \lor \neg \left(y_m \leq 2.65 \cdot 10^{-171}\right) \land \left(y_m \leq 3.6 \cdot 10^{-140} \lor \neg \left(y_m \leq 1.5 \cdot 10^{-130}\right) \land y_m \leq 2.95 \cdot 10^{-111}\right):\\ \;\;\;\;\left(1 - \frac{y_m}{x}\right) \cdot \left(1 + \frac{y_m}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y_m}{y_m}\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (if (or (<= y_m 5.4e-197)
         (and (not (<= y_m 2.65e-171))
              (or (<= y_m 3.6e-140)
                  (and (not (<= y_m 1.5e-130)) (<= y_m 2.95e-111)))))
   (* (- 1.0 (/ y_m x)) (+ 1.0 (/ y_m x)))
   (/ (- x y_m) y_m)))
y_m = fabs(y);
double code(double x, double y_m) {
	double tmp;
	if ((y_m <= 5.4e-197) || (!(y_m <= 2.65e-171) && ((y_m <= 3.6e-140) || (!(y_m <= 1.5e-130) && (y_m <= 2.95e-111))))) {
		tmp = (1.0 - (y_m / x)) * (1.0 + (y_m / x));
	} else {
		tmp = (x - y_m) / y_m;
	}
	return tmp;
}
y_m = abs(y)
real(8) function code(x, y_m)
    real(8), intent (in) :: x
    real(8), intent (in) :: y_m
    real(8) :: tmp
    if ((y_m <= 5.4d-197) .or. (.not. (y_m <= 2.65d-171)) .and. (y_m <= 3.6d-140) .or. (.not. (y_m <= 1.5d-130)) .and. (y_m <= 2.95d-111)) then
        tmp = (1.0d0 - (y_m / x)) * (1.0d0 + (y_m / x))
    else
        tmp = (x - y_m) / y_m
    end if
    code = tmp
end function
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	double tmp;
	if ((y_m <= 5.4e-197) || (!(y_m <= 2.65e-171) && ((y_m <= 3.6e-140) || (!(y_m <= 1.5e-130) && (y_m <= 2.95e-111))))) {
		tmp = (1.0 - (y_m / x)) * (1.0 + (y_m / x));
	} else {
		tmp = (x - y_m) / y_m;
	}
	return tmp;
}
y_m = math.fabs(y)
def code(x, y_m):
	tmp = 0
	if (y_m <= 5.4e-197) or (not (y_m <= 2.65e-171) and ((y_m <= 3.6e-140) or (not (y_m <= 1.5e-130) and (y_m <= 2.95e-111)))):
		tmp = (1.0 - (y_m / x)) * (1.0 + (y_m / x))
	else:
		tmp = (x - y_m) / y_m
	return tmp
y_m = abs(y)
function code(x, y_m)
	tmp = 0.0
	if ((y_m <= 5.4e-197) || (!(y_m <= 2.65e-171) && ((y_m <= 3.6e-140) || (!(y_m <= 1.5e-130) && (y_m <= 2.95e-111)))))
		tmp = Float64(Float64(1.0 - Float64(y_m / x)) * Float64(1.0 + Float64(y_m / x)));
	else
		tmp = Float64(Float64(x - y_m) / y_m);
	end
	return tmp
end
y_m = abs(y);
function tmp_2 = code(x, y_m)
	tmp = 0.0;
	if ((y_m <= 5.4e-197) || (~((y_m <= 2.65e-171)) && ((y_m <= 3.6e-140) || (~((y_m <= 1.5e-130)) && (y_m <= 2.95e-111)))))
		tmp = (1.0 - (y_m / x)) * (1.0 + (y_m / x));
	else
		tmp = (x - y_m) / y_m;
	end
	tmp_2 = tmp;
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := If[Or[LessEqual[y$95$m, 5.4e-197], And[N[Not[LessEqual[y$95$m, 2.65e-171]], $MachinePrecision], Or[LessEqual[y$95$m, 3.6e-140], And[N[Not[LessEqual[y$95$m, 1.5e-130]], $MachinePrecision], LessEqual[y$95$m, 2.95e-111]]]]], N[(N[(1.0 - N[(y$95$m / x), $MachinePrecision]), $MachinePrecision] * N[(1.0 + N[(y$95$m / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x - y$95$m), $MachinePrecision] / y$95$m), $MachinePrecision]]
\begin{array}{l}
y_m = \left|y\right|

\\
\begin{array}{l}
\mathbf{if}\;y_m \leq 5.4 \cdot 10^{-197} \lor \neg \left(y_m \leq 2.65 \cdot 10^{-171}\right) \land \left(y_m \leq 3.6 \cdot 10^{-140} \lor \neg \left(y_m \leq 1.5 \cdot 10^{-130}\right) \land y_m \leq 2.95 \cdot 10^{-111}\right):\\
\;\;\;\;\left(1 - \frac{y_m}{x}\right) \cdot \left(1 + \frac{y_m}{x}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 5.40000000000000034e-197 or 2.65000000000000009e-171 < y < 3.6e-140 or 1.49999999999999993e-130 < y < 2.95e-111

    1. Initial program 58.3%

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

        \[\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-frac59.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-def59.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-def100.0%

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

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

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

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

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

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

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

    if 5.40000000000000034e-197 < y < 2.65000000000000009e-171 or 3.6e-140 < y < 1.49999999999999993e-130 or 2.95e-111 < y

    1. Initial program 82.4%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 5.4 \cdot 10^{-197} \lor \neg \left(y \leq 2.65 \cdot 10^{-171}\right) \land \left(y \leq 3.6 \cdot 10^{-140} \lor \neg \left(y \leq 1.5 \cdot 10^{-130}\right) \land y \leq 2.95 \cdot 10^{-111}\right):\\ \;\;\;\;\left(1 - \frac{y}{x}\right) \cdot \left(1 + \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{y}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 93.1% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
t_0 := \frac{\left(x - y_m\right) \cdot \left(x + y_m\right)}{x \cdot x + y_m \cdot y_m}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\

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


\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.6%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Add Preprocessing

    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. Add Preprocessing
    3. 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-def100.0%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(0 + 2 \cdot \color{blue}{\frac{\frac{x}{y}}{\frac{y}{x}}}\right) + -1 \]
    11. Applied egg-rr79.6%

      \[\leadsto \left(0 + 2 \cdot \color{blue}{\frac{\frac{x}{y}}{\frac{y}{x}}}\right) + -1 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.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}:\\ \;\;\;\;2 \cdot \frac{\frac{x}{y}}{\frac{y}{x}} + -1\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 80.2% accurate, 0.5× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \begin{array}{l} \mathbf{if}\;y_m \leq 3.5 \cdot 10^{-197}:\\ \;\;\;\;1\\ \mathbf{elif}\;y_m \leq 4 \cdot 10^{-171} \lor \neg \left(y_m \leq 5 \cdot 10^{-140} \lor \neg \left(y_m \leq 1.45 \cdot 10^{-130}\right) \land y_m \leq 1.32 \cdot 10^{-111}\right):\\ \;\;\;\;\frac{x - y_m}{y_m}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (if (<= y_m 3.5e-197)
   1.0
   (if (or (<= y_m 4e-171)
           (not
            (or (<= y_m 5e-140)
                (and (not (<= y_m 1.45e-130)) (<= y_m 1.32e-111)))))
     (/ (- x y_m) y_m)
     1.0)))
y_m = fabs(y);
double code(double x, double y_m) {
	double tmp;
	if (y_m <= 3.5e-197) {
		tmp = 1.0;
	} else if ((y_m <= 4e-171) || !((y_m <= 5e-140) || (!(y_m <= 1.45e-130) && (y_m <= 1.32e-111)))) {
		tmp = (x - y_m) / y_m;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
y_m = abs(y)
real(8) function code(x, y_m)
    real(8), intent (in) :: x
    real(8), intent (in) :: y_m
    real(8) :: tmp
    if (y_m <= 3.5d-197) then
        tmp = 1.0d0
    else if ((y_m <= 4d-171) .or. (.not. (y_m <= 5d-140) .or. (.not. (y_m <= 1.45d-130)) .and. (y_m <= 1.32d-111))) then
        tmp = (x - y_m) / y_m
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	double tmp;
	if (y_m <= 3.5e-197) {
		tmp = 1.0;
	} else if ((y_m <= 4e-171) || !((y_m <= 5e-140) || (!(y_m <= 1.45e-130) && (y_m <= 1.32e-111)))) {
		tmp = (x - y_m) / y_m;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
y_m = math.fabs(y)
def code(x, y_m):
	tmp = 0
	if y_m <= 3.5e-197:
		tmp = 1.0
	elif (y_m <= 4e-171) or not ((y_m <= 5e-140) or (not (y_m <= 1.45e-130) and (y_m <= 1.32e-111))):
		tmp = (x - y_m) / y_m
	else:
		tmp = 1.0
	return tmp
y_m = abs(y)
function code(x, y_m)
	tmp = 0.0
	if (y_m <= 3.5e-197)
		tmp = 1.0;
	elseif ((y_m <= 4e-171) || !((y_m <= 5e-140) || (!(y_m <= 1.45e-130) && (y_m <= 1.32e-111))))
		tmp = Float64(Float64(x - y_m) / y_m);
	else
		tmp = 1.0;
	end
	return tmp
end
y_m = abs(y);
function tmp_2 = code(x, y_m)
	tmp = 0.0;
	if (y_m <= 3.5e-197)
		tmp = 1.0;
	elseif ((y_m <= 4e-171) || ~(((y_m <= 5e-140) || (~((y_m <= 1.45e-130)) && (y_m <= 1.32e-111)))))
		tmp = (x - y_m) / y_m;
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := If[LessEqual[y$95$m, 3.5e-197], 1.0, If[Or[LessEqual[y$95$m, 4e-171], N[Not[Or[LessEqual[y$95$m, 5e-140], And[N[Not[LessEqual[y$95$m, 1.45e-130]], $MachinePrecision], LessEqual[y$95$m, 1.32e-111]]]], $MachinePrecision]], N[(N[(x - y$95$m), $MachinePrecision] / y$95$m), $MachinePrecision], 1.0]]
\begin{array}{l}
y_m = \left|y\right|

\\
\begin{array}{l}
\mathbf{if}\;y_m \leq 3.5 \cdot 10^{-197}:\\
\;\;\;\;1\\

\mathbf{elif}\;y_m \leq 4 \cdot 10^{-171} \lor \neg \left(y_m \leq 5 \cdot 10^{-140} \lor \neg \left(y_m \leq 1.45 \cdot 10^{-130}\right) \land y_m \leq 1.32 \cdot 10^{-111}\right):\\
\;\;\;\;\frac{x - y_m}{y_m}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 3.4999999999999998e-197 or 3.9999999999999999e-171 < y < 5.00000000000000015e-140 or 1.45e-130 < y < 1.32e-111

    1. Initial program 58.3%

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

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

    if 3.4999999999999998e-197 < y < 3.9999999999999999e-171 or 5.00000000000000015e-140 < y < 1.45e-130 or 1.32e-111 < y

    1. Initial program 82.4%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 3.5 \cdot 10^{-197}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 4 \cdot 10^{-171} \lor \neg \left(y \leq 5 \cdot 10^{-140} \lor \neg \left(y \leq 1.45 \cdot 10^{-130}\right) \land y \leq 1.32 \cdot 10^{-111}\right):\\ \;\;\;\;\frac{x - y}{y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 80.4% accurate, 0.6× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \begin{array}{l} \mathbf{if}\;y_m \leq 1.5 \cdot 10^{-187}:\\ \;\;\;\;1\\ \mathbf{elif}\;y_m \leq 3.5 \cdot 10^{-171}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y_m \leq 6.8 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{elif}\;y_m \leq 1.5 \cdot 10^{-130}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y_m \leq 1.55 \cdot 10^{-111}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m)
 :precision binary64
 (if (<= y_m 1.5e-187)
   1.0
   (if (<= y_m 3.5e-171)
     -1.0
     (if (<= y_m 6.8e-137)
       1.0
       (if (<= y_m 1.5e-130) -1.0 (if (<= y_m 1.55e-111) 1.0 -1.0))))))
y_m = fabs(y);
double code(double x, double y_m) {
	double tmp;
	if (y_m <= 1.5e-187) {
		tmp = 1.0;
	} else if (y_m <= 3.5e-171) {
		tmp = -1.0;
	} else if (y_m <= 6.8e-137) {
		tmp = 1.0;
	} else if (y_m <= 1.5e-130) {
		tmp = -1.0;
	} else if (y_m <= 1.55e-111) {
		tmp = 1.0;
	} else {
		tmp = -1.0;
	}
	return tmp;
}
y_m = abs(y)
real(8) function code(x, y_m)
    real(8), intent (in) :: x
    real(8), intent (in) :: y_m
    real(8) :: tmp
    if (y_m <= 1.5d-187) then
        tmp = 1.0d0
    else if (y_m <= 3.5d-171) then
        tmp = -1.0d0
    else if (y_m <= 6.8d-137) then
        tmp = 1.0d0
    else if (y_m <= 1.5d-130) then
        tmp = -1.0d0
    else if (y_m <= 1.55d-111) then
        tmp = 1.0d0
    else
        tmp = -1.0d0
    end if
    code = tmp
end function
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	double tmp;
	if (y_m <= 1.5e-187) {
		tmp = 1.0;
	} else if (y_m <= 3.5e-171) {
		tmp = -1.0;
	} else if (y_m <= 6.8e-137) {
		tmp = 1.0;
	} else if (y_m <= 1.5e-130) {
		tmp = -1.0;
	} else if (y_m <= 1.55e-111) {
		tmp = 1.0;
	} else {
		tmp = -1.0;
	}
	return tmp;
}
y_m = math.fabs(y)
def code(x, y_m):
	tmp = 0
	if y_m <= 1.5e-187:
		tmp = 1.0
	elif y_m <= 3.5e-171:
		tmp = -1.0
	elif y_m <= 6.8e-137:
		tmp = 1.0
	elif y_m <= 1.5e-130:
		tmp = -1.0
	elif y_m <= 1.55e-111:
		tmp = 1.0
	else:
		tmp = -1.0
	return tmp
y_m = abs(y)
function code(x, y_m)
	tmp = 0.0
	if (y_m <= 1.5e-187)
		tmp = 1.0;
	elseif (y_m <= 3.5e-171)
		tmp = -1.0;
	elseif (y_m <= 6.8e-137)
		tmp = 1.0;
	elseif (y_m <= 1.5e-130)
		tmp = -1.0;
	elseif (y_m <= 1.55e-111)
		tmp = 1.0;
	else
		tmp = -1.0;
	end
	return tmp
end
y_m = abs(y);
function tmp_2 = code(x, y_m)
	tmp = 0.0;
	if (y_m <= 1.5e-187)
		tmp = 1.0;
	elseif (y_m <= 3.5e-171)
		tmp = -1.0;
	elseif (y_m <= 6.8e-137)
		tmp = 1.0;
	elseif (y_m <= 1.5e-130)
		tmp = -1.0;
	elseif (y_m <= 1.55e-111)
		tmp = 1.0;
	else
		tmp = -1.0;
	end
	tmp_2 = tmp;
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := If[LessEqual[y$95$m, 1.5e-187], 1.0, If[LessEqual[y$95$m, 3.5e-171], -1.0, If[LessEqual[y$95$m, 6.8e-137], 1.0, If[LessEqual[y$95$m, 1.5e-130], -1.0, If[LessEqual[y$95$m, 1.55e-111], 1.0, -1.0]]]]]
\begin{array}{l}
y_m = \left|y\right|

\\
\begin{array}{l}
\mathbf{if}\;y_m \leq 1.5 \cdot 10^{-187}:\\
\;\;\;\;1\\

\mathbf{elif}\;y_m \leq 3.5 \cdot 10^{-171}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y_m \leq 6.8 \cdot 10^{-137}:\\
\;\;\;\;1\\

\mathbf{elif}\;y_m \leq 1.5 \cdot 10^{-130}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y_m \leq 1.55 \cdot 10^{-111}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.50000000000000002e-187 or 3.49999999999999994e-171 < y < 6.80000000000000028e-137 or 1.49999999999999993e-130 < y < 1.55000000000000007e-111

    1. Initial program 58.4%

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

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

    if 1.50000000000000002e-187 < y < 3.49999999999999994e-171 or 6.80000000000000028e-137 < y < 1.49999999999999993e-130 or 1.55000000000000007e-111 < y

    1. Initial program 81.8%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 1.5 \cdot 10^{-187}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 3.5 \cdot 10^{-171}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 6.8 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.5 \cdot 10^{-130}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 1.55 \cdot 10^{-111}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 66.7% accurate, 15.0× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ -1 \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m) :precision binary64 -1.0)
y_m = fabs(y);
double code(double x, double y_m) {
	return -1.0;
}
y_m = abs(y)
real(8) function code(x, y_m)
    real(8), intent (in) :: x
    real(8), intent (in) :: y_m
    code = -1.0d0
end function
y_m = Math.abs(y);
public static double code(double x, double y_m) {
	return -1.0;
}
y_m = math.fabs(y)
def code(x, y_m):
	return -1.0
y_m = abs(y)
function code(x, y_m)
	return -1.0
end
y_m = abs(y);
function tmp = code(x, y_m)
	tmp = -1.0;
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_] := -1.0
\begin{array}{l}
y_m = \left|y\right|

\\
-1
\end{array}
Derivation
  1. Initial program 61.5%

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

    \[\leadsto \color{blue}{-1} \]
  4. Final simplification63.8%

    \[\leadsto -1 \]
  5. Add Preprocessing

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 2024020 
(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))))