?

Average Error: 38.4 → 12.4
Time: 3.6s
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 1.9 \cdot 10^{-32}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 7200000000000:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;z \leq 1.8 \cdot 10^{+38}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z)
 :precision binary64
 (if (<= z 1.9e-32)
   (- x)
   (if (<= z 7200000000000.0)
     (sqrt (+ (* x x) (+ (* y y) (* z z))))
     (if (<= z 1.8e+38) (- x) z))))
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 <= 1.9e-32) {
		tmp = -x;
	} else if (z <= 7200000000000.0) {
		tmp = sqrt(((x * x) + ((y * y) + (z * z))));
	} else if (z <= 1.8e+38) {
		tmp = -x;
	} else {
		tmp = z;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = sqrt(((x * x) + ((y * y) + (z * z))))
end function
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (z <= 1.9d-32) then
        tmp = -x
    else if (z <= 7200000000000.0d0) then
        tmp = sqrt(((x * x) + ((y * y) + (z * z))))
    else if (z <= 1.8d+38) then
        tmp = -x
    else
        tmp = z
    end if
    code = tmp
end function
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 <= 1.9e-32) {
		tmp = -x;
	} else if (z <= 7200000000000.0) {
		tmp = Math.sqrt(((x * x) + ((y * y) + (z * z))));
	} else if (z <= 1.8e+38) {
		tmp = -x;
	} else {
		tmp = z;
	}
	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 <= 1.9e-32:
		tmp = -x
	elif z <= 7200000000000.0:
		tmp = math.sqrt(((x * x) + ((y * y) + (z * z))))
	elif z <= 1.8e+38:
		tmp = -x
	else:
		tmp = z
	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 <= 1.9e-32)
		tmp = Float64(-x);
	elseif (z <= 7200000000000.0)
		tmp = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))));
	elseif (z <= 1.8e+38)
		tmp = Float64(-x);
	else
		tmp = z;
	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 <= 1.9e-32)
		tmp = -x;
	elseif (z <= 7200000000000.0)
		tmp = sqrt(((x * x) + ((y * y) + (z * z))));
	elseif (z <= 1.8e+38)
		tmp = -x;
	else
		tmp = z;
	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, 1.9e-32], (-x), If[LessEqual[z, 7200000000000.0], N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[z, 1.8e+38], (-x), z]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
\mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\
\;\;\;\;-x\\

\mathbf{elif}\;z \leq 7200000000000:\\
\;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\

\mathbf{elif}\;z \leq 1.8 \cdot 10^{+38}:\\
\;\;\;\;-x\\

\mathbf{else}:\\
\;\;\;\;z\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.4
Target0
Herbie12.4
\[\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \]

Derivation?

  1. Split input into 3 regimes
  2. if z < 1.90000000000000004e-32 or 7.2e12 < z < 1.79999999999999985e38

    1. Initial program 29.9

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

      \[\leadsto \color{blue}{-1 \cdot x} \]
    3. Simplified9.4

      \[\leadsto \color{blue}{-x} \]
      Proof

      [Start]9.4

      \[ -1 \cdot x \]

      rational_best_oopsla_all_46_json_45_simplify-74 [=>]9.4

      \[ \color{blue}{x \cdot -1} \]

      rational_best_oopsla_all_46_json_45_simplify-92 [=>]9.4

      \[ \color{blue}{-x} \]

    if 1.90000000000000004e-32 < z < 7.2e12

    1. Initial program 19.8

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

    if 1.79999999999999985e38 < z

    1. Initial program 47.2

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

      \[\leadsto \color{blue}{z} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification12.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 7200000000000:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;z \leq 1.8 \cdot 10^{+38}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternatives

Alternative 1
Error13.2
Cost524
\[\begin{array}{l} \mathbf{if}\;z \leq 3.15 \cdot 10^{-24}:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 44000000000:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 1.7 \cdot 10^{+38}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 2
Error31.4
Cost64
\[z \]

Error

Reproduce?

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

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

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