Diagrams.Backend.Cairo.Internal:setTexture from diagrams-cairo-1.3.0.3

Percentage Accurate: 84.1% → 96.4%
Time: 1.3s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\frac{x \cdot \left(y - z\right)}{y} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (/ (* x (- y z)) y))
double code(double x, double y, double z) {
	return (x * (y - z)) / y;
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * (y - z)) / y
end function
public static double code(double x, double y, double z) {
	return (x * (y - z)) / y;
}
def code(x, y, z):
	return (x * (y - z)) / y
function code(x, y, z)
	return Float64(Float64(x * Float64(y - z)) / y)
end
function tmp = code(x, y, z)
	tmp = (x * (y - z)) / y;
end
code[x_, y_, z_] := N[(N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * (y - z)) / y
END code
\frac{x \cdot \left(y - z\right)}{y}

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: 84.1% accurate, 1.0× speedup?

\[\frac{x \cdot \left(y - z\right)}{y} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (/ (* x (- y z)) y))
double code(double x, double y, double z) {
	return (x * (y - z)) / y;
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * (y - z)) / y
end function
public static double code(double x, double y, double z) {
	return (x * (y - z)) / y;
}
def code(x, y, z):
	return (x * (y - z)) / y
function code(x, y, z)
	return Float64(Float64(x * Float64(y - z)) / y)
end
function tmp = code(x, y, z)
	tmp = (x * (y - z)) / y;
end
code[x_, y_, z_] := N[(N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * (y - z)) / y
END code
\frac{x \cdot \left(y - z\right)}{y}

Alternative 1: 96.4% accurate, 0.5× speedup?

\[\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 1.8648747152849905 \cdot 10^{-81}:\\ \;\;\;\;\frac{\left|x\right| \cdot \left(y - z\right)}{y}\\ \mathbf{else}:\\ \;\;\;\;\left|x\right| - \left|x\right| \cdot \frac{z}{y}\\ \end{array} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (*
 (copysign 1.0 x)
 (if (<= (fabs x) 1.8648747152849905e-81)
   (/ (* (fabs x) (- y z)) y)
   (- (fabs x) (* (fabs x) (/ z y))))))
double code(double x, double y, double z) {
	double tmp;
	if (fabs(x) <= 1.8648747152849905e-81) {
		tmp = (fabs(x) * (y - z)) / y;
	} else {
		tmp = fabs(x) - (fabs(x) * (z / y));
	}
	return copysign(1.0, x) * tmp;
}
public static double code(double x, double y, double z) {
	double tmp;
	if (Math.abs(x) <= 1.8648747152849905e-81) {
		tmp = (Math.abs(x) * (y - z)) / y;
	} else {
		tmp = Math.abs(x) - (Math.abs(x) * (z / y));
	}
	return Math.copySign(1.0, x) * tmp;
}
def code(x, y, z):
	tmp = 0
	if math.fabs(x) <= 1.8648747152849905e-81:
		tmp = (math.fabs(x) * (y - z)) / y
	else:
		tmp = math.fabs(x) - (math.fabs(x) * (z / y))
	return math.copysign(1.0, x) * tmp
function code(x, y, z)
	tmp = 0.0
	if (abs(x) <= 1.8648747152849905e-81)
		tmp = Float64(Float64(abs(x) * Float64(y - z)) / y);
	else
		tmp = Float64(abs(x) - Float64(abs(x) * Float64(z / y)));
	end
	return Float64(copysign(1.0, x) * tmp)
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (abs(x) <= 1.8648747152849905e-81)
		tmp = (abs(x) * (y - z)) / y;
	else
		tmp = abs(x) - (abs(x) * (z / y));
	end
	tmp_2 = (sign(x) * abs(1.0)) * tmp;
end
code[x_, y_, z_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 1.8648747152849905e-81], N[(N[(N[Abs[x], $MachinePrecision] * N[(y - z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision], N[(N[Abs[x], $MachinePrecision] - N[(N[Abs[x], $MachinePrecision] * N[(z / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1.8648747152849905 \cdot 10^{-81}:\\
\;\;\;\;\frac{\left|x\right| \cdot \left(y - z\right)}{y}\\

\mathbf{else}:\\
\;\;\;\;\left|x\right| - \left|x\right| \cdot \frac{z}{y}\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.8648747152849905e-81

    1. Initial program 84.1%

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

    if 1.8648747152849905e-81 < x

    1. Initial program 84.1%

      \[\frac{x \cdot \left(y - z\right)}{y} \]
    2. Step-by-step derivation
      1. Applied rewrites96.4%

        \[\leadsto x - x \cdot \frac{z}{y} \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 2: 96.1% accurate, 0.5× speedup?

    \[\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l} \mathbf{if}\;\left|x\right| \leq 1.8648747152849905 \cdot 10^{-81}:\\ \;\;\;\;\frac{\left|x\right| \cdot \left(y - z\right)}{y}\\ \mathbf{else}:\\ \;\;\;\;\left|x\right| \cdot \frac{y - z}{y}\\ \end{array} \]
    (FPCore (x y z)
      :precision binary64
      :pre TRUE
      (*
     (copysign 1.0 x)
     (if (<= (fabs x) 1.8648747152849905e-81)
       (/ (* (fabs x) (- y z)) y)
       (* (fabs x) (/ (- y z) y)))))
    double code(double x, double y, double z) {
    	double tmp;
    	if (fabs(x) <= 1.8648747152849905e-81) {
    		tmp = (fabs(x) * (y - z)) / y;
    	} else {
    		tmp = fabs(x) * ((y - z) / y);
    	}
    	return copysign(1.0, x) * tmp;
    }
    
    public static double code(double x, double y, double z) {
    	double tmp;
    	if (Math.abs(x) <= 1.8648747152849905e-81) {
    		tmp = (Math.abs(x) * (y - z)) / y;
    	} else {
    		tmp = Math.abs(x) * ((y - z) / y);
    	}
    	return Math.copySign(1.0, x) * tmp;
    }
    
    def code(x, y, z):
    	tmp = 0
    	if math.fabs(x) <= 1.8648747152849905e-81:
    		tmp = (math.fabs(x) * (y - z)) / y
    	else:
    		tmp = math.fabs(x) * ((y - z) / y)
    	return math.copysign(1.0, x) * tmp
    
    function code(x, y, z)
    	tmp = 0.0
    	if (abs(x) <= 1.8648747152849905e-81)
    		tmp = Float64(Float64(abs(x) * Float64(y - z)) / y);
    	else
    		tmp = Float64(abs(x) * Float64(Float64(y - z) / y));
    	end
    	return Float64(copysign(1.0, x) * tmp)
    end
    
    function tmp_2 = code(x, y, z)
    	tmp = 0.0;
    	if (abs(x) <= 1.8648747152849905e-81)
    		tmp = (abs(x) * (y - z)) / y;
    	else
    		tmp = abs(x) * ((y - z) / y);
    	end
    	tmp_2 = (sign(x) * abs(1.0)) * tmp;
    end
    
    code[x_, y_, z_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 1.8648747152849905e-81], N[(N[(N[Abs[x], $MachinePrecision] * N[(y - z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * N[(N[(y - z), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
    
    \mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
    \mathbf{if}\;\left|x\right| \leq 1.8648747152849905 \cdot 10^{-81}:\\
    \;\;\;\;\frac{\left|x\right| \cdot \left(y - z\right)}{y}\\
    
    \mathbf{else}:\\
    \;\;\;\;\left|x\right| \cdot \frac{y - z}{y}\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if x < 1.8648747152849905e-81

      1. Initial program 84.1%

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

      if 1.8648747152849905e-81 < x

      1. Initial program 84.1%

        \[\frac{x \cdot \left(y - z\right)}{y} \]
      2. Step-by-step derivation
        1. Applied rewrites96.4%

          \[\leadsto x \cdot \frac{y - z}{y} \]
      3. Recombined 2 regimes into one program.
      4. Add Preprocessing

      Alternative 3: 96.1% accurate, 1.0× speedup?

      \[x \cdot \frac{y - z}{y} \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (* x (/ (- y z) y)))
      double code(double x, double y, double z) {
      	return x * ((y - z) / y);
      }
      
      real(8) function code(x, y, z)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          code = x * ((y - z) / y)
      end function
      
      public static double code(double x, double y, double z) {
      	return x * ((y - z) / y);
      }
      
      def code(x, y, z):
      	return x * ((y - z) / y)
      
      function code(x, y, z)
      	return Float64(x * Float64(Float64(y - z) / y))
      end
      
      function tmp = code(x, y, z)
      	tmp = x * ((y - z) / y);
      end
      
      code[x_, y_, z_] := N[(x * N[(N[(y - z), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]
      
      f(x, y, z):
      	x in [-inf, +inf],
      	y in [-inf, +inf],
      	z in [-inf, +inf]
      code: THEORY
      BEGIN
      f(x, y, z: real): real =
      	x * ((y - z) / y)
      END code
      x \cdot \frac{y - z}{y}
      
      Derivation
      1. Initial program 84.1%

        \[\frac{x \cdot \left(y - z\right)}{y} \]
      2. Step-by-step derivation
        1. Applied rewrites96.4%

          \[\leadsto x \cdot \frac{y - z}{y} \]
        2. Add Preprocessing

        Alternative 4: 50.9% accurate, 2.6× speedup?

        \[x \cdot 1 \]
        (FPCore (x y z)
          :precision binary64
          :pre TRUE
          (* x 1.0))
        double code(double x, double y, double z) {
        	return x * 1.0;
        }
        
        real(8) function code(x, y, z)
        use fmin_fmax_functions
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8), intent (in) :: z
            code = x * 1.0d0
        end function
        
        public static double code(double x, double y, double z) {
        	return x * 1.0;
        }
        
        def code(x, y, z):
        	return x * 1.0
        
        function code(x, y, z)
        	return Float64(x * 1.0)
        end
        
        function tmp = code(x, y, z)
        	tmp = x * 1.0;
        end
        
        code[x_, y_, z_] := N[(x * 1.0), $MachinePrecision]
        
        f(x, y, z):
        	x in [-inf, +inf],
        	y in [-inf, +inf],
        	z in [-inf, +inf]
        code: THEORY
        BEGIN
        f(x, y, z: real): real =
        	x * (1)
        END code
        x \cdot 1
        
        Derivation
        1. Initial program 84.1%

          \[\frac{x \cdot \left(y - z\right)}{y} \]
        2. Step-by-step derivation
          1. Applied rewrites96.4%

            \[\leadsto x \cdot \frac{y - z}{y} \]
          2. Taylor expanded in y around inf

            \[\leadsto x \cdot 1 \]
          3. Step-by-step derivation
            1. Applied rewrites50.9%

              \[\leadsto x \cdot 1 \]
            2. Add Preprocessing

            Reproduce

            ?
            herbie shell --seed 2026092 
            (FPCore (x y z)
              :name "Diagrams.Backend.Cairo.Internal:setTexture from diagrams-cairo-1.3.0.3"
              :precision binary64
              (/ (* x (- y z)) y))