Average Error: 37.6 → 25.4
Time: 21.9s
Precision: binary64
Cost: 13388
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}} \]
\[\begin{array}{l} \mathbf{if}\;y \leq -1.12 \cdot 10^{+133}:\\ \;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \leq 9 \cdot 10^{-248}:\\ \;\;\;\;\sqrt{\left(0.3333333333333333 \cdot \left(x \cdot x\right) + 0.3333333333333333 \cdot \left(z \cdot z\right)\right) + 0.3333333333333333 \cdot \left(y \cdot y\right)}\\ \mathbf{elif}\;y \leq 1.95 \cdot 10^{-109}:\\ \;\;\;\;\frac{\left|z\right|}{\sqrt{3}}\\ \mathbf{elif}\;y \leq 7.8 \cdot 10^{+121}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \end{array} \]
(FPCore (x y z)
 :precision binary64
 (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
(FPCore (x y z)
 :precision binary64
 (if (<= y -1.12e+133)
   (- (* y (sqrt 0.3333333333333333)))
   (if (<= y 9e-248)
     (sqrt
      (+
       (+ (* 0.3333333333333333 (* x x)) (* 0.3333333333333333 (* z z)))
       (* 0.3333333333333333 (* y y))))
     (if (<= y 1.95e-109)
       (/ (fabs z) (sqrt 3.0))
       (if (<= y 7.8e+121)
         (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0))
         (/ y (sqrt 3.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 tmp;
	if (y <= -1.12e+133) {
		tmp = -(y * sqrt(0.3333333333333333));
	} else if (y <= 9e-248) {
		tmp = sqrt((((0.3333333333333333 * (x * x)) + (0.3333333333333333 * (z * z))) + (0.3333333333333333 * (y * y))));
	} else if (y <= 1.95e-109) {
		tmp = fabs(z) / sqrt(3.0);
	} else if (y <= 7.8e+121) {
		tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	} else {
		tmp = y / sqrt(3.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) :: tmp
    if (y <= (-1.12d+133)) then
        tmp = -(y * sqrt(0.3333333333333333d0))
    else if (y <= 9d-248) then
        tmp = sqrt((((0.3333333333333333d0 * (x * x)) + (0.3333333333333333d0 * (z * z))) + (0.3333333333333333d0 * (y * y))))
    else if (y <= 1.95d-109) then
        tmp = abs(z) / sqrt(3.0d0)
    else if (y <= 7.8d+121) then
        tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
    else
        tmp = y / sqrt(3.0d0)
    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 (y <= -1.12e+133) {
		tmp = -(y * Math.sqrt(0.3333333333333333));
	} else if (y <= 9e-248) {
		tmp = Math.sqrt((((0.3333333333333333 * (x * x)) + (0.3333333333333333 * (z * z))) + (0.3333333333333333 * (y * y))));
	} else if (y <= 1.95e-109) {
		tmp = Math.abs(z) / Math.sqrt(3.0);
	} else if (y <= 7.8e+121) {
		tmp = Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	} else {
		tmp = y / Math.sqrt(3.0);
	}
	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 y <= -1.12e+133:
		tmp = -(y * math.sqrt(0.3333333333333333))
	elif y <= 9e-248:
		tmp = math.sqrt((((0.3333333333333333 * (x * x)) + (0.3333333333333333 * (z * z))) + (0.3333333333333333 * (y * y))))
	elif y <= 1.95e-109:
		tmp = math.fabs(z) / math.sqrt(3.0)
	elif y <= 7.8e+121:
		tmp = math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
	else:
		tmp = y / math.sqrt(3.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)
	tmp = 0.0
	if (y <= -1.12e+133)
		tmp = Float64(-Float64(y * sqrt(0.3333333333333333)));
	elseif (y <= 9e-248)
		tmp = sqrt(Float64(Float64(Float64(0.3333333333333333 * Float64(x * x)) + Float64(0.3333333333333333 * Float64(z * z))) + Float64(0.3333333333333333 * Float64(y * y))));
	elseif (y <= 1.95e-109)
		tmp = Float64(abs(z) / sqrt(3.0));
	elseif (y <= 7.8e+121)
		tmp = sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0));
	else
		tmp = Float64(y / sqrt(3.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)
	tmp = 0.0;
	if (y <= -1.12e+133)
		tmp = -(y * sqrt(0.3333333333333333));
	elseif (y <= 9e-248)
		tmp = sqrt((((0.3333333333333333 * (x * x)) + (0.3333333333333333 * (z * z))) + (0.3333333333333333 * (y * y))));
	elseif (y <= 1.95e-109)
		tmp = abs(z) / sqrt(3.0);
	elseif (y <= 7.8e+121)
		tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
	else
		tmp = y / sqrt(3.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_] := If[LessEqual[y, -1.12e+133], (-N[(y * N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision]), If[LessEqual[y, 9e-248], N[Sqrt[N[(N[(N[(0.3333333333333333 * N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(0.3333333333333333 * N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.3333333333333333 * N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[y, 1.95e-109], N[(N[Abs[z], $MachinePrecision] / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 7.8e+121], N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision], N[(y / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision]]]]]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \leq -1.12 \cdot 10^{+133}:\\
\;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\

\mathbf{elif}\;y \leq 9 \cdot 10^{-248}:\\
\;\;\;\;\sqrt{\left(0.3333333333333333 \cdot \left(x \cdot x\right) + 0.3333333333333333 \cdot \left(z \cdot z\right)\right) + 0.3333333333333333 \cdot \left(y \cdot y\right)}\\

\mathbf{elif}\;y \leq 1.95 \cdot 10^{-109}:\\
\;\;\;\;\frac{\left|z\right|}{\sqrt{3}}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{y}{\sqrt{3}}\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original37.6
Target25.9
Herbie25.4
\[\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 5 regimes
  2. if y < -1.12e133

    1. Initial program 58.9

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

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right) \cdot 0.3333333333333333}} \]
      Proof
    3. Taylor expanded in y around -inf 14.7

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333}\right)} \]
    4. Simplified14.7

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

    if -1.12e133 < y < 8.9999999999999992e-248

    1. Initial program 29.5

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

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right) \cdot 0.3333333333333333}} \]
      Proof
    3. Applied egg-rr29.6

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

    if 8.9999999999999992e-248 < y < 1.95000000000000011e-109

    1. Initial program 27.5

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

      \[\leadsto \sqrt{\frac{\color{blue}{{x}^{2}} + z \cdot z}{3}} \]
    3. Simplified29.0

      \[\leadsto \sqrt{\frac{\color{blue}{x \cdot x} + z \cdot z}{3}} \]
      Proof
    4. Taylor expanded in x around 0 45.2

      \[\leadsto \sqrt{\frac{\color{blue}{{z}^{2}}}{3}} \]
    5. Simplified45.2

      \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z}}{3}} \]
      Proof
    6. Applied egg-rr32.8

      \[\leadsto \color{blue}{\frac{\left|-z\right|}{\sqrt{3}}} \]
    7. Simplified32.8

      \[\leadsto \color{blue}{\frac{\left|z\right|}{\sqrt{3}}} \]
      Proof

    if 1.95000000000000011e-109 < y < 7.79999999999999967e121

    1. Initial program 28.0

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

    if 7.79999999999999967e121 < y

    1. Initial program 57.4

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

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right) \cdot 0.3333333333333333}} \]
      Proof
    3. Taylor expanded in y around inf 15.7

      \[\leadsto \color{blue}{y \cdot \sqrt{0.3333333333333333}} \]
    4. Applied egg-rr15.7

      \[\leadsto \color{blue}{\frac{y}{\sqrt{3}}} \]
  3. Recombined 5 regimes into one program.

Alternatives

Alternative 1
Error34.1
Cost8032
\[\begin{array}{l} t_0 := \sqrt{0.3333333333333333 \cdot \left(z \cdot z + x \cdot x\right)}\\ t_1 := \frac{x}{\sqrt{3}}\\ t_2 := \frac{y}{\sqrt{3}}\\ \mathbf{if}\;x \leq -8.5 \cdot 10^{+117}:\\ \;\;\;\;-t_1\\ \mathbf{elif}\;x \leq -2.8 \cdot 10^{-34}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -3.8 \cdot 10^{-77}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq -2.2 \cdot 10^{-149}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -3.7 \cdot 10^{-193}:\\ \;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 2.2 \cdot 10^{-206}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq 1.06 \cdot 10^{-83}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 1.12 \cdot 10^{+138}:\\ \;\;\;\;\sqrt{0.3333333333333333 \cdot \left(y \cdot y + x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error34.2
Cost8032
\[\begin{array}{l} t_0 := \frac{y}{\sqrt{3}}\\ t_1 := \frac{x}{\sqrt{3}}\\ \mathbf{if}\;x \leq -4.4 \cdot 10^{+113}:\\ \;\;\;\;-t_1\\ \mathbf{elif}\;x \leq -9.2 \cdot 10^{-35}:\\ \;\;\;\;\sqrt{\frac{x \cdot x + z \cdot z}{3}}\\ \mathbf{elif}\;x \leq -2.35 \cdot 10^{-71}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -2.9 \cdot 10^{-139}:\\ \;\;\;\;\sqrt{0.3333333333333333 \cdot \left(z \cdot z + x \cdot x\right)}\\ \mathbf{elif}\;x \leq -1.5 \cdot 10^{-192}:\\ \;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 1.05 \cdot 10^{-207}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.06 \cdot 10^{-83}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 6.2 \cdot 10^{+138}:\\ \;\;\;\;\sqrt{0.3333333333333333 \cdot \left(y \cdot y + x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error33.6
Cost7636
\[\begin{array}{l} t_0 := \sqrt{0.3333333333333333 \cdot \left(y \cdot y + x \cdot x\right)}\\ \mathbf{if}\;y \leq -7.5 \cdot 10^{+133}:\\ \;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \leq -3.6 \cdot 10^{-95}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 8.5 \cdot 10^{-248}:\\ \;\;\;\;\frac{x}{\sqrt{3}}\\ \mathbf{elif}\;y \leq 1.2 \cdot 10^{-109}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \leq 6.1 \cdot 10^{+122}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \end{array} \]
Alternative 4
Error29.9
Cost7504
\[\begin{array}{l} t_0 := \frac{x}{\sqrt{3}}\\ \mathbf{if}\;x \leq -8 \cdot 10^{+119}:\\ \;\;\;\;-t_0\\ \mathbf{elif}\;x \leq -1.02 \cdot 10^{-32}:\\ \;\;\;\;\sqrt{\frac{x \cdot x + z \cdot z}{3}}\\ \mathbf{elif}\;x \leq -6.5 \cdot 10^{-71}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;x \leq 4.5 \cdot 10^{-59}:\\ \;\;\;\;\sqrt{\frac{y \cdot y + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error24.8
Cost7496
\[\begin{array}{l} \mathbf{if}\;y \leq -1.12 \cdot 10^{+133}:\\ \;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \leq 2.1 \cdot 10^{+123}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \end{array} \]
Alternative 6
Error36.2
Cost7384
\[\begin{array}{l} t_0 := y \cdot \sqrt{0.3333333333333333}\\ t_1 := \frac{x}{\sqrt{3}}\\ \mathbf{if}\;y \leq -1.5 \cdot 10^{-53}:\\ \;\;\;\;-t_0\\ \mathbf{elif}\;y \leq 1.05 \cdot 10^{-247}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 2.15 \cdot 10^{-108}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \leq 0.047:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 7.1 \cdot 10^{+37}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 8.2 \cdot 10^{+48}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot x\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \end{array} \]
Alternative 7
Error36.9
Cost7184
\[\begin{array}{l} \mathbf{if}\;x \leq -0.01:\\ \;\;\;\;-\sqrt{0.3333333333333333} \cdot x\\ \mathbf{elif}\;x \leq -1.28 \cdot 10^{-194}:\\ \;\;\;\;-y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 8.6 \cdot 10^{-213}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;x \leq 7.5 \cdot 10^{-84}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\sqrt{3}}\\ \end{array} \]
Alternative 8
Error44.3
Cost6856
\[\begin{array}{l} \mathbf{if}\;x \leq 1.26 \cdot 10^{-179}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 1.9 \cdot 10^{-76}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot x\\ \end{array} \]
Alternative 9
Error44.3
Cost6856
\[\begin{array}{l} \mathbf{if}\;x \leq 4 \cdot 10^{-180}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 1.8 \cdot 10^{-76}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\sqrt{3}}\\ \end{array} \]
Alternative 10
Error44.3
Cost6856
\[\begin{array}{l} \mathbf{if}\;x \leq 1.6 \cdot 10^{-179}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;x \leq 3.1 \cdot 10^{-80}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\sqrt{3}}\\ \end{array} \]
Alternative 11
Error44.6
Cost6724
\[\begin{array}{l} \mathbf{if}\;z \leq 2.3 \cdot 10^{-49}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333}\\ \end{array} \]
Alternative 12
Error51.9
Cost6592
\[y \cdot \sqrt{0.3333333333333333} \]

Error

Reproduce

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