Average Error: 31.4 → 13.1
Time: 6.9s
Precision: binary64
Cost: 7752
\[\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{x}{y} \cdot \frac{x}{y}\\ t_1 := x \cdot x + y \cdot \left(y \cdot -4\right)\\ t_2 := \frac{t_1}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{if}\;x \leq -5.93 \cdot 10^{+88}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -5.2 \cdot 10^{-161}:\\ \;\;\;\;\frac{t_1}{\mathsf{fma}\left(y \cdot 4, y, x \cdot x\right)}\\ \mathbf{elif}\;x \leq 5.7 \cdot 10^{-160}:\\ \;\;\;\;\mathsf{fma}\left(t_0, 0.5, -1\right)\\ \mathbf{elif}\;x \leq 1.55 \cdot 10^{-29}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq 1.4 \cdot 10^{+24}:\\ \;\;\;\;-1 + \frac{t_0}{4}\\ \mathbf{elif}\;x \leq 6 \cdot 10^{+57}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;1\\ \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 (* (/ x y) (/ x y)))
        (t_1 (+ (* x x) (* y (* y -4.0))))
        (t_2 (/ t_1 (+ (* x x) (* y (* y 4.0))))))
   (if (<= x -5.93e+88)
     1.0
     (if (<= x -5.2e-161)
       (/ t_1 (fma (* y 4.0) y (* x x)))
       (if (<= x 5.7e-160)
         (fma t_0 0.5 -1.0)
         (if (<= x 1.55e-29)
           t_2
           (if (<= x 1.4e+24)
             (+ -1.0 (/ t_0 4.0))
             (if (<= x 6e+57) t_2 1.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 = (x / y) * (x / y);
	double t_1 = (x * x) + (y * (y * -4.0));
	double t_2 = t_1 / ((x * x) + (y * (y * 4.0)));
	double tmp;
	if (x <= -5.93e+88) {
		tmp = 1.0;
	} else if (x <= -5.2e-161) {
		tmp = t_1 / fma((y * 4.0), y, (x * x));
	} else if (x <= 5.7e-160) {
		tmp = fma(t_0, 0.5, -1.0);
	} else if (x <= 1.55e-29) {
		tmp = t_2;
	} else if (x <= 1.4e+24) {
		tmp = -1.0 + (t_0 / 4.0);
	} else if (x <= 6e+57) {
		tmp = t_2;
	} else {
		tmp = 1.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(Float64(x / y) * Float64(x / y))
	t_1 = Float64(Float64(x * x) + Float64(y * Float64(y * -4.0)))
	t_2 = Float64(t_1 / Float64(Float64(x * x) + Float64(y * Float64(y * 4.0))))
	tmp = 0.0
	if (x <= -5.93e+88)
		tmp = 1.0;
	elseif (x <= -5.2e-161)
		tmp = Float64(t_1 / fma(Float64(y * 4.0), y, Float64(x * x)));
	elseif (x <= 5.7e-160)
		tmp = fma(t_0, 0.5, -1.0);
	elseif (x <= 1.55e-29)
		tmp = t_2;
	elseif (x <= 1.4e+24)
		tmp = Float64(-1.0 + Float64(t_0 / 4.0));
	elseif (x <= 6e+57)
		tmp = t_2;
	else
		tmp = 1.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[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 / N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -5.93e+88], 1.0, If[LessEqual[x, -5.2e-161], N[(t$95$1 / N[(N[(y * 4.0), $MachinePrecision] * y + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 5.7e-160], N[(t$95$0 * 0.5 + -1.0), $MachinePrecision], If[LessEqual[x, 1.55e-29], t$95$2, If[LessEqual[x, 1.4e+24], N[(-1.0 + N[(t$95$0 / 4.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 6e+57], t$95$2, 1.0]]]]]]]]]
\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{x}{y} \cdot \frac{x}{y}\\
t_1 := x \cdot x + y \cdot \left(y \cdot -4\right)\\
t_2 := \frac{t_1}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\
\mathbf{if}\;x \leq -5.93 \cdot 10^{+88}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq -5.2 \cdot 10^{-161}:\\
\;\;\;\;\frac{t_1}{\mathsf{fma}\left(y \cdot 4, y, x \cdot x\right)}\\

\mathbf{elif}\;x \leq 5.7 \cdot 10^{-160}:\\
\;\;\;\;\mathsf{fma}\left(t_0, 0.5, -1\right)\\

\mathbf{elif}\;x \leq 1.55 \cdot 10^{-29}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;x \leq 1.4 \cdot 10^{+24}:\\
\;\;\;\;-1 + \frac{t_0}{4}\\

\mathbf{elif}\;x \leq 6 \cdot 10^{+57}:\\
\;\;\;\;t_2\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}

Error

Target

Original31.4
Target31.1
Herbie13.1
\[\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 5 regimes
  2. if x < -5.9300000000000002e88 or 5.9999999999999999e57 < x

    1. Initial program 47.4

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

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

    if -5.9300000000000002e88 < x < -5.19999999999999991e-161

    1. Initial program 16.9

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

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

    if -5.19999999999999991e-161 < x < 5.70000000000000038e-160

    1. Initial program 29.6

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

      \[\leadsto \color{blue}{\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} + \left(-\frac{y}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{y \cdot 4}}\right)} \]
    3. Simplified29.5

      \[\leadsto \color{blue}{\frac{x \cdot x}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)} - \left(y \cdot 4\right) \cdot \frac{y}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}} \]
      Proof
      (-.f64 (/.f64 (*.f64 x x) (fma.f64 y (*.f64 y 4) (*.f64 x x))) (*.f64 (*.f64 y 4) (/.f64 y (fma.f64 y (*.f64 y 4) (*.f64 x x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (Rewrite=> fma-udef_binary64 (+.f64 (*.f64 y (*.f64 y 4)) (*.f64 x x)))) (*.f64 (*.f64 y 4) (/.f64 y (fma.f64 y (*.f64 y 4) (*.f64 x x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (Rewrite=> +-commutative_binary64 (+.f64 (*.f64 x x) (*.f64 y (*.f64 y 4))))) (*.f64 (*.f64 y 4) (/.f64 y (fma.f64 y (*.f64 y 4) (*.f64 x x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (Rewrite<= fma-udef_binary64 (fma.f64 x x (*.f64 y (*.f64 y 4))))) (*.f64 (*.f64 y 4) (/.f64 y (fma.f64 y (*.f64 y 4) (*.f64 x x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (fma.f64 x x (*.f64 y (*.f64 y 4)))) (*.f64 (*.f64 y 4) (/.f64 y (Rewrite=> fma-udef_binary64 (+.f64 (*.f64 y (*.f64 y 4)) (*.f64 x x)))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (fma.f64 x x (*.f64 y (*.f64 y 4)))) (*.f64 (*.f64 y 4) (/.f64 y (Rewrite=> +-commutative_binary64 (+.f64 (*.f64 x x) (*.f64 y (*.f64 y 4))))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (fma.f64 x x (*.f64 y (*.f64 y 4)))) (*.f64 (*.f64 y 4) (/.f64 y (Rewrite<= fma-udef_binary64 (fma.f64 x x (*.f64 y (*.f64 y 4))))))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (fma.f64 x x (*.f64 y (*.f64 y 4)))) (Rewrite<= *-commutative_binary64 (*.f64 (/.f64 y (fma.f64 x x (*.f64 y (*.f64 y 4)))) (*.f64 y 4)))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 x x) (fma.f64 x x (*.f64 y (*.f64 y 4)))) (Rewrite<= associate-/r/_binary64 (/.f64 y (/.f64 (fma.f64 x x (*.f64 y (*.f64 y 4))) (*.f64 y 4))))): 2 points increase in error, 15 points decrease in error
      (Rewrite<= unsub-neg_binary64 (+.f64 (/.f64 (*.f64 x x) (fma.f64 x x (*.f64 y (*.f64 y 4)))) (neg.f64 (/.f64 y (/.f64 (fma.f64 x x (*.f64 y (*.f64 y 4))) (*.f64 y 4)))))): 0 points increase in error, 0 points decrease in error
    4. Taylor expanded in x around 0 14.5

      \[\leadsto \color{blue}{0.5 \cdot \frac{{x}^{2}}{{y}^{2}} - 1} \]
    5. Simplified7.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{x}{y} \cdot \frac{x}{y}, 0.5, -1\right)} \]
      Proof
      (fma.f64 (*.f64 (/.f64 x y) (/.f64 x y)) 1/2 -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 x x) (*.f64 y y))) 1/2 -1): 56 points increase in error, 3 points decrease in error
      (fma.f64 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) (*.f64 y y)) 1/2 -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (pow.f64 x 2) (Rewrite<= unpow2_binary64 (pow.f64 y 2))) 1/2 -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (pow.f64 x 2) (pow.f64 y 2)) 1/2 (Rewrite<= metadata-eval (neg.f64 1))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-neg_binary64 (-.f64 (*.f64 (/.f64 (pow.f64 x 2) (pow.f64 y 2)) 1/2) 1)): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/2 (/.f64 (pow.f64 x 2) (pow.f64 y 2)))) 1): 0 points increase in error, 0 points decrease in error

    if 5.70000000000000038e-160 < x < 1.55000000000000013e-29 or 1.4000000000000001e24 < x < 5.9999999999999999e57

    1. Initial program 15.1

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

    if 1.55000000000000013e-29 < x < 1.4000000000000001e24

    1. Initial program 17.5

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

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

      \[\leadsto \frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\color{blue}{y \cdot \left(y \cdot 4\right)}} \]
      Proof
      (*.f64 y (*.f64 y 4)): 0 points increase in error, 0 points decrease in error
      (Rewrite=> associate-*r*_binary64 (*.f64 (*.f64 y y) 4)): 2 points increase in error, 3 points decrease in error
      (*.f64 (Rewrite<= unpow2_binary64 (pow.f64 y 2)) 4): 0 points increase in error, 0 points decrease in error
      (Rewrite<= *-commutative_binary64 (*.f64 4 (pow.f64 y 2))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr29.6

      \[\leadsto \color{blue}{{\left(\frac{x}{y \cdot 2}\right)}^{2} - 1} \]
    5. Applied egg-rr29.6

      \[\leadsto \color{blue}{\frac{\frac{x}{y} \cdot \frac{x}{y}}{4}} - 1 \]
  3. Recombined 5 regimes into one program.
  4. Final simplification13.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5.93 \cdot 10^{+88}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -5.2 \cdot 10^{-161}:\\ \;\;\;\;\frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{\mathsf{fma}\left(y \cdot 4, y, x \cdot x\right)}\\ \mathbf{elif}\;x \leq 5.7 \cdot 10^{-160}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y} \cdot \frac{x}{y}, 0.5, -1\right)\\ \mathbf{elif}\;x \leq 1.55 \cdot 10^{-29}:\\ \;\;\;\;\frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{elif}\;x \leq 1.4 \cdot 10^{+24}:\\ \;\;\;\;-1 + \frac{\frac{x}{y} \cdot \frac{x}{y}}{4}\\ \mathbf{elif}\;x \leq 6 \cdot 10^{+57}:\\ \;\;\;\;\frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternatives

Alternative 1
Error12.2
Cost14928
\[\begin{array}{l} t_0 := \mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)\\ t_1 := \frac{x \cdot x}{t_0} + \frac{y}{t_0} \cdot \left(y \cdot -4\right)\\ \mathbf{if}\;x \leq -1.35 \cdot 10^{+154}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -1.22 \cdot 10^{-159}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 2.6 \cdot 10^{-156}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y} \cdot \frac{x}{y}, 0.5, -1\right)\\ \mathbf{elif}\;x \leq 6 \cdot 10^{+57}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 2
Error13.1
Cost7372
\[\begin{array}{l} t_0 := \frac{x}{y} \cdot \frac{x}{y}\\ t_1 := \frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{if}\;x \leq -5.93 \cdot 10^{+88}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -7 \cdot 10^{-162}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 6.8 \cdot 10^{-159}:\\ \;\;\;\;\mathsf{fma}\left(t_0, 0.5, -1\right)\\ \mathbf{elif}\;x \leq 2.45 \cdot 10^{-27}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.4 \cdot 10^{+24}:\\ \;\;\;\;-1 + \frac{t_0}{4}\\ \mathbf{elif}\;x \leq 6 \cdot 10^{+57}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 3
Error13.1
Cost2008
\[\begin{array}{l} t_0 := -1 + \frac{\frac{x}{y} \cdot \frac{x}{y}}{4}\\ t_1 := \frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{if}\;x \leq -5.93 \cdot 10^{+88}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -3.9 \cdot 10^{-162}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 2.25 \cdot 10^{-159}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.25 \cdot 10^{-26}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.4 \cdot 10^{+24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 6 \cdot 10^{+57}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 4
Error16.7
Cost1232
\[\begin{array}{l} t_0 := -1 + \frac{\frac{x}{y} \cdot \frac{x}{y}}{4}\\ \mathbf{if}\;x \leq -6.2 \cdot 10^{-76}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 2 \cdot 10^{-73}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 9.5 \cdot 10^{-48}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 2.2 \cdot 10^{+25}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 5
Error16.7
Cost1232
\[\begin{array}{l} t_0 := -1 + \frac{\frac{x}{y} \cdot \frac{x}{y}}{4}\\ \mathbf{if}\;x \leq -5 \cdot 10^{-78}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 2.2 \cdot 10^{-73}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 6.8 \cdot 10^{-48}:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{elif}\;x \leq 2.45 \cdot 10^{+25}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 6
Error17.1
Cost592
\[\begin{array}{l} \mathbf{if}\;x \leq -8 \cdot 10^{-98}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 1.7 \cdot 10^{-73}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 1.9 \cdot 10^{-46}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 3.45 \cdot 10^{+25}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 7
Error32.1
Cost64
\[-1 \]

Error

Reproduce

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