?

Average Error: 31.9 → 15.8
Time: 8.3s
Precision: binary64
Cost: 7236

?

\[\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 2 \cdot 10^{+132}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \frac{y}{x} \cdot \left(y \cdot \frac{8}{x}\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) 2e+132)
   (fma 0.5 (* (/ x y) (/ x y)) -1.0)
   (- 1.0 (* (/ y x) (* y (/ 8.0 x))))))
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) <= 2e+132) {
		tmp = fma(0.5, ((x / y) * (x / y)), -1.0);
	} else {
		tmp = 1.0 - ((y / x) * (y * (8.0 / x)));
	}
	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) <= 2e+132)
		tmp = fma(0.5, Float64(Float64(x / y) * Float64(x / y)), -1.0);
	else
		tmp = Float64(1.0 - Float64(Float64(y / x) * Float64(y * Float64(8.0 / x))));
	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], 2e+132], N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(1.0 - N[(N[(y / x), $MachinePrecision] * N[(y * N[(8.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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 2 \cdot 10^{+132}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\

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


\end{array}

Error?

Target

Original31.9
Target31.6
Herbie15.8
\[\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) < 1.99999999999999998e132

    1. Initial program 22.1

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

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

      [Start]22.1

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

      fma-neg [=>]22.1

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

      *-commutative [=>]22.1

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

      associate-*r* [=>]22.2

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

      distribute-rgt-neg-in [=>]22.2

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

      metadata-eval [=>]22.2

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

      fma-def [=>]22.2

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

      *-commutative [=>]22.2

      \[ \frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}{\mathsf{fma}\left(x, x, \color{blue}{y \cdot \left(y \cdot 4\right)}\right)} \]
    3. Taylor expanded in x around 0 21.7

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

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

      [Start]21.7

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

      fma-neg [=>]21.7

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

      unpow2 [=>]21.7

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

      unpow2 [=>]21.7

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

      times-frac [=>]18.4

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

      metadata-eval [=>]18.4

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

    if 1.99999999999999998e132 < (*.f64 x x)

    1. Initial program 47.1

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

      \[\leadsto \color{blue}{\frac{1}{y \cdot \left(y \cdot -4\right) - x \cdot x} \cdot \left(-\mathsf{fma}\left(x, x, y \cdot \left(y \cdot -4\right)\right)\right)} \]
    3. Taylor expanded in x around inf 17.1

      \[\leadsto \color{blue}{1 + -1 \cdot \frac{4 \cdot {y}^{2} - -4 \cdot {y}^{2}}{{x}^{2}}} \]
    4. Simplified17.0

      \[\leadsto \color{blue}{1 - \frac{y \cdot y}{\frac{x \cdot x}{8}}} \]
      Proof

      [Start]17.1

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

      mul-1-neg [=>]17.1

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

      unsub-neg [=>]17.1

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

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

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

      metadata-eval [=>]17.1

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

      unpow2 [=>]17.1

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

      associate-/l* [=>]17.0

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

      unpow2 [=>]17.0

      \[ 1 - \frac{\color{blue}{y \cdot y}}{\frac{x \cdot x}{8}} \]
    5. Applied egg-rr11.8

      \[\leadsto 1 - \color{blue}{\frac{y}{x} \cdot \left(y \cdot \frac{8}{x}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification15.8

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

Alternatives

Alternative 1
Error16.0
Cost969
\[\begin{array}{l} \mathbf{if}\;x \leq -2.5 \cdot 10^{+61} \lor \neg \left(x \leq 6 \cdot 10^{+59}\right):\\ \;\;\;\;1 - \frac{y}{x} \cdot \left(y \cdot \frac{8}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 2
Error16.2
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq -2.4 \cdot 10^{+56}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 10^{+61}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 3
Error31.7
Cost64
\[-1 \]

Error

Reproduce?

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