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

?

Percentage Accurate: 97.8% → 98.9%
Time: 10.9s
Precision: binary64
Cost: 7876

?

\[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
\[\begin{array}{l} t_1 := \left(\frac{z \cdot t}{16} + x \cdot y\right) - \frac{b \cdot a}{4}\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;c + t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z \cdot \left(t \cdot 0.0625\right)\right)\\ \end{array} \]
(FPCore (x y z t a b c)
 :precision binary64
 (+ (- (+ (* x y) (/ (* z t) 16.0)) (/ (* a b) 4.0)) c))
(FPCore (x y z t a b c)
 :precision binary64
 (let* ((t_1 (- (+ (/ (* z t) 16.0) (* x y)) (/ (* b a) 4.0))))
   (if (<= t_1 INFINITY) (+ c t_1) (fma y x (* z (* t 0.0625))))))
double code(double x, double y, double z, double t, double a, double b, double c) {
	return (((x * y) + ((z * t) / 16.0)) - ((a * b) / 4.0)) + c;
}
double code(double x, double y, double z, double t, double a, double b, double c) {
	double t_1 = (((z * t) / 16.0) + (x * y)) - ((b * a) / 4.0);
	double tmp;
	if (t_1 <= ((double) INFINITY)) {
		tmp = c + t_1;
	} else {
		tmp = fma(y, x, (z * (t * 0.0625)));
	}
	return tmp;
}
function code(x, y, z, t, a, b, c)
	return Float64(Float64(Float64(Float64(x * y) + Float64(Float64(z * t) / 16.0)) - Float64(Float64(a * b) / 4.0)) + c)
end
function code(x, y, z, t, a, b, c)
	t_1 = Float64(Float64(Float64(Float64(z * t) / 16.0) + Float64(x * y)) - Float64(Float64(b * a) / 4.0))
	tmp = 0.0
	if (t_1 <= Inf)
		tmp = Float64(c + t_1);
	else
		tmp = fma(y, x, Float64(z * Float64(t * 0.0625)));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_, c_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(N[(z * t), $MachinePrecision] / 16.0), $MachinePrecision]), $MachinePrecision] - N[(N[(a * b), $MachinePrecision] / 4.0), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_, c_] := Block[{t$95$1 = N[(N[(N[(N[(z * t), $MachinePrecision] / 16.0), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision] - N[(N[(b * a), $MachinePrecision] / 4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, Infinity], N[(c + t$95$1), $MachinePrecision], N[(y * x + N[(z * N[(t * 0.0625), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c
\begin{array}{l}
t_1 := \left(\frac{z \cdot t}{16} + x \cdot y\right) - \frac{b \cdot a}{4}\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;c + t_1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z \cdot \left(t \cdot 0.0625\right)\right)\\


\end{array}

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.

Herbie found 18 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) 16)) (/.f64 (*.f64 a b) 4)) < +inf.0

    1. Initial program 100.0%

      \[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]

    if +inf.0 < (-.f64 (+.f64 (*.f64 x y) (/.f64 (*.f64 z t) 16)) (/.f64 (*.f64 a b) 4))

    1. Initial program 0.0%

      \[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
    2. Simplified50.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, \mathsf{fma}\left(b, -0.25 \cdot a, \mathsf{fma}\left(z, \frac{t}{16}, c\right)\right)\right)} \]
      Step-by-step derivation

      [Start]0.0%

      \[ \left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]

      associate-+l- [=>]0.0%

      \[ \color{blue}{\left(x \cdot y + \frac{z \cdot t}{16}\right) - \left(\frac{a \cdot b}{4} - c\right)} \]

      +-commutative [=>]0.0%

      \[ \color{blue}{\left(\frac{z \cdot t}{16} + x \cdot y\right)} - \left(\frac{a \cdot b}{4} - c\right) \]

      associate--l+ [=>]0.0%

      \[ \color{blue}{\frac{z \cdot t}{16} + \left(x \cdot y - \left(\frac{a \cdot b}{4} - c\right)\right)} \]

      +-commutative [=>]0.0%

      \[ \color{blue}{\left(x \cdot y - \left(\frac{a \cdot b}{4} - c\right)\right) + \frac{z \cdot t}{16}} \]

      associate-+l- [=>]0.0%

      \[ \color{blue}{x \cdot y - \left(\left(\frac{a \cdot b}{4} - c\right) - \frac{z \cdot t}{16}\right)} \]

      fma-neg [=>]33.3%

      \[ \color{blue}{\mathsf{fma}\left(x, y, -\left(\left(\frac{a \cdot b}{4} - c\right) - \frac{z \cdot t}{16}\right)\right)} \]

      neg-sub0 [=>]33.3%

      \[ \mathsf{fma}\left(x, y, \color{blue}{0 - \left(\left(\frac{a \cdot b}{4} - c\right) - \frac{z \cdot t}{16}\right)}\right) \]

      associate--l- [=>]33.3%

      \[ \mathsf{fma}\left(x, y, 0 - \color{blue}{\left(\frac{a \cdot b}{4} - \left(c + \frac{z \cdot t}{16}\right)\right)}\right) \]

      associate-+l- [<=]33.3%

      \[ \mathsf{fma}\left(x, y, \color{blue}{\left(0 - \frac{a \cdot b}{4}\right) + \left(c + \frac{z \cdot t}{16}\right)}\right) \]

      neg-sub0 [<=]33.3%

      \[ \mathsf{fma}\left(x, y, \color{blue}{\left(-\frac{a \cdot b}{4}\right)} + \left(c + \frac{z \cdot t}{16}\right)\right) \]

      *-commutative [=>]33.3%

      \[ \mathsf{fma}\left(x, y, \left(-\frac{\color{blue}{b \cdot a}}{4}\right) + \left(c + \frac{z \cdot t}{16}\right)\right) \]

      associate-*r/ [<=]33.3%

      \[ \mathsf{fma}\left(x, y, \left(-\color{blue}{b \cdot \frac{a}{4}}\right) + \left(c + \frac{z \cdot t}{16}\right)\right) \]

      distribute-rgt-neg-in [=>]33.3%

      \[ \mathsf{fma}\left(x, y, \color{blue}{b \cdot \left(-\frac{a}{4}\right)} + \left(c + \frac{z \cdot t}{16}\right)\right) \]

      fma-def [=>]50.0%

      \[ \mathsf{fma}\left(x, y, \color{blue}{\mathsf{fma}\left(b, -\frac{a}{4}, c + \frac{z \cdot t}{16}\right)}\right) \]

      distribute-frac-neg [<=]50.0%

      \[ \mathsf{fma}\left(x, y, \mathsf{fma}\left(b, \color{blue}{\frac{-a}{4}}, c + \frac{z \cdot t}{16}\right)\right) \]

      neg-mul-1 [=>]50.0%

      \[ \mathsf{fma}\left(x, y, \mathsf{fma}\left(b, \frac{\color{blue}{-1 \cdot a}}{4}, c + \frac{z \cdot t}{16}\right)\right) \]

      associate-/l* [=>]50.0%

      \[ \mathsf{fma}\left(x, y, \mathsf{fma}\left(b, \color{blue}{\frac{-1}{\frac{4}{a}}}, c + \frac{z \cdot t}{16}\right)\right) \]

      associate-/r/ [=>]50.0%

      \[ \mathsf{fma}\left(x, y, \mathsf{fma}\left(b, \color{blue}{\frac{-1}{4} \cdot a}, c + \frac{z \cdot t}{16}\right)\right) \]

      metadata-eval [=>]50.0%

      \[ \mathsf{fma}\left(x, y, \mathsf{fma}\left(b, \color{blue}{-0.25} \cdot a, c + \frac{z \cdot t}{16}\right)\right) \]
    3. Taylor expanded in b around 0 66.7%

      \[\leadsto \mathsf{fma}\left(x, y, \color{blue}{c + 0.0625 \cdot \left(t \cdot z\right)}\right) \]
    4. Taylor expanded in c around 0 33.3%

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, \left(0.0625 \cdot t\right) \cdot z\right)} \]
      Step-by-step derivation

      [Start]33.3%

      \[ y \cdot x + 0.0625 \cdot \left(t \cdot z\right) \]

      fma-def [=>]66.7%

      \[ \color{blue}{\mathsf{fma}\left(y, x, 0.0625 \cdot \left(t \cdot z\right)\right)} \]

      associate-*r* [=>]66.7%

      \[ \mathsf{fma}\left(y, x, \color{blue}{\left(0.0625 \cdot t\right) \cdot z}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\frac{z \cdot t}{16} + x \cdot y\right) - \frac{b \cdot a}{4} \leq \infty:\\ \;\;\;\;c + \left(\left(\frac{z \cdot t}{16} + x \cdot y\right) - \frac{b \cdot a}{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z \cdot \left(t \cdot 0.0625\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.9%
Cost7876
\[\begin{array}{l} t_1 := \left(\frac{z \cdot t}{16} + x \cdot y\right) - \frac{b \cdot a}{4}\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;c + t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z \cdot \left(t \cdot 0.0625\right)\right)\\ \end{array} \]
Alternative 2
Accuracy98.8%
Cost19904
\[\mathsf{fma}\left(x, y, \mathsf{fma}\left(b, -0.25 \cdot a, \mathsf{fma}\left(z, \frac{t}{16}, c\right)\right)\right) \]
Alternative 3
Accuracy98.9%
Cost13632
\[\mathsf{fma}\left(t, \frac{z}{16}, \mathsf{fma}\left(x, y, c - b \cdot \frac{a}{4}\right)\right) \]
Alternative 4
Accuracy98.1%
Cost7360
\[\mathsf{fma}\left(x, y, \frac{z}{\frac{16}{t}}\right) + \left(c - \frac{a}{\frac{4}{b}}\right) \]
Alternative 5
Accuracy44.4%
Cost2660
\[\begin{array}{l} t_1 := z \cdot \left(t \cdot 0.0625\right)\\ t_2 := -0.25 \cdot \left(b \cdot a\right)\\ \mathbf{if}\;b \cdot a \leq -5.5 \cdot 10^{+79}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -520000000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -3.7 \cdot 10^{-50}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;b \cdot a \leq -9.6 \cdot 10^{-104}:\\ \;\;\;\;c\\ \mathbf{elif}\;b \cdot a \leq -2.1 \cdot 10^{-191}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 1.7 \cdot 10^{-189}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;b \cdot a \leq 9.5 \cdot 10^{-140}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 2.1 \cdot 10^{-94}:\\ \;\;\;\;c\\ \mathbf{elif}\;b \cdot a \leq 2.2 \cdot 10^{+28}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 6
Accuracy62.8%
Cost2140
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := -0.25 \cdot \left(b \cdot a\right)\\ t_3 := c + 0.0625 \cdot \left(z \cdot t\right)\\ \mathbf{if}\;b \cdot a \leq -1.66 \cdot 10^{+214}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -1.8 \cdot 10^{+174}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -2.6 \cdot 10^{+80}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -3 \cdot 10^{+16}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq -3.4 \cdot 10^{-50}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -3.2 \cdot 10^{-192}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq 1.95 \cdot 10^{+202}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 7
Accuracy68.3%
Cost2136
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := 0.0625 \cdot \left(z \cdot t\right)\\ t_3 := x \cdot y + t_2\\ t_4 := x \cdot y - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{if}\;b \cdot a \leq -2 \cdot 10^{+80}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq -2 \cdot 10^{+18}:\\ \;\;\;\;c + t_2\\ \mathbf{elif}\;b \cdot a \leq -5 \cdot 10^{-103}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{-140}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{-94}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{+22}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t_4\\ \end{array} \]
Alternative 8
Accuracy85.5%
Cost1876
\[\begin{array}{l} t_1 := c + \left(x \cdot y + 0.0625 \cdot \left(z \cdot t\right)\right)\\ t_2 := x \cdot y - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{if}\;b \cdot a \leq -5 \cdot 10^{+225}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{+22}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+118}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+185}:\\ \;\;\;\;c + b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 9
Accuracy85.2%
Cost1876
\[\begin{array}{l} t_1 := \left(b \cdot a\right) \cdot 0.25\\ t_2 := x \cdot y - t_1\\ t_3 := 0.0625 \cdot \left(z \cdot t\right)\\ t_4 := c + \left(x \cdot y + t_3\right)\\ \mathbf{if}\;b \cdot a \leq -5 \cdot 10^{+225}:\\ \;\;\;\;t_3 - t_1\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{+22}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+118}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{+154}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+185}:\\ \;\;\;\;c + b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 10
Accuracy65.6%
Cost1748
\[\begin{array}{l} t_1 := c + 0.0625 \cdot \left(z \cdot t\right)\\ t_2 := c + b \cdot \left(-0.25 \cdot a\right)\\ t_3 := c + x \cdot y\\ \mathbf{if}\;b \cdot a \leq -2 \cdot 10^{+90}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -2 \cdot 10^{+18}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -4 \cdot 10^{-50}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq -4 \cdot 10^{-189}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 10^{+26}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 11
Accuracy88.3%
Cost1746
\[\begin{array}{l} \mathbf{if}\;b \cdot a \leq -2 \cdot 10^{+108} \lor \neg \left(b \cdot a \leq -2 \cdot 10^{+18} \lor \neg \left(b \cdot a \leq -2 \cdot 10^{-81}\right) \land b \cdot a \leq 50000000000000\right):\\ \;\;\;\;x \cdot y + \left(c - \frac{a}{\frac{4}{b}}\right)\\ \mathbf{else}:\\ \;\;\;\;c + \left(x \cdot y + 0.0625 \cdot \left(z \cdot t\right)\right)\\ \end{array} \]
Alternative 12
Accuracy88.3%
Cost1746
\[\begin{array}{l} \mathbf{if}\;b \cdot a \leq -2 \cdot 10^{+108} \lor \neg \left(b \cdot a \leq -2 \cdot 10^{+18} \lor \neg \left(b \cdot a \leq -2 \cdot 10^{-81}\right) \land b \cdot a \leq 50000000000000\right):\\ \;\;\;\;\left(c + x \cdot y\right) - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;c + \left(x \cdot y + 0.0625 \cdot \left(z \cdot t\right)\right)\\ \end{array} \]
Alternative 13
Accuracy62.1%
Cost1360
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := -0.25 \cdot \left(b \cdot a\right)\\ \mathbf{if}\;b \cdot a \leq -1.55 \cdot 10^{+214}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -2.8 \cdot 10^{-109}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -8 \cdot 10^{-190}:\\ \;\;\;\;z \cdot \left(t \cdot 0.0625\right)\\ \mathbf{elif}\;b \cdot a \leq 5.8 \cdot 10^{+200}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 14
Accuracy60.6%
Cost1236
\[\begin{array}{l} t_1 := 0.0625 \cdot \left(z \cdot t\right)\\ t_2 := c + b \cdot \left(-0.25 \cdot a\right)\\ t_3 := x \cdot y + t_1\\ \mathbf{if}\;a \leq -1.3 \cdot 10^{+87}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a \leq -1.75 \cdot 10^{-28}:\\ \;\;\;\;c + x \cdot y\\ \mathbf{elif}\;a \leq -3.8 \cdot 10^{-166}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;a \leq -4.9 \cdot 10^{-233}:\\ \;\;\;\;c + t_1\\ \mathbf{elif}\;a \leq 2.5 \cdot 10^{-107}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 15
Accuracy37.1%
Cost1113
\[\begin{array}{l} t_1 := z \cdot \left(t \cdot 0.0625\right)\\ \mathbf{if}\;t \leq -3.15 \cdot 10^{-15}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t \leq 2.8 \cdot 10^{-95}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;t \leq 7500:\\ \;\;\;\;c\\ \mathbf{elif}\;t \leq 1.1 \cdot 10^{+35} \lor \neg \left(t \leq 2.7 \cdot 10^{+58}\right) \land t \leq 7.2 \cdot 10^{+171}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 16
Accuracy97.8%
Cost1088
\[c + \left(\left(\frac{z \cdot t}{16} + x \cdot y\right) - \frac{b \cdot a}{4}\right) \]
Alternative 17
Accuracy37.5%
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -3.7 \cdot 10^{+127}:\\ \;\;\;\;c\\ \mathbf{elif}\;c \leq 8.2 \cdot 10^{+65}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;c\\ \end{array} \]
Alternative 18
Accuracy22.6%
Cost64
\[c \]

Reproduce?

herbie shell --seed 2023171 
(FPCore (x y z t a b c)
  :name "Diagrams.Solve.Polynomial:quartForm  from diagrams-solve-0.1, C"
  :precision binary64
  (+ (- (+ (* x y) (/ (* z t) 16.0)) (/ (* a b) 4.0)) c))