Average Error: 35.6 → 24.7
Time: 5.5s
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 r26974737 = x;
        double r26974738 = r26974737 * r26974737;
        double r26974739 = y;
        double r26974740 = r26974739 * r26974739;
        double r26974741 = r26974738 + r26974740;
        double r26974742 = z;
        double r26974743 = r26974742 * r26974742;
        double r26974744 = r26974741 + r26974743;
        double r26974745 = sqrt(r26974744);
        return r26974745;
}

double f(double x, double y, double z) {
        double r26974746 = z;
        double r26974747 = -3.626115769186939e+168;
        bool r26974748 = r26974746 <= r26974747;
        double r26974749 = -r26974746;
        double r26974750 = 2.7963384750144555e+146;
        bool r26974751 = r26974746 <= r26974750;
        double r26974752 = y;
        double r26974753 = x;
        double r26974754 = r26974753 * r26974753;
        double r26974755 = fma(r26974752, r26974752, r26974754);
        double r26974756 = fma(r26974746, r26974746, r26974755);
        double r26974757 = sqrt(r26974756);
        double r26974758 = r26974751 ? r26974757 : r26974746;
        double r26974759 = r26974748 ? r26974749 : r26974758;
        return r26974759;
}

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