?

Average Error: 38.5 → 8.8
Time: 28.4s
Precision: binary64
Cost: 7048

?

\[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
\[\begin{array}{l} \mathbf{if}\;z \cdot z \leq 2 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \cdot z \leq 5 \cdot 10^{+247}:\\ \;\;\;\;\mathsf{hypot}\left(z, y\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z)
 :precision binary64
 (if (<= (* z z) 2e+14)
   (hypot y x)
   (if (<= (* z z) 5e+247) (hypot z y) (hypot z 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 ((z * z) <= 2e+14) {
		tmp = hypot(y, x);
	} else if ((z * z) <= 5e+247) {
		tmp = hypot(z, y);
	} else {
		tmp = hypot(z, 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 ((z * z) <= 2e+14) {
		tmp = Math.hypot(y, x);
	} else if ((z * z) <= 5e+247) {
		tmp = Math.hypot(z, y);
	} else {
		tmp = Math.hypot(z, 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 (z * z) <= 2e+14:
		tmp = math.hypot(y, x)
	elif (z * z) <= 5e+247:
		tmp = math.hypot(z, y)
	else:
		tmp = math.hypot(z, 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 (Float64(z * z) <= 2e+14)
		tmp = hypot(y, x);
	elseif (Float64(z * z) <= 5e+247)
		tmp = hypot(z, y);
	else
		tmp = hypot(z, 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 ((z * z) <= 2e+14)
		tmp = hypot(y, x);
	elseif ((z * z) <= 5e+247)
		tmp = hypot(z, y);
	else
		tmp = hypot(z, 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[N[(z * z), $MachinePrecision], 2e+14], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], If[LessEqual[N[(z * z), $MachinePrecision], 5e+247], N[Sqrt[z ^ 2 + y ^ 2], $MachinePrecision], N[Sqrt[z ^ 2 + x ^ 2], $MachinePrecision]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 2 \cdot 10^{+14}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\

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

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.5
Target0
Herbie8.8
\[\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \]

Derivation?

  1. Split input into 3 regimes
  2. if (*.f64 z z) < 2e14

    1. Initial program 30.5

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

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

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

    if 2e14 < (*.f64 z z) < 5.00000000000000023e247

    1. Initial program 29.9

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Simplified29.9

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, {y}^{2}\right)\right)}} \]
      Proof
    3. Taylor expanded in x around 0 34.0

      \[\leadsto \color{blue}{\sqrt{{z}^{2} + {y}^{2}}} \]
    4. Simplified19.0

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

    if 5.00000000000000023e247 < (*.f64 z z)

    1. Initial program 57.8

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Simplified57.8

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, {y}^{2}\right)\right)}} \]
      Proof
    3. Taylor expanded in y around 0 57.9

      \[\leadsto \color{blue}{\sqrt{{z}^{2} + {x}^{2}}} \]
    4. Simplified8.2

      \[\leadsto \color{blue}{\mathsf{hypot}\left(z, x\right)} \]
      Proof
  3. Recombined 3 regimes into one program.

Alternatives

Alternative 1
Error12.1
Cost6792
\[\begin{array}{l} \mathbf{if}\;z \leq -1.22 \cdot 10^{+129}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \leq 4.4 \cdot 10^{+81}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 2
Error8.7
Cost6792
\[\begin{array}{l} \mathbf{if}\;z \leq -3.4 \cdot 10^{+93}:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \mathbf{elif}\;z \leq 1750:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(z, x\right)\\ \end{array} \]
Alternative 3
Error36.8
Cost920
\[\begin{array}{l} \mathbf{if}\;z \leq -3.4 \cdot 10^{-17}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \leq -9 \cdot 10^{-205}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \leq -3.6 \cdot 10^{-252}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -1 \cdot 10^{-288}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \leq 3.2 \cdot 10^{-164}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 5.5 \cdot 10^{-33}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 4
Error36.8
Cost920
\[\begin{array}{l} \mathbf{if}\;z \leq -1.55 \cdot 10^{-17}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \leq -1.2 \cdot 10^{-205}:\\ \;\;\;\;\frac{\frac{z \cdot z}{y}}{2} + y\\ \mathbf{elif}\;z \leq -4 \cdot 10^{-252}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq -5.6 \cdot 10^{-289}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-162}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 1.4 \cdot 10^{-32}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 5
Error36.7
Cost724
\[\begin{array}{l} \mathbf{if}\;x \leq -2.2 \cdot 10^{-16}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -3.4 \cdot 10^{-209}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 1.32 \cdot 10^{-295}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq 9.5 \cdot 10^{-230}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 2.5 \cdot 10^{-34}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 6
Error44.3
Cost460
\[\begin{array}{l} \mathbf{if}\;z \leq -3 \cdot 10^{+74}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -8.5 \cdot 10^{-289}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \leq 1.75 \cdot 10^{-46}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 7
Error44.6
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq 5.3 \cdot 10^{-37}:\\ \;\;\;\;y\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 8
Error52.2
Cost64
\[x \]

Error

Reproduce?

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

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

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