Graphics.Rasterific.QuadraticFormula:discriminant from Rasterific-0.6.1

Percentage Accurate: 98.4% → 99.5%
Time: 2.6s
Alternatives: 3
Speedup: 1.1×

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 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: 98.4% 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.5% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(z \cdot -4, y, x \cdot x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma (* z -4.0) y (* x x)))
double code(double x, double y, double z) {
	return fma((z * -4.0), y, (x * x));
}
function code(x, y, z)
	return fma(Float64(z * -4.0), y, Float64(x * x))
end
code[x_, y_, z_] := N[(N[(z * -4.0), $MachinePrecision] * y + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(z \cdot -4, y, x \cdot x\right)
\end{array}
Derivation
  1. Initial program 98.0%

    \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{x \cdot x - \left(y \cdot 4\right) \cdot z} \]
    2. sub-negN/A

      \[\leadsto \color{blue}{x \cdot x + \left(\mathsf{neg}\left(\left(y \cdot 4\right) \cdot z\right)\right)} \]
    3. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\left(y \cdot 4\right) \cdot z\right)\right) + x \cdot x} \]
    4. lift-*.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(y \cdot 4\right) \cdot z}\right)\right) + x \cdot x \]
    5. lift-*.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(y \cdot 4\right)} \cdot z\right)\right) + x \cdot x \]
    6. associate-*l*N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{y \cdot \left(4 \cdot z\right)}\right)\right) + x \cdot x \]
    7. *-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(4 \cdot z\right) \cdot y}\right)\right) + x \cdot x \]
    8. distribute-lft-neg-inN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(4 \cdot z\right)\right) \cdot y} + x \cdot x \]
    9. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{neg}\left(4 \cdot z\right), y, x \cdot x\right)} \]
    10. distribute-lft-neg-inN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(4\right)\right) \cdot z}, y, x \cdot x\right) \]
    11. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(4\right)\right) \cdot z}, y, x \cdot x\right) \]
    12. metadata-eval100.0

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

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

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

Alternative 2: 84.1% accurate, 0.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 4 \cdot 10^{-44}:\\
\;\;\;\;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) < 3.99999999999999981e-44

    1. Initial program 100.0%

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

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

        \[\leadsto \color{blue}{\left(y \cdot z\right) \cdot -4} \]
      2. lower-*.f64N/A

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

        \[\leadsto \color{blue}{\left(z \cdot y\right)} \cdot -4 \]
      4. lower-*.f6487.9

        \[\leadsto \color{blue}{\left(z \cdot y\right)} \cdot -4 \]
    5. Applied rewrites87.9%

      \[\leadsto \color{blue}{\left(z \cdot y\right) \cdot -4} \]
    6. Step-by-step derivation
      1. Applied rewrites87.9%

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

      if 3.99999999999999981e-44 < (*.f64 x x)

      1. Initial program 96.4%

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

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

          \[\leadsto \color{blue}{\left(y \cdot z\right) \cdot -4} \]
        2. lower-*.f64N/A

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

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

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

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

          \[\leadsto \color{blue}{\left(-4 \cdot z\right) \cdot y} \]
        2. Taylor expanded in x around inf

          \[\leadsto \color{blue}{{x}^{2}} \]
        3. Step-by-step derivation
          1. unpow2N/A

            \[\leadsto \color{blue}{x \cdot x} \]
          2. lower-*.f6485.4

            \[\leadsto \color{blue}{x \cdot x} \]
        4. Applied rewrites85.4%

          \[\leadsto \color{blue}{x \cdot x} \]
      7. Recombined 2 regimes into one program.
      8. Final simplification86.6%

        \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 4 \cdot 10^{-44}:\\ \;\;\;\;y \cdot \left(z \cdot -4\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
      9. Add Preprocessing

      Alternative 3: 53.1% accurate, 3.2× speedup?

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

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

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

          \[\leadsto \color{blue}{\left(y \cdot z\right) \cdot -4} \]
        2. lower-*.f64N/A

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

          \[\leadsto \color{blue}{\left(z \cdot y\right)} \cdot -4 \]
        4. lower-*.f6449.6

          \[\leadsto \color{blue}{\left(z \cdot y\right)} \cdot -4 \]
      5. Applied rewrites49.6%

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

          \[\leadsto \color{blue}{\left(-4 \cdot z\right) \cdot y} \]
        2. Taylor expanded in x around inf

          \[\leadsto \color{blue}{{x}^{2}} \]
        3. Step-by-step derivation
          1. unpow2N/A

            \[\leadsto \color{blue}{x \cdot x} \]
          2. lower-*.f6458.1

            \[\leadsto \color{blue}{x \cdot x} \]
        4. Applied rewrites58.1%

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

        Reproduce

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