Average Error: 35.6 → 24.7
Time: 5.1s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.626115769186939 \cdot 10^{+168}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.7963384750144555 \cdot 10^{+146}:\\ \;\;\;\;\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 -3.626115769186939 \cdot 10^{+168}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 2.7963384750144555 \cdot 10^{+146}:\\
\;\;\;\;\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 r29625118 = x;
        double r29625119 = r29625118 * r29625118;
        double r29625120 = y;
        double r29625121 = r29625120 * r29625120;
        double r29625122 = r29625119 + r29625121;
        double r29625123 = z;
        double r29625124 = r29625123 * r29625123;
        double r29625125 = r29625122 + r29625124;
        double r29625126 = sqrt(r29625125);
        return r29625126;
}

double f(double x, double y, double z) {
        double r29625127 = z;
        double r29625128 = -3.626115769186939e+168;
        bool r29625129 = r29625127 <= r29625128;
        double r29625130 = -r29625127;
        double r29625131 = 2.7963384750144555e+146;
        bool r29625132 = r29625127 <= r29625131;
        double r29625133 = y;
        double r29625134 = x;
        double r29625135 = r29625134 * r29625134;
        double r29625136 = fma(r29625133, r29625133, r29625135);
        double r29625137 = fma(r29625127, r29625127, r29625136);
        double r29625138 = sqrt(r29625137);
        double r29625139 = r29625132 ? r29625138 : r29625127;
        double r29625140 = r29625129 ? r29625130 : r29625139;
        return r29625140;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original35.6
Target24.4
Herbie24.7
\[\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 < -3.626115769186939e+168

    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 12.6

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

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

    if -3.626115769186939e+168 < z < 2.7963384750144555e+146

    1. Initial program 28.2

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

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

    if 2.7963384750144555e+146 < z

    1. Initial program 58.0

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

      \[\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.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -3.626115769186939 \cdot 10^{+168}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.7963384750144555 \cdot 10^{+146}:\\ \;\;\;\;\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 2019163 +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))))