?

Average Error: 2.3 → 0.3
Time: 13.0s
Precision: binary64
Cost: 19913

?

\[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b \]
\[\begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-6} \lor \neg \left(b \leq 1.4 \cdot 10^{+40}\right):\\ \;\;\;\;b \cdot \left(z \cdot a\right) + \left(\left(x + y \cdot z\right) + t \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(z, \mathsf{fma}\left(a, b, y\right), \mathsf{fma}\left(t, a, x\right)\right)\\ \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (+ (+ (+ x (* y z)) (* t a)) (* (* a z) b)))
(FPCore (x y z t a b)
 :precision binary64
 (if (or (<= b -5e-6) (not (<= b 1.4e+40)))
   (+ (* b (* z a)) (+ (+ x (* y z)) (* t a)))
   (fma z (fma a b y) (fma t a x))))
double code(double x, double y, double z, double t, double a, double b) {
	return ((x + (y * z)) + (t * a)) + ((a * z) * b);
}
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((b <= -5e-6) || !(b <= 1.4e+40)) {
		tmp = (b * (z * a)) + ((x + (y * z)) + (t * a));
	} else {
		tmp = fma(z, fma(a, b, y), fma(t, a, x));
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	return Float64(Float64(Float64(x + Float64(y * z)) + Float64(t * a)) + Float64(Float64(a * z) * b))
end
function code(x, y, z, t, a, b)
	tmp = 0.0
	if ((b <= -5e-6) || !(b <= 1.4e+40))
		tmp = Float64(Float64(b * Float64(z * a)) + Float64(Float64(x + Float64(y * z)) + Float64(t * a)));
	else
		tmp = fma(z, fma(a, b, y), fma(t, a, x));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision] + N[(t * a), $MachinePrecision]), $MachinePrecision] + N[(N[(a * z), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[b, -5e-6], N[Not[LessEqual[b, 1.4e+40]], $MachinePrecision]], N[(N[(b * N[(z * a), $MachinePrecision]), $MachinePrecision] + N[(N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision] + N[(t * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(z * N[(a * b + y), $MachinePrecision] + N[(t * a + x), $MachinePrecision]), $MachinePrecision]]
\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-6} \lor \neg \left(b \leq 1.4 \cdot 10^{+40}\right):\\
\;\;\;\;b \cdot \left(z \cdot a\right) + \left(\left(x + y \cdot z\right) + t \cdot a\right)\\

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


\end{array}

Error?

Target

Original2.3
Target0.3
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;z < -11820553527347888000:\\ \;\;\;\;z \cdot \left(b \cdot a + y\right) + \left(x + t \cdot a\right)\\ \mathbf{elif}\;z < 4.7589743188364287 \cdot 10^{-122}:\\ \;\;\;\;\left(b \cdot z + t\right) \cdot a + \left(z \cdot y + x\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(b \cdot a + y\right) + \left(x + t \cdot a\right)\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if b < -5.00000000000000041e-6 or 1.4000000000000001e40 < b

    1. Initial program 0.6

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

    if -5.00000000000000041e-6 < b < 1.4000000000000001e40

    1. Initial program 3.4

      \[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b \]
    2. Simplified0.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, \mathsf{fma}\left(a, b, y\right), \mathsf{fma}\left(t, a, x\right)\right)} \]
      Proof

      [Start]3.4

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

      +-commutative [=>]3.4

      \[ \color{blue}{\left(a \cdot z\right) \cdot b + \left(\left(x + y \cdot z\right) + t \cdot a\right)} \]

      +-commutative [=>]3.4

      \[ \left(a \cdot z\right) \cdot b + \left(\color{blue}{\left(y \cdot z + x\right)} + t \cdot a\right) \]

      associate-+l+ [=>]3.4

      \[ \left(a \cdot z\right) \cdot b + \color{blue}{\left(y \cdot z + \left(x + t \cdot a\right)\right)} \]

      associate-+r+ [=>]3.4

      \[ \color{blue}{\left(\left(a \cdot z\right) \cdot b + y \cdot z\right) + \left(x + t \cdot a\right)} \]

      *-commutative [=>]3.4

      \[ \left(\color{blue}{\left(z \cdot a\right)} \cdot b + y \cdot z\right) + \left(x + t \cdot a\right) \]

      associate-*l* [=>]0.0

      \[ \left(\color{blue}{z \cdot \left(a \cdot b\right)} + y \cdot z\right) + \left(x + t \cdot a\right) \]

      *-commutative [=>]0.0

      \[ \left(z \cdot \left(a \cdot b\right) + \color{blue}{z \cdot y}\right) + \left(x + t \cdot a\right) \]

      distribute-lft-out [=>]0.0

      \[ \color{blue}{z \cdot \left(a \cdot b + y\right)} + \left(x + t \cdot a\right) \]

      fma-def [=>]0.0

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

      fma-def [=>]0.0

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

      +-commutative [=>]0.0

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

      fma-def [=>]0.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-6} \lor \neg \left(b \leq 1.4 \cdot 10^{+40}\right):\\ \;\;\;\;b \cdot \left(z \cdot a\right) + \left(\left(x + y \cdot z\right) + t \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(z, \mathsf{fma}\left(a, b, y\right), \mathsf{fma}\left(t, a, x\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Error2.5
Cost1092
\[\begin{array}{l} t_1 := x + y \cdot z\\ \mathbf{if}\;x \leq -5 \cdot 10^{-83}:\\ \;\;\;\;b \cdot \left(z \cdot a\right) + \left(t_1 + t \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;t_1 + \left(a \cdot \left(b \cdot z\right) + t \cdot a\right)\\ \end{array} \]
Alternative 2
Error36.4
Cost984
\[\begin{array}{l} \mathbf{if}\;x \leq -3 \cdot 10^{+171}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -5.4 \cdot 10^{-154}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;x \leq 3.8 \cdot 10^{-216}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;x \leq 4.7 \cdot 10^{-119}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;x \leq 2.6 \cdot 10^{-8}:\\ \;\;\;\;a \cdot \left(b \cdot z\right)\\ \mathbf{elif}\;x \leq 1.1 \cdot 10^{+38}:\\ \;\;\;\;t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 3
Error19.4
Cost976
\[\begin{array}{l} t_1 := t \cdot a + y \cdot z\\ \mathbf{if}\;x \leq -2.5 \cdot 10^{+34}:\\ \;\;\;\;x + y \cdot z\\ \mathbf{elif}\;x \leq 5 \cdot 10^{-125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 4.7 \cdot 10^{-8}:\\ \;\;\;\;a \cdot \left(t + b \cdot z\right)\\ \mathbf{elif}\;x \leq 2.3 \cdot 10^{+16}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x + t \cdot a\\ \end{array} \]
Alternative 4
Error18.8
Cost976
\[\begin{array}{l} t_1 := t \cdot a + y \cdot z\\ \mathbf{if}\;x \leq -7.6 \cdot 10^{+38}:\\ \;\;\;\;x + y \cdot z\\ \mathbf{elif}\;x \leq 3.45 \cdot 10^{-56}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.75 \cdot 10^{-8}:\\ \;\;\;\;b \cdot \left(z \cdot a\right) + y \cdot z\\ \mathbf{elif}\;x \leq 14600000000000:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x + t \cdot a\\ \end{array} \]
Alternative 5
Error2.7
Cost960
\[\left(x + y \cdot z\right) + \left(a \cdot \left(b \cdot z\right) + t \cdot a\right) \]
Alternative 6
Error11.6
Cost841
\[\begin{array}{l} \mathbf{if}\;t \leq -9.5 \cdot 10^{-129} \lor \neg \left(t \leq -5.6 \cdot 10^{-189}\right):\\ \;\;\;\;\left(x + y \cdot z\right) + t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x + b \cdot \left(z \cdot a\right)\\ \end{array} \]
Alternative 7
Error35.5
Cost588
\[\begin{array}{l} \mathbf{if}\;x \leq -3 \cdot 10^{+171}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -2.95 \cdot 10^{-148}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;x \leq 1.3 \cdot 10^{+37}:\\ \;\;\;\;t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 8
Error19.7
Cost585
\[\begin{array}{l} \mathbf{if}\;a \leq -1.7 \cdot 10^{-25} \lor \neg \left(a \leq 1.4 \cdot 10^{-70}\right):\\ \;\;\;\;x + t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x + y \cdot z\\ \end{array} \]
Alternative 9
Error20.1
Cost584
\[\begin{array}{l} \mathbf{if}\;a \leq -75:\\ \;\;\;\;a \cdot \left(t + b \cdot z\right)\\ \mathbf{elif}\;a \leq 1.3 \cdot 10^{-70}:\\ \;\;\;\;x + y \cdot z\\ \mathbf{else}:\\ \;\;\;\;x + t \cdot a\\ \end{array} \]
Alternative 10
Error33.2
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -2.4 \cdot 10^{+41}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 10^{+36}:\\ \;\;\;\;t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 11
Error25.1
Cost452
\[\begin{array}{l} \mathbf{if}\;y \leq 3.3 \cdot 10^{+127}:\\ \;\;\;\;x + t \cdot a\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]
Alternative 12
Error40.2
Cost64
\[x \]

Error

Reproduce?

herbie shell --seed 2023060 
(FPCore (x y z t a b)
  :name "Graphics.Rasterific.CubicBezier:cachedBezierAt from Rasterific-0.6.1"
  :precision binary64

  :herbie-target
  (if (< z -11820553527347888000.0) (+ (* z (+ (* b a) y)) (+ x (* t a))) (if (< z 4.7589743188364287e-122) (+ (* (+ (* b z) t) a) (+ (* z y) x)) (+ (* z (+ (* b a) y)) (+ x (* t a)))))

  (+ (+ (+ x (* y z)) (* t a)) (* (* a z) b)))