Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3

?

Percentage Accurate: 50.8% → 79.9%
Time: 7.7s
Precision: binary64
Cost: 2256

?

\[\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}\\ t_2 := \frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\ \mathbf{if}\;x \cdot x \leq 0:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-217}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-140}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+82}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{y}{x}}{\frac{x}{y}} \cdot -8 + 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 (* y (* y 4.0)))
        (t_1 (/ (- (* x x) t_0) (+ (* x x) t_0)))
        (t_2 (+ (* (/ (/ x y) (/ y x)) 0.5) -1.0)))
   (if (<= (* x x) 0.0)
     t_2
     (if (<= (* x x) 2e-217)
       t_1
       (if (<= (* x x) 2e-140)
         t_2
         (if (<= (* x x) 5e+82) t_1 (+ (* (/ (/ y x) (/ x y)) -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 t_2 = (((x / y) / (y / x)) * 0.5) + -1.0;
	double tmp;
	if ((x * x) <= 0.0) {
		tmp = t_2;
	} else if ((x * x) <= 2e-217) {
		tmp = t_1;
	} else if ((x * x) <= 2e-140) {
		tmp = t_2;
	} else if ((x * x) <= 5e+82) {
		tmp = t_1;
	} else {
		tmp = (((y / x) / (x / y)) * -8.0) + 1.0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = ((x * x) - ((y * 4.0d0) * y)) / ((x * x) + ((y * 4.0d0) * y))
end function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = y * (y * 4.0d0)
    t_1 = ((x * x) - t_0) / ((x * x) + t_0)
    t_2 = (((x / y) / (y / x)) * 0.5d0) + (-1.0d0)
    if ((x * x) <= 0.0d0) then
        tmp = t_2
    else if ((x * x) <= 2d-217) then
        tmp = t_1
    else if ((x * x) <= 2d-140) then
        tmp = t_2
    else if ((x * x) <= 5d+82) then
        tmp = t_1
    else
        tmp = (((y / x) / (x / y)) * (-8.0d0)) + 1.0d0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
}
public static 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 t_2 = (((x / y) / (y / x)) * 0.5) + -1.0;
	double tmp;
	if ((x * x) <= 0.0) {
		tmp = t_2;
	} else if ((x * x) <= 2e-217) {
		tmp = t_1;
	} else if ((x * x) <= 2e-140) {
		tmp = t_2;
	} else if ((x * x) <= 5e+82) {
		tmp = t_1;
	} else {
		tmp = (((y / x) / (x / y)) * -8.0) + 1.0;
	}
	return tmp;
}
def code(x, y):
	return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y))
def code(x, y):
	t_0 = y * (y * 4.0)
	t_1 = ((x * x) - t_0) / ((x * x) + t_0)
	t_2 = (((x / y) / (y / x)) * 0.5) + -1.0
	tmp = 0
	if (x * x) <= 0.0:
		tmp = t_2
	elif (x * x) <= 2e-217:
		tmp = t_1
	elif (x * x) <= 2e-140:
		tmp = t_2
	elif (x * x) <= 5e+82:
		tmp = t_1
	else:
		tmp = (((y / x) / (x / y)) * -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))
	t_2 = Float64(Float64(Float64(Float64(x / y) / Float64(y / x)) * 0.5) + -1.0)
	tmp = 0.0
	if (Float64(x * x) <= 0.0)
		tmp = t_2;
	elseif (Float64(x * x) <= 2e-217)
		tmp = t_1;
	elseif (Float64(x * x) <= 2e-140)
		tmp = t_2;
	elseif (Float64(x * x) <= 5e+82)
		tmp = t_1;
	else
		tmp = Float64(Float64(Float64(Float64(y / x) / Float64(x / y)) * -8.0) + 1.0);
	end
	return tmp
end
function tmp = code(x, y)
	tmp = ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
end
function tmp_2 = code(x, y)
	t_0 = y * (y * 4.0);
	t_1 = ((x * x) - t_0) / ((x * x) + t_0);
	t_2 = (((x / y) / (y / x)) * 0.5) + -1.0;
	tmp = 0.0;
	if ((x * x) <= 0.0)
		tmp = t_2;
	elseif ((x * x) <= 2e-217)
		tmp = t_1;
	elseif ((x * x) <= 2e-140)
		tmp = t_2;
	elseif ((x * x) <= 5e+82)
		tmp = t_1;
	else
		tmp = (((y / x) / (x / y)) * -8.0) + 1.0;
	end
	tmp_2 = 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]}, Block[{t$95$2 = N[(N[(N[(N[(x / y), $MachinePrecision] / N[(y / x), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 0.0], t$95$2, If[LessEqual[N[(x * x), $MachinePrecision], 2e-217], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 2e-140], t$95$2, If[LessEqual[N[(x * x), $MachinePrecision], 5e+82], t$95$1, N[(N[(N[(N[(y / x), $MachinePrecision] / N[(x / y), $MachinePrecision]), $MachinePrecision] * -8.0), $MachinePrecision] + 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}\\
t_2 := \frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\
\mathbf{if}\;x \cdot x \leq 0:\\
\;\;\;\;t_2\\

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

\mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-140}:\\
\;\;\;\;t_2\\

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

\mathbf{else}:\\
\;\;\;\;\frac{\frac{y}{x}}{\frac{x}{y}} \cdot -8 + 1\\


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 5 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original50.8%
Target51.3%
Herbie79.9%
\[\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) < 0.0 or 2.00000000000000016e-217 < (*.f64 x x) < 2e-140

    1. Initial program 59.5%

      \[\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 82.4%

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

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

      [Start]82.4

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

      fma-neg [=>]82.4

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

      unpow2 [=>]82.4

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

      unpow2 [=>]82.4

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

      times-frac [=>]93.2

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

      metadata-eval [=>]93.2

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

      \[\leadsto \color{blue}{{\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1} \]
      Step-by-step derivation

      [Start]93.2

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

      fma-udef [=>]93.2

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

      *-commutative [=>]93.2

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

      pow2 [=>]93.2

      \[ \color{blue}{{\left(\frac{x}{y}\right)}^{2}} \cdot 0.5 + -1 \]
    5. Applied egg-rr93.2%

      \[\leadsto \color{blue}{\frac{\frac{x}{y}}{\frac{y}{x}}} \cdot 0.5 + -1 \]
      Step-by-step derivation

      [Start]93.2

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

      unpow2 [=>]93.2

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

      clear-num [=>]93.2

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

      un-div-inv [=>]93.2

      \[ \color{blue}{\frac{\frac{x}{y}}{\frac{y}{x}}} \cdot 0.5 + -1 \]

    if 0.0 < (*.f64 x x) < 2.00000000000000016e-217 or 2e-140 < (*.f64 x x) < 5.00000000000000015e82

    1. Initial program 93.7%

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

    if 5.00000000000000015e82 < (*.f64 x x)

    1. Initial program 27.5%

      \[\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 76.4%

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

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

      [Start]76.4

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

      associate--l+ [=>]76.4

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

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

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

      metadata-eval [=>]76.4

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

      *-commutative [<=]76.4

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

      +-commutative [<=]76.4

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

      *-commutative [=>]76.4

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

      fma-def [=>]76.4

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

      unpow2 [=>]76.4

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

      unpow2 [=>]76.4

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

      times-frac [=>]83.4

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

      \[\leadsto \color{blue}{{\left(\frac{y}{x}\right)}^{2} \cdot -8 + 1} \]
      Step-by-step derivation

      [Start]83.4

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

      fma-udef [=>]83.4

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

      pow2 [=>]83.4

      \[ \color{blue}{{\left(\frac{y}{x}\right)}^{2}} \cdot -8 + 1 \]
    5. Applied egg-rr83.4%

      \[\leadsto \color{blue}{\frac{\frac{y}{x}}{\frac{x}{y}}} \cdot -8 + 1 \]
      Step-by-step derivation

      [Start]83.4

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

      unpow2 [=>]83.4

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

      clear-num [=>]83.4

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

      un-div-inv [=>]83.4

      \[ \color{blue}{\frac{\frac{y}{x}}{\frac{x}{y}}} \cdot -8 + 1 \]
  3. Recombined 3 regimes into one program.
  4. Final simplification89.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 0:\\ \;\;\;\;\frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-217}:\\ \;\;\;\;\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 2 \cdot 10^{-140}:\\ \;\;\;\;\frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+82}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{y}{x}}{\frac{x}{y}} \cdot -8 + 1\\ \end{array} \]

Alternatives

Alternative 1
Accuracy73.5%
Cost1497
\[\begin{array}{l} t_0 := \frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\ t_1 := \frac{\frac{y}{x}}{\frac{x}{y}} \cdot -8 + 1\\ \mathbf{if}\;x \leq -1.86 \cdot 10^{-25}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 5.6 \cdot 10^{-148}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.8 \cdot 10^{-109}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 2.5 \cdot 10^{-44}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 2.45 \cdot 10^{+33} \lor \neg \left(x \leq 5.5 \cdot 10^{+61}\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 2
Accuracy73.0%
Cost1232
\[\begin{array}{l} t_0 := \frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\ \mathbf{if}\;x \leq -6.5 \cdot 10^{-23}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 5.6 \cdot 10^{-148}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.8 \cdot 10^{-109}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 7 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 10^{+22}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 10^{+62}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 3
Accuracy72.4%
Cost856
\[\begin{array}{l} \mathbf{if}\;x \leq -5 \cdot 10^{-13}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 5.6 \cdot 10^{-148}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 3.8 \cdot 10^{-109}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 4 \cdot 10^{-74}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 10^{+30}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 6 \cdot 10^{+61}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 4
Accuracy49.4%
Cost64
\[-1 \]

Reproduce?

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