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

?

Percentage Accurate: 97.7% → 98.8%
Time: 11.3s
Precision: binary64
Cost: 8004

?

\[\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(t \cdot 0.0625, z, -0.25 \cdot \left(b \cdot a\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 (* t 0.0625) z (* -0.25 (* b a))))))
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((t * 0.0625), z, (-0.25 * (b * a)));
	}
	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(Float64(t * 0.0625), z, Float64(-0.25 * Float64(b * a)));
	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[(N[(t * 0.0625), $MachinePrecision] * z + N[(-0.25 * N[(b * a), $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(t \cdot 0.0625, z, -0.25 \cdot \left(b \cdot a\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 17 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. Simplified16.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, \frac{z}{\frac{16}{t}}\right) - \left(\frac{a}{\frac{4}{b}} - c\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)} \]

      sub-neg [=>]0.0%

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

      neg-mul-1 [=>]0.0%

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

      metadata-eval [<=]0.0%

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

      metadata-eval [<=]0.0%

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

      cancel-sign-sub-inv [<=]0.0%

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

      fma-def [=>]16.7%

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

      associate-/l* [=>]16.7%

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

      metadata-eval [=>]16.7%

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

      *-lft-identity [=>]16.7%

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

      associate-/l* [=>]16.7%

      \[ \mathsf{fma}\left(x, y, \frac{z}{\frac{16}{t}}\right) - \left(\color{blue}{\frac{a}{\frac{4}{b}}} - c\right) \]
    3. Applied egg-rr0.0%

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

      [Start]16.7%

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

      fma-udef [=>]0.0%

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

      associate-/l* [<=]0.0%

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

      +-commutative [=>]0.0%

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

      associate-/l* [=>]0.0%

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

      div-inv [=>]0.0%

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

      clear-num [<=]0.0%

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

      div-inv [=>]0.0%

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

      metadata-eval [=>]0.0%

      \[ \left(z \cdot \left(t \cdot \color{blue}{0.0625}\right) + x \cdot y\right) - \left(\frac{a}{\frac{4}{b}} - c\right) \]
    4. Applied egg-rr33.3%

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

      [Start]0.0%

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

      associate--l+ [=>]0.0%

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

      *-commutative [=>]0.0%

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

      fma-def [=>]33.3%

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

      associate-/r/ [=>]33.3%

      \[ \mathsf{fma}\left(t \cdot 0.0625, z, x \cdot y - \left(\color{blue}{\frac{a}{4} \cdot b} - c\right)\right) \]
    5. Taylor expanded in a around inf 83.3%

      \[\leadsto \mathsf{fma}\left(t \cdot 0.0625, z, \color{blue}{-0.25 \cdot \left(a \cdot b\right)}\right) \]
    6. Simplified83.3%

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

      [Start]83.3%

      \[ \mathsf{fma}\left(t \cdot 0.0625, z, -0.25 \cdot \left(a \cdot b\right)\right) \]

      *-commutative [<=]83.3%

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

    \[\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(t \cdot 0.0625, z, -0.25 \cdot \left(b \cdot a\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.8%
Cost8004
\[\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(t \cdot 0.0625, z, -0.25 \cdot \left(b \cdot a\right)\right)\\ \end{array} \]
Alternative 2
Accuracy99.0%
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.6%
Cost7360
\[\mathsf{fma}\left(t \cdot 0.0625, z, x \cdot y + \left(c - b \cdot \frac{a}{4}\right)\right) \]
Alternative 5
Accuracy98.5%
Cost2116
\[\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}:\\ \;\;\;\;b \cdot \left(-0.25 \cdot a\right)\\ \end{array} \]
Alternative 6
Accuracy66.3%
Cost1616
\[\begin{array}{l} t_1 := 0.0625 \cdot \left(z \cdot t\right)\\ t_2 := t_1 + x \cdot y\\ t_3 := c + b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{if}\;b \cdot a \leq -1 \cdot 10^{+162}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;b \cdot a \leq -2 \cdot 10^{-178}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;b \cdot a \leq -4 \cdot 10^{-263}:\\ \;\;\;\;c + t_1\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+164}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_3\\ \end{array} \]
Alternative 7
Accuracy36.1%
Cost1508
\[\begin{array}{l} t_1 := 0.0625 \cdot \left(z \cdot t\right)\\ t_2 := b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{if}\;y \leq -1.9 \cdot 10^{-113}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq -3.5 \cdot 10^{-264}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -4.5 \cdot 10^{-302}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq 1.9 \cdot 10^{-251}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 2.1 \cdot 10^{-213}:\\ \;\;\;\;c\\ \mathbf{elif}\;y \leq 1.1 \cdot 10^{-124}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq 1.1 \cdot 10^{-21}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 11500000000000:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq 3.7 \cdot 10^{+183}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 8
Accuracy57.0%
Cost1504
\[\begin{array}{l} t_1 := c + 0.0625 \cdot \left(z \cdot t\right)\\ t_2 := c + x \cdot y\\ t_3 := c + b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{if}\;y \leq -1.9 \cdot 10^{-113}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq -1.5 \cdot 10^{-262}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -4.2 \cdot 10^{-301}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;y \leq 3.8 \cdot 10^{-230}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 3.2 \cdot 10^{-124}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;y \leq 3.3 \cdot 10^{-22}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 54000000000000:\\ \;\;\;\;t_3\\ \mathbf{elif}\;y \leq 3.7 \cdot 10^{+183}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 9
Accuracy88.5%
Cost1353
\[\begin{array}{l} t_1 := 0.0625 \cdot \left(z \cdot t\right)\\ \mathbf{if}\;b \cdot a \leq -1 \cdot 10^{+162} \lor \neg \left(b \cdot a \leq 2 \cdot 10^{+49}\right):\\ \;\;\;\;\left(c + t_1\right) - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;c + \left(t_1 + x \cdot y\right)\\ \end{array} \]
Alternative 10
Accuracy86.1%
Cost1225
\[\begin{array}{l} \mathbf{if}\;b \cdot a \leq -1 \cdot 10^{+162} \lor \neg \left(b \cdot a \leq 2 \cdot 10^{+164}\right):\\ \;\;\;\;c + b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;c + \left(0.0625 \cdot \left(z \cdot t\right) + x \cdot y\right)\\ \end{array} \]
Alternative 11
Accuracy85.7%
Cost1225
\[\begin{array}{l} t_1 := 0.0625 \cdot \left(z \cdot t\right)\\ \mathbf{if}\;b \cdot a \leq -1 \cdot 10^{+162} \lor \neg \left(b \cdot a \leq 2 \cdot 10^{+49}\right):\\ \;\;\;\;t_1 - \left(b \cdot a\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;c + \left(t_1 + x \cdot y\right)\\ \end{array} \]
Alternative 12
Accuracy86.6%
Cost1224
\[\begin{array}{l} t_1 := \left(b \cdot a\right) \cdot 0.25\\ t_2 := 0.0625 \cdot \left(z \cdot t\right)\\ \mathbf{if}\;b \cdot a \leq -1 \cdot 10^{+162}:\\ \;\;\;\;\left(c + x \cdot y\right) - t_1\\ \mathbf{elif}\;b \cdot a \leq 2 \cdot 10^{+49}:\\ \;\;\;\;c + \left(t_2 + x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;t_2 - t_1\\ \end{array} \]
Alternative 13
Accuracy35.4%
Cost1112
\[\begin{array}{l} t_1 := 0.0625 \cdot \left(z \cdot t\right)\\ \mathbf{if}\;y \leq -1.3 \cdot 10^{-113}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 2.4 \cdot 10^{-251}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 8.2 \cdot 10^{-125}:\\ \;\;\;\;c\\ \mathbf{elif}\;y \leq 2.9 \cdot 10^{-22}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 0.0135:\\ \;\;\;\;c\\ \mathbf{elif}\;y \leq 3.7 \cdot 10^{+183}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 14
Accuracy52.0%
Cost848
\[\begin{array}{l} t_1 := c + x \cdot y\\ t_2 := 0.0625 \cdot \left(z \cdot t\right)\\ \mathbf{if}\;t \leq -9 \cdot 10^{-29}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t \leq 6.1 \cdot 10^{-69}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t \leq 8.8 \cdot 10^{-26}:\\ \;\;\;\;b \cdot \left(-0.25 \cdot a\right)\\ \mathbf{elif}\;t \leq 1.75 \cdot 10^{+190}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 15
Accuracy56.9%
Cost713
\[\begin{array}{l} \mathbf{if}\;y \leq -1.9 \cdot 10^{-113} \lor \neg \left(y \leq 6.4 \cdot 10^{+183}\right):\\ \;\;\;\;c + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;c + 0.0625 \cdot \left(z \cdot t\right)\\ \end{array} \]
Alternative 16
Accuracy34.2%
Cost456
\[\begin{array}{l} \mathbf{if}\;y \leq -1.5 \cdot 10^{-182}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 3.4 \cdot 10^{+57}:\\ \;\;\;\;c\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 17
Accuracy22.6%
Cost64
\[c \]

Reproduce?

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