Average Error: 37.7 → 25.7
Time: 4.0s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -4.0554022285261168 \cdot 10^{113}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.8186674257036996 \cdot 10^{104}:\\ \;\;\;\;\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 -4.0554022285261168 \cdot 10^{113}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 2.8186674257036996 \cdot 10^{104}:\\
\;\;\;\;\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 r688151 = x;
        double r688152 = r688151 * r688151;
        double r688153 = y;
        double r688154 = r688153 * r688153;
        double r688155 = r688152 + r688154;
        double r688156 = z;
        double r688157 = r688156 * r688156;
        double r688158 = r688155 + r688157;
        double r688159 = sqrt(r688158);
        return r688159;
}

double f(double x, double y, double z) {
        double r688160 = z;
        double r688161 = -4.055402228526117e+113;
        bool r688162 = r688160 <= r688161;
        double r688163 = -r688160;
        double r688164 = 2.8186674257036996e+104;
        bool r688165 = r688160 <= r688164;
        double r688166 = x;
        double r688167 = y;
        double r688168 = r688167 * r688167;
        double r688169 = fma(r688166, r688166, r688168);
        double r688170 = fma(r688160, r688160, r688169);
        double r688171 = sqrt(r688170);
        double r688172 = r688165 ? r688171 : r688160;
        double r688173 = r688162 ? r688163 : r688172;
        return r688173;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.6
Herbie25.7
\[\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 < -4.055402228526117e+113

    1. Initial program 56.1

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

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

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

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

    if -4.055402228526117e+113 < z < 2.8186674257036996e+104

    1. Initial program 29.3

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

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

    if 2.8186674257036996e+104 < z

    1. Initial program 55.6

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -4.0554022285261168 \cdot 10^{113}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.8186674257036996 \cdot 10^{104}:\\ \;\;\;\;\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 2020045 +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))))