Average Error: 34.7 → 23.3
Time: 19.3s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -4.907860111955181 \cdot 10^{+151}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 6.809472127167789 \cdot 10^{+153}:\\ \;\;\;\;\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 -4.907860111955181 \cdot 10^{+151}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 6.809472127167789 \cdot 10^{+153}:\\
\;\;\;\;\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 r26301921 = x;
        double r26301922 = r26301921 * r26301921;
        double r26301923 = y;
        double r26301924 = r26301923 * r26301923;
        double r26301925 = r26301922 + r26301924;
        double r26301926 = z;
        double r26301927 = r26301926 * r26301926;
        double r26301928 = r26301925 + r26301927;
        double r26301929 = sqrt(r26301928);
        return r26301929;
}

double f(double x, double y, double z) {
        double r26301930 = z;
        double r26301931 = -4.907860111955181e+151;
        bool r26301932 = r26301930 <= r26301931;
        double r26301933 = -r26301930;
        double r26301934 = 6.809472127167789e+153;
        bool r26301935 = r26301930 <= r26301934;
        double r26301936 = y;
        double r26301937 = x;
        double r26301938 = r26301937 * r26301937;
        double r26301939 = fma(r26301936, r26301936, r26301938);
        double r26301940 = fma(r26301930, r26301930, r26301939);
        double r26301941 = sqrt(r26301940);
        double r26301942 = r26301935 ? r26301941 : r26301930;
        double r26301943 = r26301932 ? r26301933 : r26301942;
        return r26301943;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original34.7
Target23.4
Herbie23.3
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \lt 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 z < -4.907860111955181e+151

    1. Initial program 58.9

      \[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
    2. Simplified58.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 13.8

      \[\leadsto \color{blue}{-1 \cdot z}\]
    4. Simplified13.8

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

    if -4.907860111955181e+151 < z < 6.809472127167789e+153

    1. Initial program 26.5

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

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

    if 6.809472127167789e+153 < z

    1. Initial program 59.2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -4.907860111955181 \cdot 10^{+151}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 6.809472127167789 \cdot 10^{+153}:\\ \;\;\;\;\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 2019164 +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))))