Data.Random.Distribution.T:$ccdf from random-fu-0.2.6.2

Percentage Accurate: 100.0% → 100.0%
Time: 2.5s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \frac{x + y}{y + y} \end{array} \]
(FPCore (x y) :precision binary64 (/ (+ x y) (+ y y)))
double code(double x, double y) {
	return (x + y) / (y + y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (y + y)
end function
public static double code(double x, double y) {
	return (x + y) / (y + y);
}
def code(x, y):
	return (x + y) / (y + y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(y + y))
end
function tmp = code(x, y)
	tmp = (x + y) / (y + y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x + y}{y + y}
\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 5 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x + y}{y + y} \end{array} \]
(FPCore (x y) :precision binary64 (/ (+ x y) (+ y y)))
double code(double x, double y) {
	return (x + y) / (y + y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (y + y)
end function
public static double code(double x, double y) {
	return (x + y) / (y + y);
}
def code(x, y):
	return (x + y) / (y + y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(y + y))
end
function tmp = code(x, y)
	tmp = (x + y) / (y + y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x + y}{y + y}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x + y}{y + y} \end{array} \]
(FPCore (x y) :precision binary64 (/ (+ x y) (+ y y)))
double code(double x, double y) {
	return (x + y) / (y + y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) / (y + y)
end function
public static double code(double x, double y) {
	return (x + y) / (y + y);
}
def code(x, y):
	return (x + y) / (y + y)
function code(x, y)
	return Float64(Float64(x + y) / Float64(y + y))
end
function tmp = code(x, y)
	tmp = (x + y) / (y + y);
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x + y}{y + y}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{x + y}{y + y} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 74.2% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{y + y}\\ \mathbf{if}\;x \leq -2.9 \cdot 10^{+49}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 5.5 \cdot 10^{-52}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ x (+ y y))))
   (if (<= x -2.9e+49) t_0 (if (<= x 5.5e-52) 0.5 t_0))))
double code(double x, double y) {
	double t_0 = x / (y + y);
	double tmp;
	if (x <= -2.9e+49) {
		tmp = t_0;
	} else if (x <= 5.5e-52) {
		tmp = 0.5;
	} else {
		tmp = t_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) :: tmp
    t_0 = x / (y + y)
    if (x <= (-2.9d+49)) then
        tmp = t_0
    else if (x <= 5.5d-52) then
        tmp = 0.5d0
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = x / (y + y);
	double tmp;
	if (x <= -2.9e+49) {
		tmp = t_0;
	} else if (x <= 5.5e-52) {
		tmp = 0.5;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = x / (y + y)
	tmp = 0
	if x <= -2.9e+49:
		tmp = t_0
	elif x <= 5.5e-52:
		tmp = 0.5
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(x / Float64(y + y))
	tmp = 0.0
	if (x <= -2.9e+49)
		tmp = t_0;
	elseif (x <= 5.5e-52)
		tmp = 0.5;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = x / (y + y);
	tmp = 0.0;
	if (x <= -2.9e+49)
		tmp = t_0;
	elseif (x <= 5.5e-52)
		tmp = 0.5;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(x / N[(y + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -2.9e+49], t$95$0, If[LessEqual[x, 5.5e-52], 0.5, t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x}{y + y}\\
\mathbf{if}\;x \leq -2.9 \cdot 10^{+49}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x \leq 5.5 \cdot 10^{-52}:\\
\;\;\;\;0.5\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2.9e49 or 5.5e-52 < x

    1. Initial program 100.0%

      \[\frac{x + y}{y + y} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \mathsf{/.f64}\left(\color{blue}{x}, \mathsf{+.f64}\left(y, y\right)\right) \]
    4. Step-by-step derivation
      1. Simplified80.6%

        \[\leadsto \frac{\color{blue}{x}}{y + y} \]

      if -2.9e49 < x < 5.5e-52

      1. Initial program 100.0%

        \[\frac{x + y}{y + y} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{1}{2}} \]
      4. Step-by-step derivation
        1. Simplified79.9%

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

      Alternative 3: 74.0% accurate, 0.5× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{0.5}{\frac{y}{x}}\\ \mathbf{if}\;x \leq -8.6 \cdot 10^{+50}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 1.95 \cdot 10^{-50}:\\ \;\;\;\;0.5\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
      (FPCore (x y)
       :precision binary64
       (let* ((t_0 (/ 0.5 (/ y x))))
         (if (<= x -8.6e+50) t_0 (if (<= x 1.95e-50) 0.5 t_0))))
      double code(double x, double y) {
      	double t_0 = 0.5 / (y / x);
      	double tmp;
      	if (x <= -8.6e+50) {
      		tmp = t_0;
      	} else if (x <= 1.95e-50) {
      		tmp = 0.5;
      	} else {
      		tmp = t_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) :: tmp
          t_0 = 0.5d0 / (y / x)
          if (x <= (-8.6d+50)) then
              tmp = t_0
          else if (x <= 1.95d-50) then
              tmp = 0.5d0
          else
              tmp = t_0
          end if
          code = tmp
      end function
      
      public static double code(double x, double y) {
      	double t_0 = 0.5 / (y / x);
      	double tmp;
      	if (x <= -8.6e+50) {
      		tmp = t_0;
      	} else if (x <= 1.95e-50) {
      		tmp = 0.5;
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      def code(x, y):
      	t_0 = 0.5 / (y / x)
      	tmp = 0
      	if x <= -8.6e+50:
      		tmp = t_0
      	elif x <= 1.95e-50:
      		tmp = 0.5
      	else:
      		tmp = t_0
      	return tmp
      
      function code(x, y)
      	t_0 = Float64(0.5 / Float64(y / x))
      	tmp = 0.0
      	if (x <= -8.6e+50)
      		tmp = t_0;
      	elseif (x <= 1.95e-50)
      		tmp = 0.5;
      	else
      		tmp = t_0;
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y)
      	t_0 = 0.5 / (y / x);
      	tmp = 0.0;
      	if (x <= -8.6e+50)
      		tmp = t_0;
      	elseif (x <= 1.95e-50)
      		tmp = 0.5;
      	else
      		tmp = t_0;
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := Block[{t$95$0 = N[(0.5 / N[(y / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -8.6e+50], t$95$0, If[LessEqual[x, 1.95e-50], 0.5, t$95$0]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := \frac{0.5}{\frac{y}{x}}\\
      \mathbf{if}\;x \leq -8.6 \cdot 10^{+50}:\\
      \;\;\;\;t\_0\\
      
      \mathbf{elif}\;x \leq 1.95 \cdot 10^{-50}:\\
      \;\;\;\;0.5\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_0\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < -8.5999999999999994e50 or 1.9500000000000001e-50 < x

        1. Initial program 100.0%

          \[\frac{x + y}{y + y} \]
        2. Add Preprocessing
        3. Taylor expanded in x around inf

          \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{x}{y}} \]
        4. Step-by-step derivation
          1. associate-*r/N/A

            \[\leadsto \frac{\frac{1}{2} \cdot x}{\color{blue}{y}} \]
          2. *-rgt-identityN/A

            \[\leadsto \frac{\frac{1}{2} \cdot x}{y \cdot \color{blue}{1}} \]
          3. lft-mult-inverseN/A

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

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

            \[\leadsto \frac{\frac{1}{2} \cdot x}{\frac{y \cdot 1}{x} \cdot x} \]
          6. *-rgt-identityN/A

            \[\leadsto \frac{\frac{1}{2} \cdot x}{\frac{y}{x} \cdot x} \]
          7. *-commutativeN/A

            \[\leadsto \frac{\frac{1}{2} \cdot x}{x \cdot \color{blue}{\frac{y}{x}}} \]
          8. associate-/r*N/A

            \[\leadsto \frac{\frac{\frac{1}{2} \cdot x}{x}}{\color{blue}{\frac{y}{x}}} \]
          9. associate-/l*N/A

            \[\leadsto \frac{\frac{1}{2} \cdot \frac{x}{x}}{\frac{\color{blue}{y}}{x}} \]
          10. *-rgt-identityN/A

            \[\leadsto \frac{\frac{1}{2} \cdot \frac{x \cdot 1}{x}}{\frac{y}{x}} \]
          11. associate-*r/N/A

            \[\leadsto \frac{\frac{1}{2} \cdot \left(x \cdot \frac{1}{x}\right)}{\frac{y}{x}} \]
          12. rgt-mult-inverseN/A

            \[\leadsto \frac{\frac{1}{2} \cdot 1}{\frac{y}{x}} \]
          13. metadata-evalN/A

            \[\leadsto \frac{\frac{1}{2}}{\frac{\color{blue}{y}}{x}} \]
          14. /-lowering-/.f64N/A

            \[\leadsto \mathsf{/.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{y}{x}\right)}\right) \]
          15. /-lowering-/.f6480.3%

            \[\leadsto \mathsf{/.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(y, \color{blue}{x}\right)\right) \]
        5. Simplified80.3%

          \[\leadsto \color{blue}{\frac{0.5}{\frac{y}{x}}} \]

        if -8.5999999999999994e50 < x < 1.9500000000000001e-50

        1. Initial program 100.0%

          \[\frac{x + y}{y + y} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{\frac{1}{2}} \]
        4. Step-by-step derivation
          1. Simplified79.9%

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

        Alternative 4: 100.0% accurate, 1.0× speedup?

        \[\begin{array}{l} \\ 0.5 - -0.5 \cdot \frac{x}{y} \end{array} \]
        (FPCore (x y) :precision binary64 (- 0.5 (* -0.5 (/ x y))))
        double code(double x, double y) {
        	return 0.5 - (-0.5 * (x / y));
        }
        
        real(8) function code(x, y)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            code = 0.5d0 - ((-0.5d0) * (x / y))
        end function
        
        public static double code(double x, double y) {
        	return 0.5 - (-0.5 * (x / y));
        }
        
        def code(x, y):
        	return 0.5 - (-0.5 * (x / y))
        
        function code(x, y)
        	return Float64(0.5 - Float64(-0.5 * Float64(x / y)))
        end
        
        function tmp = code(x, y)
        	tmp = 0.5 - (-0.5 * (x / y));
        end
        
        code[x_, y_] := N[(0.5 - N[(-0.5 * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        0.5 - -0.5 \cdot \frac{x}{y}
        \end{array}
        
        Derivation
        1. Initial program 100.0%

          \[\frac{x + y}{y + y} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{\frac{1}{2} + \frac{1}{2} \cdot \frac{x}{y}} \]
        4. Step-by-step derivation
          1. metadata-evalN/A

            \[\leadsto 1 \cdot \frac{1}{2} + \color{blue}{\frac{1}{2}} \cdot \frac{x}{y} \]
          2. *-lft-identityN/A

            \[\leadsto 1 \cdot \frac{1}{2} + \frac{1}{2} \cdot \frac{1 \cdot x}{y} \]
          3. associate-*l/N/A

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

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

            \[\leadsto 1 \cdot \frac{1}{2} + x \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y}\right)} \]
          6. fma-undefineN/A

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

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, x \cdot \frac{\frac{1}{2} \cdot 1}{y}\right) \]
          8. metadata-evalN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, x \cdot \frac{\frac{1}{2}}{y}\right) \]
          9. associate-*r/N/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{x \cdot \frac{1}{2}}{y}\right) \]
          10. *-commutativeN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2} \cdot x}{y}\right) \]
          11. *-lft-identityN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2} \cdot x}{1 \cdot y}\right) \]
          12. metadata-evalN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2} \cdot x}{\left(-1 \cdot -1\right) \cdot y}\right) \]
          13. associate-*r*N/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2} \cdot x}{-1 \cdot \left(-1 \cdot y\right)}\right) \]
          14. times-fracN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2}}{-1} \cdot \frac{x}{-1 \cdot y}\right) \]
          15. mul-1-negN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2}}{-1} \cdot \frac{x}{\mathsf{neg}\left(y\right)}\right) \]
          16. distribute-neg-frac2N/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \frac{\frac{1}{2}}{-1} \cdot \left(\mathsf{neg}\left(\frac{x}{y}\right)\right)\right) \]
          17. distribute-rgt-neg-outN/A

            \[\leadsto \mathsf{fma}\left(1, \frac{1}{2}, \mathsf{neg}\left(\frac{\frac{1}{2}}{-1} \cdot \frac{x}{y}\right)\right) \]
          18. fmm-undefN/A

            \[\leadsto 1 \cdot \frac{1}{2} - \color{blue}{\frac{\frac{1}{2}}{-1} \cdot \frac{x}{y}} \]
          19. metadata-evalN/A

            \[\leadsto \frac{1}{2} - \color{blue}{\frac{\frac{1}{2}}{-1}} \cdot \frac{x}{y} \]
          20. --lowering--.f64N/A

            \[\leadsto \mathsf{\_.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{\frac{1}{2}}{-1} \cdot \frac{x}{y}\right)}\right) \]
          21. *-lowering-*.f64N/A

            \[\leadsto \mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\left(\frac{\frac{1}{2}}{-1}\right), \color{blue}{\left(\frac{x}{y}\right)}\right)\right) \]
          22. metadata-evalN/A

            \[\leadsto \mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \left(\frac{\color{blue}{x}}{y}\right)\right)\right) \]
          23. /-lowering-/.f6499.7%

            \[\leadsto \mathsf{\_.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{/.f64}\left(x, \color{blue}{y}\right)\right)\right) \]
        5. Simplified99.7%

          \[\leadsto \color{blue}{0.5 - -0.5 \cdot \frac{x}{y}} \]
        6. Add Preprocessing

        Alternative 5: 51.1% accurate, 7.0× speedup?

        \[\begin{array}{l} \\ 0.5 \end{array} \]
        (FPCore (x y) :precision binary64 0.5)
        double code(double x, double y) {
        	return 0.5;
        }
        
        real(8) function code(x, y)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            code = 0.5d0
        end function
        
        public static double code(double x, double y) {
        	return 0.5;
        }
        
        def code(x, y):
        	return 0.5
        
        function code(x, y)
        	return 0.5
        end
        
        function tmp = code(x, y)
        	tmp = 0.5;
        end
        
        code[x_, y_] := 0.5
        
        \begin{array}{l}
        
        \\
        0.5
        \end{array}
        
        Derivation
        1. Initial program 100.0%

          \[\frac{x + y}{y + y} \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto \color{blue}{\frac{1}{2}} \]
        4. Step-by-step derivation
          1. Simplified51.1%

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

          Developer Target 1: 100.0% accurate, 1.0× speedup?

          \[\begin{array}{l} \\ 0.5 \cdot \frac{x}{y} + 0.5 \end{array} \]
          (FPCore (x y) :precision binary64 (+ (* 0.5 (/ x y)) 0.5))
          double code(double x, double y) {
          	return (0.5 * (x / y)) + 0.5;
          }
          
          real(8) function code(x, y)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              code = (0.5d0 * (x / y)) + 0.5d0
          end function
          
          public static double code(double x, double y) {
          	return (0.5 * (x / y)) + 0.5;
          }
          
          def code(x, y):
          	return (0.5 * (x / y)) + 0.5
          
          function code(x, y)
          	return Float64(Float64(0.5 * Float64(x / y)) + 0.5)
          end
          
          function tmp = code(x, y)
          	tmp = (0.5 * (x / y)) + 0.5;
          end
          
          code[x_, y_] := N[(N[(0.5 * N[(x / y), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          0.5 \cdot \frac{x}{y} + 0.5
          \end{array}
          

          Reproduce

          ?
          herbie shell --seed 2024161 
          (FPCore (x y)
            :name "Data.Random.Distribution.T:$ccdf from random-fu-0.2.6.2"
            :precision binary64
          
            :alt
            (! :herbie-platform default (+ (* 1/2 (/ x y)) 1/2))
          
            (/ (+ x y) (+ y y)))