Graphics.Rasterific.QuadraticFormula:discriminant from Rasterific-0.6.1

Percentage Accurate: 98.2% → 99.7%
Time: 3.5s
Alternatives: 5
Speedup: 0.8×

Specification

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

\\
x \cdot x - \left(y \cdot 4\right) \cdot 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 5 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: 98.2% accurate, 1.0× speedup?

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

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

Alternative 1: 99.7% accurate, 0.1× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 8.5 \cdot 10^{+172}:\\ \;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y z)
 :precision binary64
 (if (<= x 8.5e+172) (fma x x (* y (* z -4.0))) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	double tmp;
	if (x <= 8.5e+172) {
		tmp = fma(x, x, (y * (z * -4.0)));
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
function code(x, y, z)
	tmp = 0.0
	if (x <= 8.5e+172)
		tmp = fma(x, x, Float64(y * Float64(z * -4.0)));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_, y_, z_] := If[LessEqual[x, 8.5e+172], N[(x * x + N[(y * N[(z * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 8.5 \cdot 10^{+172}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(z \cdot -4\right)\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 8.50000000000000053e172

    1. Initial program 97.3%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Step-by-step derivation
      1. fma-neg98.2%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, -\left(y \cdot 4\right) \cdot z\right)} \]
      2. *-commutative98.2%

        \[\leadsto \mathsf{fma}\left(x, x, -\color{blue}{z \cdot \left(y \cdot 4\right)}\right) \]
      3. *-commutative98.2%

        \[\leadsto \mathsf{fma}\left(x, x, -z \cdot \color{blue}{\left(4 \cdot y\right)}\right) \]
      4. associate-*l*98.2%

        \[\leadsto \mathsf{fma}\left(x, x, -\color{blue}{\left(z \cdot 4\right) \cdot y}\right) \]
      5. distribute-lft-neg-in98.2%

        \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{\left(-z \cdot 4\right) \cdot y}\right) \]
      6. *-commutative98.2%

        \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{y \cdot \left(-z \cdot 4\right)}\right) \]
      7. distribute-rgt-neg-in98.2%

        \[\leadsto \mathsf{fma}\left(x, x, y \cdot \color{blue}{\left(z \cdot \left(-4\right)\right)}\right) \]
      8. metadata-eval98.2%

        \[\leadsto \mathsf{fma}\left(x, x, y \cdot \left(z \cdot \color{blue}{-4}\right)\right) \]
    3. Simplified98.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, y \cdot \left(z \cdot -4\right)\right)} \]

    if 8.50000000000000053e172 < x

    1. Initial program 94.3%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow2100.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified100.0%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 8.5 \cdot 10^{+172}:\\ \;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 2: 99.4% accurate, 0.1× speedup?

\[\begin{array}{l} x = |x|\\ \\ \mathsf{fma}\left(y, z \cdot -4, x \cdot x\right) \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y z) :precision binary64 (fma y (* z -4.0) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	return fma(y, (z * -4.0), (x * x));
}
x = abs(x)
function code(x, y, z)
	return fma(y, Float64(z * -4.0), Float64(x * x))
end
NOTE: x should be positive before calling this function
code[x_, y_, z_] := N[(y * N[(z * -4.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
\mathsf{fma}\left(y, z \cdot -4, x \cdot x\right)
\end{array}
Derivation
  1. Initial program 96.9%

    \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
  2. Step-by-step derivation
    1. sub-neg96.9%

      \[\leadsto \color{blue}{x \cdot x + \left(-\left(y \cdot 4\right) \cdot z\right)} \]
    2. +-commutative96.9%

      \[\leadsto \color{blue}{\left(-\left(y \cdot 4\right) \cdot z\right) + x \cdot x} \]
    3. distribute-rgt-neg-out96.9%

      \[\leadsto \color{blue}{\left(y \cdot 4\right) \cdot \left(-z\right)} + x \cdot x \]
    4. associate-*l*96.9%

      \[\leadsto \color{blue}{y \cdot \left(4 \cdot \left(-z\right)\right)} + x \cdot x \]
    5. fma-def99.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, 4 \cdot \left(-z\right), x \cdot x\right)} \]
    6. *-commutative99.2%

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{\left(-z\right) \cdot 4}, x \cdot x\right) \]
    7. distribute-lft-neg-in99.2%

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{-z \cdot 4}, x \cdot x\right) \]
    8. distribute-rgt-neg-in99.2%

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{z \cdot \left(-4\right)}, x \cdot x\right) \]
    9. metadata-eval99.2%

      \[\leadsto \mathsf{fma}\left(y, z \cdot \color{blue}{-4}, x \cdot x\right) \]
  3. Simplified99.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y, z \cdot -4, x \cdot x\right)} \]
  4. Final simplification99.2%

    \[\leadsto \mathsf{fma}\left(y, z \cdot -4, x \cdot x\right) \]

Alternative 3: 99.3% accurate, 0.8× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.15 \cdot 10^{+146}:\\ \;\;\;\;x \cdot x - z \cdot \left(y \cdot 4\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y z)
 :precision binary64
 (if (<= x 1.15e+146) (- (* x x) (* z (* y 4.0))) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	double tmp;
	if (x <= 1.15e+146) {
		tmp = (x * x) - (z * (y * 4.0));
	} else {
		tmp = x * x;
	}
	return tmp;
}
NOTE: x should be positive 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 (x <= 1.15d+146) then
        tmp = (x * x) - (z * (y * 4.0d0))
    else
        tmp = x * x
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= 1.15e+146) {
		tmp = (x * x) - (z * (y * 4.0));
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
def code(x, y, z):
	tmp = 0
	if x <= 1.15e+146:
		tmp = (x * x) - (z * (y * 4.0))
	else:
		tmp = x * x
	return tmp
x = abs(x)
function code(x, y, z)
	tmp = 0.0
	if (x <= 1.15e+146)
		tmp = Float64(Float64(x * x) - Float64(z * Float64(y * 4.0)));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= 1.15e+146)
		tmp = (x * x) - (z * (y * 4.0));
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_, y_, z_] := If[LessEqual[x, 1.15e+146], N[(N[(x * x), $MachinePrecision] - N[(z * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.15 \cdot 10^{+146}:\\
\;\;\;\;x \cdot x - z \cdot \left(y \cdot 4\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.15e146

    1. Initial program 97.2%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]

    if 1.15e146 < x

    1. Initial program 94.9%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow2100.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified100.0%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.15 \cdot 10^{+146}:\\ \;\;\;\;x \cdot x - z \cdot \left(y \cdot 4\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 4: 83.1% accurate, 1.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1.65 \cdot 10^{+117}:\\ \;\;\;\;y \cdot \left(z \cdot -4\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y z)
 :precision binary64
 (if (<= (* x x) 1.65e+117) (* y (* z -4.0)) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	double tmp;
	if ((x * x) <= 1.65e+117) {
		tmp = y * (z * -4.0);
	} else {
		tmp = x * x;
	}
	return tmp;
}
NOTE: x should be positive 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 ((x * x) <= 1.65d+117) then
        tmp = y * (z * (-4.0d0))
    else
        tmp = x * x
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x, double y, double z) {
	double tmp;
	if ((x * x) <= 1.65e+117) {
		tmp = y * (z * -4.0);
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
def code(x, y, z):
	tmp = 0
	if (x * x) <= 1.65e+117:
		tmp = y * (z * -4.0)
	else:
		tmp = x * x
	return tmp
x = abs(x)
function code(x, y, z)
	tmp = 0.0
	if (Float64(x * x) <= 1.65e+117)
		tmp = Float64(y * Float64(z * -4.0));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((x * x) <= 1.65e+117)
		tmp = y * (z * -4.0);
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_, y_, z_] := If[LessEqual[N[(x * x), $MachinePrecision], 1.65e+117], N[(y * N[(z * -4.0), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1.65 \cdot 10^{+117}:\\
\;\;\;\;y \cdot \left(z \cdot -4\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 1.6499999999999999e117

    1. Initial program 100.0%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Taylor expanded in x around 0 78.4%

      \[\leadsto \color{blue}{-4 \cdot \left(y \cdot z\right)} \]
    3. Step-by-step derivation
      1. *-commutative78.4%

        \[\leadsto \color{blue}{\left(y \cdot z\right) \cdot -4} \]
      2. associate-*r*78.4%

        \[\leadsto \color{blue}{y \cdot \left(z \cdot -4\right)} \]
      3. *-commutative78.4%

        \[\leadsto y \cdot \color{blue}{\left(-4 \cdot z\right)} \]
    4. Simplified78.4%

      \[\leadsto \color{blue}{y \cdot \left(-4 \cdot z\right)} \]

    if 1.6499999999999999e117 < (*.f64 x x)

    1. Initial program 93.3%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Taylor expanded in x around inf 91.8%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow291.9%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified91.9%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification84.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1.65 \cdot 10^{+117}:\\ \;\;\;\;y \cdot \left(z \cdot -4\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 5: 52.6% accurate, 3.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ x \cdot x \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y z) :precision binary64 (* x x))
x = abs(x);
double code(double x, double y, double z) {
	return x * x;
}
NOTE: x should be positive 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 * x
end function
x = Math.abs(x);
public static double code(double x, double y, double z) {
	return x * x;
}
x = abs(x)
def code(x, y, z):
	return x * x
x = abs(x)
function code(x, y, z)
	return Float64(x * x)
end
x = abs(x)
function tmp = code(x, y, z)
	tmp = x * x;
end
NOTE: x should be positive before calling this function
code[x_, y_, z_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
x \cdot x
\end{array}
Derivation
  1. Initial program 96.9%

    \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
  2. Taylor expanded in x around inf 57.9%

    \[\leadsto \color{blue}{{x}^{2}} \]
  3. Step-by-step derivation
    1. unpow257.9%

      \[\leadsto \color{blue}{x \cdot x} \]
  4. Simplified57.9%

    \[\leadsto \color{blue}{x \cdot x} \]
  5. Final simplification57.9%

    \[\leadsto x \cdot x \]

Reproduce

?
herbie shell --seed 2023297 
(FPCore (x y z)
  :name "Graphics.Rasterific.QuadraticFormula:discriminant from Rasterific-0.6.1"
  :precision binary64
  (- (* x x) (* (* y 4.0) z)))