Average Error: 37.2 → 25.3
Time: 10.7s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.034544328209387650857898454272795734085 \cdot 10^{120}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -2.199889829164358641265399324899705881924 \cdot 10^{-308}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le 7.77534484921746487956074712382930961875 \cdot 10^{-254}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \le 1.516560416068030987985164968577384239716 \cdot 10^{132}:\\ \;\;\;\;\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 -3.034544328209387650857898454272795734085 \cdot 10^{120}:\\
\;\;\;\;-z\\

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

\mathbf{elif}\;z \le 7.77534484921746487956074712382930961875 \cdot 10^{-254}:\\
\;\;\;\;y\\

\mathbf{elif}\;z \le 1.516560416068030987985164968577384239716 \cdot 10^{132}:\\
\;\;\;\;\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 r408358 = x;
        double r408359 = r408358 * r408358;
        double r408360 = y;
        double r408361 = r408360 * r408360;
        double r408362 = r408359 + r408361;
        double r408363 = z;
        double r408364 = r408363 * r408363;
        double r408365 = r408362 + r408364;
        double r408366 = sqrt(r408365);
        return r408366;
}

double f(double x, double y, double z) {
        double r408367 = z;
        double r408368 = -3.0345443282093877e+120;
        bool r408369 = r408367 <= r408368;
        double r408370 = -r408367;
        double r408371 = -2.1998898291643586e-308;
        bool r408372 = r408367 <= r408371;
        double r408373 = x;
        double r408374 = y;
        double r408375 = r408374 * r408374;
        double r408376 = fma(r408373, r408373, r408375);
        double r408377 = fma(r408367, r408367, r408376);
        double r408378 = sqrt(r408377);
        double r408379 = 7.775344849217465e-254;
        bool r408380 = r408367 <= r408379;
        double r408381 = 1.516560416068031e+132;
        bool r408382 = r408367 <= r408381;
        double r408383 = r408382 ? r408378 : r408367;
        double r408384 = r408380 ? r408374 : r408383;
        double r408385 = r408372 ? r408378 : r408384;
        double r408386 = r408369 ? r408370 : r408385;
        return r408386;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.2
Target24.7
Herbie25.3
\[\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 4 regimes
  2. if z < -3.0345443282093877e+120

    1. Initial program 57.6

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

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

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

    if -3.0345443282093877e+120 < z < -2.1998898291643586e-308 or 7.775344849217465e-254 < z < 1.516560416068031e+132

    1. Initial program 27.9

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

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

    if -2.1998898291643586e-308 < z < 7.775344849217465e-254

    1. Initial program 32.4

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

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

      \[\leadsto \color{blue}{y}\]

    if 1.516560416068031e+132 < z

    1. Initial program 59.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -3.034544328209387650857898454272795734085 \cdot 10^{120}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -2.199889829164358641265399324899705881924 \cdot 10^{-308}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le 7.77534484921746487956074712382930961875 \cdot 10^{-254}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \le 1.516560416068030987985164968577384239716 \cdot 10^{132}:\\ \;\;\;\;\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 2019208 +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))))