?

Average Error: 38.4 → 11.6
Time: 20.3s
Precision: binary64
Cost: 26956

?

\[ \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 := z \cdot \sqrt{0.3333333333333333}\\ t_1 := \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq -63000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -1.05 \cdot 10^{-7}:\\ \;\;\;\;t_0 + 0.5 \cdot \left(\left({y}^{2} + {x}^{2}\right) \cdot \frac{\sqrt{0.3333333333333333}}{z}\right)\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{-65}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \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 (* z (sqrt 0.3333333333333333)))
        (t_1 (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0))))
   (if (<= x -7.2e+146)
     (* (sqrt 0.3333333333333333) (- x))
     (if (<= x -63000000.0)
       t_1
       (if (<= x -1.05e-7)
         (+
          t_0
          (*
           0.5
           (* (+ (pow y 2.0) (pow x 2.0)) (/ (sqrt 0.3333333333333333) z))))
         (if (<= x -3.8e-65) t_1 t_0))))))
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 = z * sqrt(0.3333333333333333);
	double t_1 = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	double tmp;
	if (x <= -7.2e+146) {
		tmp = sqrt(0.3333333333333333) * -x;
	} else if (x <= -63000000.0) {
		tmp = t_1;
	} else if (x <= -1.05e-7) {
		tmp = t_0 + (0.5 * ((pow(y, 2.0) + pow(x, 2.0)) * (sqrt(0.3333333333333333) / z)));
	} else if (x <= -3.8e-65) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	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) :: t_1
    real(8) :: tmp
    t_0 = z * sqrt(0.3333333333333333d0)
    t_1 = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
    if (x <= (-7.2d+146)) then
        tmp = sqrt(0.3333333333333333d0) * -x
    else if (x <= (-63000000.0d0)) then
        tmp = t_1
    else if (x <= (-1.05d-7)) then
        tmp = t_0 + (0.5d0 * (((y ** 2.0d0) + (x ** 2.0d0)) * (sqrt(0.3333333333333333d0) / z)))
    else if (x <= (-3.8d-65)) then
        tmp = t_1
    else
        tmp = t_0
    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 = z * Math.sqrt(0.3333333333333333);
	double t_1 = Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	double tmp;
	if (x <= -7.2e+146) {
		tmp = Math.sqrt(0.3333333333333333) * -x;
	} else if (x <= -63000000.0) {
		tmp = t_1;
	} else if (x <= -1.05e-7) {
		tmp = t_0 + (0.5 * ((Math.pow(y, 2.0) + Math.pow(x, 2.0)) * (Math.sqrt(0.3333333333333333) / z)));
	} else if (x <= -3.8e-65) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	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 = z * math.sqrt(0.3333333333333333)
	t_1 = math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
	tmp = 0
	if x <= -7.2e+146:
		tmp = math.sqrt(0.3333333333333333) * -x
	elif x <= -63000000.0:
		tmp = t_1
	elif x <= -1.05e-7:
		tmp = t_0 + (0.5 * ((math.pow(y, 2.0) + math.pow(x, 2.0)) * (math.sqrt(0.3333333333333333) / z)))
	elif x <= -3.8e-65:
		tmp = t_1
	else:
		tmp = t_0
	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(z * sqrt(0.3333333333333333))
	t_1 = sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0))
	tmp = 0.0
	if (x <= -7.2e+146)
		tmp = Float64(sqrt(0.3333333333333333) * Float64(-x));
	elseif (x <= -63000000.0)
		tmp = t_1;
	elseif (x <= -1.05e-7)
		tmp = Float64(t_0 + Float64(0.5 * Float64(Float64((y ^ 2.0) + (x ^ 2.0)) * Float64(sqrt(0.3333333333333333) / z))));
	elseif (x <= -3.8e-65)
		tmp = t_1;
	else
		tmp = t_0;
	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 = z * sqrt(0.3333333333333333);
	t_1 = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	tmp = 0.0;
	if (x <= -7.2e+146)
		tmp = sqrt(0.3333333333333333) * -x;
	elseif (x <= -63000000.0)
		tmp = t_1;
	elseif (x <= -1.05e-7)
		tmp = t_0 + (0.5 * (((y ^ 2.0) + (x ^ 2.0)) * (sqrt(0.3333333333333333) / z)));
	elseif (x <= -3.8e-65)
		tmp = t_1;
	else
		tmp = t_0;
	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[(z * N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = 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[x, -7.2e+146], N[(N[Sqrt[0.3333333333333333], $MachinePrecision] * (-x)), $MachinePrecision], If[LessEqual[x, -63000000.0], t$95$1, If[LessEqual[x, -1.05e-7], N[(t$95$0 + N[(0.5 * N[(N[(N[Power[y, 2.0], $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] * N[(N[Sqrt[0.3333333333333333], $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, -3.8e-65], t$95$1, t$95$0]]]]]]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
t_0 := z \cdot \sqrt{0.3333333333333333}\\
t_1 := \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\
\mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\

\mathbf{elif}\;x \leq -63000000:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq -1.05 \cdot 10^{-7}:\\
\;\;\;\;t_0 + 0.5 \cdot \left(\left({y}^{2} + {x}^{2}\right) \cdot \frac{\sqrt{0.3333333333333333}}{z}\right)\\

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

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.4
Target19.8
Herbie11.6
\[\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 x < -7.1999999999999997e146

    1. Initial program 62.1

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

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

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

      [Start]8.4

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

      rational.json-simplify-43 [=>]8.4

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

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

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

    if -7.1999999999999997e146 < x < -6.3e7 or -1.05e-7 < x < -3.8000000000000002e-65

    1. Initial program 20.0

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

    if -6.3e7 < x < -1.05e-7

    1. Initial program 21.0

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

      \[\leadsto \color{blue}{0.5 \cdot \frac{\left({y}^{2} + {x}^{2}\right) \cdot \sqrt{0.3333333333333333}}{z} + z \cdot \sqrt{0.3333333333333333}} \]
    3. Simplified28.1

      \[\leadsto \color{blue}{z \cdot \sqrt{0.3333333333333333} + 0.5 \cdot \left(\left({y}^{2} + {x}^{2}\right) \cdot \frac{\sqrt{0.3333333333333333}}{z}\right)} \]
      Proof

      [Start]28.1

      \[ 0.5 \cdot \frac{\left({y}^{2} + {x}^{2}\right) \cdot \sqrt{0.3333333333333333}}{z} + z \cdot \sqrt{0.3333333333333333} \]

      rational.json-simplify-1 [=>]28.1

      \[ \color{blue}{z \cdot \sqrt{0.3333333333333333} + 0.5 \cdot \frac{\left({y}^{2} + {x}^{2}\right) \cdot \sqrt{0.3333333333333333}}{z}} \]

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

      \[ z \cdot \sqrt{0.3333333333333333} + 0.5 \cdot \frac{\color{blue}{\sqrt{0.3333333333333333} \cdot \left({y}^{2} + {x}^{2}\right)}}{z} \]

      rational.json-simplify-49 [=>]28.1

      \[ z \cdot \sqrt{0.3333333333333333} + 0.5 \cdot \color{blue}{\left(\left({y}^{2} + {x}^{2}\right) \cdot \frac{\sqrt{0.3333333333333333}}{z}\right)} \]

    if -3.8000000000000002e-65 < x

    1. Initial program 31.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq -63000000:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{elif}\;x \leq -1.05 \cdot 10^{-7}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333} + 0.5 \cdot \left(\left({y}^{2} + {x}^{2}\right) \cdot \frac{\sqrt{0.3333333333333333}}{z}\right)\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{-65}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \end{array} \]

Alternatives

Alternative 1
Error11.7
Cost7760
\[\begin{array}{l} t_0 := z \cdot \sqrt{0.3333333333333333}\\ t_1 := \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{if}\;x \leq -3.1 \cdot 10^{+147}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq -960000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-7}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -3 \cdot 10^{-71}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error13.2
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq -3600000000:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \end{array} \]
Alternative 3
Error31.1
Cost6592
\[z \cdot \sqrt{0.3333333333333333} \]

Error

Reproduce?

herbie shell --seed 2023074 
(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)))