Average Error: 31.5 → 4.2
Time: 13.3s
Precision: binary64
Cost: 14788
\[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
\[\begin{array}{l} t_0 := \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\ t_1 := \mathsf{hypot}\left(x, y \cdot 2\right)\\ \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+289}:\\ \;\;\;\;\frac{1}{t_1} \cdot \frac{x \cdot x}{t_1} - t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right) - t_0\\ \end{array} \]
(FPCore (x y)
 :precision binary64
 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ y (+ y (* 0.25 (* x (/ x y)))))) (t_1 (hypot x (* y 2.0))))
   (if (<= (* x x) 5e+289)
     (- (* (/ 1.0 t_1) (/ (* x x) t_1)) t_0)
     (- (fma -4.0 (* (/ y x) (/ y x)) 1.0) t_0))))
double code(double x, double y) {
	return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
}
double code(double x, double y) {
	double t_0 = y / (y + (0.25 * (x * (x / y))));
	double t_1 = hypot(x, (y * 2.0));
	double tmp;
	if ((x * x) <= 5e+289) {
		tmp = ((1.0 / t_1) * ((x * x) / t_1)) - t_0;
	} else {
		tmp = fma(-4.0, ((y / x) * (y / x)), 1.0) - t_0;
	}
	return tmp;
}
function code(x, y)
	return Float64(Float64(Float64(x * x) - Float64(Float64(y * 4.0) * y)) / Float64(Float64(x * x) + Float64(Float64(y * 4.0) * y)))
end
function code(x, y)
	t_0 = Float64(y / Float64(y + Float64(0.25 * Float64(x * Float64(x / y)))))
	t_1 = hypot(x, Float64(y * 2.0))
	tmp = 0.0
	if (Float64(x * x) <= 5e+289)
		tmp = Float64(Float64(Float64(1.0 / t_1) * Float64(Float64(x * x) / t_1)) - t_0);
	else
		tmp = Float64(fma(-4.0, Float64(Float64(y / x) * Float64(y / x)), 1.0) - t_0);
	end
	return tmp
end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] - N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[(y / N[(y + N[(0.25 * N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[x ^ 2 + N[(y * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 5e+289], N[(N[(N[(1.0 / t$95$1), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] / t$95$1), $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision], N[(N[(-4.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision] - t$95$0), $MachinePrecision]]]]
\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}
\begin{array}{l}
t_0 := \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\
t_1 := \mathsf{hypot}\left(x, y \cdot 2\right)\\
\mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+289}:\\
\;\;\;\;\frac{1}{t_1} \cdot \frac{x \cdot x}{t_1} - t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right) - t_0\\


\end{array}

Error

Target

Original31.5
Target31.2
Herbie4.2
\[\begin{array}{l} \mathbf{if}\;\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} < 0.9743233849626781:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + \left(y \cdot y\right) \cdot 4} - \frac{\left(y \cdot y\right) \cdot 4}{x \cdot x + \left(y \cdot y\right) \cdot 4}\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{x}{\sqrt{x \cdot x + \left(y \cdot y\right) \cdot 4}}\right)}^{2} - \frac{\left(y \cdot y\right) \cdot 4}{x \cdot x + \left(y \cdot y\right) \cdot 4}\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if (*.f64 x x) < 5.00000000000000031e289

    1. Initial program 21.1

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied egg-rr20.7

      \[\leadsto \color{blue}{\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{y \cdot 4}}} \]
    3. Taylor expanded in x around 0 5.1

      \[\leadsto \frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\color{blue}{0.25 \cdot \frac{{x}^{2}}{y} + y}} \]
    4. Simplified5.1

      \[\leadsto \frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\color{blue}{\mathsf{fma}\left(0.25, \frac{x}{y} \cdot x, y\right)}} \]
      Proof
      (fma.f64 1/4 (*.f64 (/.f64 x y) x) y): 0 points increase in error, 0 points decrease in error
      (fma.f64 1/4 (Rewrite<= associate-/r/_binary64 (/.f64 x (/.f64 y x))) y): 11 points increase in error, 17 points decrease in error
      (fma.f64 1/4 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 x x) y)) y): 32 points increase in error, 12 points decrease in error
      (fma.f64 1/4 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) y) y): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 1/4 (/.f64 (pow.f64 x 2) y)) y)): 0 points increase in error, 0 points decrease in error
    5. Applied egg-rr5.1

      \[\leadsto \frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\color{blue}{0.25 \cdot \left(x \cdot \frac{x}{y}\right) + y}} \]
    6. Applied egg-rr2.7

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(x, y \cdot 2\right)} \cdot \frac{x \cdot x}{\mathsf{hypot}\left(x, y \cdot 2\right)}} - \frac{y}{0.25 \cdot \left(x \cdot \frac{x}{y}\right) + y} \]

    if 5.00000000000000031e289 < (*.f64 x x)

    1. Initial program 61.3

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied egg-rr61.3

      \[\leadsto \color{blue}{\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{y \cdot 4}}} \]
    3. Taylor expanded in x around 0 60.4

      \[\leadsto \frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\color{blue}{0.25 \cdot \frac{{x}^{2}}{y} + y}} \]
    4. Simplified60.4

      \[\leadsto \frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\color{blue}{\mathsf{fma}\left(0.25, \frac{x}{y} \cdot x, y\right)}} \]
      Proof
      (fma.f64 1/4 (*.f64 (/.f64 x y) x) y): 0 points increase in error, 0 points decrease in error
      (fma.f64 1/4 (Rewrite<= associate-/r/_binary64 (/.f64 x (/.f64 y x))) y): 11 points increase in error, 17 points decrease in error
      (fma.f64 1/4 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 x x) y)) y): 32 points increase in error, 12 points decrease in error
      (fma.f64 1/4 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) y) y): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 1/4 (/.f64 (pow.f64 x 2) y)) y)): 0 points increase in error, 0 points decrease in error
    5. Taylor expanded in x around inf 16.7

      \[\leadsto \color{blue}{\left(1 + -4 \cdot \frac{{y}^{2}}{{x}^{2}}\right)} - \frac{y}{\mathsf{fma}\left(0.25, \frac{x}{y} \cdot x, y\right)} \]
    6. Simplified8.3

      \[\leadsto \color{blue}{\mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right)} - \frac{y}{\mathsf{fma}\left(0.25, \frac{x}{y} \cdot x, y\right)} \]
      Proof
      (fma.f64 -4 (*.f64 (/.f64 y x) (/.f64 y x)) 1): 0 points increase in error, 0 points decrease in error
      (fma.f64 -4 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 y y) (*.f64 x x))) 1): 52 points increase in error, 6 points decrease in error
      (fma.f64 -4 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 y 2)) (*.f64 x x)) 1): 0 points increase in error, 0 points decrease in error
      (fma.f64 -4 (/.f64 (pow.f64 y 2) (Rewrite<= unpow2_binary64 (pow.f64 x 2))) 1): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 -4 (/.f64 (pow.f64 y 2) (pow.f64 x 2))) 1)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 1 (*.f64 -4 (/.f64 (pow.f64 y 2) (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
    7. Applied egg-rr8.3

      \[\leadsto \mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right) - \frac{y}{\color{blue}{0.25 \cdot \left(x \cdot \frac{x}{y}\right) + y}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification4.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+289}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(x, y \cdot 2\right)} \cdot \frac{x \cdot x}{\mathsf{hypot}\left(x, y \cdot 2\right)} - \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right) - \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error12.3
Cost8140
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ t_2 := x \cdot \left(\frac{x}{y} \cdot \frac{0.5}{y}\right) + -1\\ \mathbf{if}\;y \leq -1.0940465293235568 \cdot 10^{+99}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq -1.5985860938331456 \cdot 10^{-63}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 4.747370275977222 \cdot 10^{-91}:\\ \;\;\;\;\mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right) - \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\ \mathbf{elif}\;y \leq 8.911105642492523 \cdot 10^{+87}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 2
Error5.9
Cost8132
\[\begin{array}{l} t_0 := \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\ \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+289}:\\ \;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-4, \frac{y}{x} \cdot \frac{y}{x}, 1\right) - t_0\\ \end{array} \]
Alternative 3
Error12.3
Cost7308
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ t_2 := x \cdot \left(\frac{x}{y} \cdot \frac{0.5}{y}\right) + -1\\ \mathbf{if}\;y \leq -1.0940465293235568 \cdot 10^{+99}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq -1.5985860938331456 \cdot 10^{-63}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 4.747370275977222 \cdot 10^{-91}:\\ \;\;\;\;1 + -8 \cdot {\left(\frac{x}{y}\right)}^{-2}\\ \mathbf{elif}\;y \leq 8.911105642492523 \cdot 10^{+87}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 4
Error17.0
Cost1868
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := x \cdot \left(\frac{x}{y} \cdot \frac{0.5}{y}\right) + -1\\ \mathbf{if}\;t_0 \leq 4 \cdot 10^{-169}:\\ \;\;\;\;1 + y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right)\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+158}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+179}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error12.4
Cost1744
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ t_2 := x \cdot \left(\frac{x}{y} \cdot \frac{0.5}{y}\right) + -1\\ \mathbf{if}\;y \leq -1.0940465293235568 \cdot 10^{+99}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq -1.5985860938331456 \cdot 10^{-63}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 4.747370275977222 \cdot 10^{-91}:\\ \;\;\;\;1 + y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right)\\ \mathbf{elif}\;y \leq 8.911105642492523 \cdot 10^{+87}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 6
Error16.8
Cost968
\[\begin{array}{l} \mathbf{if}\;y \leq -1.5985860938331456 \cdot 10^{-63}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 4.747370275977222 \cdot 10^{-91}:\\ \;\;\;\;1 + y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 7
Error17.0
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -1.5985860938331456 \cdot 10^{-63}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 4.2224998622300256 \cdot 10^{-92}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 8
Error31.8
Cost64
\[1 \]

Error

Reproduce

herbie shell --seed 2022306 
(FPCore (x y)
  :name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))) 0.9743233849626781) (- (/ (* x x) (+ (* x x) (* (* y y) 4.0))) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))) (- (pow (/ x (sqrt (+ (* x x) (* (* y y) 4.0)))) 2.0) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))))

  (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))