?

Average Error: 2.2 → 1.3
Time: 13.0s
Precision: binary64
Cost: 19780

?

\[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b \]
\[\begin{array}{l} \mathbf{if}\;z \leq 10^{-39}:\\ \;\;\;\;\left(t \cdot a + \left(x + z \cdot y\right)\right) + \left(z \cdot a\right) \cdot b\\ \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 (<= z 1e-39)
   (+ (+ (* t a) (+ x (* z y))) (* (* z a) b))
   (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 (z <= 1e-39) {
		tmp = ((t * a) + (x + (z * y))) + ((z * a) * b);
	} 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 (z <= 1e-39)
		tmp = Float64(Float64(Float64(t * a) + Float64(x + Float64(z * y))) + Float64(Float64(z * a) * b));
	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[LessEqual[z, 1e-39], N[(N[(N[(t * a), $MachinePrecision] + N[(x + N[(z * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(z * a), $MachinePrecision] * b), $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}\;z \leq 10^{-39}:\\
\;\;\;\;\left(t \cdot a + \left(x + z \cdot y\right)\right) + \left(z \cdot a\right) \cdot b\\

\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.2
Target0.3
Herbie1.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 z < 9.99999999999999929e-40

    1. Initial program 1.6

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

    if 9.99999999999999929e-40 < z

    1. Initial program 4.0

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

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

      [Start]4.0

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

      +-commutative [=>]4.0

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

      +-commutative [=>]4.0

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

      associate-+l+ [=>]4.0

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

      associate-+r+ [=>]4.0

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

      *-commutative [=>]4.0

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

      associate-*l* [=>]0.2

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

      *-commutative [=>]0.2

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

      distribute-lft-out [=>]0.2

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

      fma-def [=>]0.2

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

      fma-def [=>]0.2

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

      +-commutative [=>]0.2

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

      fma-def [=>]0.2

      \[ \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 simplification1.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 10^{-39}:\\ \;\;\;\;\left(t \cdot a + \left(x + z \cdot y\right)\right) + \left(z \cdot a\right) \cdot b\\ \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
Error1.4
Cost1988
\[\begin{array}{l} t_1 := x + z \cdot y\\ t_2 := \left(t \cdot a + t_1\right) + \left(z \cdot a\right) \cdot b\\ \mathbf{if}\;t_2 \leq 2 \cdot 10^{+298}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot \left(z \cdot b\right) + t \cdot a\right) + t_1\\ \end{array} \]
Alternative 2
Error22.2
Cost1769
\[\begin{array}{l} t_1 := x + z \cdot y\\ t_2 := z \cdot \left(y + a \cdot b\right)\\ t_3 := x + t \cdot a\\ \mathbf{if}\;z \leq -3.4 \cdot 10^{+192}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -5.2 \cdot 10^{+38}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq -7.6 \cdot 10^{-71}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -1.7 \cdot 10^{-117}:\\ \;\;\;\;a \cdot \left(z \cdot b\right)\\ \mathbf{elif}\;z \leq -4.6 \cdot 10^{-136}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;z \leq -2.8 \cdot 10^{-149}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq 2.1 \cdot 10^{-46}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;z \leq 0.00021 \lor \neg \left(z \leq 2.15 \cdot 10^{+55}\right) \land z \leq 1.2 \cdot 10^{+130}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error34.3
Cost1244
\[\begin{array}{l} t_1 := \left(z \cdot a\right) \cdot b\\ \mathbf{if}\;x \leq -1.2 \cdot 10^{-6}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -7.8 \cdot 10^{-255}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq -1.8 \cdot 10^{-302}:\\ \;\;\;\;a \cdot \left(z \cdot b\right)\\ \mathbf{elif}\;x \leq 2.55 \cdot 10^{-291}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq 2.7 \cdot 10^{-70}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 3.15 \cdot 10^{-6}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;x \leq 6200:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 4
Error34.5
Cost980
\[\begin{array}{l} t_1 := a \cdot \left(z \cdot b\right)\\ \mathbf{if}\;x \leq -1.25 \cdot 10^{-7}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq -6 \cdot 10^{-255}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq -3.5 \cdot 10^{-306}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 5.1 \cdot 10^{-290}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq 0.0145:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 5
Error27.6
Cost980
\[\begin{array}{l} t_1 := x + t \cdot a\\ \mathbf{if}\;x \leq -1.7 \cdot 10^{-165}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -1.42 \cdot 10^{-247}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq -1.7 \cdot 10^{-303}:\\ \;\;\;\;a \cdot \left(z \cdot b\right)\\ \mathbf{elif}\;x \leq 7.5 \cdot 10^{-295}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq 6.5 \cdot 10^{-111}:\\ \;\;\;\;\left(z \cdot a\right) \cdot b\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error2.7
Cost960
\[\left(a \cdot \left(z \cdot b\right) + t \cdot a\right) + \left(x + z \cdot y\right) \]
Alternative 7
Error21.0
Cost844
\[\begin{array}{l} t_1 := a \cdot \left(t + z \cdot b\right)\\ \mathbf{if}\;a \leq -3.2 \cdot 10^{+156}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \leq -3.9 \cdot 10^{-31}:\\ \;\;\;\;x + \left(z \cdot a\right) \cdot b\\ \mathbf{elif}\;a \leq 4.8 \cdot 10^{-30}:\\ \;\;\;\;x + z \cdot y\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 8
Error20.8
Cost844
\[\begin{array}{l} t_1 := a \cdot \left(t + z \cdot b\right)\\ \mathbf{if}\;a \leq -1.7 \cdot 10^{+161}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \leq -4 \cdot 10^{-31}:\\ \;\;\;\;x + a \cdot \left(z \cdot b\right)\\ \mathbf{elif}\;a \leq 2.55 \cdot 10^{-30}:\\ \;\;\;\;x + z \cdot y\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 9
Error12.6
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -2.3 \cdot 10^{-102} \lor \neg \left(a \leq 1.5 \cdot 10^{-30}\right):\\ \;\;\;\;x + a \cdot \left(t + z \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;x + z \cdot y\\ \end{array} \]
Alternative 10
Error8.1
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -7 \cdot 10^{-36} \lor \neg \left(a \leq 1.6 \cdot 10^{-30}\right):\\ \;\;\;\;x + a \cdot \left(t + z \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x + t \cdot a\right) + z \cdot y\\ \end{array} \]
Alternative 11
Error20.8
Cost713
\[\begin{array}{l} \mathbf{if}\;a \leq -4.2 \cdot 10^{+105} \lor \neg \left(a \leq 4.8 \cdot 10^{-30}\right):\\ \;\;\;\;a \cdot \left(t + z \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;x + z \cdot y\\ \end{array} \]
Alternative 12
Error32.6
Cost588
\[\begin{array}{l} \mathbf{if}\;x \leq -1.55 \cdot 10^{-6}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 1.75 \cdot 10^{-264}:\\ \;\;\;\;z \cdot y\\ \mathbf{elif}\;x \leq 12000:\\ \;\;\;\;t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 13
Error19.9
Cost585
\[\begin{array}{l} \mathbf{if}\;a \leq -1.1 \cdot 10^{-9} \lor \neg \left(a \leq 2 \cdot 10^{-30}\right):\\ \;\;\;\;x + t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x + z \cdot y\\ \end{array} \]
Alternative 14
Error33.0
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -3 \cdot 10^{-43}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 78000:\\ \;\;\;\;t \cdot a\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 15
Error39.8
Cost64
\[x \]

Error

Reproduce?

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