Average Error: 38.0 → 26.0
Time: 12.4s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -7.934095148612053024551715240512471476073 \cdot 10^{53}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 4.199061233445226736763450374564950587912 \cdot 10^{130}:\\ \;\;\;\;\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 -7.934095148612053024551715240512471476073 \cdot 10^{53}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 4.199061233445226736763450374564950587912 \cdot 10^{130}:\\
\;\;\;\;\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 r31081856 = x;
        double r31081857 = r31081856 * r31081856;
        double r31081858 = y;
        double r31081859 = r31081858 * r31081858;
        double r31081860 = r31081857 + r31081859;
        double r31081861 = z;
        double r31081862 = r31081861 * r31081861;
        double r31081863 = r31081860 + r31081862;
        double r31081864 = sqrt(r31081863);
        return r31081864;
}

double f(double x, double y, double z) {
        double r31081865 = z;
        double r31081866 = -7.934095148612053e+53;
        bool r31081867 = r31081865 <= r31081866;
        double r31081868 = -r31081865;
        double r31081869 = 4.1990612334452267e+130;
        bool r31081870 = r31081865 <= r31081869;
        double r31081871 = y;
        double r31081872 = x;
        double r31081873 = r31081872 * r31081872;
        double r31081874 = fma(r31081871, r31081871, r31081873);
        double r31081875 = fma(r31081865, r31081865, r31081874);
        double r31081876 = sqrt(r31081875);
        double r31081877 = r31081870 ? r31081876 : r31081865;
        double r31081878 = r31081867 ? r31081868 : r31081877;
        return r31081878;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.0
Target25.7
Herbie26.0
\[\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 < -7.934095148612053e+53

    1. Initial program 49.6

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

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

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

    if -7.934095148612053e+53 < z < 4.1990612334452267e+130

    1. Initial program 29.8

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

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

    if 4.1990612334452267e+130 < z

    1. Initial program 59.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -7.934095148612053024551715240512471476073 \cdot 10^{53}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 4.199061233445226736763450374564950587912 \cdot 10^{130}:\\ \;\;\;\;\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 2019168 +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))))