Diagrams.Solve.Polynomial:quartForm from diagrams-solve-0.1, B

Percentage Accurate: 100.0% → 100.0%
Time: 4.1s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (+ (- (* (/ 1.0 8.0) x) (/ (* y z) 2.0)) t))
double code(double x, double y, double z, double t) {
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (((1.0d0 / 8.0d0) * x) - ((y * z) / 2.0d0)) + t
end function
public static double code(double x, double y, double z, double t) {
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
}
def code(x, y, z, t):
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t
function code(x, y, z, t)
	return Float64(Float64(Float64(Float64(1.0 / 8.0) * x) - Float64(Float64(y * z) / 2.0)) + t)
end
function tmp = code(x, y, z, t)
	tmp = (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
end
code[x_, y_, z_, t_] := N[(N[(N[(N[(1.0 / 8.0), $MachinePrecision] * x), $MachinePrecision] - N[(N[(y * z), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t
\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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (+ (- (* (/ 1.0 8.0) x) (/ (* y z) 2.0)) t))
double code(double x, double y, double z, double t) {
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (((1.0d0 / 8.0d0) * x) - ((y * z) / 2.0d0)) + t
end function
public static double code(double x, double y, double z, double t) {
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
}
def code(x, y, z, t):
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t
function code(x, y, z, t)
	return Float64(Float64(Float64(Float64(1.0 / 8.0) * x) - Float64(Float64(y * z) / 2.0)) + t)
end
function tmp = code(x, y, z, t)
	tmp = (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
end
code[x_, y_, z_, t_] := N[(N[(N[(N[(1.0 / 8.0), $MachinePrecision] * x), $MachinePrecision] - N[(N[(y * z), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (+ (- (* (/ 1.0 8.0) x) (/ (* y z) 2.0)) t))
double code(double x, double y, double z, double t) {
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (((1.0d0 / 8.0d0) * x) - ((y * z) / 2.0d0)) + t
end function
public static double code(double x, double y, double z, double t) {
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
}
def code(x, y, z, t):
	return (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t
function code(x, y, z, t)
	return Float64(Float64(Float64(Float64(1.0 / 8.0) * x) - Float64(Float64(y * z) / 2.0)) + t)
end
function tmp = code(x, y, z, t)
	tmp = (((1.0 / 8.0) * x) - ((y * z) / 2.0)) + t;
end
code[x_, y_, z_, t_] := N[(N[(N[(N[(1.0 / 8.0), $MachinePrecision] * x), $MachinePrecision] - N[(N[(y * z), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision] + t), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 87.7% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \mathsf{fma}\left(y, z \cdot -0.5, t\right)\\ \mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+29}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;y \cdot z \leq 2000000000:\\ \;\;\;\;\mathsf{fma}\left(0.125, x, t\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (fma y (* z -0.5) t)))
   (if (<= (* y z) -2e+29)
     t_1
     (if (<= (* y z) 2000000000.0) (fma 0.125 x t) t_1))))
double code(double x, double y, double z, double t) {
	double t_1 = fma(y, (z * -0.5), t);
	double tmp;
	if ((y * z) <= -2e+29) {
		tmp = t_1;
	} else if ((y * z) <= 2000000000.0) {
		tmp = fma(0.125, x, t);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y, z, t)
	t_1 = fma(y, Float64(z * -0.5), t)
	tmp = 0.0
	if (Float64(y * z) <= -2e+29)
		tmp = t_1;
	elseif (Float64(y * z) <= 2000000000.0)
		tmp = fma(0.125, x, t);
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(y * N[(z * -0.5), $MachinePrecision] + t), $MachinePrecision]}, If[LessEqual[N[(y * z), $MachinePrecision], -2e+29], t$95$1, If[LessEqual[N[(y * z), $MachinePrecision], 2000000000.0], N[(0.125 * x + t), $MachinePrecision], t$95$1]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(y, z \cdot -0.5, t\right)\\
\mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+29}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;y \cdot z \leq 2000000000:\\
\;\;\;\;\mathsf{fma}\left(0.125, x, t\right)\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < -1.99999999999999983e29 or 2e9 < (*.f64 y z)

    1. Initial program 100.0%

      \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{t - \frac{1}{2} \cdot \left(y \cdot z\right)} \]
    4. Step-by-step derivation
      1. cancel-sign-sub-invN/A

        \[\leadsto \color{blue}{t + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right) \cdot \left(y \cdot z\right)} \]
      2. metadata-evalN/A

        \[\leadsto t + \color{blue}{\frac{-1}{2}} \cdot \left(y \cdot z\right) \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{-1}{2} \cdot \left(y \cdot z\right) + t} \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\left(y \cdot z\right) \cdot \frac{-1}{2}} + t \]
      5. associate-*l*N/A

        \[\leadsto \color{blue}{y \cdot \left(z \cdot \frac{-1}{2}\right)} + t \]
      6. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\left(\frac{-1}{2} \cdot z\right)} + t \]
      7. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, \frac{-1}{2} \cdot z, t\right)} \]
      8. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(y, \color{blue}{z \cdot \frac{-1}{2}}, t\right) \]
      9. lower-*.f6492.6

        \[\leadsto \mathsf{fma}\left(y, \color{blue}{z \cdot -0.5}, t\right) \]
    5. Applied rewrites92.6%

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

    if -1.99999999999999983e29 < (*.f64 y z) < 2e9

    1. Initial program 100.0%

      \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \color{blue}{t + \frac{1}{8} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{8} \cdot x + t} \]
      2. lower-fma.f6491.5

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x, t\right)} \]
    5. Applied rewrites91.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x, t\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 83.5% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := y \cdot \left(z \cdot -0.5\right)\\ \mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+55}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;y \cdot z \leq 10^{+130}:\\ \;\;\;\;\mathsf{fma}\left(0.125, x, t\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (* y (* z -0.5))))
   (if (<= (* y z) -2e+55) t_1 (if (<= (* y z) 1e+130) (fma 0.125 x t) t_1))))
double code(double x, double y, double z, double t) {
	double t_1 = y * (z * -0.5);
	double tmp;
	if ((y * z) <= -2e+55) {
		tmp = t_1;
	} else if ((y * z) <= 1e+130) {
		tmp = fma(0.125, x, t);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y, z, t)
	t_1 = Float64(y * Float64(z * -0.5))
	tmp = 0.0
	if (Float64(y * z) <= -2e+55)
		tmp = t_1;
	elseif (Float64(y * z) <= 1e+130)
		tmp = fma(0.125, x, t);
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(y * N[(z * -0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(y * z), $MachinePrecision], -2e+55], t$95$1, If[LessEqual[N[(y * z), $MachinePrecision], 1e+130], N[(0.125 * x + t), $MachinePrecision], t$95$1]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := y \cdot \left(z \cdot -0.5\right)\\
\mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+55}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;y \cdot z \leq 10^{+130}:\\
\;\;\;\;\mathsf{fma}\left(0.125, x, t\right)\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < -2.00000000000000002e55 or 1.0000000000000001e130 < (*.f64 y z)

    1. Initial program 100.0%

      \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

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

        \[\leadsto \color{blue}{\left(y \cdot z\right) \cdot \frac{-1}{2}} \]
      2. associate-*l*N/A

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

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

        \[\leadsto \color{blue}{y \cdot \left(\frac{-1}{2} \cdot z\right)} \]
      5. *-commutativeN/A

        \[\leadsto y \cdot \color{blue}{\left(z \cdot \frac{-1}{2}\right)} \]
      6. lower-*.f6488.5

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

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

    if -2.00000000000000002e55 < (*.f64 y z) < 1.0000000000000001e130

    1. Initial program 100.0%

      \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \color{blue}{t + \frac{1}{8} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{8} \cdot x + t} \]
      2. lower-fma.f6486.8

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x, t\right)} \]
    5. Applied rewrites86.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x, t\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 64.2% accurate, 5.6× speedup?

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

\\
\mathsf{fma}\left(0.125, x, t\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
  2. Add Preprocessing
  3. Taylor expanded in y around 0

    \[\leadsto \color{blue}{t + \frac{1}{8} \cdot x} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \color{blue}{\frac{1}{8} \cdot x + t} \]
    2. lower-fma.f6457.1

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x, t\right)} \]
  5. Applied rewrites57.1%

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x, t\right)} \]
  6. Add Preprocessing

Alternative 5: 32.5% accurate, 6.5× speedup?

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

\\
x \cdot 0.125
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{1}{8} \cdot x - \frac{y \cdot z}{2}\right) + t \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{1}{8} \cdot x} \]
  4. Step-by-step derivation
    1. lower-*.f6427.8

      \[\leadsto \color{blue}{0.125 \cdot x} \]
  5. Applied rewrites27.8%

    \[\leadsto \color{blue}{0.125 \cdot x} \]
  6. Final simplification27.8%

    \[\leadsto x \cdot 0.125 \]
  7. Add Preprocessing

Developer Target 1: 100.0% accurate, 1.1× speedup?

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

\\
\left(\frac{x}{8} + t\right) - \frac{z}{2} \cdot y
\end{array}

Reproduce

?
herbie shell --seed 2024232 
(FPCore (x y z t)
  :name "Diagrams.Solve.Polynomial:quartForm  from diagrams-solve-0.1, B"
  :precision binary64

  :alt
  (! :herbie-platform default (- (+ (/ x 8) t) (* (/ z 2) y)))

  (+ (- (* (/ 1.0 8.0) x) (/ (* y z) 2.0)) t))