Average Error: 2.9 → 0.5
Time: 5.1s
Precision: binary64
\[ \begin{array}{c}[y, z, t] = \mathsf{sort}([y, z, t])\\ \end{array} \]
\[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
\[\begin{array}{l} \mathbf{if}\;z \leq 10^{-138}:\\ \;\;\;\;\mathsf{fma}\left(y, t \cdot \left(z \cdot -9\right), \mathsf{fma}\left(27, a \cdot b, 2 \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(27, a \cdot b, \mathsf{fma}\left(t \cdot -9, z \cdot y, 2 \cdot x\right)\right)\\ \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (+ (- (* x 2.0) (* (* (* y 9.0) z) t)) (* (* a 27.0) b)))
(FPCore (x y z t a b)
 :precision binary64
 (if (<= z 1e-138)
   (fma y (* t (* z -9.0)) (fma 27.0 (* a b) (* 2.0 x)))
   (fma 27.0 (* a b) (fma (* t -9.0) (* z y) (* 2.0 x)))))
double code(double x, double y, double z, double t, double a, double b) {
	return ((x * 2.0) - (((y * 9.0) * z) * t)) + ((a * 27.0) * b);
}
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (z <= 1e-138) {
		tmp = fma(y, (t * (z * -9.0)), fma(27.0, (a * b), (2.0 * x)));
	} else {
		tmp = fma(27.0, (a * b), fma((t * -9.0), (z * y), (2.0 * x)));
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	return Float64(Float64(Float64(x * 2.0) - Float64(Float64(Float64(y * 9.0) * z) * t)) + Float64(Float64(a * 27.0) * b))
end
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (z <= 1e-138)
		tmp = fma(y, Float64(t * Float64(z * -9.0)), fma(27.0, Float64(a * b), Float64(2.0 * x)));
	else
		tmp = fma(27.0, Float64(a * b), fma(Float64(t * -9.0), Float64(z * y), Float64(2.0 * x)));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x * 2.0), $MachinePrecision] - N[(N[(N[(y * 9.0), $MachinePrecision] * z), $MachinePrecision] * t), $MachinePrecision]), $MachinePrecision] + N[(N[(a * 27.0), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[z, 1e-138], N[(y * N[(t * N[(z * -9.0), $MachinePrecision]), $MachinePrecision] + N[(27.0 * N[(a * b), $MachinePrecision] + N[(2.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(27.0 * N[(a * b), $MachinePrecision] + N[(N[(t * -9.0), $MachinePrecision] * N[(z * y), $MachinePrecision] + N[(2.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b
\begin{array}{l}
\mathbf{if}\;z \leq 10^{-138}:\\
\;\;\;\;\mathsf{fma}\left(y, t \cdot \left(z \cdot -9\right), \mathsf{fma}\left(27, a \cdot b, 2 \cdot x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(27, a \cdot b, \mathsf{fma}\left(t \cdot -9, z \cdot y, 2 \cdot x\right)\right)\\


\end{array}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Bits error versus t

Bits error versus a

Bits error versus b

Target

Original2.9
Target3.2
Herbie0.5
\[\begin{array}{l} \mathbf{if}\;y < 7.590524218811189 \cdot 10^{-161}:\\ \;\;\;\;\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + a \cdot \left(27 \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 2 - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)\right) + \left(a \cdot 27\right) \cdot b\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if z < 1.00000000000000007e-138

    1. Initial program 3.7

      \[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
    2. Simplified0.6

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 2, \mathsf{fma}\left(y, z \cdot \left(t \cdot -9\right), a \cdot \left(27 \cdot b\right)\right)\right)} \]
    3. Taylor expanded in x around 0 0.5

      \[\leadsto \color{blue}{\left(27 \cdot \left(a \cdot b\right) + 2 \cdot x\right) - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)} \]
    4. Simplified0.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(27, a \cdot b, \mathsf{fma}\left(y, t \cdot \left(z \cdot -9\right), 2 \cdot x\right)\right)} \]
    5. Taylor expanded in y around 0 0.5

      \[\leadsto \mathsf{fma}\left(27, a \cdot b, \color{blue}{2 \cdot x - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)}\right) \]
    6. Simplified3.5

      \[\leadsto \mathsf{fma}\left(27, a \cdot b, \color{blue}{\mathsf{fma}\left(t \cdot -9, y \cdot z, 2 \cdot x\right)}\right) \]
    7. Taylor expanded in a around 0 0.5

      \[\leadsto \color{blue}{\left(27 \cdot \left(a \cdot b\right) + 2 \cdot x\right) - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)} \]
    8. Simplified0.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, t \cdot \left(z \cdot -9\right), \mathsf{fma}\left(27, a \cdot b, 2 \cdot x\right)\right)} \]

    if 1.00000000000000007e-138 < z

    1. Initial program 0.7

      \[\left(x \cdot 2 - \left(\left(y \cdot 9\right) \cdot z\right) \cdot t\right) + \left(a \cdot 27\right) \cdot b \]
    2. Simplified9.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 2, \mathsf{fma}\left(y, z \cdot \left(t \cdot -9\right), a \cdot \left(27 \cdot b\right)\right)\right)} \]
    3. Taylor expanded in x around 0 9.1

      \[\leadsto \color{blue}{\left(27 \cdot \left(a \cdot b\right) + 2 \cdot x\right) - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)} \]
    4. Simplified9.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(27, a \cdot b, \mathsf{fma}\left(y, t \cdot \left(z \cdot -9\right), 2 \cdot x\right)\right)} \]
    5. Taylor expanded in y around 0 9.1

      \[\leadsto \mathsf{fma}\left(27, a \cdot b, \color{blue}{2 \cdot x - 9 \cdot \left(y \cdot \left(t \cdot z\right)\right)}\right) \]
    6. Simplified0.7

      \[\leadsto \mathsf{fma}\left(27, a \cdot b, \color{blue}{\mathsf{fma}\left(t \cdot -9, y \cdot z, 2 \cdot x\right)}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 10^{-138}:\\ \;\;\;\;\mathsf{fma}\left(y, t \cdot \left(z \cdot -9\right), \mathsf{fma}\left(27, a \cdot b, 2 \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(27, a \cdot b, \mathsf{fma}\left(t \cdot -9, z \cdot y, 2 \cdot x\right)\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022166 
(FPCore (x y z t a b)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, A"
  :precision binary64

  :herbie-target
  (if (< y 7.590524218811189e-161) (+ (- (* x 2.0) (* (* (* y 9.0) z) t)) (* a (* 27.0 b))) (+ (- (* x 2.0) (* 9.0 (* y (* t z)))) (* (* a 27.0) b)))

  (+ (- (* x 2.0) (* (* (* y 9.0) z) t)) (* (* a 27.0) b)))