Diagrams.Solve.Polynomial:quartForm from diagrams-solve-0.1, D

Percentage Accurate: 100.0% → 100.0%
Time: 2.3s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \frac{x \cdot y}{2} - \frac{z}{8} \end{array} \]
(FPCore (x y z) :precision binary64 (- (/ (* x y) 2.0) (/ z 8.0)))
double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
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) / 2.0d0) - (z / 8.0d0)
end function
public static double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
def code(x, y, z):
	return ((x * y) / 2.0) - (z / 8.0)
function code(x, y, z)
	return Float64(Float64(Float64(x * y) / 2.0) - Float64(z / 8.0))
end
function tmp = code(x, y, z)
	tmp = ((x * y) / 2.0) - (z / 8.0);
end
code[x_, y_, z_] := N[(N[(N[(x * y), $MachinePrecision] / 2.0), $MachinePrecision] - N[(z / 8.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot y}{2} - \frac{z}{8}
\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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x \cdot y}{2} - \frac{z}{8} \end{array} \]
(FPCore (x y z) :precision binary64 (- (/ (* x y) 2.0) (/ z 8.0)))
double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
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) / 2.0d0) - (z / 8.0d0)
end function
public static double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
def code(x, y, z):
	return ((x * y) / 2.0) - (z / 8.0)
function code(x, y, z)
	return Float64(Float64(Float64(x * y) / 2.0) - Float64(z / 8.0))
end
function tmp = code(x, y, z)
	tmp = ((x * y) / 2.0) - (z / 8.0);
end
code[x_, y_, z_] := N[(N[(N[(x * y), $MachinePrecision] / 2.0), $MachinePrecision] - N[(z / 8.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot y}{2} - \frac{z}{8}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x \cdot y}{2} - \frac{z}{8} \end{array} \]
(FPCore (x y z) :precision binary64 (- (/ (* x y) 2.0) (/ z 8.0)))
double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
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) / 2.0d0) - (z / 8.0d0)
end function
public static double code(double x, double y, double z) {
	return ((x * y) / 2.0) - (z / 8.0);
}
def code(x, y, z):
	return ((x * y) / 2.0) - (z / 8.0)
function code(x, y, z)
	return Float64(Float64(Float64(x * y) / 2.0) - Float64(z / 8.0))
end
function tmp = code(x, y, z)
	tmp = ((x * y) / 2.0) - (z / 8.0);
end
code[x_, y_, z_] := N[(N[(N[(x * y), $MachinePrecision] / 2.0), $MachinePrecision] - N[(z / 8.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{x \cdot y}{2} - \frac{z}{8}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{x \cdot y}{2} - \frac{z}{8} \]
  2. Final simplification100.0%

    \[\leadsto \frac{x \cdot y}{2} - \frac{z}{8} \]

Alternative 2: 67.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4.2 \cdot 10^{+89} \lor \neg \left(x \leq 5 \cdot 10^{-145}\right):\\ \;\;\;\;y \cdot \left(x \cdot 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot -0.125\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -4.2e+89) (not (<= x 5e-145))) (* y (* x 0.5)) (* z -0.125)))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -4.2e+89) || !(x <= 5e-145)) {
		tmp = y * (x * 0.5);
	} else {
		tmp = z * -0.125;
	}
	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 ((x <= (-4.2d+89)) .or. (.not. (x <= 5d-145))) then
        tmp = y * (x * 0.5d0)
    else
        tmp = z * (-0.125d0)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((x <= -4.2e+89) || !(x <= 5e-145)) {
		tmp = y * (x * 0.5);
	} else {
		tmp = z * -0.125;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (x <= -4.2e+89) or not (x <= 5e-145):
		tmp = y * (x * 0.5)
	else:
		tmp = z * -0.125
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((x <= -4.2e+89) || !(x <= 5e-145))
		tmp = Float64(y * Float64(x * 0.5));
	else
		tmp = Float64(z * -0.125);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((x <= -4.2e+89) || ~((x <= 5e-145)))
		tmp = y * (x * 0.5);
	else
		tmp = z * -0.125;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[x, -4.2e+89], N[Not[LessEqual[x, 5e-145]], $MachinePrecision]], N[(y * N[(x * 0.5), $MachinePrecision]), $MachinePrecision], N[(z * -0.125), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.2 \cdot 10^{+89} \lor \neg \left(x \leq 5 \cdot 10^{-145}\right):\\
\;\;\;\;y \cdot \left(x \cdot 0.5\right)\\

\mathbf{else}:\\
\;\;\;\;z \cdot -0.125\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -4.19999999999999972e89 or 4.9999999999999998e-145 < x

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{0.5 \cdot \left(y \cdot x\right)} \]
    3. Step-by-step derivation
      1. associate-*r*66.9%

        \[\leadsto \color{blue}{\left(0.5 \cdot y\right) \cdot x} \]
      2. *-commutative66.9%

        \[\leadsto \color{blue}{\left(y \cdot 0.5\right)} \cdot x \]
      3. associate-*r*66.9%

        \[\leadsto \color{blue}{y \cdot \left(0.5 \cdot x\right)} \]
    4. Simplified66.9%

      \[\leadsto \color{blue}{y \cdot \left(0.5 \cdot x\right)} \]

    if -4.19999999999999972e89 < x < 4.9999999999999998e-145

    1. Initial program 100.0%

      \[\frac{x \cdot y}{2} - \frac{z}{8} \]
    2. Taylor expanded in x around 0 74.7%

      \[\leadsto \color{blue}{-0.125 \cdot z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification70.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.2 \cdot 10^{+89} \lor \neg \left(x \leq 5 \cdot 10^{-145}\right):\\ \;\;\;\;y \cdot \left(x \cdot 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot -0.125\\ \end{array} \]

Alternative 3: 50.3% accurate, 3.0× speedup?

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

\\
z \cdot -0.125
\end{array}
Derivation
  1. Initial program 100.0%

    \[\frac{x \cdot y}{2} - \frac{z}{8} \]
  2. Taylor expanded in x around 0 52.4%

    \[\leadsto \color{blue}{-0.125 \cdot z} \]
  3. Final simplification52.4%

    \[\leadsto z \cdot -0.125 \]

Reproduce

?
herbie shell --seed 2023187 
(FPCore (x y z)
  :name "Diagrams.Solve.Polynomial:quartForm  from diagrams-solve-0.1, D"
  :precision binary64
  (- (/ (* x y) 2.0) (/ z 8.0)))