?

Average Error: 38.5 → 12.7
Time: 11.6s
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} \mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\ \;\;\;\;\frac{x}{\sqrt{0.3333333333333333}} \cdot -0.3333333333333333\\ \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}:\\ \;\;\;\;0.3333333333333333 \cdot \frac{z}{\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
 (if (<= z 1.9e-32)
   (* (/ x (sqrt 0.3333333333333333)) -0.3333333333333333)
   (if (<= z 7200000000000.0)
     (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0))
     (if (<= z 1.8e+38)
       (* (sqrt 0.3333333333333333) (- x))
       (* 0.3333333333333333 (/ 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 tmp;
	if (z <= 1.9e-32) {
		tmp = (x / sqrt(0.3333333333333333)) * -0.3333333333333333;
	} else if (z <= 7200000000000.0) {
		tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	} else if (z <= 1.8e+38) {
		tmp = sqrt(0.3333333333333333) * -x;
	} else {
		tmp = 0.3333333333333333 * (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) :: tmp
    if (z <= 1.9d-32) then
        tmp = (x / sqrt(0.3333333333333333d0)) * (-0.3333333333333333d0)
    else if (z <= 7200000000000.0d0) then
        tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
    else if (z <= 1.8d+38) then
        tmp = sqrt(0.3333333333333333d0) * -x
    else
        tmp = 0.3333333333333333d0 * (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 tmp;
	if (z <= 1.9e-32) {
		tmp = (x / Math.sqrt(0.3333333333333333)) * -0.3333333333333333;
	} else if (z <= 7200000000000.0) {
		tmp = Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	} else if (z <= 1.8e+38) {
		tmp = Math.sqrt(0.3333333333333333) * -x;
	} else {
		tmp = 0.3333333333333333 * (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):
	tmp = 0
	if z <= 1.9e-32:
		tmp = (x / math.sqrt(0.3333333333333333)) * -0.3333333333333333
	elif z <= 7200000000000.0:
		tmp = math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
	elif z <= 1.8e+38:
		tmp = math.sqrt(0.3333333333333333) * -x
	else:
		tmp = 0.3333333333333333 * (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)
	tmp = 0.0
	if (z <= 1.9e-32)
		tmp = Float64(Float64(x / sqrt(0.3333333333333333)) * -0.3333333333333333);
	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 = Float64(sqrt(0.3333333333333333) * Float64(-x));
	else
		tmp = Float64(0.3333333333333333 * 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)
	tmp = 0.0;
	if (z <= 1.9e-32)
		tmp = (x / sqrt(0.3333333333333333)) * -0.3333333333333333;
	elseif (z <= 7200000000000.0)
		tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	elseif (z <= 1.8e+38)
		tmp = sqrt(0.3333333333333333) * -x;
	else
		tmp = 0.3333333333333333 * (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_] := If[LessEqual[z, 1.9e-32], N[(N[(x / N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision] * -0.3333333333333333), $MachinePrecision], 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], N[(N[Sqrt[0.3333333333333333], $MachinePrecision] * (-x)), $MachinePrecision], N[(0.3333333333333333 * N[(z / N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\
\;\;\;\;\frac{x}{\sqrt{0.3333333333333333}} \cdot -0.3333333333333333\\

\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}:\\
\;\;\;\;0.3333333333333333 \cdot \frac{z}{\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 4 regimes
  2. if z < 1.90000000000000004e-32

    1. Initial program 30.9

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
    2. Applied egg-rr47.0

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

      \[\leadsto \color{blue}{-0.3333333333333333 \cdot \frac{x}{\sqrt{0.3333333333333333}}} \]
    4. Simplified7.8

      \[\leadsto \color{blue}{\frac{x}{\sqrt{0.3333333333333333}} \cdot -0.3333333333333333} \]
      Proof

      [Start]7.8

      \[ -0.3333333333333333 \cdot \frac{x}{\sqrt{0.3333333333333333}} \]

      rational_best.json-simplify-2 [=>]7.8

      \[ \color{blue}{\frac{x}{\sqrt{0.3333333333333333}} \cdot -0.3333333333333333} \]

    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 7.2e12 < z < 1.79999999999999985e38

    1. Initial program 20.5

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

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

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

      [Start]29.9

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

      rational_best.json-simplify-44 [=>]29.9

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

      rational_best.json-simplify-2 [=>]29.9

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

      rational_best.json-simplify-12 [=>]29.9

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

    if 1.79999999999999985e38 < z

    1. Initial program 47.2

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
    2. Applied egg-rr60.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 1.9 \cdot 10^{-32}:\\ \;\;\;\;\frac{x}{\sqrt{0.3333333333333333}} \cdot -0.3333333333333333\\ \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}:\\ \;\;\;\;0.3333333333333333 \cdot \frac{z}{\sqrt{0.3333333333333333}}\\ \end{array} \]

Alternatives

Alternative 1
Error13.4
Cost7116
\[\begin{array}{l} t_0 := \sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{if}\;z \leq 1.1 \cdot 10^{-24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 58000000000:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;z \leq 1.6 \cdot 10^{+38}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.3333333333333333 \cdot \frac{z}{\sqrt{0.3333333333333333}}\\ \end{array} \]
Alternative 2
Error13.4
Cost7116
\[\begin{array}{l} t_0 := \frac{x}{\sqrt{0.3333333333333333}} \cdot -0.3333333333333333\\ \mathbf{if}\;z \leq 3.15 \cdot 10^{-24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 44000000000:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;z \leq 1.7 \cdot 10^{+38}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.3333333333333333 \cdot \frac{z}{\sqrt{0.3333333333333333}}\\ \end{array} \]
Alternative 3
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 1.7 \cdot 10^{-24}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 135000000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.8 \cdot 10^{+38}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
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)))