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

Percentage Accurate: 85.4% → 96.1%
Time: 6.8s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \frac{x \cdot \left(y - z\right)}{y} \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (- y z)) y))
double code(double x, double y, double z) {
	return (x * (y - z)) / y;
}
real(8) function code(x, y, z)
    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]
\begin{array}{l}

\\
\frac{x \cdot \left(y - z\right)}{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 3 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: 85.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x \cdot \left(y - z\right)}{y} \end{array} \]
(FPCore (x y z) :precision binary64 (/ (* x (- y z)) y))
double code(double x, double y, double z) {
	return (x * (y - z)) / y;
}
real(8) function code(x, y, z)
    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]
\begin{array}{l}

\\
\frac{x \cdot \left(y - z\right)}{y}
\end{array}

Alternative 1: 96.1% accurate, 0.6× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \begin{array}{l} \mathbf{if}\;x\_m \leq 2.8 \cdot 10^{-86}:\\ \;\;\;\;\frac{x\_m \cdot \left(y - z\right)}{y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x\_m}{\frac{y}{y - z}}\\ \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z)
 :precision binary64
 (* x_s (if (<= x_m 2.8e-86) (/ (* x_m (- y z)) y) (/ x_m (/ y (- y z))))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (x_m <= 2.8e-86) {
		tmp = (x_m * (y - z)) / y;
	} else {
		tmp = x_m / (y / (y - z));
	}
	return x_s * tmp;
}
x\_m = abs(x)
x\_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (x_m <= 2.8d-86) then
        tmp = (x_m * (y - z)) / y
    else
        tmp = x_m / (y / (y - z))
    end if
    code = x_s * tmp
end function
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (x_m <= 2.8e-86) {
		tmp = (x_m * (y - z)) / y;
	} else {
		tmp = x_m / (y / (y - z));
	}
	return x_s * tmp;
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m, y, z):
	tmp = 0
	if x_m <= 2.8e-86:
		tmp = (x_m * (y - z)) / y
	else:
		tmp = x_m / (y / (y - z))
	return x_s * tmp
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m, y, z)
	tmp = 0.0
	if (x_m <= 2.8e-86)
		tmp = Float64(Float64(x_m * Float64(y - z)) / y);
	else
		tmp = Float64(x_m / Float64(y / Float64(y - z)));
	end
	return Float64(x_s * tmp)
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp_2 = code(x_s, x_m, y, z)
	tmp = 0.0;
	if (x_m <= 2.8e-86)
		tmp = (x_m * (y - z)) / y;
	else
		tmp = x_m / (y / (y - z));
	end
	tmp_2 = x_s * tmp;
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[x$95$m, 2.8e-86], N[(N[(x$95$m * N[(y - z), $MachinePrecision]), $MachinePrecision] / y), $MachinePrecision], N[(x$95$m / N[(y / N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 2.8 \cdot 10^{-86}:\\
\;\;\;\;\frac{x\_m \cdot \left(y - z\right)}{y}\\

\mathbf{else}:\\
\;\;\;\;\frac{x\_m}{\frac{y}{y - z}}\\


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

    1. Initial program 86.9%

      \[\frac{x \cdot \left(y - z\right)}{y} \]
    2. Add Preprocessing

    if 2.80000000000000009e-86 < x

    1. Initial program 85.8%

      \[\frac{x \cdot \left(y - z\right)}{y} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. associate-/l*N/A

        \[\leadsto x \cdot \color{blue}{\frac{y - z}{y}} \]
      2. clear-numN/A

        \[\leadsto x \cdot \frac{1}{\color{blue}{\frac{y}{y - z}}} \]
      3. un-div-invN/A

        \[\leadsto \frac{x}{\color{blue}{\frac{y}{y - z}}} \]
      4. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(x, \color{blue}{\left(\frac{y}{y - z}\right)}\right) \]
      5. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(x, \mathsf{/.f64}\left(y, \color{blue}{\left(y - z\right)}\right)\right) \]
      6. --lowering--.f6499.9%

        \[\leadsto \mathsf{/.f64}\left(x, \mathsf{/.f64}\left(y, \mathsf{\_.f64}\left(y, \color{blue}{z}\right)\right)\right) \]
    4. Applied egg-rr99.9%

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

Alternative 2: 96.5% accurate, 1.0× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \frac{x\_m}{\frac{y}{y - z}} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z) :precision binary64 (* x_s (/ x_m (/ y (- y z)))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
	return x_s * (x_m / (y / (y - z)));
}
x\_m = abs(x)
x\_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x_s * (x_m / (y / (y - z)))
end function
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m, double y, double z) {
	return x_s * (x_m / (y / (y - z)));
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m, y, z):
	return x_s * (x_m / (y / (y - z)))
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m, y, z)
	return Float64(x_s * Float64(x_m / Float64(y / Float64(y - z))))
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m, y, z)
	tmp = x_s * (x_m / (y / (y - z)));
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * N[(x$95$m / N[(y / N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot \frac{x\_m}{\frac{y}{y - z}}
\end{array}
Derivation
  1. Initial program 86.6%

    \[\frac{x \cdot \left(y - z\right)}{y} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. associate-/l*N/A

      \[\leadsto x \cdot \color{blue}{\frac{y - z}{y}} \]
    2. clear-numN/A

      \[\leadsto x \cdot \frac{1}{\color{blue}{\frac{y}{y - z}}} \]
    3. un-div-invN/A

      \[\leadsto \frac{x}{\color{blue}{\frac{y}{y - z}}} \]
    4. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(x, \color{blue}{\left(\frac{y}{y - z}\right)}\right) \]
    5. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(x, \mathsf{/.f64}\left(y, \color{blue}{\left(y - z\right)}\right)\right) \]
    6. --lowering--.f6496.4%

      \[\leadsto \mathsf{/.f64}\left(x, \mathsf{/.f64}\left(y, \mathsf{\_.f64}\left(y, \color{blue}{z}\right)\right)\right) \]
  4. Applied egg-rr96.4%

    \[\leadsto \color{blue}{\frac{x}{\frac{y}{y - z}}} \]
  5. Add Preprocessing

Alternative 3: 49.5% accurate, 7.0× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot x\_m \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z) :precision binary64 (* x_s x_m))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
	return x_s * x_m;
}
x\_m = abs(x)
x\_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x_s * x_m
end function
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m, double y, double z) {
	return x_s * x_m;
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
def code(x_s, x_m, y, z):
	return x_s * x_m
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m, y, z)
	return Float64(x_s * x_m)
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
function tmp = code(x_s, x_m, y, z)
	tmp = x_s * x_m;
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * x$95$m), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot x\_m
\end{array}
Derivation
  1. Initial program 86.6%

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

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

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

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

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z < -2.060202331921739 \cdot 10^{+104}:\\ \;\;\;\;x - \frac{z \cdot x}{y}\\ \mathbf{elif}\;z < 1.6939766013828526 \cdot 10^{+213}:\\ \;\;\;\;\frac{x}{\frac{y}{y - z}}\\ \mathbf{else}:\\ \;\;\;\;\left(y - z\right) \cdot \frac{x}{y}\\ \end{array} \end{array} \]
    (FPCore (x y z)
     :precision binary64
     (if (< z -2.060202331921739e+104)
       (- x (/ (* z x) y))
       (if (< z 1.6939766013828526e+213) (/ x (/ y (- y z))) (* (- y z) (/ x y)))))
    double code(double x, double y, double z) {
    	double tmp;
    	if (z < -2.060202331921739e+104) {
    		tmp = x - ((z * x) / y);
    	} else if (z < 1.6939766013828526e+213) {
    		tmp = x / (y / (y - z));
    	} else {
    		tmp = (y - z) * (x / y);
    	}
    	return tmp;
    }
    
    real(8) function code(x, y, z)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        real(8) :: tmp
        if (z < (-2.060202331921739d+104)) then
            tmp = x - ((z * x) / y)
        else if (z < 1.6939766013828526d+213) then
            tmp = x / (y / (y - z))
        else
            tmp = (y - z) * (x / y)
        end if
        code = tmp
    end function
    
    public static double code(double x, double y, double z) {
    	double tmp;
    	if (z < -2.060202331921739e+104) {
    		tmp = x - ((z * x) / y);
    	} else if (z < 1.6939766013828526e+213) {
    		tmp = x / (y / (y - z));
    	} else {
    		tmp = (y - z) * (x / y);
    	}
    	return tmp;
    }
    
    def code(x, y, z):
    	tmp = 0
    	if z < -2.060202331921739e+104:
    		tmp = x - ((z * x) / y)
    	elif z < 1.6939766013828526e+213:
    		tmp = x / (y / (y - z))
    	else:
    		tmp = (y - z) * (x / y)
    	return tmp
    
    function code(x, y, z)
    	tmp = 0.0
    	if (z < -2.060202331921739e+104)
    		tmp = Float64(x - Float64(Float64(z * x) / y));
    	elseif (z < 1.6939766013828526e+213)
    		tmp = Float64(x / Float64(y / Float64(y - z)));
    	else
    		tmp = Float64(Float64(y - z) * Float64(x / y));
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y, z)
    	tmp = 0.0;
    	if (z < -2.060202331921739e+104)
    		tmp = x - ((z * x) / y);
    	elseif (z < 1.6939766013828526e+213)
    		tmp = x / (y / (y - z));
    	else
    		tmp = (y - z) * (x / y);
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_, z_] := If[Less[z, -2.060202331921739e+104], N[(x - N[(N[(z * x), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision], If[Less[z, 1.6939766013828526e+213], N[(x / N[(y / N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(y - z), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;z < -2.060202331921739 \cdot 10^{+104}:\\
    \;\;\;\;x - \frac{z \cdot x}{y}\\
    
    \mathbf{elif}\;z < 1.6939766013828526 \cdot 10^{+213}:\\
    \;\;\;\;\frac{x}{\frac{y}{y - z}}\\
    
    \mathbf{else}:\\
    \;\;\;\;\left(y - z\right) \cdot \frac{x}{y}\\
    
    
    \end{array}
    \end{array}
    

    Reproduce

    ?
    herbie shell --seed 2024191 
    (FPCore (x y z)
      :name "Diagrams.Backend.Cairo.Internal:setTexture from diagrams-cairo-1.3.0.3"
      :precision binary64
    
      :alt
      (! :herbie-platform default (if (< z -206020233192173900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- x (/ (* z x) y)) (if (< z 1693976601382852600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (/ x (/ y (- y z))) (* (- y z) (/ x y)))))
    
      (/ (* x (- y z)) y))