Diagrams.Trail:splitAtParam from diagrams-lib-1.3.0.3, B

Percentage Accurate: 88.2% → 100.0%
Time: 3.4s
Alternatives: 4
Speedup: 1.0×

Specification

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

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

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

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

Alternative 1: 100.0% accurate, 1.0× speedup?

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

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

    \[\frac{x \cdot y}{y + 1} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-/.f64N/A

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

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

      \[\leadsto \color{blue}{x \cdot \frac{y}{y + 1}} \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{\frac{y}{y + 1} \cdot x} \]
    5. lower-*.f64N/A

      \[\leadsto \color{blue}{\frac{y}{y + 1} \cdot x} \]
    6. lower-/.f64100.0

      \[\leadsto \color{blue}{\frac{y}{y + 1}} \cdot x \]
    7. lift-+.f64N/A

      \[\leadsto \frac{y}{\color{blue}{y + 1}} \cdot x \]
    8. +-commutativeN/A

      \[\leadsto \frac{y}{\color{blue}{1 + y}} \cdot x \]
    9. lower-+.f64100.0

      \[\leadsto \frac{y}{\color{blue}{1 + y}} \cdot x \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\frac{y}{1 + y} \cdot x} \]
  5. Final simplification100.0%

    \[\leadsto x \cdot \frac{y}{1 + y} \]
  6. Add Preprocessing

Alternative 2: 99.0% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
t_0 := x - \frac{x}{y}\\
\mathbf{if}\;y \leq -1:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 1:\\
\;\;\;\;\left(x - x \cdot y\right) \cdot y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1 or 1 < y

    1. Initial program 74.8%

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

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

        \[\leadsto x + \color{blue}{\left(\mathsf{neg}\left(\frac{x}{y}\right)\right)} \]
      2. unsub-negN/A

        \[\leadsto \color{blue}{x - \frac{x}{y}} \]
      3. lower--.f64N/A

        \[\leadsto \color{blue}{x - \frac{x}{y}} \]
      4. lower-/.f6499.4

        \[\leadsto x - \color{blue}{\frac{x}{y}} \]
    5. Applied rewrites99.4%

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

    if -1 < y < 1

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{y \cdot \left(x + -1 \cdot \left(x \cdot y\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

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

        \[\leadsto \color{blue}{\left(x + -1 \cdot \left(x \cdot y\right)\right) \cdot y} \]
      3. mul-1-negN/A

        \[\leadsto \left(x + \color{blue}{\left(\mathsf{neg}\left(x \cdot y\right)\right)}\right) \cdot y \]
      4. unsub-negN/A

        \[\leadsto \color{blue}{\left(x - x \cdot y\right)} \cdot y \]
      5. lower--.f64N/A

        \[\leadsto \color{blue}{\left(x - x \cdot y\right)} \cdot y \]
      6. *-commutativeN/A

        \[\leadsto \left(x - \color{blue}{y \cdot x}\right) \cdot y \]
      7. lower-*.f6498.6

        \[\leadsto \left(x - \color{blue}{y \cdot x}\right) \cdot y \]
    5. Applied rewrites98.6%

      \[\leadsto \color{blue}{\left(x - y \cdot x\right) \cdot y} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;x - \frac{x}{y}\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;\left(x - x \cdot y\right) \cdot y\\ \mathbf{else}:\\ \;\;\;\;x - \frac{x}{y}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 52.0% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y, y, y\right) \cdot x \end{array} \]
(FPCore (x y) :precision binary64 (* (fma y y y) x))
double code(double x, double y) {
	return fma(y, y, y) * x;
}
function code(x, y)
	return Float64(fma(y, y, y) * x)
end
code[x_, y_] := N[(N[(y * y + y), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y, y, y\right) \cdot x
\end{array}
Derivation
  1. Initial program 87.4%

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

    \[\leadsto \color{blue}{y \cdot \left(x + -1 \cdot \left(x \cdot y\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

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

      \[\leadsto \color{blue}{\left(x + -1 \cdot \left(x \cdot y\right)\right) \cdot y} \]
    3. mul-1-negN/A

      \[\leadsto \left(x + \color{blue}{\left(\mathsf{neg}\left(x \cdot y\right)\right)}\right) \cdot y \]
    4. unsub-negN/A

      \[\leadsto \color{blue}{\left(x - x \cdot y\right)} \cdot y \]
    5. lower--.f64N/A

      \[\leadsto \color{blue}{\left(x - x \cdot y\right)} \cdot y \]
    6. *-commutativeN/A

      \[\leadsto \left(x - \color{blue}{y \cdot x}\right) \cdot y \]
    7. lower-*.f6449.8

      \[\leadsto \left(x - \color{blue}{y \cdot x}\right) \cdot y \]
  5. Applied rewrites49.8%

    \[\leadsto \color{blue}{\left(x - y \cdot x\right) \cdot y} \]
  6. Step-by-step derivation
    1. Applied rewrites49.8%

      \[\leadsto \mathsf{fma}\left(-y, x, x\right) \cdot y \]
    2. Step-by-step derivation
      1. Applied rewrites49.8%

        \[\leadsto \left(1 - y\right) \cdot \color{blue}{\left(y \cdot x\right)} \]
      2. Applied rewrites51.1%

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, y, y\right) \cdot x} \]
      3. Add Preprocessing

      Alternative 4: 51.4% accurate, 3.3× speedup?

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

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

        \[\leadsto \color{blue}{y \cdot \left(x + -1 \cdot \left(x \cdot y\right)\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

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

          \[\leadsto \color{blue}{\left(x + -1 \cdot \left(x \cdot y\right)\right) \cdot y} \]
        3. mul-1-negN/A

          \[\leadsto \left(x + \color{blue}{\left(\mathsf{neg}\left(x \cdot y\right)\right)}\right) \cdot y \]
        4. unsub-negN/A

          \[\leadsto \color{blue}{\left(x - x \cdot y\right)} \cdot y \]
        5. lower--.f64N/A

          \[\leadsto \color{blue}{\left(x - x \cdot y\right)} \cdot y \]
        6. *-commutativeN/A

          \[\leadsto \left(x - \color{blue}{y \cdot x}\right) \cdot y \]
        7. lower-*.f6449.8

          \[\leadsto \left(x - \color{blue}{y \cdot x}\right) \cdot y \]
      5. Applied rewrites49.8%

        \[\leadsto \color{blue}{\left(x - y \cdot x\right) \cdot y} \]
      6. Step-by-step derivation
        1. Applied rewrites49.8%

          \[\leadsto \mathsf{fma}\left(-y, x, x\right) \cdot y \]
        2. Taylor expanded in y around 0

          \[\leadsto \color{blue}{x \cdot y} \]
        3. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \color{blue}{y \cdot x} \]
          2. lower-*.f6450.7

            \[\leadsto \color{blue}{y \cdot x} \]
        4. Applied rewrites50.7%

          \[\leadsto \color{blue}{y \cdot x} \]
        5. Final simplification50.7%

          \[\leadsto x \cdot y \]
        6. Add Preprocessing

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

        \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{y \cdot y} - \left(\frac{x}{y} - x\right)\\ \mathbf{if}\;y < -3693.8482788297247:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y < 6799310503.41891:\\ \;\;\;\;\frac{x \cdot y}{y + 1}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
        (FPCore (x y)
         :precision binary64
         (let* ((t_0 (- (/ x (* y y)) (- (/ x y) x))))
           (if (< y -3693.8482788297247)
             t_0
             (if (< y 6799310503.41891) (/ (* x y) (+ y 1.0)) t_0))))
        double code(double x, double y) {
        	double t_0 = (x / (y * y)) - ((x / y) - x);
        	double tmp;
        	if (y < -3693.8482788297247) {
        		tmp = t_0;
        	} else if (y < 6799310503.41891) {
        		tmp = (x * y) / (y + 1.0);
        	} 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)) - ((x / y) - x)
            if (y < (-3693.8482788297247d0)) then
                tmp = t_0
            else if (y < 6799310503.41891d0) then
                tmp = (x * y) / (y + 1.0d0)
            else
                tmp = t_0
            end if
            code = tmp
        end function
        
        public static double code(double x, double y) {
        	double t_0 = (x / (y * y)) - ((x / y) - x);
        	double tmp;
        	if (y < -3693.8482788297247) {
        		tmp = t_0;
        	} else if (y < 6799310503.41891) {
        		tmp = (x * y) / (y + 1.0);
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        def code(x, y):
        	t_0 = (x / (y * y)) - ((x / y) - x)
        	tmp = 0
        	if y < -3693.8482788297247:
        		tmp = t_0
        	elif y < 6799310503.41891:
        		tmp = (x * y) / (y + 1.0)
        	else:
        		tmp = t_0
        	return tmp
        
        function code(x, y)
        	t_0 = Float64(Float64(x / Float64(y * y)) - Float64(Float64(x / y) - x))
        	tmp = 0.0
        	if (y < -3693.8482788297247)
        		tmp = t_0;
        	elseif (y < 6799310503.41891)
        		tmp = Float64(Float64(x * y) / Float64(y + 1.0));
        	else
        		tmp = t_0;
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y)
        	t_0 = (x / (y * y)) - ((x / y) - x);
        	tmp = 0.0;
        	if (y < -3693.8482788297247)
        		tmp = t_0;
        	elseif (y < 6799310503.41891)
        		tmp = (x * y) / (y + 1.0);
        	else
        		tmp = t_0;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_] := Block[{t$95$0 = N[(N[(x / N[(y * y), $MachinePrecision]), $MachinePrecision] - N[(N[(x / y), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]}, If[Less[y, -3693.8482788297247], t$95$0, If[Less[y, 6799310503.41891], N[(N[(x * y), $MachinePrecision] / N[(y + 1.0), $MachinePrecision]), $MachinePrecision], t$95$0]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        t_0 := \frac{x}{y \cdot y} - \left(\frac{x}{y} - x\right)\\
        \mathbf{if}\;y < -3693.8482788297247:\\
        \;\;\;\;t\_0\\
        
        \mathbf{elif}\;y < 6799310503.41891:\\
        \;\;\;\;\frac{x \cdot y}{y + 1}\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0\\
        
        
        \end{array}
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2024332 
        (FPCore (x y)
          :name "Diagrams.Trail:splitAtParam  from diagrams-lib-1.3.0.3, B"
          :precision binary64
        
          :alt
          (! :herbie-platform default (if (< y -36938482788297247/10000000000000) (- (/ x (* y y)) (- (/ x y) x)) (if (< y 679931050341891/100000) (/ (* x y) (+ y 1)) (- (/ x (* y y)) (- (/ x y) x)))))
        
          (/ (* x y) (+ y 1.0)))