Diagrams.TwoD.Layout.CirclePacking:approxRadius from diagrams-contrib-1.3.0.5

Percentage Accurate: 44.9% → 58.1%
Time: 10.6s
Alternatives: 4
Speedup: 244.0×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{y \cdot 2}\\ \frac{\tan t\_0}{\sin t\_0} \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ x (* y 2.0)))) (/ (tan t_0) (sin t_0))))
double code(double x, double y) {
	double t_0 = x / (y * 2.0);
	return tan(t_0) / sin(t_0);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    t_0 = x / (y * 2.0d0)
    code = tan(t_0) / sin(t_0)
end function
public static double code(double x, double y) {
	double t_0 = x / (y * 2.0);
	return Math.tan(t_0) / Math.sin(t_0);
}
def code(x, y):
	t_0 = x / (y * 2.0)
	return math.tan(t_0) / math.sin(t_0)
function code(x, y)
	t_0 = Float64(x / Float64(y * 2.0))
	return Float64(tan(t_0) / sin(t_0))
end
function tmp = code(x, y)
	t_0 = x / (y * 2.0);
	tmp = tan(t_0) / sin(t_0);
end
code[x_, y_] := Block[{t$95$0 = N[(x / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, N[(N[Tan[t$95$0], $MachinePrecision] / N[Sin[t$95$0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x}{y \cdot 2}\\
\frac{\tan t\_0}{\sin t\_0}
\end{array}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 44.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{y \cdot 2}\\ \frac{\tan t\_0}{\sin t\_0} \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ x (* y 2.0)))) (/ (tan t_0) (sin t_0))))
double code(double x, double y) {
	double t_0 = x / (y * 2.0);
	return tan(t_0) / sin(t_0);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    t_0 = x / (y * 2.0d0)
    code = tan(t_0) / sin(t_0)
end function
public static double code(double x, double y) {
	double t_0 = x / (y * 2.0);
	return Math.tan(t_0) / Math.sin(t_0);
}
def code(x, y):
	t_0 = x / (y * 2.0)
	return math.tan(t_0) / math.sin(t_0)
function code(x, y)
	t_0 = Float64(x / Float64(y * 2.0))
	return Float64(tan(t_0) / sin(t_0))
end
function tmp = code(x, y)
	t_0 = x / (y * 2.0);
	tmp = tan(t_0) / sin(t_0);
end
code[x_, y_] := Block[{t$95$0 = N[(x / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, N[(N[Tan[t$95$0], $MachinePrecision] / N[Sin[t$95$0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x}{y \cdot 2}\\
\frac{\tan t\_0}{\sin t\_0}
\end{array}
\end{array}

Alternative 1: 58.1% accurate, 1.6× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;\frac{x\_m}{y\_m \cdot 2} \leq 10^{+154}:\\ \;\;\;\;\frac{-1}{\cos \left(\mathsf{fma}\left(-0.5, \frac{x\_m}{y\_m}, \mathsf{PI}\left(\right)\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
x_m = (fabs.f64 x)
(FPCore (x_m y_m)
 :precision binary64
 (if (<= (/ x_m (* y_m 2.0)) 1e+154)
   (/ -1.0 (cos (fma -0.5 (/ x_m y_m) (PI))))
   1.0))
\begin{array}{l}
y_m = \left|y\right|
\\
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;\frac{x\_m}{y\_m \cdot 2} \leq 10^{+154}:\\
\;\;\;\;\frac{-1}{\cos \left(\mathsf{fma}\left(-0.5, \frac{x\_m}{y\_m}, \mathsf{PI}\left(\right)\right)\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 x (*.f64 y #s(literal 2 binary64))) < 1.00000000000000004e154

    1. Initial program 50.4%

      \[\frac{\tan \left(\frac{x}{y \cdot 2}\right)}{\sin \left(\frac{x}{y \cdot 2}\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{1}{\cos \left(\frac{1}{2} \cdot \frac{x}{y}\right)}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{1}{\cos \left(\frac{1}{2} \cdot \frac{x}{y}\right)}} \]
      2. associate-*r/N/A

        \[\leadsto \frac{1}{\cos \color{blue}{\left(\frac{\frac{1}{2} \cdot x}{y}\right)}} \]
      3. *-commutativeN/A

        \[\leadsto \frac{1}{\cos \left(\frac{\color{blue}{x \cdot \frac{1}{2}}}{y}\right)} \]
      4. associate-*r/N/A

        \[\leadsto \frac{1}{\cos \color{blue}{\left(x \cdot \frac{\frac{1}{2}}{y}\right)}} \]
      5. metadata-evalN/A

        \[\leadsto \frac{1}{\cos \left(x \cdot \frac{\color{blue}{\frac{1}{2} \cdot 1}}{y}\right)} \]
      6. associate-*r/N/A

        \[\leadsto \frac{1}{\cos \left(x \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y}\right)}\right)} \]
      7. lower-cos.f64N/A

        \[\leadsto \frac{1}{\color{blue}{\cos \left(x \cdot \left(\frac{1}{2} \cdot \frac{1}{y}\right)\right)}} \]
      8. *-commutativeN/A

        \[\leadsto \frac{1}{\cos \color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right)}} \]
      9. lower-*.f64N/A

        \[\leadsto \frac{1}{\cos \color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right)}} \]
      10. associate-*r/N/A

        \[\leadsto \frac{1}{\cos \left(\color{blue}{\frac{\frac{1}{2} \cdot 1}{y}} \cdot x\right)} \]
      11. metadata-evalN/A

        \[\leadsto \frac{1}{\cos \left(\frac{\color{blue}{\frac{1}{2}}}{y} \cdot x\right)} \]
      12. lower-/.f6459.2

        \[\leadsto \frac{1}{\cos \left(\color{blue}{\frac{0.5}{y}} \cdot x\right)} \]
    5. Applied rewrites59.2%

      \[\leadsto \color{blue}{\frac{1}{\cos \left(\frac{0.5}{y} \cdot x\right)}} \]
    6. Step-by-step derivation
      1. Applied rewrites60.3%

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

      if 1.00000000000000004e154 < (/.f64 x (*.f64 y #s(literal 2 binary64)))

      1. Initial program 8.7%

        \[\frac{\tan \left(\frac{x}{y \cdot 2}\right)}{\sin \left(\frac{x}{y \cdot 2}\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{1} \]
      4. Step-by-step derivation
        1. Applied rewrites13.4%

          \[\leadsto \color{blue}{1} \]
      5. Recombined 2 regimes into one program.
      6. Add Preprocessing

      Alternative 2: 56.4% accurate, 1.9× speedup?

      \[\begin{array}{l} y_m = \left|y\right| \\ x_m = \left|x\right| \\ \frac{-1}{\cos \left(\mathsf{fma}\left(\frac{-0.5}{y\_m}, x\_m, \mathsf{PI}\left(\right)\right)\right)} \end{array} \]
      y_m = (fabs.f64 y)
      x_m = (fabs.f64 x)
      (FPCore (x_m y_m)
       :precision binary64
       (/ -1.0 (cos (fma (/ -0.5 y_m) x_m (PI)))))
      \begin{array}{l}
      y_m = \left|y\right|
      \\
      x_m = \left|x\right|
      
      \\
      \frac{-1}{\cos \left(\mathsf{fma}\left(\frac{-0.5}{y\_m}, x\_m, \mathsf{PI}\left(\right)\right)\right)}
      \end{array}
      
      Derivation
      1. Initial program 43.4%

        \[\frac{\tan \left(\frac{x}{y \cdot 2}\right)}{\sin \left(\frac{x}{y \cdot 2}\right)} \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{\frac{1}{\cos \left(\frac{1}{2} \cdot \frac{x}{y}\right)}} \]
      4. Step-by-step derivation
        1. lower-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{\cos \left(\frac{1}{2} \cdot \frac{x}{y}\right)}} \]
        2. associate-*r/N/A

          \[\leadsto \frac{1}{\cos \color{blue}{\left(\frac{\frac{1}{2} \cdot x}{y}\right)}} \]
        3. *-commutativeN/A

          \[\leadsto \frac{1}{\cos \left(\frac{\color{blue}{x \cdot \frac{1}{2}}}{y}\right)} \]
        4. associate-*r/N/A

          \[\leadsto \frac{1}{\cos \color{blue}{\left(x \cdot \frac{\frac{1}{2}}{y}\right)}} \]
        5. metadata-evalN/A

          \[\leadsto \frac{1}{\cos \left(x \cdot \frac{\color{blue}{\frac{1}{2} \cdot 1}}{y}\right)} \]
        6. associate-*r/N/A

          \[\leadsto \frac{1}{\cos \left(x \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y}\right)}\right)} \]
        7. lower-cos.f64N/A

          \[\leadsto \frac{1}{\color{blue}{\cos \left(x \cdot \left(\frac{1}{2} \cdot \frac{1}{y}\right)\right)}} \]
        8. *-commutativeN/A

          \[\leadsto \frac{1}{\cos \color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right)}} \]
        9. lower-*.f64N/A

          \[\leadsto \frac{1}{\cos \color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right)}} \]
        10. associate-*r/N/A

          \[\leadsto \frac{1}{\cos \left(\color{blue}{\frac{\frac{1}{2} \cdot 1}{y}} \cdot x\right)} \]
        11. metadata-evalN/A

          \[\leadsto \frac{1}{\cos \left(\frac{\color{blue}{\frac{1}{2}}}{y} \cdot x\right)} \]
        12. lower-/.f6450.6

          \[\leadsto \frac{1}{\cos \left(\color{blue}{\frac{0.5}{y}} \cdot x\right)} \]
      5. Applied rewrites50.6%

        \[\leadsto \color{blue}{\frac{1}{\cos \left(\frac{0.5}{y} \cdot x\right)}} \]
      6. Step-by-step derivation
        1. Applied rewrites51.4%

          \[\leadsto \frac{1}{\sin \left(\mathsf{fma}\left(\frac{0.5}{y}, x, \frac{\mathsf{PI}\left(\right)}{2}\right)\right)} \]
        2. Step-by-step derivation
          1. Applied rewrites51.6%

            \[\leadsto \frac{-1}{\color{blue}{\cos \left(\mathsf{fma}\left(\frac{-0.5}{y}, x, \mathsf{PI}\left(\right)\right)\right)}} \]
          2. Add Preprocessing

          Alternative 3: 56.5% accurate, 244.0× speedup?

          \[\begin{array}{l} y_m = \left|y\right| \\ x_m = \left|x\right| \\ 1 \end{array} \]
          y_m = (fabs.f64 y)
          x_m = (fabs.f64 x)
          (FPCore (x_m y_m) :precision binary64 1.0)
          y_m = fabs(y);
          x_m = fabs(x);
          double code(double x_m, double y_m) {
          	return 1.0;
          }
          
          y_m = abs(y)
          x_m = abs(x)
          real(8) function code(x_m, y_m)
              real(8), intent (in) :: x_m
              real(8), intent (in) :: y_m
              code = 1.0d0
          end function
          
          y_m = Math.abs(y);
          x_m = Math.abs(x);
          public static double code(double x_m, double y_m) {
          	return 1.0;
          }
          
          y_m = math.fabs(y)
          x_m = math.fabs(x)
          def code(x_m, y_m):
          	return 1.0
          
          y_m = abs(y)
          x_m = abs(x)
          function code(x_m, y_m)
          	return 1.0
          end
          
          y_m = abs(y);
          x_m = abs(x);
          function tmp = code(x_m, y_m)
          	tmp = 1.0;
          end
          
          y_m = N[Abs[y], $MachinePrecision]
          x_m = N[Abs[x], $MachinePrecision]
          code[x$95$m_, y$95$m_] := 1.0
          
          \begin{array}{l}
          y_m = \left|y\right|
          \\
          x_m = \left|x\right|
          
          \\
          1
          \end{array}
          
          Derivation
          1. Initial program 43.4%

            \[\frac{\tan \left(\frac{x}{y \cdot 2}\right)}{\sin \left(\frac{x}{y \cdot 2}\right)} \]
          2. Add Preprocessing
          3. Taylor expanded in x around 0

            \[\leadsto \color{blue}{1} \]
          4. Step-by-step derivation
            1. Applied rewrites50.9%

              \[\leadsto \color{blue}{1} \]
            2. Add Preprocessing

            Alternative 4: 3.1% accurate, 244.0× speedup?

            \[\begin{array}{l} y_m = \left|y\right| \\ x_m = \left|x\right| \\ 0 \end{array} \]
            y_m = (fabs.f64 y)
            x_m = (fabs.f64 x)
            (FPCore (x_m y_m) :precision binary64 0.0)
            y_m = fabs(y);
            x_m = fabs(x);
            double code(double x_m, double y_m) {
            	return 0.0;
            }
            
            y_m = abs(y)
            x_m = abs(x)
            real(8) function code(x_m, y_m)
                real(8), intent (in) :: x_m
                real(8), intent (in) :: y_m
                code = 0.0d0
            end function
            
            y_m = Math.abs(y);
            x_m = Math.abs(x);
            public static double code(double x_m, double y_m) {
            	return 0.0;
            }
            
            y_m = math.fabs(y)
            x_m = math.fabs(x)
            def code(x_m, y_m):
            	return 0.0
            
            y_m = abs(y)
            x_m = abs(x)
            function code(x_m, y_m)
            	return 0.0
            end
            
            y_m = abs(y);
            x_m = abs(x);
            function tmp = code(x_m, y_m)
            	tmp = 0.0;
            end
            
            y_m = N[Abs[y], $MachinePrecision]
            x_m = N[Abs[x], $MachinePrecision]
            code[x$95$m_, y$95$m_] := 0.0
            
            \begin{array}{l}
            y_m = \left|y\right|
            \\
            x_m = \left|x\right|
            
            \\
            0
            \end{array}
            
            Derivation
            1. Initial program 43.4%

              \[\frac{\tan \left(\frac{x}{y \cdot 2}\right)}{\sin \left(\frac{x}{y \cdot 2}\right)} \]
            2. Add Preprocessing
            3. Applied rewrites3.1%

              \[\leadsto \color{blue}{0} \]
            4. Add Preprocessing

            Developer Target 1: 56.5% accurate, 0.4× speedup?

            \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{y \cdot 2}\\ t_1 := \sin t\_0\\ \mathbf{if}\;y < -1.2303690911306994 \cdot 10^{+114}:\\ \;\;\;\;1\\ \mathbf{elif}\;y < -9.102852406811914 \cdot 10^{-222}:\\ \;\;\;\;\frac{t\_1}{t\_1 \cdot \log \left(e^{\cos t\_0}\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
            (FPCore (x y)
             :precision binary64
             (let* ((t_0 (/ x (* y 2.0))) (t_1 (sin t_0)))
               (if (< y -1.2303690911306994e+114)
                 1.0
                 (if (< y -9.102852406811914e-222)
                   (/ t_1 (* t_1 (log (exp (cos t_0)))))
                   1.0))))
            double code(double x, double y) {
            	double t_0 = x / (y * 2.0);
            	double t_1 = sin(t_0);
            	double tmp;
            	if (y < -1.2303690911306994e+114) {
            		tmp = 1.0;
            	} else if (y < -9.102852406811914e-222) {
            		tmp = t_1 / (t_1 * log(exp(cos(t_0))));
            	} else {
            		tmp = 1.0;
            	}
            	return tmp;
            }
            
            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) :: tmp
                t_0 = x / (y * 2.0d0)
                t_1 = sin(t_0)
                if (y < (-1.2303690911306994d+114)) then
                    tmp = 1.0d0
                else if (y < (-9.102852406811914d-222)) then
                    tmp = t_1 / (t_1 * log(exp(cos(t_0))))
                else
                    tmp = 1.0d0
                end if
                code = tmp
            end function
            
            public static double code(double x, double y) {
            	double t_0 = x / (y * 2.0);
            	double t_1 = Math.sin(t_0);
            	double tmp;
            	if (y < -1.2303690911306994e+114) {
            		tmp = 1.0;
            	} else if (y < -9.102852406811914e-222) {
            		tmp = t_1 / (t_1 * Math.log(Math.exp(Math.cos(t_0))));
            	} else {
            		tmp = 1.0;
            	}
            	return tmp;
            }
            
            def code(x, y):
            	t_0 = x / (y * 2.0)
            	t_1 = math.sin(t_0)
            	tmp = 0
            	if y < -1.2303690911306994e+114:
            		tmp = 1.0
            	elif y < -9.102852406811914e-222:
            		tmp = t_1 / (t_1 * math.log(math.exp(math.cos(t_0))))
            	else:
            		tmp = 1.0
            	return tmp
            
            function code(x, y)
            	t_0 = Float64(x / Float64(y * 2.0))
            	t_1 = sin(t_0)
            	tmp = 0.0
            	if (y < -1.2303690911306994e+114)
            		tmp = 1.0;
            	elseif (y < -9.102852406811914e-222)
            		tmp = Float64(t_1 / Float64(t_1 * log(exp(cos(t_0)))));
            	else
            		tmp = 1.0;
            	end
            	return tmp
            end
            
            function tmp_2 = code(x, y)
            	t_0 = x / (y * 2.0);
            	t_1 = sin(t_0);
            	tmp = 0.0;
            	if (y < -1.2303690911306994e+114)
            		tmp = 1.0;
            	elseif (y < -9.102852406811914e-222)
            		tmp = t_1 / (t_1 * log(exp(cos(t_0))));
            	else
            		tmp = 1.0;
            	end
            	tmp_2 = tmp;
            end
            
            code[x_, y_] := Block[{t$95$0 = N[(x / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sin[t$95$0], $MachinePrecision]}, If[Less[y, -1.2303690911306994e+114], 1.0, If[Less[y, -9.102852406811914e-222], N[(t$95$1 / N[(t$95$1 * N[Log[N[Exp[N[Cos[t$95$0], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.0]]]]
            
            \begin{array}{l}
            
            \\
            \begin{array}{l}
            t_0 := \frac{x}{y \cdot 2}\\
            t_1 := \sin t\_0\\
            \mathbf{if}\;y < -1.2303690911306994 \cdot 10^{+114}:\\
            \;\;\;\;1\\
            
            \mathbf{elif}\;y < -9.102852406811914 \cdot 10^{-222}:\\
            \;\;\;\;\frac{t\_1}{t\_1 \cdot \log \left(e^{\cos t\_0}\right)}\\
            
            \mathbf{else}:\\
            \;\;\;\;1\\
            
            
            \end{array}
            \end{array}
            

            Reproduce

            ?
            herbie shell --seed 2024338 
            (FPCore (x y)
              :name "Diagrams.TwoD.Layout.CirclePacking:approxRadius from diagrams-contrib-1.3.0.5"
              :precision binary64
            
              :alt
              (! :herbie-platform default (if (< y -1230369091130699400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) 1 (if (< y -4551426203405957/500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (/ (sin (/ x (* y 2))) (* (sin (/ x (* y 2))) (log (exp (cos (/ x (* y 2))))))) 1)))
            
              (/ (tan (/ x (* y 2.0))) (sin (/ x (* y 2.0)))))