Average Error: 31.6 → 13.2
Time: 4.5s
Precision: binary64
Cost: 8016
\[\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 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-324}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-192}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{-93}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+251}:\\ \;\;\;\;t_1\\ \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
 (let* ((t_0 (* y (* y 4.0))) (t_1 (/ (- (* x x) t_0) (+ (* x x) t_0))))
   (if (<= (* x x) 5e-324)
     -1.0
     (if (<= (* x x) 2e-192)
       t_1
       (if (<= (* x x) 5e-93)
         -1.0
         (if (<= (* x x) 5e+251) t_1 (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 t_0 = y * (y * 4.0);
	double t_1 = ((x * x) - t_0) / ((x * x) + t_0);
	double tmp;
	if ((x * x) <= 5e-324) {
		tmp = -1.0;
	} else if ((x * x) <= 2e-192) {
		tmp = t_1;
	} else if ((x * x) <= 5e-93) {
		tmp = -1.0;
	} else if ((x * x) <= 5e+251) {
		tmp = t_1;
	} 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)
	t_0 = Float64(y * Float64(y * 4.0))
	t_1 = Float64(Float64(Float64(x * x) - t_0) / Float64(Float64(x * x) + t_0))
	tmp = 0.0
	if (Float64(x * x) <= 5e-324)
		tmp = -1.0;
	elseif (Float64(x * x) <= 2e-192)
		tmp = t_1;
	elseif (Float64(x * x) <= 5e-93)
		tmp = -1.0;
	elseif (Float64(x * x) <= 5e+251)
		tmp = t_1;
	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_] := Block[{t$95$0 = N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 5e-324], -1.0, If[LessEqual[N[(x * x), $MachinePrecision], 2e-192], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 5e-93], -1.0, If[LessEqual[N[(x * x), $MachinePrecision], 5e+251], t$95$1, 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}
t_0 := y \cdot \left(y \cdot 4\right)\\
t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\
\mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-324}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-192}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{-93}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+251}:\\
\;\;\;\;t_1\\

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


\end{array}

Error

Target

Original31.6
Target31.3
Herbie13.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 3 regimes
  2. if (*.f64 x x) < 4.94066e-324 or 2.0000000000000002e-192 < (*.f64 x x) < 4.99999999999999994e-93

    1. Initial program 27.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 13.6

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

    if 4.94066e-324 < (*.f64 x x) < 2.0000000000000002e-192 or 4.99999999999999994e-93 < (*.f64 x x) < 5.0000000000000005e251

    1. Initial program 15.9

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

    if 5.0000000000000005e251 < (*.f64 x x)

    1. Initial program 55.8

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

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

      \[\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): 58 points increase in error, 7 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))))): 1 points increase in error, 1 points decrease in error
  3. Recombined 3 regimes into one program.
  4. Final simplification13.2

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

Alternatives

Alternative 1
Error13.2
Cost2256
\[\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}\\ \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-324}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-192}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{-93}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+251}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1 + \frac{y \cdot -4}{\frac{x}{\frac{y}{x}}}\\ \end{array} \]
Alternative 2
Error16.7
Cost1232
\[\begin{array}{l} \mathbf{if}\;y \leq -1.1 \cdot 10^{-13}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 3 \cdot 10^{-98}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 7.4 \cdot 10^{-63}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 1.26 \cdot 10^{+62}:\\ \;\;\;\;1 + -4 \cdot \frac{y \cdot y}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 3
Error16.6
Cost1232
\[\begin{array}{l} \mathbf{if}\;y \leq -6.8 \cdot 10^{+39}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 2.75 \cdot 10^{-98}:\\ \;\;\;\;1 + \frac{y \cdot -4}{\frac{x}{\frac{y}{x}}}\\ \mathbf{elif}\;y \leq 4.2 \cdot 10^{-64}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 6.4 \cdot 10^{+61}:\\ \;\;\;\;1 + -4 \cdot \frac{y \cdot y}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 4
Error16.6
Cost592
\[\begin{array}{l} \mathbf{if}\;y \leq -6 \cdot 10^{-14}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 2.55 \cdot 10^{-98}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 10^{-64}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 9 \cdot 10^{+61}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 5
Error32.7
Cost64
\[-1 \]

Error

Reproduce

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