| Alternative 1 | |
|---|---|
| Error | 4.6 |
| Cost | 1988 |
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y) :precision binary64 (let* ((t_0 (+ (* x x) (* y y)))) (if (<= (/ (* (- x y) (+ x y)) t_0) 2.0) (/ (- (* x x) (* y y)) t_0) -1.0)))
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 * x) + (y * y);
double tmp;
if ((((x - y) * (x + y)) / t_0) <= 2.0) {
tmp = ((x * x) - (y * y)) / t_0;
} else {
tmp = -1.0;
}
return tmp;
}
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
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 * x) + (y * y)
if ((((x - y) * (x + y)) / t_0) <= 2.0d0) then
tmp = ((x * x) - (y * y)) / t_0
else
tmp = -1.0d0
end if
code = tmp
end function
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 * x) + (y * y);
double tmp;
if ((((x - y) * (x + y)) / t_0) <= 2.0) {
tmp = ((x * x) - (y * y)) / t_0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): return ((x - y) * (x + y)) / ((x * x) + (y * y))
def code(x, y): t_0 = (x * x) + (y * y) tmp = 0 if (((x - y) * (x + y)) / t_0) <= 2.0: tmp = ((x * x) - (y * y)) / t_0 else: tmp = -1.0 return tmp
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 * x) + Float64(y * y)) tmp = 0.0 if (Float64(Float64(Float64(x - y) * Float64(x + y)) / t_0) <= 2.0) tmp = Float64(Float64(Float64(x * x) - Float64(y * y)) / t_0); else tmp = -1.0; end return tmp end
function tmp = code(x, y) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); end
function tmp_2 = code(x, y) t_0 = (x * x) + (y * y); tmp = 0.0; if ((((x - y) * (x + y)) / t_0) <= 2.0) tmp = ((x * x) - (y * y)) / t_0; else tmp = -1.0; end tmp_2 = tmp; 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 * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision], 2.0], N[(N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision], -1.0]]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
t_0 := x \cdot x + y \cdot y\\
\mathbf{if}\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{t_0} \leq 2:\\
\;\;\;\;\frac{x \cdot x - y \cdot y}{t_0}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
Results
| Original | 20.1 |
|---|---|
| Target | 0.0 |
| Herbie | 4.6 |
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 0.0
Simplified0.0
[Start]0.0 | \[ \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\] |
|---|---|
rational_best-simplify-62 [=>]0.0 | \[ \frac{\left(x - y\right) \cdot \color{blue}{\left(y - \left(-x\right)\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-111 [=>]0.0 | \[ \frac{\color{blue}{\left(x - y\right) \cdot y - \left(x - y\right) \cdot \left(-x\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-3 [=>]0.0 | \[ \frac{\color{blue}{y \cdot \left(x - y\right)} - \left(x - y\right) \cdot \left(-x\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-110 [<=]0.0 | \[ \frac{\color{blue}{\left(y \cdot x - y \cdot y\right)} - \left(x - y\right) \cdot \left(-x\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-65 [=>]0.0 | \[ \frac{\color{blue}{-\left(\left(x - y\right) \cdot \left(-x\right) - \left(y \cdot x - y \cdot y\right)\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-56 [=>]0.0 | \[ \frac{-\left(\color{blue}{x \cdot \left(-\left(x - y\right)\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-65 [<=]0.0 | \[ \frac{-\left(x \cdot \color{blue}{\left(y - x\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-110 [<=]0.0 | \[ \frac{-\left(\color{blue}{\left(x \cdot y - x \cdot x\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-3 [<=]0.0 | \[ \frac{-\left(\left(\color{blue}{y \cdot x} - x \cdot x\right) - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-61 [=>]0.0 | \[ \frac{-\left(\color{blue}{\left(y \cdot x + \left(-x \cdot x\right)\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-61 [=>]0.0 | \[ \frac{-\left(\left(y \cdot x + \left(-x \cdot x\right)\right) - \color{blue}{\left(y \cdot x + \left(-y \cdot y\right)\right)}\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-1 [=>]0.0 | \[ \frac{-\left(\left(y \cdot x + \left(-x \cdot x\right)\right) - \color{blue}{\left(\left(-y \cdot y\right) + y \cdot x\right)}\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-112 [=>]0.0 | \[ \frac{-\color{blue}{\left(\left(-x \cdot x\right) - \left(-y \cdot y\right)\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-65 [<=]0.0 | \[ \frac{\color{blue}{\left(-y \cdot y\right) - \left(-x \cdot x\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-62 [<=]0.0 | \[ \frac{\color{blue}{x \cdot x + \left(-y \cdot y\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-61 [<=]0.0 | \[ \frac{\color{blue}{x \cdot x - y \cdot y}}{x \cdot x + y \cdot y}
\] |
if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 64.0
Simplified64.0
[Start]64.0 | \[ \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\] |
|---|---|
rational_best-simplify-62 [=>]64.0 | \[ \frac{\left(x - y\right) \cdot \color{blue}{\left(y - \left(-x\right)\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-111 [=>]64.0 | \[ \frac{\color{blue}{\left(x - y\right) \cdot y - \left(x - y\right) \cdot \left(-x\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-3 [=>]64.0 | \[ \frac{\color{blue}{y \cdot \left(x - y\right)} - \left(x - y\right) \cdot \left(-x\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-110 [<=]64.0 | \[ \frac{\color{blue}{\left(y \cdot x - y \cdot y\right)} - \left(x - y\right) \cdot \left(-x\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-65 [=>]64.0 | \[ \frac{\color{blue}{-\left(\left(x - y\right) \cdot \left(-x\right) - \left(y \cdot x - y \cdot y\right)\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-56 [=>]64.0 | \[ \frac{-\left(\color{blue}{x \cdot \left(-\left(x - y\right)\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-65 [<=]64.0 | \[ \frac{-\left(x \cdot \color{blue}{\left(y - x\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-110 [<=]64.0 | \[ \frac{-\left(\color{blue}{\left(x \cdot y - x \cdot x\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-3 [<=]64.0 | \[ \frac{-\left(\left(\color{blue}{y \cdot x} - x \cdot x\right) - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-61 [=>]64.0 | \[ \frac{-\left(\color{blue}{\left(y \cdot x + \left(-x \cdot x\right)\right)} - \left(y \cdot x - y \cdot y\right)\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-61 [=>]64.0 | \[ \frac{-\left(\left(y \cdot x + \left(-x \cdot x\right)\right) - \color{blue}{\left(y \cdot x + \left(-y \cdot y\right)\right)}\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-1 [=>]64.0 | \[ \frac{-\left(\left(y \cdot x + \left(-x \cdot x\right)\right) - \color{blue}{\left(\left(-y \cdot y\right) + y \cdot x\right)}\right)}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-112 [=>]64.0 | \[ \frac{-\color{blue}{\left(\left(-x \cdot x\right) - \left(-y \cdot y\right)\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-65 [<=]64.0 | \[ \frac{\color{blue}{\left(-y \cdot y\right) - \left(-x \cdot x\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-62 [<=]64.0 | \[ \frac{\color{blue}{x \cdot x + \left(-y \cdot y\right)}}{x \cdot x + y \cdot y}
\] |
rational_best-simplify-61 [<=]64.0 | \[ \frac{\color{blue}{x \cdot x - y \cdot y}}{x \cdot x + y \cdot y}
\] |
Taylor expanded in x around 0 14.6
Final simplification4.6
| Alternative 1 | |
|---|---|
| Error | 4.6 |
| Cost | 1988 |
| Alternative 2 | |
|---|---|
| Error | 11.1 |
| Cost | 328 |
| Alternative 3 | |
|---|---|
| Error | 21.8 |
| Cost | 64 |
herbie shell --seed 2023104
(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))))