?

Average Error: 38.5 → 12.7
Time: 4.1s
Precision: binary64
Cost: 7496

?

\[ \begin{array}{c}[x, y, z] = \mathsf{sort}([x, y, z])\\ \end{array} \]
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
\[\begin{array}{l} t_0 := \sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 7200000000000:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{elif}\;z \leq 1.8 \cdot 10^{+38}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \end{array} \]
(FPCore (x y z)
 :precision binary64
 (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* (sqrt 0.3333333333333333) (- x))))
   (if (<= z 1.9e-32)
     t_0
     (if (<= z 7200000000000.0)
       (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0))
       (if (<= z 1.8e+38) t_0 (* z (sqrt 0.3333333333333333)))))))
double code(double x, double y, double z) {
	return sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
double code(double x, double y, double z) {
	double t_0 = sqrt(0.3333333333333333) * -x;
	double tmp;
	if (z <= 1.9e-32) {
		tmp = t_0;
	} else if (z <= 7200000000000.0) {
		tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	} else if (z <= 1.8e+38) {
		tmp = t_0;
	} else {
		tmp = z * sqrt(0.3333333333333333);
	}
	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)) / 3.0d0))
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(0.3333333333333333d0) * -x
    if (z <= 1.9d-32) then
        tmp = t_0
    else if (z <= 7200000000000.0d0) then
        tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
    else if (z <= 1.8d+38) then
        tmp = t_0
    else
        tmp = z * sqrt(0.3333333333333333d0)
    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)) / 3.0));
}
public static double code(double x, double y, double z) {
	double t_0 = Math.sqrt(0.3333333333333333) * -x;
	double tmp;
	if (z <= 1.9e-32) {
		tmp = t_0;
	} else if (z <= 7200000000000.0) {
		tmp = Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	} else if (z <= 1.8e+38) {
		tmp = t_0;
	} else {
		tmp = z * Math.sqrt(0.3333333333333333);
	}
	return tmp;
}
def code(x, y, z):
	return math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
def code(x, y, z):
	t_0 = math.sqrt(0.3333333333333333) * -x
	tmp = 0
	if z <= 1.9e-32:
		tmp = t_0
	elif z <= 7200000000000.0:
		tmp = math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
	elif z <= 1.8e+38:
		tmp = t_0
	else:
		tmp = z * math.sqrt(0.3333333333333333)
	return tmp
function code(x, y, z)
	return sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0))
end
function code(x, y, z)
	t_0 = Float64(sqrt(0.3333333333333333) * Float64(-x))
	tmp = 0.0
	if (z <= 1.9e-32)
		tmp = t_0;
	elseif (z <= 7200000000000.0)
		tmp = sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0));
	elseif (z <= 1.8e+38)
		tmp = t_0;
	else
		tmp = Float64(z * sqrt(0.3333333333333333));
	end
	return tmp
end
function tmp = code(x, y, z)
	tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
end
function tmp_2 = code(x, y, z)
	t_0 = sqrt(0.3333333333333333) * -x;
	tmp = 0.0;
	if (z <= 1.9e-32)
		tmp = t_0;
	elseif (z <= 7200000000000.0)
		tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	elseif (z <= 1.8e+38)
		tmp = t_0;
	else
		tmp = z * sqrt(0.3333333333333333);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision]
code[x_, y_, z_] := Block[{t$95$0 = N[(N[Sqrt[0.3333333333333333], $MachinePrecision] * (-x)), $MachinePrecision]}, If[LessEqual[z, 1.9e-32], t$95$0, If[LessEqual[z, 7200000000000.0], N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision], If[LessEqual[z, 1.8e+38], t$95$0, N[(z * N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision]]]]]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
t_0 := \sqrt{0.3333333333333333} \cdot \left(-x\right)\\
\mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\
\;\;\;\;t_0\\

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

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

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{0.3333333333333333}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.5
Target20.2
Herbie12.7
\[\begin{array}{l} \mathbf{if}\;z < -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z < 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \end{array} \]

Derivation?

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

    1. Initial program 30.0

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

      \[\leadsto \color{blue}{-1 \cdot \left(\sqrt{0.3333333333333333} \cdot x\right)} \]
    3. Simplified9.7

      \[\leadsto \color{blue}{\sqrt{0.3333333333333333} \cdot \left(-x\right)} \]
      Proof

      [Start]9.7

      \[ -1 \cdot \left(\sqrt{0.3333333333333333} \cdot x\right) \]

      rational_best_oopsla_all_46_json_45_simplify-7 [=>]9.7

      \[ \color{blue}{\sqrt{0.3333333333333333} \cdot \left(-1 \cdot x\right)} \]

      rational_best_oopsla_all_46_json_45_simplify-74 [<=]9.7

      \[ \sqrt{0.3333333333333333} \cdot \color{blue}{\left(x \cdot -1\right)} \]

      rational_best_oopsla_all_46_json_45_simplify-94 [<=]9.7

      \[ \sqrt{0.3333333333333333} \cdot \color{blue}{\left(-x\right)} \]

    if 1.90000000000000004e-32 < z < 7.2e12

    1. Initial program 20.0

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

    if 1.79999999999999985e38 < z

    1. Initial program 47.2

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

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

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

Alternatives

Alternative 1
Error13.4
Cost7052
\[\begin{array}{l} t_0 := \sqrt{0.3333333333333333} \cdot \left(-x\right)\\ t_1 := z \cdot \sqrt{0.3333333333333333}\\ \mathbf{if}\;z \leq 2.8 \cdot 10^{-24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 44000000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.7 \cdot 10^{+38}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error31.5
Cost6592
\[z \cdot \sqrt{0.3333333333333333} \]

Error

Reproduce?

herbie shell --seed 2023090 
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"
  :precision binary64

  :herbie-target
  (if (< z -6.396479394109776e+136) (/ (- z) (sqrt 3.0)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3.0)) (* (sqrt 0.3333333333333333) z)))

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