?

Average Error: 38.0 → 11.5
Time: 6.5s
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 -1.3 \cdot 10^{+134}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -1.7 \cdot 10^{+16}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -2.8 \cdot 10^{-35}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-64}:\\ \;\;\;\;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 -1.3e+134)
     (- x)
     (if (<= x -1.7e+16)
       t_0
       (if (<= x -2.8e-35) z (if (<= x -1.9e-64) 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 <= -1.3e+134) {
		tmp = -x;
	} else if (x <= -1.7e+16) {
		tmp = t_0;
	} else if (x <= -2.8e-35) {
		tmp = z;
	} else if (x <= -1.9e-64) {
		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 <= (-1.3d+134)) then
        tmp = -x
    else if (x <= (-1.7d+16)) then
        tmp = t_0
    else if (x <= (-2.8d-35)) then
        tmp = z
    else if (x <= (-1.9d-64)) 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 <= -1.3e+134) {
		tmp = -x;
	} else if (x <= -1.7e+16) {
		tmp = t_0;
	} else if (x <= -2.8e-35) {
		tmp = z;
	} else if (x <= -1.9e-64) {
		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 <= -1.3e+134:
		tmp = -x
	elif x <= -1.7e+16:
		tmp = t_0
	elif x <= -2.8e-35:
		tmp = z
	elif x <= -1.9e-64:
		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 <= -1.3e+134)
		tmp = Float64(-x);
	elseif (x <= -1.7e+16)
		tmp = t_0;
	elseif (x <= -2.8e-35)
		tmp = z;
	elseif (x <= -1.9e-64)
		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 <= -1.3e+134)
		tmp = -x;
	elseif (x <= -1.7e+16)
		tmp = t_0;
	elseif (x <= -2.8e-35)
		tmp = z;
	elseif (x <= -1.9e-64)
		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, -1.3e+134], (-x), If[LessEqual[x, -1.7e+16], t$95$0, If[LessEqual[x, -2.8e-35], z, If[LessEqual[x, -1.9e-64], 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 -1.3 \cdot 10^{+134}:\\
\;\;\;\;-x\\

\mathbf{elif}\;x \leq -1.7 \cdot 10^{+16}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -2.8 \cdot 10^{-35}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \leq -1.9 \cdot 10^{-64}:\\
\;\;\;\;t_0\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

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

Derivation?

  1. Split input into 3 regimes
  2. if x < -1.3000000000000001e134

    1. Initial program 59.2

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

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

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

      [Start]9.6

      \[ -1 \cdot x \]

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

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

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

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

    if -1.3000000000000001e134 < x < -1.7e16 or -2.8e-35 < x < -1.9000000000000001e-64

    1. Initial program 18.0

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

    if -1.7e16 < x < -2.8e-35 or -1.9000000000000001e-64 < x

    1. Initial program 30.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.3 \cdot 10^{+134}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -1.7 \cdot 10^{+16}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{elif}\;x \leq -2.8 \cdot 10^{-35}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-64}:\\ \;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternatives

Alternative 1
Error13.7
Cost788
\[\begin{array}{l} \mathbf{if}\;x \leq -3.3 \cdot 10^{+89}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -5.1 \cdot 10^{+61}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -2.1 \cdot 10^{+16}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -1.95 \cdot 10^{-42}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq -2.65 \cdot 10^{-61}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Alternative 2
Error30.9
Cost64
\[z \]

Error

Reproduce?

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

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

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