Kahan p9 Example

Percentage Accurate: 67.3% → 100.0%
Time: 11.2s
Alternatives: 6
Speedup: 1.2×

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 6 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: 67.3% 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{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 70.3%

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{x - y}{\frac{\color{blue}{x \cdot x + y \cdot y}}{x + y}} \]
    3. fma-def70.8%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\left(x - y\right) \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)}}} \]
    3. times-frac70.8%

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

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

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

      \[\leadsto \frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x + y}{\sqrt{\color{blue}{x \cdot x + y \cdot y}}} \]
    7. 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)}} \]
  4. 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)}} \]
  5. Final simplification99.9%

    \[\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: 100.0% accurate, 0.1× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \frac{\frac{x - y\_m}{\mathsf{hypot}\left(x, y\_m\right)}}{\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(Float64(x - y_m) / 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[(N[(x - y$95$m), $MachinePrecision] / N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x ^ 2 + y$95$m ^ 2], $MachinePrecision] / N[(x + y$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|

\\
\frac{\frac{x - y\_m}{\mathsf{hypot}\left(x, y\_m\right)}}{\frac{\mathsf{hypot}\left(x, y\_m\right)}{x + y\_m}}
\end{array}
Derivation
  1. Initial program 70.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. fma-def70.3%

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

      \[\leadsto \frac{\left(x - y\right) \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)}}} \]
    3. times-frac70.8%

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

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

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

      \[\leadsto \frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x + y}{\sqrt{\color{blue}{x \cdot x + y \cdot y}}} \]
    7. 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)}} \]
  4. 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)}} \]
  5. Step-by-step derivation
    1. clear-num99.9%

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

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

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

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

Alternative 4: 67.3% accurate, 1.0× speedup?

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

\\
\frac{\left(x - y\_m\right) \cdot \left(x + y\_m\right)}{x \cdot x + y\_m \cdot y\_m}
\end{array}
Derivation
  1. Initial program 70.3%

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

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

Alternative 5: 67.8% accurate, 1.2× speedup?

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

\\
\frac{1}{y\_m} \cdot \left(\left(x - y\_m\right) \cdot \left(1 + \frac{x}{y\_m}\right)\right)
\end{array}
Derivation
  1. Initial program 70.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. fma-def70.3%

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

      \[\leadsto \frac{\left(x - y\right) \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)}}} \]
    3. times-frac70.8%

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

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

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

      \[\leadsto \frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x + y}{\sqrt{\color{blue}{x \cdot x + y \cdot y}}} \]
    7. 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)}} \]
  4. 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)}} \]
  5. Taylor expanded in x around 0 16.0%

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

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

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

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

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

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

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

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

Alternative 6: 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 70.3%

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

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

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

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

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

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

    \[\leadsto \color{blue}{-1} \]
  6. Final simplification64.2%

    \[\leadsto -1 \]
  7. 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 2024033 
(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))))