?

Average Error: 31.8 → 12.6
Time: 6.6s
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 := y \cdot \left(y \cdot 4\right)\\ t_1 := x \cdot x - t_0\\ t_2 := \mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)\\ \mathbf{if}\;x \leq -1.6 \cdot 10^{+101}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq -1.12 \cdot 10^{-94}:\\ \;\;\;\;\frac{t_1}{\mathsf{fma}\left(y \cdot 4, y, x \cdot x\right)}\\ \mathbf{elif}\;x \leq 1.2 \cdot 10^{-79}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x \cdot \frac{x}{y}}{y}, -1\right)\\ \mathbf{elif}\;x \leq 8 \cdot 10^{+31}:\\ \;\;\;\;\frac{t_1}{x \cdot x + t_0}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \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))
        (t_2 (fma (* (/ y x) (/ y x)) -8.0 1.0)))
   (if (<= x -1.6e+101)
     t_2
     (if (<= x -1.12e-94)
       (/ t_1 (fma (* y 4.0) y (* x x)))
       (if (<= x 1.2e-79)
         (fma 0.5 (/ (* x (/ x y)) y) -1.0)
         (if (<= x 8e+31) (/ t_1 (+ (* x x) t_0)) t_2))))))
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;
	double t_2 = fma(((y / x) * (y / x)), -8.0, 1.0);
	double tmp;
	if (x <= -1.6e+101) {
		tmp = t_2;
	} else if (x <= -1.12e-94) {
		tmp = t_1 / fma((y * 4.0), y, (x * x));
	} else if (x <= 1.2e-79) {
		tmp = fma(0.5, ((x * (x / y)) / y), -1.0);
	} else if (x <= 8e+31) {
		tmp = t_1 / ((x * x) + t_0);
	} else {
		tmp = t_2;
	}
	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(x * x) - t_0)
	t_2 = fma(Float64(Float64(y / x) * Float64(y / x)), -8.0, 1.0)
	tmp = 0.0
	if (x <= -1.6e+101)
		tmp = t_2;
	elseif (x <= -1.12e-94)
		tmp = Float64(t_1 / fma(Float64(y * 4.0), y, Float64(x * x)));
	elseif (x <= 1.2e-79)
		tmp = fma(0.5, Float64(Float64(x * Float64(x / y)) / y), -1.0);
	elseif (x <= 8e+31)
		tmp = Float64(t_1 / Float64(Float64(x * x) + t_0));
	else
		tmp = t_2;
	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[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision] * -8.0 + 1.0), $MachinePrecision]}, If[LessEqual[x, -1.6e+101], t$95$2, If[LessEqual[x, -1.12e-94], N[(t$95$1 / N[(N[(y * 4.0), $MachinePrecision] * y + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.2e-79], N[(0.5 * N[(N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[x, 8e+31], N[(t$95$1 / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision], t$95$2]]]]]]]
\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 := x \cdot x - t_0\\
t_2 := \mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)\\
\mathbf{if}\;x \leq -1.6 \cdot 10^{+101}:\\
\;\;\;\;t_2\\

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

\mathbf{elif}\;x \leq 1.2 \cdot 10^{-79}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \frac{x \cdot \frac{x}{y}}{y}, -1\right)\\

\mathbf{elif}\;x \leq 8 \cdot 10^{+31}:\\
\;\;\;\;\frac{t_1}{x \cdot x + t_0}\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}

Error?

Target

Original31.8
Target31.5
Herbie12.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 4 regimes
  2. if x < -1.60000000000000003e101 or 7.9999999999999997e31 < x

    1. Initial program 47.7

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

      \[\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)}} \]
    3. Taylor expanded in x around inf 18.2

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

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

      [Start]18.2

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

      associate--l+ [=>]18.2

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

      +-commutative [=>]18.2

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

      distribute-rgt-out-- [=>]18.2

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

      metadata-eval [=>]18.2

      \[ \frac{{y}^{2}}{{x}^{2}} \cdot \color{blue}{-8} + 1 \]

      fma-def [=>]18.2

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

      unpow2 [=>]18.2

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

      unpow2 [=>]18.2

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

      times-frac [=>]12.7

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

    if -1.60000000000000003e101 < x < -1.12e-94

    1. Initial program 14.8

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

      \[\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 -1.12e-94 < x < 1.20000000000000003e-79

    1. Initial program 25.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 0 15.7

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

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

      [Start]15.7

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

      fma-neg [=>]15.7

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

      unpow2 [=>]15.7

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

      unpow2 [=>]15.7

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

      associate-/r* [=>]10.9

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

      metadata-eval [=>]10.9

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

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

    if 1.20000000000000003e-79 < x < 7.9999999999999997e31

    1. Initial program 16.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.6 \cdot 10^{+101}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)\\ \mathbf{elif}\;x \leq -1.12 \cdot 10^{-94}:\\ \;\;\;\;\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 1.2 \cdot 10^{-79}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x \cdot \frac{x}{y}}{y}, -1\right)\\ \mathbf{elif}\;x \leq 8 \cdot 10^{+31}:\\ \;\;\;\;\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
Error12.5
Cost7504
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)\\ t_2 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{if}\;x \leq -2.2 \cdot 10^{+101}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-106}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq 1.05 \cdot 10^{-79}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x \cdot \frac{x}{y}}{y}, -1\right)\\ \mathbf{elif}\;x \leq 8 \cdot 10^{+31}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error12.7
Cost7372
\[\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 \leq -2.2 \cdot 10^{+101}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -1.8 \cdot 10^{-112}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 9.8 \cdot 10^{-80}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x \cdot \frac{x}{y}}{y}, -1\right)\\ \mathbf{elif}\;x \leq 8 \cdot 10^{+31}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 3
Error12.9
Cost1744
\[\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 \leq -2.2 \cdot 10^{+101}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -1.36 \cdot 10^{-95}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 10^{-79}:\\ \;\;\;\;-1 + \frac{x}{y} \cdot \frac{x}{y \cdot 4}\\ \mathbf{elif}\;x \leq 8 \cdot 10^{+31}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 4
Error18.3
Cost1232
\[\begin{array}{l} t_0 := -1 + \frac{x}{y} \cdot \frac{x}{y \cdot 4}\\ \mathbf{if}\;y \leq -2 \cdot 10^{-65}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 3.5 \cdot 10^{-138}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 3 \cdot 10^{-23}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 1.08 \cdot 10^{+68}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error18.5
Cost592
\[\begin{array}{l} \mathbf{if}\;y \leq -5 \cdot 10^{-65}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 3.5 \cdot 10^{-138}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 10^{-24}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 1.08 \cdot 10^{+68}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 6
Error31.5
Cost64
\[-1 \]

Error

Reproduce?

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