Graphics.Rasterific.QuadraticFormula:discriminant from Rasterific-0.6.1

Percentage Accurate: 98.4% → 99.6%
Time: 2.3s
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.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.6% accurate, 0.1× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 10^{+196}:\\ \;\;\;\;\mathsf{fma}\left(x, x, z \cdot \left(y \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 1e+196) (fma x x (* z (* y -4.0))) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	double tmp;
	if (x <= 1e+196) {
		tmp = fma(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 <= 1e+196)
		tmp = fma(x, x, Float64(z * Float64(y * -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, 1e+196], N[(x * x + 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 10^{+196}:\\
\;\;\;\;\mathsf{fma}\left(x, x, z \cdot \left(y \cdot -4\right)\right)\\

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


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

    1. Initial program 99.1%

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

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

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

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

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

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

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

    if 9.9999999999999995e195 < x

    1. Initial program 86.7%

      \[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 simplification99.6%

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

Alternative 2: 99.5% 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 97.6%

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{z \cdot \left(-4\right)}, x \cdot x\right) \]
    8. 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 6.8 \cdot 10^{+145}:\\ \;\;\;\;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 6.8e+145) (- (* x x) (* z (* y 4.0))) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	double tmp;
	if (x <= 6.8e+145) {
		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 <= 6.8d+145) 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 <= 6.8e+145) {
		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 <= 6.8e+145:
		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 <= 6.8e+145)
		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 <= 6.8e+145)
		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, 6.8e+145], 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 6.8 \cdot 10^{+145}:\\
\;\;\;\;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 < 6.7999999999999998e145

    1. Initial program 99.5%

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

    if 6.7999999999999998e145 < x

    1. Initial program 86.8%

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

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

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

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

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

Alternative 4: 83.9% accurate, 1.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1.8 \cdot 10^{-107}:\\ \;\;\;\;-4 \cdot \left(y \cdot z\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.8e-107) (* -4.0 (* y z)) (* x x)))
x = abs(x);
double code(double x, double y, double z) {
	double tmp;
	if ((x * x) <= 1.8e-107) {
		tmp = -4.0 * (y * z);
	} 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.8d-107) then
        tmp = (-4.0d0) * (y * z)
    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.8e-107) {
		tmp = -4.0 * (y * z);
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
def code(x, y, z):
	tmp = 0
	if (x * x) <= 1.8e-107:
		tmp = -4.0 * (y * z)
	else:
		tmp = x * x
	return tmp
x = abs(x)
function code(x, y, z)
	tmp = 0.0
	if (Float64(x * x) <= 1.8e-107)
		tmp = Float64(-4.0 * Float64(y * z));
	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.8e-107)
		tmp = -4.0 * (y * z);
	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.8e-107], N[(-4.0 * N[(y * z), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1.8 \cdot 10^{-107}:\\
\;\;\;\;-4 \cdot \left(y \cdot z\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 1.79999999999999988e-107

    1. Initial program 100.0%

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

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

    if 1.79999999999999988e-107 < (*.f64 x x)

    1. Initial program 96.0%

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

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

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

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

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

Alternative 5: 52.7% 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 97.6%

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

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

      \[\leadsto \color{blue}{x \cdot x} \]
  4. Simplified55.6%

    \[\leadsto \color{blue}{x \cdot x} \]
  5. Final simplification55.6%

    \[\leadsto x \cdot x \]

Reproduce

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