Average Error: 31.7 → 4.0
Time: 10.0s
Precision: binary64
Cost: 8392
\[\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 0:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{\frac{y}{x}}, \frac{0.5}{y}, -1\right)\\ \mathbf{elif}\;x \cdot x \leq 10^{+286}:\\ \;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right) + 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
 (if (<= (* x x) 0.0)
   (fma (/ x (/ y x)) (/ 0.5 y) -1.0)
   (if (<= (* x x) 1e+286)
     (-
      (/ (* x x) (fma x x (* y (* y 4.0))))
      (/ y (+ y (* 0.25 (* x (/ x y))))))
     (+ (* y (* -8.0 (/ (/ y x) x))) 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) <= 0.0) {
		tmp = fma((x / (y / x)), (0.5 / y), -1.0);
	} else if ((x * x) <= 1e+286) {
		tmp = ((x * x) / fma(x, x, (y * (y * 4.0)))) - (y / (y + (0.25 * (x * (x / y)))));
	} else {
		tmp = (y * (-8.0 * ((y / x) / x))) + 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) <= 0.0)
		tmp = fma(Float64(x / Float64(y / x)), Float64(0.5 / y), -1.0);
	elseif (Float64(x * x) <= 1e+286)
		tmp = Float64(Float64(Float64(x * x) / fma(x, x, Float64(y * Float64(y * 4.0)))) - Float64(y / Float64(y + Float64(0.25 * Float64(x * Float64(x / y))))));
	else
		tmp = Float64(Float64(y * Float64(-8.0 * Float64(Float64(y / x) / x))) + 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], 0.0], N[(N[(x / N[(y / x), $MachinePrecision]), $MachinePrecision] * N[(0.5 / y), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 1e+286], N[(N[(N[(x * x), $MachinePrecision] / N[(x * x + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(y / N[(y + N[(0.25 * N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(y * N[(-8.0 * N[(N[(y / x), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 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 0:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{\frac{y}{x}}, \frac{0.5}{y}, -1\right)\\

\mathbf{elif}\;x \cdot x \leq 10^{+286}:\\
\;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)} - \frac{y}{y + 0.25 \cdot \left(x \cdot \frac{x}{y}\right)}\\

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


\end{array}

Error

Target

Original31.7
Target31.4
Herbie4.0
\[\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 3 regimes
  2. if (*.f64 x x) < 0.0

    1. Initial program 31.0

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{x}{\frac{y}{x}}, \frac{0.5}{y}, -1\right)} \]
      Proof
      (fma.f64 (/.f64 x (/.f64 y x)) (/.f64 1/2 y) -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 x x) y)) (/.f64 1/2 y) -1): 29 points increase in error, 5 points decrease in error
      (fma.f64 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) y) (/.f64 1/2 y) -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 (pow.f64 x 2) y) (/.f64 1/2 y) (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) y) (/.f64 1/2 y)) 1)): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 (pow.f64 x 2) 1/2) (*.f64 y y))) 1): 26 points increase in error, 5 points decrease in error
      (-.f64 (/.f64 (*.f64 (pow.f64 x 2) 1/2) (Rewrite<= unpow2_binary64 (pow.f64 y 2))) 1): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= associate-*l/_binary64 (*.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 0.0 < (*.f64 x x) < 1.00000000000000003e286

    1. Initial program 16.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-rr15.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 0.0

      \[\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. Simplified0.0

      \[\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): 10 points increase in error, 5 points decrease in error
      (fma.f64 1/4 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 x x) y)) y): 23 points increase in error, 14 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-rr0.0

      \[\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}} \]

    if 1.00000000000000003e286 < (*.f64 x x)

    1. Initial program 60.9

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{-8}{x}, \frac{y}{\frac{x}{y}}, 1\right)} \]
      Proof
      (fma.f64 (/.f64 -8 x) (/.f64 y (/.f64 x y)) 1): 0 points increase in error, 0 points decrease in error
      (fma.f64 (/.f64 -8 x) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 y y) x)) 1): 27 points increase in error, 7 points decrease in error
      (fma.f64 (/.f64 -8 x) (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 y 2)) x) 1): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (/.f64 -8 x) (/.f64 (pow.f64 y 2) x)) 1)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 -8 (pow.f64 y 2)) (*.f64 x x))) 1): 27 points increase in error, 3 points decrease in error
      (+.f64 (/.f64 (*.f64 -8 (pow.f64 y 2)) (Rewrite<= unpow2_binary64 (pow.f64 x 2))) 1): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= associate-*r/_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))))): 3 points increase in error, 1 points decrease in error
    4. Applied egg-rr8.0

      \[\leadsto \color{blue}{-8 \cdot {\left(\frac{x}{y}\right)}^{-2} + 1} \]
    5. Taylor expanded in x around 0 15.5

      \[\leadsto \color{blue}{-8 \cdot \frac{{y}^{2}}{{x}^{2}}} + 1 \]
    6. Simplified8.0

      \[\leadsto \color{blue}{y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right)} + 1 \]
      Proof
      (*.f64 y (*.f64 -8 (/.f64 (/.f64 y x) x))): 0 points increase in error, 0 points decrease in error
      (*.f64 y (*.f64 -8 (Rewrite<= associate-/r*_binary64 (/.f64 y (*.f64 x x))))): 31 points increase in error, 22 points decrease in error
      (*.f64 y (*.f64 -8 (/.f64 y (Rewrite<= unpow2_binary64 (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
      (*.f64 y (Rewrite=> associate-*r/_binary64 (/.f64 (*.f64 -8 y) (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= *-commutative_binary64 (*.f64 (/.f64 (*.f64 -8 y) (pow.f64 x 2)) y)): 0 points increase in error, 0 points decrease in error
      (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 (*.f64 -8 y) y) (pow.f64 x 2))): 30 points increase in error, 17 points decrease in error
      (/.f64 (Rewrite<= associate-*r*_binary64 (*.f64 -8 (*.f64 y y))) (pow.f64 x 2)): 0 points increase in error, 0 points decrease in error
      (/.f64 (*.f64 -8 (Rewrite<= unpow2_binary64 (pow.f64 y 2))) (pow.f64 x 2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*r/_binary64 (*.f64 -8 (/.f64 (pow.f64 y 2) (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
  3. Recombined 3 regimes into one program.
  4. Final simplification4.0

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

Alternatives

Alternative 1
Error14.5
Cost8076
\[\begin{array}{l} t_0 := 1 + -8 \cdot {\left(\frac{x}{y}\right)}^{-2}\\ t_1 := y \cdot \left(y \cdot 4\right)\\ t_2 := \frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + t_1}\\ \mathbf{if}\;t_1 \leq 0:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-271}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-14}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 10000000000000:\\ \;\;\;\;1\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+106}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 10^{+175}:\\ \;\;\;\;1\\ \mathbf{elif}\;t_1 \leq 10^{+217}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;-1 + x \cdot \left(\frac{0.5}{y} \cdot \frac{x}{y}\right)\\ \end{array} \]
Alternative 2
Error14.6
Cost4320
\[\begin{array}{l} t_0 := y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right) + 1\\ t_1 := y \cdot \left(y \cdot 4\right)\\ t_2 := \frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + t_1}\\ \mathbf{if}\;t_1 \leq 0:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-271}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{-14}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 10000000000000:\\ \;\;\;\;1\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+106}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 10^{+175}:\\ \;\;\;\;1\\ \mathbf{elif}\;t_1 \leq 10^{+217}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;-1 + x \cdot \left(\frac{0.5}{y} \cdot \frac{x}{y}\right)\\ \end{array} \]
Alternative 3
Error16.7
Cost1868
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right) + 1\\ \mathbf{if}\;t_0 \leq 2 \cdot 10^{+73}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+106}:\\ \;\;\;\;-1\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+177}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;-1 + x \cdot \left(\frac{0.5}{y} \cdot \frac{x}{y}\right)\\ \end{array} \]
Alternative 4
Error16.8
Cost968
\[\begin{array}{l} \mathbf{if}\;y \leq -5.933369789884985 \cdot 10^{+94}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 3.2180008266265876 \cdot 10^{+22}:\\ \;\;\;\;y \cdot \left(-8 \cdot \frac{\frac{y}{x}}{x}\right) + 1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 5
Error17.6
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -5.933369789884985 \cdot 10^{+94}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 6.135310894040638 \cdot 10^{-64}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 6
Error31.7
Cost64
\[-1 \]

Error

Reproduce

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