Average Error: 34.7 → 23.4
Time: 6.9s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -9.787469103683682 \cdot 10^{+146}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 3.0789079137361923 \cdot 10^{+128}:\\ \;\;\;\;\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 -9.787469103683682 \cdot 10^{+146}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 3.0789079137361923 \cdot 10^{+128}:\\
\;\;\;\;\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 r27107532 = x;
        double r27107533 = r27107532 * r27107532;
        double r27107534 = y;
        double r27107535 = r27107534 * r27107534;
        double r27107536 = r27107533 + r27107535;
        double r27107537 = z;
        double r27107538 = r27107537 * r27107537;
        double r27107539 = r27107536 + r27107538;
        double r27107540 = sqrt(r27107539);
        return r27107540;
}

double f(double x, double y, double z) {
        double r27107541 = z;
        double r27107542 = -9.787469103683682e+146;
        bool r27107543 = r27107541 <= r27107542;
        double r27107544 = -r27107541;
        double r27107545 = 3.0789079137361923e+128;
        bool r27107546 = r27107541 <= r27107545;
        double r27107547 = y;
        double r27107548 = x;
        double r27107549 = r27107548 * r27107548;
        double r27107550 = fma(r27107547, r27107547, r27107549);
        double r27107551 = fma(r27107541, r27107541, r27107550);
        double r27107552 = sqrt(r27107551);
        double r27107553 = r27107546 ? r27107552 : r27107541;
        double r27107554 = r27107543 ? r27107544 : r27107553;
        return r27107554;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original34.7
Target23.5
Herbie23.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 3 regimes
  2. if z < -9.787469103683682e+146

    1. Initial program 57.8

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

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

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

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

    if -9.787469103683682e+146 < z < 3.0789079137361923e+128

    1. Initial program 26.4

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

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

    if 3.0789079137361923e+128 < z

    1. Initial program 54.7

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -9.787469103683682 \cdot 10^{+146}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 3.0789079137361923 \cdot 10^{+128}:\\ \;\;\;\;\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 2019162 +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))))