| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 13632 |
\[\frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x + y}{\mathsf{hypot}\left(x, y\right)}
\]
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y) :precision binary64 (let* ((t_0 (/ (- x y) (hypot x y)))) (/ t_0 (/ (- x y) (* t_0 (+ x y))))))
double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
double code(double x, double y) {
double t_0 = (x - y) / hypot(x, y);
return t_0 / ((x - y) / (t_0 * (x + y)));
}
public static double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
public static double code(double x, double y) {
double t_0 = (x - y) / Math.hypot(x, y);
return t_0 / ((x - y) / (t_0 * (x + y)));
}
def code(x, y): return ((x - y) * (x + y)) / ((x * x) + (y * y))
def code(x, y): t_0 = (x - y) / math.hypot(x, y) return t_0 / ((x - y) / (t_0 * (x + y)))
function code(x, y) return Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) end
function code(x, y) t_0 = Float64(Float64(x - y) / hypot(x, y)) return Float64(t_0 / Float64(Float64(x - y) / Float64(t_0 * Float64(x + y)))) end
function tmp = code(x, y) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); end
function tmp = code(x, y) t_0 = (x - y) / hypot(x, y); tmp = t_0 / ((x - y) / (t_0 * (x + 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]
code[x_, y_] := Block[{t$95$0 = N[(N[(x - y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 / N[(N[(x - y), $MachinePrecision] / N[(t$95$0 * N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
t_0 := \frac{x - y}{\mathsf{hypot}\left(x, y\right)}\\
\frac{t_0}{\frac{x - y}{t_0 \cdot \left(x + y\right)}}
\end{array}
Results
| Original | 67.5% |
|---|---|
| Target | 99.9% |
| Herbie | 99.9% |
Initial program 67.5%
Applied egg-rr99.9%
[Start]67.5 | \[ \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\] |
|---|---|
add-sqr-sqrt [=>]67.5 | \[ \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}}}
\] |
times-frac [=>]67.3 | \[ \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}}
\] |
hypot-def [=>]67.4 | \[ \frac{x - y}{\color{blue}{\mathsf{hypot}\left(x, y\right)}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}
\] |
hypot-def [=>]99.9 | \[ \frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x + y}{\color{blue}{\mathsf{hypot}\left(x, y\right)}}
\] |
Applied egg-rr100.0%
[Start]99.9 | \[ \frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x + y}{\mathsf{hypot}\left(x, y\right)}
\] |
|---|---|
associate-*r/ [=>]100.0 | \[ \color{blue}{\frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \left(x + y\right)}{\mathsf{hypot}\left(x, y\right)}}
\] |
associate-/l* [=>]100.0 | \[ \color{blue}{\frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x + y}}}
\] |
Applied egg-rr66.7%
[Start]100.0 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x + y}}
\] |
|---|---|
flip-+ [=>]67.2 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{\color{blue}{\frac{x \cdot x - y \cdot y}{x - y}}}}
\] |
associate-/r/ [=>]67.0 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\color{blue}{\frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot \left(x - y\right)}}
\] |
sub-neg [=>]67.0 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot \color{blue}{\left(x + \left(-y\right)\right)}}
\] |
distribute-lft-in [=>]66.7 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\color{blue}{\frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot x + \frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot \left(-y\right)}}
\] |
Simplified99.9%
[Start]66.7 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot x + \frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot \left(-y\right)}
\] |
|---|---|
distribute-lft-out [=>]67.0 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\color{blue}{\frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot \left(x + \left(-y\right)\right)}}
\] |
sub-neg [<=]67.0 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y} \cdot \color{blue}{\left(x - y\right)}}
\] |
*-commutative [<=]67.0 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\color{blue}{\left(x - y\right) \cdot \frac{\mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y}}}
\] |
associate-*r/ [=>]67.5 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\color{blue}{\frac{\left(x - y\right) \cdot \mathsf{hypot}\left(x, y\right)}{x \cdot x - y \cdot y}}}
\] |
associate-/l* [=>]67.2 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\color{blue}{\frac{x - y}{\frac{x \cdot x - y \cdot y}{\mathsf{hypot}\left(x, y\right)}}}}
\] |
difference-of-squares [=>]67.2 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{x - y}{\frac{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}{\mathsf{hypot}\left(x, y\right)}}}
\] |
associate-*r/ [<=]99.9 | \[ \frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)}}{\frac{x - y}{\color{blue}{\left(x + y\right) \cdot \frac{x - y}{\mathsf{hypot}\left(x, y\right)}}}}
\] |
Final simplification99.9%
| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 13632 |
| Alternative 2 | |
|---|---|
| Accuracy | 100.0% |
| Cost | 13632 |
| Alternative 3 | |
|---|---|
| Accuracy | 93.0% |
| Cost | 2116 |
| Alternative 4 | |
|---|---|
| Accuracy | 92.5% |
| Cost | 1988 |
| Alternative 5 | |
|---|---|
| Accuracy | 82.5% |
| Cost | 1232 |
| Alternative 6 | |
|---|---|
| Accuracy | 82.3% |
| Cost | 1105 |
| Alternative 7 | |
|---|---|
| Accuracy | 81.8% |
| Cost | 848 |
| Alternative 8 | |
|---|---|
| Accuracy | 81.8% |
| Cost | 592 |
| Alternative 9 | |
|---|---|
| Accuracy | 66.4% |
| Cost | 64 |
herbie shell --seed 2023140
(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))))