Average Error: 31.0 → 14.0
Time: 16.9s
Precision: binary64
Cost: 14156
\[\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 := \mathsf{fma}\left(-8, \frac{y}{x} \cdot \frac{y}{x}, 1\right)\\ \mathbf{if}\;x \leq -1.8 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 7.8 \cdot 10^{-107}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;x \leq 5.5 \cdot 10^{+116}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \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 (fma -8.0 (* (/ y x) (/ y x)) 1.0)))
   (if (<= x -1.8e+41)
     t_0
     (if (<= x 7.8e-107)
       (fma 0.5 (* (/ x y) (/ x y)) (- 1.0))
       (if (<= x 5.5e+116)
         (/ (fma x x (* (* y y) -4.0)) (fma x x (* (* y 4.0) y)))
         t_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 = fma(-8.0, ((y / x) * (y / x)), 1.0);
	double tmp;
	if (x <= -1.8e+41) {
		tmp = t_0;
	} else if (x <= 7.8e-107) {
		tmp = fma(0.5, ((x / y) * (x / y)), -1.0);
	} else if (x <= 5.5e+116) {
		tmp = fma(x, x, ((y * y) * -4.0)) / fma(x, x, ((y * 4.0) * y));
	} else {
		tmp = t_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 = fma(-8.0, Float64(Float64(y / x) * Float64(y / x)), 1.0)
	tmp = 0.0
	if (x <= -1.8e+41)
		tmp = t_0;
	elseif (x <= 7.8e-107)
		tmp = fma(0.5, Float64(Float64(x / y) * Float64(x / y)), Float64(-1.0));
	elseif (x <= 5.5e+116)
		tmp = Float64(fma(x, x, Float64(Float64(y * y) * -4.0)) / fma(x, x, Float64(Float64(y * 4.0) * y)));
	else
		tmp = t_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[(-8.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]}, If[LessEqual[x, -1.8e+41], t$95$0, If[LessEqual[x, 7.8e-107], N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + (-1.0)), $MachinePrecision], If[LessEqual[x, 5.5e+116], N[(N[(x * x + N[(N[(y * y), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision] / N[(x * x + N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$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 := \mathsf{fma}\left(-8, \frac{y}{x} \cdot \frac{y}{x}, 1\right)\\
\mathbf{if}\;x \leq -1.8 \cdot 10^{+41}:\\
\;\;\;\;t_0\\

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

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

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error

Target

Original31.0
Target30.7
Herbie14.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 x < -1.80000000000000013e41 or 5.50000000000000035e116 < x

    1. Initial program 48.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 y around 0 18.1

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

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

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

    if -1.80000000000000013e41 < x < 7.8000000000000002e-107

    1. Initial program 23.6

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

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

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

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

    if 7.8000000000000002e-107 < x < 5.50000000000000035e116

    1. Initial program 15.6

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}} \]
      Proof
  3. Recombined 3 regimes into one program.

Alternatives

Alternative 1
Error12.2
Cost7496
\[\begin{array}{l} t_0 := \left(y \cdot 4\right) \cdot y\\ \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-260}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+224}:\\ \;\;\;\;\frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-8, \frac{y}{x} \cdot \frac{y}{x}, 1\right)\\ \end{array} \]
Alternative 2
Error14.0
Cost7372
\[\begin{array}{l} t_0 := \mathsf{fma}\left(-8, \frac{y}{x} \cdot \frac{y}{x}, 1\right)\\ t_1 := \left(y \cdot 4\right) \cdot y\\ \mathbf{if}\;x \leq -1.35 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.2 \cdot 10^{-109}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;x \leq 4.5 \cdot 10^{+120}:\\ \;\;\;\;\frac{x \cdot x - t_1}{x \cdot x + t_1}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error12.4
Cost1736
\[\begin{array}{l} t_0 := \left(y \cdot 4\right) \cdot y\\ \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-260}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+224}:\\ \;\;\;\;\frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 4
Error16.9
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq -1.3 \cdot 10^{+39}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 6.1 \cdot 10^{-89}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 5
Error31.9
Cost64
\[-1 \]

Error

Reproduce

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