Average Error: 32.1 → 5.6
Time: 5.2s
Precision: binary64
Cost: 14404
\[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
\[\begin{array}{l} \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+299}:\\ \;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\mathsf{fma}\left(0.25, \frac{x \cdot x}{y}, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)\\ \end{array} \]
(FPCore (x y)
 :precision binary64
 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
 :precision binary64
 (if (<= (* x x) 5e+299)
   (- (/ (* x x) (fma x x (* y (* y 4.0)))) (/ y (fma 0.25 (/ (* x x) y) y)))
   (fma (* (/ y x) (/ y x)) -8.0 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 tmp;
	if ((x * x) <= 5e+299) {
		tmp = ((x * x) / fma(x, x, (y * (y * 4.0)))) - (y / fma(0.25, ((x * x) / y), y));
	} else {
		tmp = fma(((y / x) * (y / x)), -8.0, 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)
	tmp = 0.0
	if (Float64(x * x) <= 5e+299)
		tmp = Float64(Float64(Float64(x * x) / fma(x, x, Float64(y * Float64(y * 4.0)))) - Float64(y / fma(0.25, Float64(Float64(x * x) / y), y)));
	else
		tmp = fma(Float64(Float64(y / x) * Float64(y / x)), -8.0, 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_] := If[LessEqual[N[(x * x), $MachinePrecision], 5e+299], N[(N[(N[(x * x), $MachinePrecision] / N[(x * x + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(y / N[(0.25 * N[(N[(x * x), $MachinePrecision] / y), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision] * -8.0 + 1.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}
\mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+299}:\\
\;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{\mathsf{fma}\left(0.25, \frac{x \cdot x}{y}, y\right)}\\

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


\end{array}

Error

Target

Original32.1
Target31.8
Herbie5.6
\[\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.0000000000000003e299

    1. Initial program 21.2

      \[\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.8

      \[\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 4.8

      \[\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. Simplified4.8

      \[\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 \cdot x}{y}, y\right)}} \]
      Proof
      (fma.f64 1/4 (/.f64 (*.f64 x x) y) y): 0 points increase in error, 0 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

    if 5.0000000000000003e299 < (*.f64 x x)

    1. Initial program 62.7

      \[\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 16.9

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)} \]
      Proof
      (fma.f64 (*.f64 (/.f64 y x) (/.f64 y x)) -8 1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 y y) (*.f64 x x))) -8 1): 38 points increase in error, 6 points decrease in error
      (fma.f64 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 y 2)) (*.f64 x x)) -8 1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (pow.f64 y 2) (Rewrite<= unpow2_binary64 (pow.f64 x 2))) -8 1): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (/.f64 (pow.f64 y 2) (pow.f64 x 2)) -8) 1)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= *-commutative_binary64 (*.f64 -8 (/.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 -8 (/.f64 (pow.f64 y 2) (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
      (+.f64 1 (Rewrite=> *-commutative_binary64 (*.f64 (/.f64 (pow.f64 y 2) (pow.f64 x 2)) -8))): 0 points increase in error, 0 points decrease in error
      (+.f64 1 (*.f64 (/.f64 (pow.f64 y 2) (pow.f64 x 2)) (Rewrite<= metadata-eval (-.f64 -4 4)))): 0 points increase in error, 0 points decrease in error
      (+.f64 1 (Rewrite<= distribute-rgt-out--_binary64 (-.f64 (*.f64 -4 (/.f64 (pow.f64 y 2) (pow.f64 x 2))) (*.f64 4 (/.f64 (pow.f64 y 2) (pow.f64 x 2)))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate--l+_binary64 (-.f64 (+.f64 1 (*.f64 -4 (/.f64 (pow.f64 y 2) (pow.f64 x 2)))) (*.f64 4 (/.f64 (pow.f64 y 2) (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification5.6

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

Alternatives

Alternative 1
Error13.3
Cost7504
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)\\ t_1 := \frac{x \cdot \frac{x}{y}}{y \cdot 2} + -1\\ t_2 := \frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{if}\;y \leq -3.2 \cdot 10^{+121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -2.6 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -5 \cdot 10^{-144}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq 4.7 \cdot 10^{-123}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 8.8 \cdot 10^{+66}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error13.4
Cost1876
\[\begin{array}{l} t_0 := \frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ t_1 := \frac{x \cdot \frac{x}{y}}{y \cdot 2} + -1\\ \mathbf{if}\;y \leq -3.2 \cdot 10^{+121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -2.6 \cdot 10^{+57}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq -5.8 \cdot 10^{-143}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 3.9 \cdot 10^{-122}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 6.8 \cdot 10^{+66}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error16.4
Cost1232
\[\begin{array}{l} t_0 := \frac{x \cdot \frac{x}{y}}{y \cdot 2} + -1\\ \mathbf{if}\;y \leq -3.2 \cdot 10^{+121}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -2.3 \cdot 10^{+94}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq -1.72 \cdot 10^{-25}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 6.8 \cdot 10^{+52}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error16.8
Cost592
\[\begin{array}{l} \mathbf{if}\;y \leq -4.8 \cdot 10^{+121}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -2 \cdot 10^{+57}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq -2 \cdot 10^{-27}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 4.3 \cdot 10^{+32}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 5
Error31.9
Cost64
\[-1 \]

Error

Reproduce

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