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

Percentage Accurate: 99.7% → 99.7%
Time: 1.1s
Alternatives: 1
Speedup: 1.0×

Specification

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

\\
\left(x \cdot 27\right) \cdot 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 1 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.7% accurate, 1.0× speedup?

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

\\
\left(x \cdot 27\right) \cdot y
\end{array}

Alternative 1: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} y\_m = \left|y\right| \\ y\_s = \mathsf{copysign}\left(1, y\right) \\ [x, y_m] = \mathsf{sort}([x, y_m])\\ \\ y\_s \cdot \left(y\_m \cdot \left(27 \cdot x\right)\right) \end{array} \]
y\_m = (fabs.f64 y)
y\_s = (copysign.f64 #s(literal 1 binary64) y)
NOTE: x and y_m should be sorted in increasing order before calling this function.
(FPCore (y_s x y_m) :precision binary64 (* y_s (* y_m (* 27.0 x))))
y\_m = fabs(y);
y\_s = copysign(1.0, y);
assert(x < y_m);
double code(double y_s, double x, double y_m) {
	return y_s * (y_m * (27.0 * x));
}
y\_m = abs(y)
y\_s = copysign(1.0d0, y)
NOTE: x and y_m should be sorted in increasing order before calling this function.
real(8) function code(y_s, x, y_m)
    real(8), intent (in) :: y_s
    real(8), intent (in) :: x
    real(8), intent (in) :: y_m
    code = y_s * (y_m * (27.0d0 * x))
end function
y\_m = Math.abs(y);
y\_s = Math.copySign(1.0, y);
assert x < y_m;
public static double code(double y_s, double x, double y_m) {
	return y_s * (y_m * (27.0 * x));
}
y\_m = math.fabs(y)
y\_s = math.copysign(1.0, y)
[x, y_m] = sort([x, y_m])
def code(y_s, x, y_m):
	return y_s * (y_m * (27.0 * x))
y\_m = abs(y)
y\_s = copysign(1.0, y)
x, y_m = sort([x, y_m])
function code(y_s, x, y_m)
	return Float64(y_s * Float64(y_m * Float64(27.0 * x)))
end
y\_m = abs(y);
y\_s = sign(y) * abs(1.0);
x, y_m = num2cell(sort([x, y_m])){:}
function tmp = code(y_s, x, y_m)
	tmp = y_s * (y_m * (27.0 * x));
end
y\_m = N[Abs[y], $MachinePrecision]
y\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x and y_m should be sorted in increasing order before calling this function.
code[y$95$s_, x_, y$95$m_] := N[(y$95$s * N[(y$95$m * N[(27.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y\_m = \left|y\right|
\\
y\_s = \mathsf{copysign}\left(1, y\right)
\\
[x, y_m] = \mathsf{sort}([x, y_m])\\
\\
y\_s \cdot \left(y\_m \cdot \left(27 \cdot x\right)\right)
\end{array}
Derivation
  1. Initial program 99.4%

    \[\left(x \cdot 27\right) \cdot y \]
  2. Add Preprocessing
  3. Final simplification99.4%

    \[\leadsto y \cdot \left(27 \cdot x\right) \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2024273 
(FPCore (x y)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, F"
  :precision binary64
  (* (* x 27.0) y))