Average Error: 37.3 → 25.2
Time: 5.6s
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 r30538986 = x;
        double r30538987 = r30538986 * r30538986;
        double r30538988 = y;
        double r30538989 = r30538988 * r30538988;
        double r30538990 = r30538987 + r30538989;
        double r30538991 = z;
        double r30538992 = r30538991 * r30538991;
        double r30538993 = r30538990 + r30538992;
        double r30538994 = sqrt(r30538993);
        return r30538994;
}

double f(double x, double y, double z) {
        double r30538995 = z;
        double r30538996 = -1.6436486557967425e+126;
        bool r30538997 = r30538995 <= r30538996;
        double r30538998 = -r30538995;
        double r30538999 = 5.367114864082889e+82;
        bool r30539000 = r30538995 <= r30538999;
        double r30539001 = y;
        double r30539002 = x;
        double r30539003 = r30539002 * r30539002;
        double r30539004 = fma(r30539001, r30539001, r30539003);
        double r30539005 = fma(r30538995, r30538995, r30539004);
        double r30539006 = sqrt(r30539005);
        double r30539007 = r30539000 ? r30539006 : r30538995;
        double r30539008 = r30538997 ? r30538998 : r30539007;
        return r30539008;
}

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