Average Error: 35.6 → 24.7
Time: 5.8s
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 r29047709 = x;
        double r29047710 = r29047709 * r29047709;
        double r29047711 = y;
        double r29047712 = r29047711 * r29047711;
        double r29047713 = r29047710 + r29047712;
        double r29047714 = z;
        double r29047715 = r29047714 * r29047714;
        double r29047716 = r29047713 + r29047715;
        double r29047717 = sqrt(r29047716);
        return r29047717;
}

double f(double x, double y, double z) {
        double r29047718 = z;
        double r29047719 = -3.626115769186939e+168;
        bool r29047720 = r29047718 <= r29047719;
        double r29047721 = -r29047718;
        double r29047722 = 2.7963384750144555e+146;
        bool r29047723 = r29047718 <= r29047722;
        double r29047724 = y;
        double r29047725 = x;
        double r29047726 = r29047725 * r29047725;
        double r29047727 = fma(r29047724, r29047724, r29047726);
        double r29047728 = fma(r29047718, r29047718, r29047727);
        double r29047729 = sqrt(r29047728);
        double r29047730 = r29047723 ? r29047729 : r29047718;
        double r29047731 = r29047720 ? r29047721 : r29047730;
        return r29047731;
}

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))))