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

\mathbf{elif}\;z \le 4.318337911418744458050220115099994596476 \cdot 10^{113}:\\
\;\;\;\;\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 r372848 = x;
        double r372849 = r372848 * r372848;
        double r372850 = y;
        double r372851 = r372850 * r372850;
        double r372852 = r372849 + r372851;
        double r372853 = z;
        double r372854 = r372853 * r372853;
        double r372855 = r372852 + r372854;
        double r372856 = sqrt(r372855);
        return r372856;
}

double f(double x, double y, double z) {
        double r372857 = z;
        double r372858 = -2.5043710848889442e+85;
        bool r372859 = r372857 <= r372858;
        double r372860 = -r372857;
        double r372861 = 4.3183379114187445e+113;
        bool r372862 = r372857 <= r372861;
        double r372863 = x;
        double r372864 = y;
        double r372865 = r372864 * r372864;
        double r372866 = fma(r372863, r372863, r372865);
        double r372867 = fma(r372857, r372857, r372866);
        double r372868 = sqrt(r372867);
        double r372869 = r372862 ? r372868 : r372857;
        double r372870 = r372859 ? r372860 : r372869;
        return r372870;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.0
Target25.3
Herbie25.5
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109775845820908799933348003545 \cdot 10^{136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \lt 7.320293694404182125923160810847974073098 \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 < -2.5043710848889442e+85

    1. Initial program 52.3

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

      \[\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.4

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

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

    if -2.5043710848889442e+85 < z < 4.3183379114187445e+113

    1. Initial program 29.6

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

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

    if 4.3183379114187445e+113 < z

    1. Initial program 55.9

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

      \[\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.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.504371084888944228251954331687314845323 \cdot 10^{85}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 4.318337911418744458050220115099994596476 \cdot 10^{113}:\\ \;\;\;\;\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 2019347 +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))))