?

Average Error: 20.1 → 4.6
Time: 1.9s
Precision: binary64
Cost: 1988

?

\[\left(0 < x \land x < 1\right) \land y < 1\]
\[\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} \]
(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}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.1
Target0.0
Herbie4.6
\[\begin{array}{l} \mathbf{if}\;0.5 < \left|\frac{x}{y}\right| \land \left|\frac{x}{y}\right| < 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} \]

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 0.0

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

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

      [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)))

    1. Initial program 64.0

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

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

      [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} \]
    3. Taylor expanded in x around 0 14.6

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

    \[\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{x \cdot x - y \cdot y}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Alternatives

Alternative 1
Error4.6
Cost1988
\[\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 1.7974736898995642 \cdot 10^{+308}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 2
Error11.1
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -7.5 \cdot 10^{-137}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 5 \cdot 10^{-153}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 3
Error21.8
Cost64
\[-1 \]

Error

Reproduce?

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