Average Error: 37.7 → 25.6
Time: 9.1s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.232124088806183972086523325778622918373 \cdot 10^{105}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.200001758568738958396911833976314317879 \cdot 10^{47}:\\ \;\;\;\;\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.232124088806183972086523325778622918373 \cdot 10^{105}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 2.200001758568738958396911833976314317879 \cdot 10^{47}:\\
\;\;\;\;\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 r438994 = x;
        double r438995 = r438994 * r438994;
        double r438996 = y;
        double r438997 = r438996 * r438996;
        double r438998 = r438995 + r438997;
        double r438999 = z;
        double r439000 = r438999 * r438999;
        double r439001 = r438998 + r439000;
        double r439002 = sqrt(r439001);
        return r439002;
}

double f(double x, double y, double z) {
        double r439003 = z;
        double r439004 = -2.232124088806184e+105;
        bool r439005 = r439003 <= r439004;
        double r439006 = -r439003;
        double r439007 = 2.200001758568739e+47;
        bool r439008 = r439003 <= r439007;
        double r439009 = x;
        double r439010 = y;
        double r439011 = r439010 * r439010;
        double r439012 = fma(r439009, r439009, r439011);
        double r439013 = fma(r439003, r439003, r439012);
        double r439014 = sqrt(r439013);
        double r439015 = r439008 ? r439014 : r439003;
        double r439016 = r439005 ? r439006 : r439015;
        return r439016;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.0
Herbie25.6
\[\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.232124088806184e+105

    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.9

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

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

    if -2.232124088806184e+105 < z < 2.200001758568739e+47

    1. Initial program 28.8

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

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

    if 2.200001758568739e+47 < z

    1. Initial program 49.4

      \[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
    2. Simplified49.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 21.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.232124088806183972086523325778622918373 \cdot 10^{105}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.200001758568738958396911833976314317879 \cdot 10^{47}:\\ \;\;\;\;\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 2019209 +o rules:numerics
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
  :precision binary64

  :herbie-target
  (if (< z -6.3964793941097758e136) (- z) (if (< z 7.3202936944041821e117) (sqrt (+ (+ (* z z) (* x x)) (* y y))) z))

  (sqrt (+ (+ (* x x) (* y y)) (* z z))))