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

?

Percentage Accurate: 97.8% → 98.9%
Time: 17.7s
Precision: binary64
Cost: 13632

?

\[\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c \]
\[\mathsf{fma}\left(t, \frac{z}{16}, \mathsf{fma}\left(x, y, c - b \cdot \frac{a}{4}\right)\right) \]
(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
 (fma t (/ z 16.0) (fma x y (- c (* b (/ a 4.0))))))
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) {
	return fma(t, (z / 16.0), fma(x, y, (c - (b * (a / 4.0)))));
}
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)
	return fma(t, Float64(z / 16.0), fma(x, y, Float64(c - Float64(b * Float64(a / 4.0)))))
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_] := N[(t * N[(z / 16.0), $MachinePrecision] + N[(x * y + N[(c - N[(b * N[(a / 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\left(x \cdot y + \frac{z \cdot t}{16}\right) - \frac{a \cdot b}{4}\right) + c
\mathsf{fma}\left(t, \frac{z}{16}, \mathsf{fma}\left(x, y, c - b \cdot \frac{a}{4}\right)\right)

Local Percentage Accuracy?

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 16 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Derivation?

  1. Initial program 98.8%

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

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

    [Start]98.8

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

    associate-+l- [=>]98.8

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

    +-commutative [=>]98.8

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

    associate--l+ [=>]98.8

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

    associate-*l/ [<=]98.8

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

    *-commutative [=>]98.8

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

    fma-def [=>]99.6

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

    fma-neg [=>]99.6

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

    neg-sub0 [=>]99.6

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

    associate-+l- [<=]99.6

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

    neg-sub0 [<=]99.6

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

    +-commutative [=>]99.6

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

    unsub-neg [=>]99.6

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

    *-commutative [=>]99.6

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

    associate-*r/ [<=]99.6

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

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

Alternatives

Alternative 1
Accuracy98.1%
Cost7360
\[\mathsf{fma}\left(x, y, \frac{z}{\frac{16}{t}}\right) + \left(c - \frac{a}{\frac{4}{b}}\right) \]
Alternative 2
Accuracy64.3%
Cost3308
\[\begin{array}{l} t_1 := \left(t \cdot z\right) \cdot 0.0625\\ t_2 := c + t_1\\ t_3 := c + \left(b \cdot a\right) \cdot -0.25\\ t_4 := x \cdot y + t_1\\ t_5 := c + x \cdot y\\ \mathbf{if}\;b \cdot a \leq -2 \cdot 10^{+235}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq -4 \cdot 10^{+113}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq -1.5 \cdot 10^{+70}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -4 \cdot 10^{+28}:\\ \;\;\;\;t_5\\ \mathbf{elif}\;b \cdot a \leq -1 \cdot 10^{-43}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -1 \cdot 10^{-88}:\\ \;\;\;\;t_5\\ \mathbf{elif}\;b \cdot a \leq -1 \cdot 10^{-152}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq -5 \cdot 10^{-225}:\\ \;\;\;\;t_5\\ \mathbf{elif}\;b \cdot a \leq 4 \cdot 10^{-183}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{-13}:\\ \;\;\;\;t_5\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+155}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_3\\ \end{array} \]
Alternative 3
Accuracy66.3%
Cost3048
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := \left(t \cdot z\right) \cdot 0.0625\\ t_3 := c + t_2\\ t_4 := x \cdot y + t_2\\ \mathbf{if}\;b \cdot a \leq -1.5 \cdot 10^{+106}:\\ \;\;\;\;x \cdot y - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{elif}\;b \cdot a \leq -1.5 \cdot 10^{+70}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq -4 \cdot 10^{+28}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -1 \cdot 10^{-43}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq -1 \cdot 10^{-88}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq -1 \cdot 10^{-152}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq -5 \cdot 10^{-225}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 4 \cdot 10^{-183}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;b \cdot a \leq 5 \cdot 10^{-13}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+155}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;c + \left(b \cdot a\right) \cdot -0.25\\ \end{array} \]
Alternative 4
Accuracy62.4%
Cost1488
\[\begin{array}{l} t_1 := c + \left(t \cdot z\right) \cdot 0.0625\\ t_2 := \left(b \cdot a\right) \cdot -0.25\\ \mathbf{if}\;b \cdot a \leq -3.3 \cdot 10^{+235}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -1.06 \cdot 10^{+70}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 4.1 \cdot 10^{-13}:\\ \;\;\;\;c + x \cdot y\\ \mathbf{elif}\;b \cdot a \leq 1.05 \cdot 10^{+199}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 5
Accuracy36.4%
Cost1380
\[\begin{array}{l} t_1 := \left(b \cdot a\right) \cdot -0.25\\ t_2 := z \cdot \left(t \cdot 0.0625\right)\\ \mathbf{if}\;x \leq -2.75 \cdot 10^{+140}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq -3.9 \cdot 10^{+77}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -5.6 \cdot 10^{-11}:\\ \;\;\;\;c\\ \mathbf{elif}\;x \leq -1.4 \cdot 10^{-82}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq -7.8 \cdot 10^{-236}:\\ \;\;\;\;c\\ \mathbf{elif}\;x \leq 1.35 \cdot 10^{-296}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.02 \cdot 10^{-270}:\\ \;\;\;\;c\\ \mathbf{elif}\;x \leq 1.5 \cdot 10^{-146}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq 1.85 \cdot 10^{-65}:\\ \;\;\;\;c\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 6
Accuracy60.4%
Cost1372
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := c + \left(t \cdot z\right) \cdot 0.0625\\ t_3 := c + \left(b \cdot a\right) \cdot -0.25\\ \mathbf{if}\;t \leq -0.00017:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t \leq -6 \cdot 10^{-143}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t \leq 3.8 \cdot 10^{-248}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;t \leq 8.2 \cdot 10^{-168}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t \leq 2 \cdot 10^{-121}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;t \leq 4 \cdot 10^{-41}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t \leq 5.1 \cdot 10^{+74}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 7
Accuracy62.0%
Cost1360
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := \left(b \cdot a\right) \cdot -0.25\\ \mathbf{if}\;b \cdot a \leq -7.5 \cdot 10^{+188}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq 6.4 \cdot 10^{-13}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \cdot a \leq 1.6 \cdot 10^{+60}:\\ \;\;\;\;z \cdot \left(t \cdot 0.0625\right)\\ \mathbf{elif}\;b \cdot a \leq 7.2 \cdot 10^{+197}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 8
Accuracy87.9%
Cost1352
\[\begin{array}{l} t_1 := \left(t \cdot z\right) \cdot 0.0625\\ \mathbf{if}\;b \cdot a \leq -5 \cdot 10^{+195}:\\ \;\;\;\;c + \left(x \cdot y - \frac{b \cdot a}{4}\right)\\ \mathbf{elif}\;b \cdot a \leq 1000000000:\\ \;\;\;\;c + \left(x \cdot y + t_1\right)\\ \mathbf{else}:\\ \;\;\;\;\left(c + t_1\right) - \left(b \cdot a\right) \cdot 0.25\\ \end{array} \]
Alternative 9
Accuracy89.4%
Cost1352
\[\begin{array}{l} t_1 := \left(t \cdot z\right) \cdot 0.0625\\ t_2 := \left(b \cdot a\right) \cdot 0.25\\ t_3 := x \cdot y + t_1\\ \mathbf{if}\;b \cdot a \leq -1.5 \cdot 10^{+106}:\\ \;\;\;\;t_3 - t_2\\ \mathbf{elif}\;b \cdot a \leq 1000000000:\\ \;\;\;\;c + t_3\\ \mathbf{else}:\\ \;\;\;\;\left(c + t_1\right) - t_2\\ \end{array} \]
Alternative 10
Accuracy85.9%
Cost1225
\[\begin{array}{l} \mathbf{if}\;b \cdot a \leq -2 \cdot 10^{+242} \lor \neg \left(b \cdot a \leq 10^{+195}\right):\\ \;\;\;\;x \cdot y - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;c + \left(x \cdot y + \left(t \cdot z\right) \cdot 0.0625\right)\\ \end{array} \]
Alternative 11
Accuracy87.9%
Cost1225
\[\begin{array}{l} \mathbf{if}\;b \cdot a \leq -5 \cdot 10^{+195} \lor \neg \left(b \cdot a \leq 2 \cdot 10^{+155}\right):\\ \;\;\;\;c + \left(x \cdot y - \frac{b \cdot a}{4}\right)\\ \mathbf{else}:\\ \;\;\;\;c + \left(x \cdot y + \left(t \cdot z\right) \cdot 0.0625\right)\\ \end{array} \]
Alternative 12
Accuracy97.8%
Cost1088
\[c + \left(\left(\frac{t \cdot z}{16} + x \cdot y\right) - \frac{b \cdot a}{4}\right) \]
Alternative 13
Accuracy36.4%
Cost984
\[\begin{array}{l} t_1 := z \cdot \left(t \cdot 0.0625\right)\\ \mathbf{if}\;x \leq -1.2 \cdot 10^{+102}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq -9 \cdot 10^{-11}:\\ \;\;\;\;c\\ \mathbf{elif}\;x \leq -2 \cdot 10^{-82}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.2 \cdot 10^{-266}:\\ \;\;\;\;c\\ \mathbf{elif}\;x \leq 6.2 \cdot 10^{-148}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.85 \cdot 10^{-65}:\\ \;\;\;\;c\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 14
Accuracy35.7%
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -1.55 \cdot 10^{+102}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 1.75 \cdot 10^{-65}:\\ \;\;\;\;c\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 15
Accuracy22.3%
Cost64
\[c \]

Reproduce?

herbie shell --seed 2023160 
(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))