?

Average Error: 20.5 → 5.4
Time: 3.8s
Precision: binary64
Cost: 1356

?

\[\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 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;y \leq -2 \cdot 10^{+155}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -2.46 \cdot 10^{-147}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.55 \cdot 10^{-162}:\\ \;\;\;\;\left(-\frac{y}{x}\right) + \left(1 + \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))))
   (if (<= y -2e+155)
     -1.0
     (if (<= y -2.46e-147)
       t_0
       (if (<= y 1.55e-162) (+ (- (/ y x)) (+ 1.0 (/ y x))) t_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 - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (y <= -2e+155) {
		tmp = -1.0;
	} else if (y <= -2.46e-147) {
		tmp = t_0;
	} else if (y <= 1.55e-162) {
		tmp = -(y / x) + (1.0 + (y / x));
	} else {
		tmp = t_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 - y) * (x + y)) / ((x * x) + (y * y))
    if (y <= (-2d+155)) then
        tmp = -1.0d0
    else if (y <= (-2.46d-147)) then
        tmp = t_0
    else if (y <= 1.55d-162) then
        tmp = -(y / x) + (1.0d0 + (y / x))
    else
        tmp = t_0
    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 - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (y <= -2e+155) {
		tmp = -1.0;
	} else if (y <= -2.46e-147) {
		tmp = t_0;
	} else if (y <= 1.55e-162) {
		tmp = -(y / x) + (1.0 + (y / x));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	return ((x - y) * (x + y)) / ((x * x) + (y * y))
def code(x, y):
	t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y))
	tmp = 0
	if y <= -2e+155:
		tmp = -1.0
	elif y <= -2.46e-147:
		tmp = t_0
	elif y <= 1.55e-162:
		tmp = -(y / x) + (1.0 + (y / x))
	else:
		tmp = t_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(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)))
	tmp = 0.0
	if (y <= -2e+155)
		tmp = -1.0;
	elseif (y <= -2.46e-147)
		tmp = t_0;
	elseif (y <= 1.55e-162)
		tmp = Float64(Float64(-Float64(y / x)) + Float64(1.0 + Float64(y / x)));
	else
		tmp = t_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 - y) * (x + y)) / ((x * x) + (y * y));
	tmp = 0.0;
	if (y <= -2e+155)
		tmp = -1.0;
	elseif (y <= -2.46e-147)
		tmp = t_0;
	elseif (y <= 1.55e-162)
		tmp = -(y / x) + (1.0 + (y / x));
	else
		tmp = t_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[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -2e+155], -1.0, If[LessEqual[y, -2.46e-147], t$95$0, If[LessEqual[y, 1.55e-162], N[((-N[(y / x), $MachinePrecision]) + N[(1.0 + N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;y \leq -2 \cdot 10^{+155}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -2.46 \cdot 10^{-147}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;y \leq 1.55 \cdot 10^{-162}:\\
\;\;\;\;\left(-\frac{y}{x}\right) + \left(1 + \frac{y}{x}\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.5
Target0.0
Herbie5.4
\[\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 3 regimes
  2. if y < -2.00000000000000001e155

    1. Initial program 64.0

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

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

    if -2.00000000000000001e155 < y < -2.4599999999999999e-147 or 1.5499999999999999e-162 < y

    1. Initial program 0.2

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]

    if -2.4599999999999999e-147 < y < 1.5499999999999999e-162

    1. Initial program 29.3

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Applied egg-rr31.8

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\left(x \cdot x + y \cdot y\right) \cdot \left(\left(x \cdot x + y \cdot y\right) \cdot \frac{1}{x \cdot x + y \cdot y}\right)}} \]
    3. Taylor expanded in x around inf 16.1

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

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

      [Start]16.1

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

      rational_best_oopsla_all_46_json_45_simplify-35 [=>]16.1

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

      rational_best_oopsla_all_46_json_45_simplify-82 [=>]16.1

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

      rational_best_oopsla_all_46_json_45_simplify-74 [=>]16.1

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

      rational_best_oopsla_all_46_json_45_simplify-92 [=>]16.1

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

      rational_best_oopsla_all_46_json_45_simplify-35 [=>]16.1

      \[ \left(-\frac{y}{x}\right) + \color{blue}{\left(1 + \frac{y}{x}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification5.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -2 \cdot 10^{+155}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -2.46 \cdot 10^{-147}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq 1.55 \cdot 10^{-162}:\\ \;\;\;\;\left(-\frac{y}{x}\right) + \left(1 + \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \end{array} \]

Alternatives

Alternative 1
Error12.7
Cost904
\[\begin{array}{l} t_0 := \frac{x}{y} + \left(-1 - \frac{x}{y}\right)\\ \mathbf{if}\;y \leq -6.8 \cdot 10^{-90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 3.4 \cdot 10^{-96}:\\ \;\;\;\;\left(-\frac{y}{x}\right) + \left(1 + \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error12.4
Cost840
\[\begin{array}{l} t_0 := \frac{x}{y} + \left(-1 - \frac{x}{y}\right)\\ \mathbf{if}\;y \leq -7.5 \cdot 10^{-98}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.7 \cdot 10^{-104}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error12.7
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -8.5 \cdot 10^{-97}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 3.4 \cdot 10^{-96}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 4
Error21.5
Cost64
\[-1 \]

Error

Reproduce?

herbie shell --seed 2023090 
(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))))