Graphics.Rasterific.QuadraticFormula:discriminant from Rasterific-0.6.1

Percentage Accurate: 98.5% → 99.6%
Time: 4.0s
Alternatives: 5
Speedup: 0.4×

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.5% 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.6% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
t_0 := z \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;t\_0 \leq 4 \cdot 10^{+168}:\\
\;\;\;\;x \cdot x - t\_0\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(\frac{x \cdot x}{y} - z \cdot 4\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 y #s(literal 4 binary64)) z) < 3.9999999999999997e168

    1. Initial program 100.0%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Add Preprocessing

    if 3.9999999999999997e168 < (*.f64 (*.f64 y #s(literal 4 binary64)) z)

    1. Initial program 80.6%

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

      \[\leadsto \color{blue}{y \cdot \left(\frac{{x}^{2}}{y} - 4 \cdot z\right)} \]
    4. Step-by-step derivation
      1. unpow293.5%

        \[\leadsto y \cdot \left(\frac{\color{blue}{x \cdot x}}{y} - 4 \cdot z\right) \]
    5. Applied egg-rr93.5%

      \[\leadsto y \cdot \left(\frac{\color{blue}{x \cdot x}}{y} - 4 \cdot z\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.2%

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

Alternative 2: 98.9% accurate, 0.1× speedup?

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

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

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

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

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

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

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

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

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

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

Alternative 3: 99.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot x - z \cdot \left(y \cdot 4\right)\\ \mathbf{if}\;t\_0 \leq \infty:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;4 \cdot \left(y \cdot z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (- (* x x) (* z (* y 4.0)))))
   (if (<= t_0 INFINITY) t_0 (* 4.0 (* y z)))))
double code(double x, double y, double z) {
	double t_0 = (x * x) - (z * (y * 4.0));
	double tmp;
	if (t_0 <= ((double) INFINITY)) {
		tmp = t_0;
	} else {
		tmp = 4.0 * (y * z);
	}
	return tmp;
}
public static double code(double x, double y, double z) {
	double t_0 = (x * x) - (z * (y * 4.0));
	double tmp;
	if (t_0 <= Double.POSITIVE_INFINITY) {
		tmp = t_0;
	} else {
		tmp = 4.0 * (y * z);
	}
	return tmp;
}
def code(x, y, z):
	t_0 = (x * x) - (z * (y * 4.0))
	tmp = 0
	if t_0 <= math.inf:
		tmp = t_0
	else:
		tmp = 4.0 * (y * z)
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(x * x) - Float64(z * Float64(y * 4.0)))
	tmp = 0.0
	if (t_0 <= Inf)
		tmp = t_0;
	else
		tmp = Float64(4.0 * Float64(y * z));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = (x * x) - (z * (y * 4.0));
	tmp = 0.0;
	if (t_0 <= Inf)
		tmp = t_0;
	else
		tmp = 4.0 * (y * z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] - N[(z * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], t$95$0, N[(4.0 * N[(y * z), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x \cdot x - z \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;t\_0 \leq \infty:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;4 \cdot \left(y \cdot z\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 x x) (*.f64 (*.f64 y #s(literal 4 binary64)) z)) < +inf.0

    1. Initial program 100.0%

      \[x \cdot x - \left(y \cdot 4\right) \cdot z \]
    2. Add Preprocessing

    if +inf.0 < (-.f64 (*.f64 x x) (*.f64 (*.f64 y #s(literal 4 binary64)) z))

    1. Initial program 0.0%

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

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

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

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

        \[\leadsto \color{blue}{z \cdot \left(y \cdot -4\right)} \]
      4. rem-square-sqrt0.0%

        \[\leadsto \color{blue}{\sqrt{z \cdot \left(y \cdot -4\right)} \cdot \sqrt{z \cdot \left(y \cdot -4\right)}} \]
      5. fabs-sqr0.0%

        \[\leadsto \color{blue}{\left|\sqrt{z \cdot \left(y \cdot -4\right)} \cdot \sqrt{z \cdot \left(y \cdot -4\right)}\right|} \]
      6. rem-square-sqrt66.7%

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

        \[\leadsto \left|\color{blue}{\left(z \cdot y\right) \cdot -4}\right| \]
      8. *-commutative66.7%

        \[\leadsto \left|\color{blue}{\left(y \cdot z\right)} \cdot -4\right| \]
      9. *-commutative66.7%

        \[\leadsto \left|\color{blue}{-4 \cdot \left(y \cdot z\right)}\right| \]
      10. metadata-eval66.7%

        \[\leadsto \left|\color{blue}{\left(-4\right)} \cdot \left(y \cdot z\right)\right| \]
      11. distribute-lft-neg-in66.7%

        \[\leadsto \left|\color{blue}{-4 \cdot \left(y \cdot z\right)}\right| \]
      12. fabs-neg66.7%

        \[\leadsto \color{blue}{\left|4 \cdot \left(y \cdot z\right)\right|} \]
      13. rem-square-sqrt66.7%

        \[\leadsto \left|\color{blue}{\sqrt{4 \cdot \left(y \cdot z\right)} \cdot \sqrt{4 \cdot \left(y \cdot z\right)}}\right| \]
      14. fabs-sqr66.7%

        \[\leadsto \color{blue}{\sqrt{4 \cdot \left(y \cdot z\right)} \cdot \sqrt{4 \cdot \left(y \cdot z\right)}} \]
      15. rem-square-sqrt66.7%

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

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

        \[\leadsto \color{blue}{y \cdot \left(z \cdot 4\right)} \]
    5. Simplified66.7%

      \[\leadsto \color{blue}{y \cdot \left(z \cdot 4\right)} \]
    6. Taylor expanded in y around 0 66.7%

      \[\leadsto \color{blue}{4 \cdot \left(y \cdot z\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.2%

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

Alternative 4: 52.8% accurate, 1.8× speedup?

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

\\
y \cdot \left(z \cdot -4\right)
\end{array}
Derivation
  1. Initial program 97.7%

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

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

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

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

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

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

    \[\leadsto y \cdot \left(z \cdot -4\right) \]
  7. Add Preprocessing

Alternative 5: 5.8% accurate, 1.8× speedup?

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

\\
4 \cdot \left(y \cdot z\right)
\end{array}
Derivation
  1. Initial program 97.7%

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{z \cdot \left(y \cdot -4\right)} \cdot \sqrt{z \cdot \left(y \cdot -4\right)}} \]
    5. fabs-sqr26.4%

      \[\leadsto \color{blue}{\left|\sqrt{z \cdot \left(y \cdot -4\right)} \cdot \sqrt{z \cdot \left(y \cdot -4\right)}\right|} \]
    6. rem-square-sqrt29.3%

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

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

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

      \[\leadsto \left|\color{blue}{-4 \cdot \left(y \cdot z\right)}\right| \]
    10. metadata-eval29.3%

      \[\leadsto \left|\color{blue}{\left(-4\right)} \cdot \left(y \cdot z\right)\right| \]
    11. distribute-lft-neg-in29.3%

      \[\leadsto \left|\color{blue}{-4 \cdot \left(y \cdot z\right)}\right| \]
    12. fabs-neg29.3%

      \[\leadsto \color{blue}{\left|4 \cdot \left(y \cdot z\right)\right|} \]
    13. rem-square-sqrt5.9%

      \[\leadsto \left|\color{blue}{\sqrt{4 \cdot \left(y \cdot z\right)} \cdot \sqrt{4 \cdot \left(y \cdot z\right)}}\right| \]
    14. fabs-sqr5.9%

      \[\leadsto \color{blue}{\sqrt{4 \cdot \left(y \cdot z\right)} \cdot \sqrt{4 \cdot \left(y \cdot z\right)}} \]
    15. rem-square-sqrt6.3%

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

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

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

    \[\leadsto \color{blue}{y \cdot \left(z \cdot 4\right)} \]
  6. Taylor expanded in y around 0 6.3%

    \[\leadsto \color{blue}{4 \cdot \left(y \cdot z\right)} \]
  7. Add Preprocessing

Reproduce

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