Average Error: 6.3 → 0.8
Time: 9.7s
Precision: binary64
Cost: 7240
\[x \cdot x - \left(y \cdot 4\right) \cdot \left(z \cdot z - t\right) \]
\[\begin{array}{l} \mathbf{if}\;z \leq -9 \cdot 10^{+153}:\\ \;\;\;\;x \cdot x + z \cdot \left(y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{+39}:\\ \;\;\;\;x \cdot x + \left(\left(z \cdot z\right) \cdot \left(y \cdot -4\right) - t \cdot \left(y \cdot -4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, x, z \cdot \left(z \cdot \left(y \cdot -4\right)\right)\right)\\ \end{array} \]
(FPCore (x y z t) :precision binary64 (- (* x x) (* (* y 4.0) (- (* z z) t))))
(FPCore (x y z t)
 :precision binary64
 (if (<= z -9e+153)
   (+ (* x x) (* z (* y (* z -4.0))))
   (if (<= z 2.6e+39)
     (+ (* x x) (- (* (* z z) (* y -4.0)) (* t (* y -4.0))))
     (fma x x (* z (* z (* y -4.0)))))))
double code(double x, double y, double z, double t) {
	return (x * x) - ((y * 4.0) * ((z * z) - t));
}
double code(double x, double y, double z, double t) {
	double tmp;
	if (z <= -9e+153) {
		tmp = (x * x) + (z * (y * (z * -4.0)));
	} else if (z <= 2.6e+39) {
		tmp = (x * x) + (((z * z) * (y * -4.0)) - (t * (y * -4.0)));
	} else {
		tmp = fma(x, x, (z * (z * (y * -4.0))));
	}
	return tmp;
}
function code(x, y, z, t)
	return Float64(Float64(x * x) - Float64(Float64(y * 4.0) * Float64(Float64(z * z) - t)))
end
function code(x, y, z, t)
	tmp = 0.0
	if (z <= -9e+153)
		tmp = Float64(Float64(x * x) + Float64(z * Float64(y * Float64(z * -4.0))));
	elseif (z <= 2.6e+39)
		tmp = Float64(Float64(x * x) + Float64(Float64(Float64(z * z) * Float64(y * -4.0)) - Float64(t * Float64(y * -4.0))));
	else
		tmp = fma(x, x, Float64(z * Float64(z * Float64(y * -4.0))));
	end
	return tmp
end
code[x_, y_, z_, t_] := N[(N[(x * x), $MachinePrecision] - N[(N[(y * 4.0), $MachinePrecision] * N[(N[(z * z), $MachinePrecision] - t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_] := If[LessEqual[z, -9e+153], N[(N[(x * x), $MachinePrecision] + N[(z * N[(y * N[(z * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 2.6e+39], N[(N[(x * x), $MachinePrecision] + N[(N[(N[(z * z), $MachinePrecision] * N[(y * -4.0), $MachinePrecision]), $MachinePrecision] - N[(t * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * x + N[(z * N[(z * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
x \cdot x - \left(y \cdot 4\right) \cdot \left(z \cdot z - t\right)
\begin{array}{l}
\mathbf{if}\;z \leq -9 \cdot 10^{+153}:\\
\;\;\;\;x \cdot x + z \cdot \left(y \cdot \left(z \cdot -4\right)\right)\\

\mathbf{elif}\;z \leq 2.6 \cdot 10^{+39}:\\
\;\;\;\;x \cdot x + \left(\left(z \cdot z\right) \cdot \left(y \cdot -4\right) - t \cdot \left(y \cdot -4\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x, z \cdot \left(z \cdot \left(y \cdot -4\right)\right)\right)\\


\end{array}

Error

Target

Original6.3
Target6.3
Herbie0.8
\[x \cdot x - 4 \cdot \left(y \cdot \left(z \cdot z - t\right)\right) \]

Derivation

  1. Split input into 3 regimes
  2. if z < -9.0000000000000002e153

    1. Initial program 63.7

      \[x \cdot x - \left(y \cdot 4\right) \cdot \left(z \cdot z - t\right) \]
    2. Taylor expanded in z around inf 63.7

      \[\leadsto x \cdot x - \color{blue}{4 \cdot \left(y \cdot {z}^{2}\right)} \]
    3. Simplified0.3

      \[\leadsto x \cdot x - \color{blue}{z \cdot \left(y \cdot \left(4 \cdot z\right)\right)} \]
      Proof
      (*.f64 z (*.f64 y (*.f64 4 z))): 0 points increase in error, 0 points decrease in error
      (*.f64 z (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 y 4) z))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= *-commutative_binary64 (*.f64 (*.f64 (*.f64 y 4) z) z)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*r*_binary64 (*.f64 (*.f64 y 4) (*.f64 z z))): 53 points increase in error, 25 points decrease in error
      (*.f64 (Rewrite=> *-commutative_binary64 (*.f64 4 y)) (*.f64 z z)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*r*_binary64 (*.f64 4 (*.f64 y (*.f64 z z)))): 3 points increase in error, 2 points decrease in error
      (*.f64 4 (*.f64 y (Rewrite<= unpow2_binary64 (pow.f64 z 2)))): 0 points increase in error, 0 points decrease in error

    if -9.0000000000000002e153 < z < 2.6e39

    1. Initial program 0.1

      \[x \cdot x - \left(y \cdot 4\right) \cdot \left(z \cdot z - t\right) \]
    2. Applied egg-rr0.1

      \[\leadsto x \cdot x - \color{blue}{\left(\left(z \cdot z\right) \cdot \left(y \cdot 4\right) + \left(-t\right) \cdot \left(y \cdot 4\right)\right)} \]

    if 2.6e39 < z

    1. Initial program 22.8

      \[x \cdot x - \left(y \cdot 4\right) \cdot \left(z \cdot z - t\right) \]
    2. Taylor expanded in z around inf 28.1

      \[\leadsto x \cdot x - \color{blue}{4 \cdot \left(y \cdot {z}^{2}\right)} \]
    3. Simplified28.1

      \[\leadsto x \cdot x - \color{blue}{4 \cdot \left(y \cdot \left(z \cdot z\right)\right)} \]
      Proof
      (*.f64 4 (*.f64 y (*.f64 z z))): 0 points increase in error, 0 points decrease in error
      (*.f64 4 (*.f64 y (Rewrite<= unpow2_binary64 (pow.f64 z 2)))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr33.8

      \[\leadsto x \cdot x - \color{blue}{{\left(\left(z \cdot \sqrt{y}\right) \cdot 2\right)}^{2}} \]
    5. Taylor expanded in x around 0 28.1

      \[\leadsto \color{blue}{{x}^{2} + -4 \cdot \left(y \cdot {z}^{2}\right)} \]
    6. Simplified5.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, \left(\left(y \cdot -4\right) \cdot z\right) \cdot z\right)} \]
      Proof
      (fma.f64 x x (*.f64 (*.f64 (*.f64 y -4) z) z)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (*.f64 y (Rewrite<= metadata-eval (neg.f64 4))) z) z)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 y 4))) z) z)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (neg.f64 (*.f64 (Rewrite<= rem-square-sqrt_binary64 (*.f64 (sqrt.f64 y) (sqrt.f64 y))) 4)) z) z)): 154 points increase in error, 8 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (neg.f64 (*.f64 (*.f64 (sqrt.f64 y) (sqrt.f64 y)) (Rewrite<= metadata-eval (*.f64 2 2)))) z) z)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (neg.f64 (Rewrite<= swap-sqr_binary64 (*.f64 (*.f64 (sqrt.f64 y) 2) (*.f64 (sqrt.f64 y) 2)))) z) z)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (neg.f64 (Rewrite<= unpow2_binary64 (pow.f64 (*.f64 (sqrt.f64 y) 2) 2))) z) z)): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (Rewrite<= associate-*r*_binary64 (*.f64 (neg.f64 (pow.f64 (*.f64 (sqrt.f64 y) 2) 2)) (*.f64 z z)))): 17 points increase in error, 6 points decrease in error
      (fma.f64 x x (Rewrite<= distribute-lft-neg-in_binary64 (neg.f64 (*.f64 (pow.f64 (*.f64 (sqrt.f64 y) 2) 2) (*.f64 z z))))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (Rewrite=> distribute-rgt-neg-in_binary64 (*.f64 (pow.f64 (*.f64 (sqrt.f64 y) 2) 2) (neg.f64 (*.f64 z z))))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (Rewrite=> unpow2_binary64 (*.f64 (*.f64 (sqrt.f64 y) 2) (*.f64 (sqrt.f64 y) 2))) (neg.f64 (*.f64 z z)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (Rewrite=> swap-sqr_binary64 (*.f64 (*.f64 (sqrt.f64 y) (sqrt.f64 y)) (*.f64 2 2))) (neg.f64 (*.f64 z z)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 (Rewrite=> rem-square-sqrt_binary64 y) (*.f64 2 2)) (neg.f64 (*.f64 z z)))): 4 points increase in error, 130 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 y (Rewrite=> metadata-eval 4)) (neg.f64 (*.f64 z z)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (Rewrite<= *-commutative_binary64 (*.f64 4 y)) (neg.f64 (*.f64 z z)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 (*.f64 4 y) (*.f64 z z))))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (neg.f64 (Rewrite<= associate-*r*_binary64 (*.f64 4 (*.f64 y (*.f64 z z)))))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (neg.f64 (Rewrite=> *-commutative_binary64 (*.f64 (*.f64 y (*.f64 z z)) 4)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (Rewrite=> distribute-rgt-neg-in_binary64 (*.f64 (*.f64 y (*.f64 z z)) (neg.f64 4)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (*.f64 (*.f64 y (*.f64 z z)) (Rewrite=> metadata-eval -4))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> fma-udef_binary64 (+.f64 (*.f64 x x) (*.f64 (*.f64 y (*.f64 z z)) -4))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) (*.f64 (*.f64 y (*.f64 z z)) -4)): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (*.f64 (*.f64 y (Rewrite<= unpow2_binary64 (pow.f64 z 2))) -4)): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (Rewrite<= *-commutative_binary64 (*.f64 -4 (*.f64 y (pow.f64 z 2))))): 0 points increase in error, 0 points decrease in error
  3. Recombined 3 regimes into one program.
  4. Final simplification0.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -9 \cdot 10^{+153}:\\ \;\;\;\;x \cdot x + z \cdot \left(y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{+39}:\\ \;\;\;\;x \cdot x + \left(\left(z \cdot z\right) \cdot \left(y \cdot -4\right) - t \cdot \left(y \cdot -4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, x, z \cdot \left(z \cdot \left(y \cdot -4\right)\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Error15.0
Cost1356
\[\begin{array}{l} t_1 := \left(y \cdot 4\right) \cdot \left(t - z \cdot z\right)\\ \mathbf{if}\;x \cdot x \leq 1.04 \cdot 10^{-67}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 10^{-55}:\\ \;\;\;\;z \cdot \left(-4 \cdot \left(z \cdot y\right)\right)\\ \mathbf{elif}\;x \cdot x \leq 1.05 \cdot 10^{-22}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
Alternative 2
Error0.8
Cost1352
\[\begin{array}{l} t_1 := x \cdot x + z \cdot \left(y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{if}\;z \leq -1.32 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{+39}:\\ \;\;\;\;x \cdot x + \left(\left(z \cdot z\right) \cdot \left(y \cdot -4\right) - t \cdot \left(y \cdot -4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error7.7
Cost1224
\[\begin{array}{l} \mathbf{if}\;z \cdot z \leq 2 \cdot 10^{-24}:\\ \;\;\;\;x \cdot x + \left(y \cdot 4\right) \cdot t\\ \mathbf{elif}\;z \cdot z \leq 2 \cdot 10^{+225}:\\ \;\;\;\;x \cdot x + -4 \cdot \left(y \cdot \left(z \cdot z\right)\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(-4 \cdot \left(z \cdot y\right)\right)\\ \end{array} \]
Alternative 4
Error0.8
Cost1096
\[\begin{array}{l} t_1 := x \cdot x + z \cdot \left(y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{if}\;z \leq -1.2 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{+39}:\\ \;\;\;\;x \cdot x + \left(y \cdot 4\right) \cdot \left(t - z \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Error26.6
Cost976
\[\begin{array}{l} t_1 := 4 \cdot \left(y \cdot t\right)\\ t_2 := z \cdot \left(-4 \cdot \left(z \cdot y\right)\right)\\ \mathbf{if}\;z \leq -2.15 \cdot 10^{-11}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq 1.5 \cdot 10^{-212}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 5 \cdot 10^{-162}:\\ \;\;\;\;x \cdot x\\ \mathbf{elif}\;z \leq 2.95 \cdot 10^{+34}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 6
Error9.7
Cost972
\[\begin{array}{l} t_1 := z \cdot \left(-4 \cdot \left(z \cdot y\right)\right)\\ \mathbf{if}\;z \leq -1 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -2.2 \cdot 10^{-14}:\\ \;\;\;\;\left(y \cdot 4\right) \cdot \left(t - z \cdot z\right)\\ \mathbf{elif}\;z \leq 5.8 \cdot 10^{+93}:\\ \;\;\;\;x \cdot x + \left(y \cdot 4\right) \cdot t\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Error6.5
Cost968
\[\begin{array}{l} t_1 := x \cdot x + z \cdot \left(y \cdot \left(z \cdot -4\right)\right)\\ \mathbf{if}\;z \leq -5.5 \cdot 10^{-12}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.95 \cdot 10^{+34}:\\ \;\;\;\;x \cdot x + \left(y \cdot 4\right) \cdot t\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 8
Error25.5
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -3.5 \cdot 10^{-25}:\\ \;\;\;\;x \cdot x\\ \mathbf{elif}\;x \leq 3.4 \cdot 10^{-11}:\\ \;\;\;\;4 \cdot \left(y \cdot t\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
Alternative 9
Error41.9
Cost192
\[x \cdot x \]

Error

Reproduce

herbie shell --seed 2022330 
(FPCore (x y z t)
  :name "Graphics.Rasterific.Shading:$sradialGradientWithFocusShader from Rasterific-0.6.1, B"
  :precision binary64

  :herbie-target
  (- (* x x) (* 4.0 (* y (- (* z z) t))))

  (- (* x x) (* (* y 4.0) (- (* z z) t))))