Average Error: 38.2 → 26.0
Time: 3.1s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.4065040948592316 \cdot 10^{146}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 3.54010436725920069 \cdot 10^{76}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\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 -1.4065040948592316 \cdot 10^{146}:\\
\;\;\;\;-z\\

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

\mathbf{else}:\\
\;\;\;\;z\\

\end{array}
double f(double x, double y, double z) {
        double r708934 = x;
        double r708935 = r708934 * r708934;
        double r708936 = y;
        double r708937 = r708936 * r708936;
        double r708938 = r708935 + r708937;
        double r708939 = z;
        double r708940 = r708939 * r708939;
        double r708941 = r708938 + r708940;
        double r708942 = sqrt(r708941);
        return r708942;
}

double f(double x, double y, double z) {
        double r708943 = z;
        double r708944 = -1.4065040948592316e+146;
        bool r708945 = r708943 <= r708944;
        double r708946 = -r708943;
        double r708947 = 3.540104367259201e+76;
        bool r708948 = r708943 <= r708947;
        double r708949 = x;
        double r708950 = y;
        double r708951 = r708950 * r708950;
        double r708952 = fma(r708949, r708949, r708951);
        double r708953 = fma(r708943, r708943, r708952);
        double r708954 = sqrt(r708953);
        double r708955 = r708948 ? r708954 : r708943;
        double r708956 = r708945 ? r708946 : r708955;
        return r708956;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.9
Herbie26.0
\[\begin{array}{l} \mathbf{if}\;z \lt -6.3964793941097758 \cdot 10^{136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \lt 7.3202936944041821 \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 < -1.4065040948592316e+146

    1. Initial program 62.4

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

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

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

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

    if -1.4065040948592316e+146 < z < 3.540104367259201e+76

    1. Initial program 29.5

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

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

    if 3.540104367259201e+76 < z

    1. Initial program 52.4

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.4065040948592316 \cdot 10^{146}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 3.54010436725920069 \cdot 10^{76}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]

Reproduce

herbie shell --seed 2020047 +o rules:numerics
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
  :precision binary64

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