?

Average Error: 20.2 → 2.7
Time: 11.8s
Precision: binary64
Cost: 1865

?

\[\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{y}{x} \cdot \frac{y}{x}\\ \mathbf{if}\;y \leq -1.45 \cdot 10^{-203} \lor \neg \left(y \leq 1.8 \cdot 10^{-243}\right):\\ \;\;\;\;\frac{x - y}{y \cdot \frac{y}{y + x} + \frac{1}{y + x} \cdot \left(x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(\left(1 - \frac{y}{x}\right) - t_0\right) + \left(\frac{y}{x} - t_0\right)\\ \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* (/ y x) (/ y x))))
   (if (or (<= y -1.45e-203) (not (<= y 1.8e-243)))
     (/ (- x y) (+ (* y (/ y (+ y x))) (* (/ 1.0 (+ y x)) (* x x))))
     (+ (- (- 1.0 (/ y x)) t_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 = (y / x) * (y / x);
	double tmp;
	if ((y <= -1.45e-203) || !(y <= 1.8e-243)) {
		tmp = (x - y) / ((y * (y / (y + x))) + ((1.0 / (y + x)) * (x * x)));
	} else {
		tmp = ((1.0 - (y / x)) - t_0) + ((y / x) - 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 = (y / x) * (y / x)
    if ((y <= (-1.45d-203)) .or. (.not. (y <= 1.8d-243))) then
        tmp = (x - y) / ((y * (y / (y + x))) + ((1.0d0 / (y + x)) * (x * x)))
    else
        tmp = ((1.0d0 - (y / x)) - t_0) + ((y / x) - 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 = (y / x) * (y / x);
	double tmp;
	if ((y <= -1.45e-203) || !(y <= 1.8e-243)) {
		tmp = (x - y) / ((y * (y / (y + x))) + ((1.0 / (y + x)) * (x * x)));
	} else {
		tmp = ((1.0 - (y / x)) - t_0) + ((y / x) - t_0);
	}
	return tmp;
}
def code(x, y):
	return ((x - y) * (x + y)) / ((x * x) + (y * y))
def code(x, y):
	t_0 = (y / x) * (y / x)
	tmp = 0
	if (y <= -1.45e-203) or not (y <= 1.8e-243):
		tmp = (x - y) / ((y * (y / (y + x))) + ((1.0 / (y + x)) * (x * x)))
	else:
		tmp = ((1.0 - (y / x)) - t_0) + ((y / x) - 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(y / x) * Float64(y / x))
	tmp = 0.0
	if ((y <= -1.45e-203) || !(y <= 1.8e-243))
		tmp = Float64(Float64(x - y) / Float64(Float64(y * Float64(y / Float64(y + x))) + Float64(Float64(1.0 / Float64(y + x)) * Float64(x * x))));
	else
		tmp = Float64(Float64(Float64(1.0 - Float64(y / x)) - t_0) + Float64(Float64(y / x) - 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 = (y / x) * (y / x);
	tmp = 0.0;
	if ((y <= -1.45e-203) || ~((y <= 1.8e-243)))
		tmp = (x - y) / ((y * (y / (y + x))) + ((1.0 / (y + x)) * (x * x)));
	else
		tmp = ((1.0 - (y / x)) - t_0) + ((y / x) - 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[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[y, -1.45e-203], N[Not[LessEqual[y, 1.8e-243]], $MachinePrecision]], N[(N[(x - y), $MachinePrecision] / N[(N[(y * N[(y / N[(y + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(1.0 / N[(y + x), $MachinePrecision]), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 - N[(y / x), $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision] + N[(N[(y / x), $MachinePrecision] - t$95$0), $MachinePrecision]), $MachinePrecision]]]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
t_0 := \frac{y}{x} \cdot \frac{y}{x}\\
\mathbf{if}\;y \leq -1.45 \cdot 10^{-203} \lor \neg \left(y \leq 1.8 \cdot 10^{-243}\right):\\
\;\;\;\;\frac{x - y}{y \cdot \frac{y}{y + x} + \frac{1}{y + x} \cdot \left(x \cdot x\right)}\\

\mathbf{else}:\\
\;\;\;\;\left(\left(1 - \frac{y}{x}\right) - t_0\right) + \left(\frac{y}{x} - t_0\right)\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.2
Target0.1
Herbie2.7
\[\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 y < -1.4499999999999999e-203 or 1.8000000000000001e-243 < y

    1. Initial program 18.0

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{x + y}}} \]
      Proof

      [Start]18.0

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

      associate-/l* [=>]18.0

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

      fma-def [=>]18.0

      \[ \frac{x - y}{\frac{\color{blue}{\mathsf{fma}\left(x, x, y \cdot y\right)}}{x + y}} \]
    3. Applied egg-rr18.1

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

      \[\leadsto \frac{x - y}{\color{blue}{\left(e^{\mathsf{log1p}\left(y \cdot \frac{y}{x + y}\right)} - 1\right)} + \frac{1}{x + y} \cdot \left(x \cdot x\right)} \]
    5. Simplified1.2

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

      [Start]49.5

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

      expm1-def [=>]27.4

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

      expm1-log1p [=>]1.2

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

      +-commutative [=>]1.2

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

    if -1.4499999999999999e-203 < y < 1.8000000000000001e-243

    1. Initial program 30.2

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

      \[\leadsto \color{blue}{\left(\frac{y}{x} + \left(1 + \left(-1 \cdot \frac{{y}^{2}}{{x}^{2}} + -1 \cdot \frac{y}{x}\right)\right)\right) - \frac{{y}^{2}}{{x}^{2}}} \]
    3. Simplified9.2

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

      [Start]30.2

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

      +-commutative [=>]30.2

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

      associate--l+ [=>]30.2

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

      +-commutative [=>]30.2

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

      mul-1-neg [=>]30.2

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

      unsub-neg [=>]30.2

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

      associate-+r- [=>]30.2

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

      mul-1-neg [=>]30.2

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

      unsub-neg [=>]30.2

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

      unpow2 [=>]30.2

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

      unpow2 [=>]30.2

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

      times-frac [=>]30.2

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

      unpow2 [=>]30.2

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

      unpow2 [=>]30.2

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

      times-frac [=>]9.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.45 \cdot 10^{-203} \lor \neg \left(y \leq 1.8 \cdot 10^{-243}\right):\\ \;\;\;\;\frac{x - y}{y \cdot \frac{y}{y + x} + \frac{1}{y + x} \cdot \left(x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(\left(1 - \frac{y}{x}\right) - \frac{y}{x} \cdot \frac{y}{x}\right) + \left(\frac{y}{x} - \frac{y}{x} \cdot \frac{y}{x}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error2.8
Cost1609
\[\begin{array}{l} \mathbf{if}\;y \leq -1.65 \cdot 10^{-203} \lor \neg \left(y \leq 1.6 \cdot 10^{-243}\right):\\ \;\;\;\;\frac{x - y}{y \cdot \frac{y}{y + x} + \frac{1}{y + x} \cdot \left(x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \end{array} \]
Alternative 2
Error5.0
Cost1357
\[\begin{array}{l} \mathbf{if}\;y \leq -2 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.6 \cdot 10^{-162} \lor \neg \left(y \leq 1.38 \cdot 10^{-163}\right):\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \end{array} \]
Alternative 3
Error4.8
Cost1357
\[\begin{array}{l} \mathbf{if}\;y \leq -2 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.6 \cdot 10^{-162} \lor \neg \left(y \leq 1.38 \cdot 10^{-163}\right):\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\left(x - y\right) + \frac{y + y}{\frac{x}{y}}}\\ \end{array} \]
Alternative 4
Error11.3
Cost841
\[\begin{array}{l} \mathbf{if}\;y \leq -1.15 \cdot 10^{-123} \lor \neg \left(y \leq 7.2 \cdot 10^{-177}\right):\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 5
Error11.2
Cost841
\[\begin{array}{l} \mathbf{if}\;y \leq -1.15 \cdot 10^{-123} \lor \neg \left(y \leq 3.4 \cdot 10^{-176}\right):\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \end{array} \]
Alternative 6
Error11.4
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -1.15 \cdot 10^{-123}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 3 \cdot 10^{-153}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 7
Error21.6
Cost64
\[-1 \]

Error

Reproduce?

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