?

Average Error: 31.9 → 13.5
Time: 4.7s
Precision: binary64
Cost: 7768

?

\[\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 \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ t_1 := \mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{if}\;x \leq -9.2 \cdot 10^{+142}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -7.8 \cdot 10^{+109}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -2.2 \cdot 10^{+51}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -8 \cdot 10^{+41}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -1.1 \cdot 10^{-129}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.7 \cdot 10^{-45}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 2.45 \cdot 10^{+117}:\\ \;\;\;\;t_0\\ \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 x) (* y (* y -4.0))) (+ (* x x) (* y (* y 4.0)))))
        (t_1 (fma 0.5 (* (/ x y) (/ x y)) -1.0)))
   (if (<= x -9.2e+142)
     1.0
     (if (<= x -7.8e+109)
       t_1
       (if (<= x -2.2e+51)
         t_0
         (if (<= x -8e+41)
           t_1
           (if (<= x -1.1e-129)
             t_0
             (if (<= x 3.7e-45) t_1 (if (<= x 2.45e+117) t_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 = ((x * x) + (y * (y * -4.0))) / ((x * x) + (y * (y * 4.0)));
	double t_1 = fma(0.5, ((x / y) * (x / y)), -1.0);
	double tmp;
	if (x <= -9.2e+142) {
		tmp = 1.0;
	} else if (x <= -7.8e+109) {
		tmp = t_1;
	} else if (x <= -2.2e+51) {
		tmp = t_0;
	} else if (x <= -8e+41) {
		tmp = t_1;
	} else if (x <= -1.1e-129) {
		tmp = t_0;
	} else if (x <= 3.7e-45) {
		tmp = t_1;
	} else if (x <= 2.45e+117) {
		tmp = t_0;
	} 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(Float64(x * x) + Float64(y * Float64(y * -4.0))) / Float64(Float64(x * x) + Float64(y * Float64(y * 4.0))))
	t_1 = fma(0.5, Float64(Float64(x / y) * Float64(x / y)), -1.0)
	tmp = 0.0
	if (x <= -9.2e+142)
		tmp = 1.0;
	elseif (x <= -7.8e+109)
		tmp = t_1;
	elseif (x <= -2.2e+51)
		tmp = t_0;
	elseif (x <= -8e+41)
		tmp = t_1;
	elseif (x <= -1.1e-129)
		tmp = t_0;
	elseif (x <= 3.7e-45)
		tmp = t_1;
	elseif (x <= 2.45e+117)
		tmp = t_0;
	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[(N[(x * x), $MachinePrecision] + N[(y * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[x, -9.2e+142], 1.0, If[LessEqual[x, -7.8e+109], t$95$1, If[LessEqual[x, -2.2e+51], t$95$0, If[LessEqual[x, -8e+41], t$95$1, If[LessEqual[x, -1.1e-129], t$95$0, If[LessEqual[x, 3.7e-45], t$95$1, If[LessEqual[x, 2.45e+117], t$95$0, 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 \cdot x + y \cdot \left(y \cdot -4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\
t_1 := \mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\
\mathbf{if}\;x \leq -9.2 \cdot 10^{+142}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq -7.8 \cdot 10^{+109}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq -2.2 \cdot 10^{+51}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -8 \cdot 10^{+41}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq -1.1 \cdot 10^{-129}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 3.7 \cdot 10^{-45}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq 2.45 \cdot 10^{+117}:\\
\;\;\;\;t_0\\

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


\end{array}

Error?

Target

Original31.9
Target31.5
Herbie13.5
\[\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 x < -9.20000000000000009e142 or 2.45e117 < x

    1. Initial program 57.2

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

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

    if -9.20000000000000009e142 < x < -7.80000000000000032e109 or -2.19999999999999992e51 < x < -8.00000000000000005e41 or -1.10000000000000001e-129 < x < 3.7e-45

    1. Initial program 25.3

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

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

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

      [Start]18.8

      \[ 0.5 \cdot \frac{{x}^{2}}{{y}^{2}} - 1 \]

      fma-neg [=>]18.8

      \[ \color{blue}{\mathsf{fma}\left(0.5, \frac{{x}^{2}}{{y}^{2}}, -1\right)} \]

      unpow2 [=>]18.8

      \[ \mathsf{fma}\left(0.5, \frac{\color{blue}{x \cdot x}}{{y}^{2}}, -1\right) \]

      unpow2 [=>]18.8

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

      metadata-eval [=>]18.8

      \[ \mathsf{fma}\left(0.5, \frac{x \cdot x}{y \cdot y}, \color{blue}{-1}\right) \]
    4. Applied egg-rr13.9

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

    if -7.80000000000000032e109 < x < -2.19999999999999992e51 or -8.00000000000000005e41 < x < -1.10000000000000001e-129 or 3.7e-45 < x < 2.45e117

    1. Initial program 16.3

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -9.2 \cdot 10^{+142}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -7.8 \cdot 10^{+109}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;x \leq -2.2 \cdot 10^{+51}:\\ \;\;\;\;\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 -8 \cdot 10^{+41}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;x \leq -1.1 \cdot 10^{-129}:\\ \;\;\;\;\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 3.7 \cdot 10^{-45}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;x \leq 2.45 \cdot 10^{+117}:\\ \;\;\;\;\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
Error14.7
Cost1744
\[\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)}\\ \mathbf{if}\;y \leq -3.5 \cdot 10^{+97}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -7.6 \cdot 10^{-129}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.85 \cdot 10^{-67}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.66 \cdot 10^{-18}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 3.1 \cdot 10^{+85}:\\ \;\;\;\;1 + \frac{-4 \cdot \left(y \cdot y\right)}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 2
Error16.6
Cost1492
\[\begin{array}{l} t_0 := \frac{x \cdot x}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{if}\;x \leq -4.6 \cdot 10^{+51}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -1.72 \cdot 10^{+41}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq -1600:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -3.9 \cdot 10^{-27}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq -4.7 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 2 \cdot 10^{+28}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 3
Error16.7
Cost1100
\[\begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{+69}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -4.9 \cdot 10^{+41}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq -170000:\\ \;\;\;\;1 + \frac{-4 \cdot \left(y \cdot y\right)}{x \cdot x}\\ \mathbf{elif}\;x \leq -4 \cdot 10^{-27}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq -3.5 \cdot 10^{-74}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 1.15 \cdot 10^{+28}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 4
Error16.6
Cost856
\[\begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+53}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -2 \cdot 10^{+41}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq -8200:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -3.9 \cdot 10^{-27}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq -5.8 \cdot 10^{-74}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 2 \cdot 10^{+28}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 5
Error32.2
Cost64
\[-1 \]

Error

Reproduce?

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