Average Error: 37.3 → 25.2
Time: 6.3s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.643648655796742516992848026301703974375 \cdot 10^{126}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 5.367114864082888858240124126679124813161 \cdot 10^{82}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}
\begin{array}{l}
\mathbf{if}\;z \le -1.643648655796742516992848026301703974375 \cdot 10^{126}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 5.367114864082888858240124126679124813161 \cdot 10^{82}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;z\\

\end{array}
double f(double x, double y, double z) {
        double r21681178 = x;
        double r21681179 = r21681178 * r21681178;
        double r21681180 = y;
        double r21681181 = r21681180 * r21681180;
        double r21681182 = r21681179 + r21681181;
        double r21681183 = z;
        double r21681184 = r21681183 * r21681183;
        double r21681185 = r21681182 + r21681184;
        double r21681186 = sqrt(r21681185);
        return r21681186;
}

double f(double x, double y, double z) {
        double r21681187 = z;
        double r21681188 = -1.6436486557967425e+126;
        bool r21681189 = r21681187 <= r21681188;
        double r21681190 = -r21681187;
        double r21681191 = 5.367114864082889e+82;
        bool r21681192 = r21681187 <= r21681191;
        double r21681193 = y;
        double r21681194 = x;
        double r21681195 = r21681194 * r21681194;
        double r21681196 = fma(r21681193, r21681193, r21681195);
        double r21681197 = fma(r21681187, r21681187, r21681196);
        double r21681198 = sqrt(r21681197);
        double r21681199 = r21681192 ? r21681198 : r21681187;
        double r21681200 = r21681189 ? r21681190 : r21681199;
        return r21681200;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.3
Target25.0
Herbie25.2
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109775845820908799933348003545 \cdot 10^{136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \lt 7.320293694404182125923160810847974073098 \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 z < -1.6436486557967425e+126

    1. Initial program 58.6

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

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

      \[\leadsto \color{blue}{-1 \cdot z}\]
    4. Simplified15.7

      \[\leadsto \color{blue}{-z}\]

    if -1.6436486557967425e+126 < z < 5.367114864082889e+82

    1. Initial program 28.7

      \[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
    2. Simplified28.7

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}}\]

    if 5.367114864082889e+82 < z

    1. Initial program 51.9

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

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

      \[\leadsto \color{blue}{z}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification25.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.643648655796742516992848026301703974375 \cdot 10^{126}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 5.367114864082888858240124126679124813161 \cdot 10^{82}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]

Reproduce

herbie shell --seed 2019172 +o rules:numerics
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"

  :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))))