?

Average Error: 38.1 → 12.4
Time: 2.3s
Precision: binary64
Cost: 8924

?

\[ \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 \cdot x \leq 5 \cdot 10^{-99}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+22}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 10^{+32}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+122}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{+158}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \cdot x \leq 10^{+211}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-x\\ \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 x) 5e-99)
     z
     (if (<= (* x x) 5e+22)
       t_0
       (if (<= (* x x) 1e+32)
         z
         (if (<= (* x x) 5e+115)
           t_0
           (if (<= (* x x) 5e+122)
             z
             (if (<= (* x x) 2e+158)
               (- x)
               (if (<= (* x x) 1e+211) t_0 (- 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 t_0 = sqrt(((x * x) + ((y * y) + (z * z))));
	double tmp;
	if ((x * x) <= 5e-99) {
		tmp = z;
	} else if ((x * x) <= 5e+22) {
		tmp = t_0;
	} else if ((x * x) <= 1e+32) {
		tmp = z;
	} else if ((x * x) <= 5e+115) {
		tmp = t_0;
	} else if ((x * x) <= 5e+122) {
		tmp = z;
	} else if ((x * x) <= 2e+158) {
		tmp = -x;
	} else if ((x * x) <= 1e+211) {
		tmp = t_0;
	} else {
		tmp = -x;
	}
	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 * x) <= 5d-99) then
        tmp = z
    else if ((x * x) <= 5d+22) then
        tmp = t_0
    else if ((x * x) <= 1d+32) then
        tmp = z
    else if ((x * x) <= 5d+115) then
        tmp = t_0
    else if ((x * x) <= 5d+122) then
        tmp = z
    else if ((x * x) <= 2d+158) then
        tmp = -x
    else if ((x * x) <= 1d+211) then
        tmp = t_0
    else
        tmp = -x
    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 * x) <= 5e-99) {
		tmp = z;
	} else if ((x * x) <= 5e+22) {
		tmp = t_0;
	} else if ((x * x) <= 1e+32) {
		tmp = z;
	} else if ((x * x) <= 5e+115) {
		tmp = t_0;
	} else if ((x * x) <= 5e+122) {
		tmp = z;
	} else if ((x * x) <= 2e+158) {
		tmp = -x;
	} else if ((x * x) <= 1e+211) {
		tmp = t_0;
	} else {
		tmp = -x;
	}
	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 * x) <= 5e-99:
		tmp = z
	elif (x * x) <= 5e+22:
		tmp = t_0
	elif (x * x) <= 1e+32:
		tmp = z
	elif (x * x) <= 5e+115:
		tmp = t_0
	elif (x * x) <= 5e+122:
		tmp = z
	elif (x * x) <= 2e+158:
		tmp = -x
	elif (x * x) <= 1e+211:
		tmp = t_0
	else:
		tmp = -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)
	t_0 = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))))
	tmp = 0.0
	if (Float64(x * x) <= 5e-99)
		tmp = z;
	elseif (Float64(x * x) <= 5e+22)
		tmp = t_0;
	elseif (Float64(x * x) <= 1e+32)
		tmp = z;
	elseif (Float64(x * x) <= 5e+115)
		tmp = t_0;
	elseif (Float64(x * x) <= 5e+122)
		tmp = z;
	elseif (Float64(x * x) <= 2e+158)
		tmp = Float64(-x);
	elseif (Float64(x * x) <= 1e+211)
		tmp = t_0;
	else
		tmp = Float64(-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)
	t_0 = sqrt(((x * x) + ((y * y) + (z * z))));
	tmp = 0.0;
	if ((x * x) <= 5e-99)
		tmp = z;
	elseif ((x * x) <= 5e+22)
		tmp = t_0;
	elseif ((x * x) <= 1e+32)
		tmp = z;
	elseif ((x * x) <= 5e+115)
		tmp = t_0;
	elseif ((x * x) <= 5e+122)
		tmp = z;
	elseif ((x * x) <= 2e+158)
		tmp = -x;
	elseif ((x * x) <= 1e+211)
		tmp = t_0;
	else
		tmp = -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_] := 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[N[(x * x), $MachinePrecision], 5e-99], z, If[LessEqual[N[(x * x), $MachinePrecision], 5e+22], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 1e+32], z, If[LessEqual[N[(x * x), $MachinePrecision], 5e+115], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 5e+122], z, If[LessEqual[N[(x * x), $MachinePrecision], 2e+158], (-x), If[LessEqual[N[(x * x), $MachinePrecision], 1e+211], t$95$0, (-x)]]]]]]]]
\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 \cdot x \leq 5 \cdot 10^{-99}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+22}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot x \leq 10^{+32}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+115}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+122}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{+158}:\\
\;\;\;\;-x\\

\mathbf{elif}\;x \cdot x \leq 10^{+211}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;-x\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

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

Derivation?

  1. Split input into 3 regimes
  2. if (*.f64 x x) < 4.99999999999999969e-99 or 4.9999999999999996e22 < (*.f64 x x) < 1.00000000000000005e32 or 5.00000000000000008e115 < (*.f64 x x) < 4.99999999999999989e122

    1. Initial program 28.7

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

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

      [Start]28.7

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

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

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

      \[\leadsto \color{blue}{z} \]

    if 4.99999999999999969e-99 < (*.f64 x x) < 4.9999999999999996e22 or 1.00000000000000005e32 < (*.f64 x x) < 5.00000000000000008e115 or 1.99999999999999991e158 < (*.f64 x x) < 9.9999999999999996e210

    1. Initial program 21.3

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

    if 4.99999999999999989e122 < (*.f64 x x) < 1.99999999999999991e158 or 9.9999999999999996e210 < (*.f64 x x)

    1. Initial program 52.5

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

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

      [Start]52.5

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

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

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

      \[\leadsto \color{blue}{-1 \cdot x} \]
    4. Simplified11.8

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

      [Start]11.8

      \[ -1 \cdot x \]

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-99}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+22}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;x \cdot x \leq 10^{+32}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+115}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+122}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{+158}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \cdot x \leq 10^{+211}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]

Alternatives

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

Error

Reproduce?

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

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

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