Average Error: 38.8 → 10.9
Time: 3.1s
Precision: binary64
Cost: 7368
\[ \begin{array}{c}[x, y, z] = \mathsf{sort}([x, y, z])\\ \end{array} \]
\[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
\[\begin{array}{l} \mathbf{if}\;z \leq 6.5 \cdot 10^{-42}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \leq 1.56 \cdot 10^{+97}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(z, y\right)\\ \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z)
 :precision binary64
 (if (<= z 6.5e-42)
   (hypot y x)
   (if (<= z 1.56e+97) (sqrt (+ (* x x) (+ (* y y) (* z z)))) (hypot z y))))
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 (z <= 6.5e-42) {
		tmp = hypot(y, x);
	} else if (z <= 1.56e+97) {
		tmp = sqrt(((x * x) + ((y * y) + (z * z))));
	} else {
		tmp = hypot(z, y);
	}
	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 (z <= 6.5e-42) {
		tmp = Math.hypot(y, x);
	} else if (z <= 1.56e+97) {
		tmp = Math.sqrt(((x * x) + ((y * y) + (z * z))));
	} else {
		tmp = Math.hypot(z, y);
	}
	return tmp;
}
def code(x, y, z):
	return math.sqrt(((x * x) + ((y * y) + (z * z))))
def code(x, y, z):
	tmp = 0
	if z <= 6.5e-42:
		tmp = math.hypot(y, x)
	elif z <= 1.56e+97:
		tmp = math.sqrt(((x * x) + ((y * y) + (z * z))))
	else:
		tmp = math.hypot(z, y)
	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 (z <= 6.5e-42)
		tmp = hypot(y, x);
	elseif (z <= 1.56e+97)
		tmp = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))));
	else
		tmp = hypot(z, y);
	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 (z <= 6.5e-42)
		tmp = hypot(y, x);
	elseif (z <= 1.56e+97)
		tmp = sqrt(((x * x) + ((y * y) + (z * z))));
	else
		tmp = hypot(z, y);
	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[z, 6.5e-42], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], If[LessEqual[z, 1.56e+97], N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[z ^ 2 + y ^ 2], $MachinePrecision]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
\mathbf{if}\;z \leq 6.5 \cdot 10^{-42}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\

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

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


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.8
Target0.0
Herbie10.9
\[\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \]

Derivation

  1. Split input into 3 regimes
  2. if z < 6.4999999999999998e-42

    1. Initial program 31.6

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

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

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

      [Start]37.1

      \[ \sqrt{{y}^{2} + {x}^{2}} \]

      unpow2 [=>]37.1

      \[ \sqrt{\color{blue}{y \cdot y} + {x}^{2}} \]

      unpow2 [=>]37.1

      \[ \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]

      hypot-def [=>]6.7

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

    if 6.4999999999999998e-42 < z < 1.56e97

    1. Initial program 19.7

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]

    if 1.56e97 < z

    1. Initial program 53.0

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

      \[\leadsto \color{blue}{\sqrt{{z}^{2} + {y}^{2}}} \]
    3. Simplified10.6

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

      [Start]53.4

      \[ \sqrt{{z}^{2} + {y}^{2}} \]

      unpow2 [=>]53.4

      \[ \sqrt{\color{blue}{z \cdot z} + {y}^{2}} \]

      unpow2 [=>]53.4

      \[ \sqrt{z \cdot z + \color{blue}{y \cdot y}} \]

      hypot-def [=>]10.6

      \[ \color{blue}{\mathsf{hypot}\left(z, y\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification10.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 6.5 \cdot 10^{-42}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \leq 1.56 \cdot 10^{+97}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(z, y\right)\\ \end{array} \]

Alternatives

Alternative 1
Error12.8
Cost6660
\[\begin{array}{l} \mathbf{if}\;x \leq -2.6 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 2
Error12.5
Cost6660
\[\begin{array}{l} \mathbf{if}\;x \leq -1.35 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(z, y\right)\\ \end{array} \]
Alternative 3
Error13.0
Cost260
\[\begin{array}{l} \mathbf{if}\;x \leq -7.6 \cdot 10^{-9}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 4
Error31.2
Cost64
\[z \]

Error

Reproduce

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

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

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