Average Error: 38.2 → 26.0
Time: 3.9s
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 r641064 = x;
        double r641065 = r641064 * r641064;
        double r641066 = y;
        double r641067 = r641066 * r641066;
        double r641068 = r641065 + r641067;
        double r641069 = z;
        double r641070 = r641069 * r641069;
        double r641071 = r641068 + r641070;
        double r641072 = sqrt(r641071);
        return r641072;
}

double f(double x, double y, double z) {
        double r641073 = z;
        double r641074 = -1.4065040948592316e+146;
        bool r641075 = r641073 <= r641074;
        double r641076 = -r641073;
        double r641077 = 3.540104367259201e+76;
        bool r641078 = r641073 <= r641077;
        double r641079 = x;
        double r641080 = y;
        double r641081 = r641080 * r641080;
        double r641082 = fma(r641079, r641079, r641081);
        double r641083 = fma(r641073, r641073, r641082);
        double r641084 = sqrt(r641083);
        double r641085 = r641078 ? r641084 : r641073;
        double r641086 = r641075 ? r641076 : r641085;
        return r641086;
}

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