?

Average Error: 38.3 → 11.3
Time: 2.6s
Precision: binary64
Cost: 7632

?

\[ \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} t_0 := \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -132000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -8.6 \cdot 10^{-8}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{-65}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (sqrt (+ (* x x) (+ (* y y) (* z z))))))
   (if (<= x -7.2e+146)
     (- x)
     (if (<= x -132000000.0)
       t_0
       (if (<= x -8.6e-8) z (if (<= x -3.8e-65) t_0 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 t_0 = sqrt(((x * x) + ((y * y) + (z * z))));
	double tmp;
	if (x <= -7.2e+146) {
		tmp = -x;
	} else if (x <= -132000000.0) {
		tmp = t_0;
	} else if (x <= -8.6e-8) {
		tmp = z;
	} else if (x <= -3.8e-65) {
		tmp = t_0;
	} 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) :: t_0
    real(8) :: tmp
    t_0 = sqrt(((x * x) + ((y * y) + (z * z))))
    if (x <= (-7.2d+146)) then
        tmp = -x
    else if (x <= (-132000000.0d0)) then
        tmp = t_0
    else if (x <= (-8.6d-8)) then
        tmp = z
    else if (x <= (-3.8d-65)) then
        tmp = t_0
    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 t_0 = Math.sqrt(((x * x) + ((y * y) + (z * z))));
	double tmp;
	if (x <= -7.2e+146) {
		tmp = -x;
	} else if (x <= -132000000.0) {
		tmp = t_0;
	} else if (x <= -8.6e-8) {
		tmp = z;
	} else if (x <= -3.8e-65) {
		tmp = t_0;
	} else {
		tmp = z;
	}
	return tmp;
}
def code(x, y, z):
	return math.sqrt(((x * x) + ((y * y) + (z * z))))
def code(x, y, z):
	t_0 = math.sqrt(((x * x) + ((y * y) + (z * z))))
	tmp = 0
	if x <= -7.2e+146:
		tmp = -x
	elif x <= -132000000.0:
		tmp = t_0
	elif x <= -8.6e-8:
		tmp = z
	elif x <= -3.8e-65:
		tmp = t_0
	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)
	t_0 = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))))
	tmp = 0.0
	if (x <= -7.2e+146)
		tmp = Float64(-x);
	elseif (x <= -132000000.0)
		tmp = t_0;
	elseif (x <= -8.6e-8)
		tmp = z;
	elseif (x <= -3.8e-65)
		tmp = t_0;
	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)
	t_0 = sqrt(((x * x) + ((y * y) + (z * z))));
	tmp = 0.0;
	if (x <= -7.2e+146)
		tmp = -x;
	elseif (x <= -132000000.0)
		tmp = t_0;
	elseif (x <= -8.6e-8)
		tmp = z;
	elseif (x <= -3.8e-65)
		tmp = t_0;
	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_] := Block[{t$95$0 = N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[x, -7.2e+146], (-x), If[LessEqual[x, -132000000.0], t$95$0, If[LessEqual[x, -8.6e-8], z, If[LessEqual[x, -3.8e-65], t$95$0, z]]]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
t_0 := \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\
\mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\
\;\;\;\;-x\\

\mathbf{elif}\;x \leq -132000000:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -8.6 \cdot 10^{-8}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \leq -3.8 \cdot 10^{-65}:\\
\;\;\;\;t_0\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.3
Target0.0
Herbie11.3
\[\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \]

Derivation?

  1. Split input into 3 regimes
  2. if x < -7.1999999999999997e146

    1. Initial program 62.0

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

      \[\leadsto \color{blue}{\sqrt{z \cdot z + \left(x \cdot x + y \cdot y\right)}} \]
      Proof

      [Start]62.0

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

      rational.json-simplify-41 [<=]62.0

      \[ \sqrt{\color{blue}{z \cdot z + \left(x \cdot x + y \cdot y\right)}} \]
    3. Taylor expanded in x around -inf 8.0

      \[\leadsto \color{blue}{-1 \cdot x} \]
    4. Simplified8.0

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

      [Start]8.0

      \[ -1 \cdot x \]

      rational.json-simplify-2 [=>]8.0

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

      rational.json-simplify-9 [=>]8.0

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

    if -7.1999999999999997e146 < x < -1.32e8 or -8.6000000000000002e-8 < x < -3.8000000000000002e-65

    1. Initial program 19.9

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

    if -1.32e8 < x < -8.6000000000000002e-8 or -3.8000000000000002e-65 < x

    1. Initial program 31.1

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

      \[\leadsto \color{blue}{\sqrt{z \cdot z + \left(x \cdot x + y \cdot y\right)}} \]
      Proof

      [Start]31.1

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

      rational.json-simplify-41 [<=]31.1

      \[ \sqrt{\color{blue}{z \cdot z + \left(x \cdot x + y \cdot y\right)}} \]
    3. Taylor expanded in z around inf 7.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -132000000:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;x \leq -8.6 \cdot 10^{-8}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{-65}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternatives

Alternative 1
Error12.9
Cost260
\[\begin{array}{l} \mathbf{if}\;x \leq -15500000000:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 2
Error31.0
Cost64
\[z \]

Error

Reproduce?

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

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

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