Average Error: 37.6 → 28.0
Time: 4.2s
Precision: binary64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;y \leq -1.3608159443905432 \cdot 10^{+154}:\\ \;\;\;\;-0.5 \cdot \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right) - y\\ \mathbf{elif}\;y \leq 2.64775110294404 \cdot 10^{+108}:\\ \;\;\;\;\sqrt{z \cdot z + \left(x \cdot x + y \cdot y\right)}\\ \mathbf{else}:\\ \;\;\;\;y + \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right) \cdot 0.5\\ \end{array}\]
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}
\begin{array}{l}
\mathbf{if}\;y \leq -1.3608159443905432 \cdot 10^{+154}:\\
\;\;\;\;-0.5 \cdot \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right) - y\\

\mathbf{elif}\;y \leq 2.64775110294404 \cdot 10^{+108}:\\
\;\;\;\;\sqrt{z \cdot z + \left(x \cdot x + y \cdot y\right)}\\

\mathbf{else}:\\
\;\;\;\;y + \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right) \cdot 0.5\\

\end{array}
(FPCore (x y z) :precision binary64 (sqrt (+ (+ (* x x) (* y y)) (* z z))))
(FPCore (x y z)
 :precision binary64
 (if (<= y -1.3608159443905432e+154)
   (- (* -0.5 (+ (/ (* z z) y) (/ (* x x) y))) y)
   (if (<= y 2.64775110294404e+108)
     (sqrt (+ (* z z) (+ (* x x) (* y y))))
     (+ y (* (+ (/ (* z z) y) (/ (* x x) y)) 0.5)))))
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 tmp;
	if (y <= -1.3608159443905432e+154) {
		tmp = (-0.5 * (((z * z) / y) + ((x * x) / y))) - y;
	} else if (y <= 2.64775110294404e+108) {
		tmp = sqrt((z * z) + ((x * x) + (y * y)));
	} else {
		tmp = y + ((((z * z) / y) + ((x * x) / y)) * 0.5);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original37.6
Target25.5
Herbie28.0
\[\begin{array}{l} \mathbf{if}\;z < -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z < 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -1.36081594439054316e154

    1. Initial program 64.0

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

      \[\leadsto \color{blue}{-\left(0.5 \cdot \frac{{x}^{2}}{y} + \left(y + 0.5 \cdot \frac{{z}^{2}}{y}\right)\right)}\]
    3. Simplified27.4

      \[\leadsto \color{blue}{\left(-y\right) + -0.5 \cdot \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right)}\]

    if -1.36081594439054316e154 < y < 2.64775110294404e108

    1. Initial program 28.3

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

    if 2.64775110294404e108 < y

    1. Initial program 55.7

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

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

      \[\leadsto \color{blue}{y + 0.5 \cdot \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification28.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.3608159443905432 \cdot 10^{+154}:\\ \;\;\;\;-0.5 \cdot \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right) - y\\ \mathbf{elif}\;y \leq 2.64775110294404 \cdot 10^{+108}:\\ \;\;\;\;\sqrt{z \cdot z + \left(x \cdot x + y \cdot y\right)}\\ \mathbf{else}:\\ \;\;\;\;y + \left(\frac{z \cdot z}{y} + \frac{x \cdot x}{y}\right) \cdot 0.5\\ \end{array}\]

Reproduce

herbie shell --seed 2020339 
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
  :precision binary64

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

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