Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, B

Percentage Accurate: 99.8% → 99.8%
Time: 7.2s
Alternatives: 6
Speedup: N/A×

Specification

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

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

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

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

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ x \cdot \left(3 \cdot y\right) - z \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (- (* x (* 3.0 y)) z))
assert(x < y && y < z);
double code(double x, double y, double z) {
	return (x * (3.0 * y)) - z;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * (3.0d0 * y)) - z
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	return (x * (3.0 * y)) - z;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	return (x * (3.0 * y)) - z
x, y, z = sort([x, y, z])
function code(x, y, z)
	return Float64(Float64(x * Float64(3.0 * y)) - z)
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
	tmp = (x * (3.0 * y)) - z;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := N[(N[(x * N[(3.0 * y), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
x \cdot \left(3 \cdot y\right) - z
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 75.9% accurate, 0.2× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ \begin{array}{l} t_0 := y \cdot \left(x \cdot 3\right)\\ t_1 := x \cdot \left(3 \cdot y\right)\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{+56}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 10^{-71}:\\ \;\;\;\;-z\\ \mathbf{elif}\;t_0 \leq 2000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+110}:\\ \;\;\;\;-z\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(x \cdot y\right)\\ \end{array} \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (* x 3.0))) (t_1 (* x (* 3.0 y))))
   (if (<= t_0 -5e+56)
     t_1
     (if (<= t_0 1e-71)
       (- z)
       (if (<= t_0 2000000.0)
         t_1
         (if (<= t_0 5e+110) (- z) (* 3.0 (* x y))))))))
assert(x < y && y < z);
double code(double x, double y, double z) {
	double t_0 = y * (x * 3.0);
	double t_1 = x * (3.0 * y);
	double tmp;
	if (t_0 <= -5e+56) {
		tmp = t_1;
	} else if (t_0 <= 1e-71) {
		tmp = -z;
	} else if (t_0 <= 2000000.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+110) {
		tmp = -z;
	} else {
		tmp = 3.0 * (x * y);
	}
	return tmp;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = y * (x * 3.0d0)
    t_1 = x * (3.0d0 * y)
    if (t_0 <= (-5d+56)) then
        tmp = t_1
    else if (t_0 <= 1d-71) then
        tmp = -z
    else if (t_0 <= 2000000.0d0) then
        tmp = t_1
    else if (t_0 <= 5d+110) then
        tmp = -z
    else
        tmp = 3.0d0 * (x * y)
    end if
    code = tmp
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	double t_0 = y * (x * 3.0);
	double t_1 = x * (3.0 * y);
	double tmp;
	if (t_0 <= -5e+56) {
		tmp = t_1;
	} else if (t_0 <= 1e-71) {
		tmp = -z;
	} else if (t_0 <= 2000000.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+110) {
		tmp = -z;
	} else {
		tmp = 3.0 * (x * y);
	}
	return tmp;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	t_0 = y * (x * 3.0)
	t_1 = x * (3.0 * y)
	tmp = 0
	if t_0 <= -5e+56:
		tmp = t_1
	elif t_0 <= 1e-71:
		tmp = -z
	elif t_0 <= 2000000.0:
		tmp = t_1
	elif t_0 <= 5e+110:
		tmp = -z
	else:
		tmp = 3.0 * (x * y)
	return tmp
x, y, z = sort([x, y, z])
function code(x, y, z)
	t_0 = Float64(y * Float64(x * 3.0))
	t_1 = Float64(x * Float64(3.0 * y))
	tmp = 0.0
	if (t_0 <= -5e+56)
		tmp = t_1;
	elseif (t_0 <= 1e-71)
		tmp = Float64(-z);
	elseif (t_0 <= 2000000.0)
		tmp = t_1;
	elseif (t_0 <= 5e+110)
		tmp = Float64(-z);
	else
		tmp = Float64(3.0 * Float64(x * y));
	end
	return tmp
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
	t_0 = y * (x * 3.0);
	t_1 = x * (3.0 * y);
	tmp = 0.0;
	if (t_0 <= -5e+56)
		tmp = t_1;
	elseif (t_0 <= 1e-71)
		tmp = -z;
	elseif (t_0 <= 2000000.0)
		tmp = t_1;
	elseif (t_0 <= 5e+110)
		tmp = -z;
	else
		tmp = 3.0 * (x * y);
	end
	tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(x * 3.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x * N[(3.0 * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -5e+56], t$95$1, If[LessEqual[t$95$0, 1e-71], (-z), If[LessEqual[t$95$0, 2000000.0], t$95$1, If[LessEqual[t$95$0, 5e+110], (-z), N[(3.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
t_0 := y \cdot \left(x \cdot 3\right)\\
t_1 := x \cdot \left(3 \cdot y\right)\\
\mathbf{if}\;t_0 \leq -5 \cdot 10^{+56}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 10^{-71}:\\
\;\;\;\;-z\\

\mathbf{elif}\;t_0 \leq 2000000:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+110}:\\
\;\;\;\;-z\\

\mathbf{else}:\\
\;\;\;\;3 \cdot \left(x \cdot y\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 71.6% accurate, 0.8× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq -4.7 \cdot 10^{-80} \lor \neg \left(y \leq 5.4 \cdot 10^{+117}\right):\\ \;\;\;\;3 \cdot \left(x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;-z\\ \end{array} \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
 :precision binary64
 (if (or (<= y -4.7e-80) (not (<= y 5.4e+117))) (* 3.0 (* x y)) (- z)))
assert(x < y && y < z);
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -4.7e-80) || !(y <= 5.4e+117)) {
		tmp = 3.0 * (x * y);
	} else {
		tmp = -z;
	}
	return tmp;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
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 ((y <= (-4.7d-80)) .or. (.not. (y <= 5.4d+117))) then
        tmp = 3.0d0 * (x * y)
    else
        tmp = -z
    end if
    code = tmp
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	double tmp;
	if ((y <= -4.7e-80) || !(y <= 5.4e+117)) {
		tmp = 3.0 * (x * y);
	} else {
		tmp = -z;
	}
	return tmp;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	tmp = 0
	if (y <= -4.7e-80) or not (y <= 5.4e+117):
		tmp = 3.0 * (x * y)
	else:
		tmp = -z
	return tmp
x, y, z = sort([x, y, z])
function code(x, y, z)
	tmp = 0.0
	if ((y <= -4.7e-80) || !(y <= 5.4e+117))
		tmp = Float64(3.0 * Float64(x * y));
	else
		tmp = Float64(-z);
	end
	return tmp
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((y <= -4.7e-80) || ~((y <= 5.4e+117)))
		tmp = 3.0 * (x * y);
	else
		tmp = -z;
	end
	tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := If[Or[LessEqual[y, -4.7e-80], N[Not[LessEqual[y, 5.4e+117]], $MachinePrecision]], N[(3.0 * N[(x * y), $MachinePrecision]), $MachinePrecision], (-z)]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.7 \cdot 10^{-80} \lor \neg \left(y \leq 5.4 \cdot 10^{+117}\right):\\
\;\;\;\;3 \cdot \left(x \cdot y\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ 3 \cdot \left(x \cdot y\right) - z \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (- (* 3.0 (* x y)) z))
assert(x < y && y < z);
double code(double x, double y, double z) {
	return (3.0 * (x * y)) - z;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (3.0d0 * (x * y)) - z
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	return (3.0 * (x * y)) - z;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	return (3.0 * (x * y)) - z
x, y, z = sort([x, y, z])
function code(x, y, z)
	return Float64(Float64(3.0 * Float64(x * y)) - z)
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
	tmp = (3.0 * (x * y)) - z;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := N[(N[(3.0 * N[(x * y), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
3 \cdot \left(x \cdot y\right) - z
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 49.8% accurate, 3.5× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ -z \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 (- z))
assert(x < y && y < z);
double code(double x, double y, double z) {
	return -z;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = -z
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	return -z;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	return -z
x, y, z = sort([x, y, z])
function code(x, y, z)
	return Float64(-z)
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
	tmp = -z;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := (-z)
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
-z
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 2.3% accurate, 7.0× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ z \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 z)
assert(x < y && y < z);
double code(double x, double y, double z) {
	return z;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = z
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	return z;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	return z
x, y, z = sort([x, y, z])
function code(x, y, z)
	return z
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
	tmp = z;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := z
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
z
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Developer target: 99.8% accurate, 1.0× speedup?

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

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

Reproduce

?
herbie shell --seed 2024010 
(FPCore (x y z)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, B"
  :precision binary64

  :herbie-target
  (- (* x (* 3.0 y)) z)

  (- (* (* x 3.0) y) z))