Average Error: 34.9 → 25.4
Time: 7.0s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;x \le -6.208442960829875 \cdot 10^{+137}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \le -1.9111814054112648 \cdot 10^{-98}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;x \le -8.598460374864875 \cdot 10^{-202}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \le 9.794009169674087 \cdot 10^{+109}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}
\begin{array}{l}
\mathbf{if}\;x \le -6.208442960829875 \cdot 10^{+137}:\\
\;\;\;\;-x\\

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

\mathbf{elif}\;x \le -8.598460374864875 \cdot 10^{-202}:\\
\;\;\;\;z\\

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

\mathbf{else}:\\
\;\;\;\;x\\

\end{array}
double f(double x, double y, double z) {
        double r26156988 = x;
        double r26156989 = r26156988 * r26156988;
        double r26156990 = y;
        double r26156991 = r26156990 * r26156990;
        double r26156992 = r26156989 + r26156991;
        double r26156993 = z;
        double r26156994 = r26156993 * r26156993;
        double r26156995 = r26156992 + r26156994;
        double r26156996 = sqrt(r26156995);
        return r26156996;
}

double f(double x, double y, double z) {
        double r26156997 = x;
        double r26156998 = -6.208442960829875e+137;
        bool r26156999 = r26156997 <= r26156998;
        double r26157000 = -r26156997;
        double r26157001 = -1.9111814054112648e-98;
        bool r26157002 = r26156997 <= r26157001;
        double r26157003 = z;
        double r26157004 = y;
        double r26157005 = r26156997 * r26156997;
        double r26157006 = fma(r26157004, r26157004, r26157005);
        double r26157007 = fma(r26157003, r26157003, r26157006);
        double r26157008 = sqrt(r26157007);
        double r26157009 = -8.598460374864875e-202;
        bool r26157010 = r26156997 <= r26157009;
        double r26157011 = 9.794009169674087e+109;
        bool r26157012 = r26156997 <= r26157011;
        double r26157013 = r26157012 ? r26157008 : r26156997;
        double r26157014 = r26157010 ? r26157003 : r26157013;
        double r26157015 = r26157002 ? r26157008 : r26157014;
        double r26157016 = r26156999 ? r26157000 : r26157015;
        return r26157016;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original34.9
Target23.3
Herbie25.4
\[\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 4 regimes
  2. if x < -6.208442960829875e+137

    1. Initial program 56.2

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

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

      \[\leadsto \sqrt{\color{blue}{{y}^{2} + \left({x}^{2} + {z}^{2}\right)}}\]
    4. Simplified56.2

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

      \[\leadsto \color{blue}{-1 \cdot x}\]
    6. Simplified15.0

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

    if -6.208442960829875e+137 < x < -1.9111814054112648e-98 or -8.598460374864875e-202 < x < 9.794009169674087e+109

    1. Initial program 27.0

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

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

    if -1.9111814054112648e-98 < x < -8.598460374864875e-202

    1. Initial program 28.1

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

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

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

    if 9.794009169674087e+109 < x

    1. Initial program 52.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -6.208442960829875 \cdot 10^{+137}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \le -1.9111814054112648 \cdot 10^{-98}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;x \le -8.598460374864875 \cdot 10^{-202}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \le 9.794009169674087 \cdot 10^{+109}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]

Reproduce

herbie shell --seed 2019158 +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))))