Average Error: 37.5 → 9.1
Time: 13.9s
Precision: binary64
Cost: 7500
\[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
\[\begin{array}{l} \mathbf{if}\;y \leq -4.9 \cdot 10^{+93}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;y \leq 1.4 \cdot 10^{-28}:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \mathbf{elif}\;y \leq 6.4 \cdot 10^{+15}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;y \leq 8.2 \cdot 10^{+51}:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z)
 :precision binary64
 (if (<= y -4.9e+93)
   (hypot y x)
   (if (<= y 1.4e-28)
     (hypot z x)
     (if (<= y 6.4e+15)
       (sqrt (+ (* x x) (+ (* y y) (* z z))))
       (if (<= y 8.2e+51) (hypot z x) (hypot y x))))))
double code(double x, double y, double z) {
	return sqrt(((x * x) + ((y * y) + (z * z))));
}
double code(double x, double y, double z) {
	double tmp;
	if (y <= -4.9e+93) {
		tmp = hypot(y, x);
	} else if (y <= 1.4e-28) {
		tmp = hypot(z, x);
	} else if (y <= 6.4e+15) {
		tmp = sqrt(((x * x) + ((y * y) + (z * z))));
	} else if (y <= 8.2e+51) {
		tmp = hypot(z, x);
	} else {
		tmp = hypot(y, x);
	}
	return tmp;
}
public static double code(double x, double y, double z) {
	return Math.sqrt(((x * x) + ((y * y) + (z * z))));
}
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -4.9e+93) {
		tmp = Math.hypot(y, x);
	} else if (y <= 1.4e-28) {
		tmp = Math.hypot(z, x);
	} else if (y <= 6.4e+15) {
		tmp = Math.sqrt(((x * x) + ((y * y) + (z * z))));
	} else if (y <= 8.2e+51) {
		tmp = Math.hypot(z, x);
	} else {
		tmp = Math.hypot(y, x);
	}
	return tmp;
}
def code(x, y, z):
	return math.sqrt(((x * x) + ((y * y) + (z * z))))
def code(x, y, z):
	tmp = 0
	if y <= -4.9e+93:
		tmp = math.hypot(y, x)
	elif y <= 1.4e-28:
		tmp = math.hypot(z, x)
	elif y <= 6.4e+15:
		tmp = math.sqrt(((x * x) + ((y * y) + (z * z))))
	elif y <= 8.2e+51:
		tmp = math.hypot(z, x)
	else:
		tmp = math.hypot(y, x)
	return tmp
function code(x, y, z)
	return sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))))
end
function code(x, y, z)
	tmp = 0.0
	if (y <= -4.9e+93)
		tmp = hypot(y, x);
	elseif (y <= 1.4e-28)
		tmp = hypot(z, x);
	elseif (y <= 6.4e+15)
		tmp = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))));
	elseif (y <= 8.2e+51)
		tmp = hypot(z, x);
	else
		tmp = hypot(y, x);
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = sqrt(((x * x) + ((y * y) + (z * z))));
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -4.9e+93)
		tmp = hypot(y, x);
	elseif (y <= 1.4e-28)
		tmp = hypot(z, x);
	elseif (y <= 6.4e+15)
		tmp = sqrt(((x * x) + ((y * y) + (z * z))));
	elseif (y <= 8.2e+51)
		tmp = hypot(z, x);
	else
		tmp = hypot(y, x);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[y, -4.9e+93], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], If[LessEqual[y, 1.4e-28], N[Sqrt[z ^ 2 + x ^ 2], $MachinePrecision], If[LessEqual[y, 6.4e+15], N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[y, 8.2e+51], N[Sqrt[z ^ 2 + x ^ 2], $MachinePrecision], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision]]]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
\mathbf{if}\;y \leq -4.9 \cdot 10^{+93}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\

\mathbf{elif}\;y \leq 1.4 \cdot 10^{-28}:\\
\;\;\;\;\mathsf{hypot}\left(z, x\right)\\

\mathbf{elif}\;y \leq 6.4 \cdot 10^{+15}:\\
\;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\

\mathbf{elif}\;y \leq 8.2 \cdot 10^{+51}:\\
\;\;\;\;\mathsf{hypot}\left(z, x\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original37.5
Target0.0
Herbie9.1
\[\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \]

Derivation

  1. Split input into 3 regimes
  2. if y < -4.89999999999999969e93 or 8.20000000000000021e51 < y

    1. Initial program 51.0

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around 0 51.9

      \[\leadsto \color{blue}{\sqrt{{y}^{2} + {x}^{2}}} \]
    3. Simplified51.9

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}} \]
      Proof
    4. Applied egg-rr10.2

      \[\leadsto \color{blue}{\sqrt{\mathsf{hypot}\left(x, y\right)} \cdot \sqrt{\mathsf{hypot}\left(x, y\right)}} \]
    5. Simplified9.8

      \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
      Proof

    if -4.89999999999999969e93 < y < 1.3999999999999999e-28 or 6.4e15 < y < 8.20000000000000021e51

    1. Initial program 28.9

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in y around 0 35.8

      \[\leadsto \sqrt{x \cdot x + \color{blue}{{z}^{2}}} \]
    3. Simplified35.8

      \[\leadsto \sqrt{x \cdot x + \color{blue}{z \cdot z}} \]
      Proof
    4. Applied egg-rr7.8

      \[\leadsto \color{blue}{\sqrt{\mathsf{hypot}\left(x, z\right)} \cdot \sqrt{\mathsf{hypot}\left(x, z\right)}} \]
    5. Simplified7.4

      \[\leadsto \color{blue}{\mathsf{hypot}\left(z, x\right)} \]
      Proof

    if 1.3999999999999999e-28 < y < 6.4e15

    1. Initial program 28.9

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  3. Recombined 3 regimes into one program.

Alternatives

Alternative 1
Error8.8
Cost7308
\[\begin{array}{l} \mathbf{if}\;y \cdot y \leq 0.0002:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \mathbf{elif}\;y \cdot y \leq 5 \cdot 10^{+31}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;y \cdot y \leq 10^{+182}:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \end{array} \]
Alternative 2
Error13.6
Cost7056
\[\begin{array}{l} \mathbf{if}\;z \leq -1.55 \cdot 10^{+172}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \leq -4.8 \cdot 10^{+72}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \leq -4.8 \cdot 10^{+15}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \leq 1.15 \cdot 10^{+139}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 3
Error37.5
Cost1580
\[\begin{array}{l} \mathbf{if}\;z \leq -1.28 \cdot 10^{-7}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \leq -2.5 \cdot 10^{-81}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -7.5 \cdot 10^{-108}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \leq -3.9 \cdot 10^{-203}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -7.2 \cdot 10^{-273}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \leq 2.15 \cdot 10^{-203}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 6.2 \cdot 10^{-137}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 2.9 \cdot 10^{-46}:\\ \;\;\;\;-y\\ \mathbf{elif}\;z \leq 6 \cdot 10^{-35}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 1.6 \cdot 10^{+36}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 2.8 \cdot 10^{+52}:\\ \;\;\;\;-y\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 4
Error36.6
Cost592
\[\begin{array}{l} \mathbf{if}\;x \leq -0.00027:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -2.5 \cdot 10^{-194}:\\ \;\;\;\;-y\\ \mathbf{elif}\;x \leq 5.8 \cdot 10^{-180}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 4.2 \cdot 10^{-80}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 5
Error36.5
Cost460
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-35}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq 3.9 \cdot 10^{-180}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 4.2 \cdot 10^{-74}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 6
Error44.2
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq 2.3 \cdot 10^{-180}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 2.4 \cdot 10^{-75}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 7
Error44.2
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq 3.45 \cdot 10^{-54}:\\ \;\;\;\;y\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 8
Error52.0
Cost64
\[x \]

Error

Reproduce

herbie shell --seed 2023010 
(FPCore (x y z)
  :name "bug366 (missed optimization)"
  :precision binary64

  :herbie-target
  (hypot x (hypot y z))

  (sqrt (+ (* x x) (+ (* y y) (* z z)))))