Average Error: 37.1 → 26.5
Time: 9.6s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.410192111830645335958391081170087462693 \cdot 10^{85}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 7.631866408902892617751544103182699118184 \cdot 10^{-240}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;z \le 3.960391598269188371406907095464583000374 \cdot 10^{-142}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \le 5.351693709150228607664599863770357835557 \cdot 10^{134}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\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.410192111830645335958391081170087462693 \cdot 10^{85}:\\
\;\;\;\;-z\\

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

\mathbf{elif}\;z \le 3.960391598269188371406907095464583000374 \cdot 10^{-142}:\\
\;\;\;\;x\\

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

\mathbf{else}:\\
\;\;\;\;z\\

\end{array}
double f(double x, double y, double z) {
        double r27605353 = x;
        double r27605354 = r27605353 * r27605353;
        double r27605355 = y;
        double r27605356 = r27605355 * r27605355;
        double r27605357 = r27605354 + r27605356;
        double r27605358 = z;
        double r27605359 = r27605358 * r27605358;
        double r27605360 = r27605357 + r27605359;
        double r27605361 = sqrt(r27605360);
        return r27605361;
}

double f(double x, double y, double z) {
        double r27605362 = z;
        double r27605363 = -2.4101921118306453e+85;
        bool r27605364 = r27605362 <= r27605363;
        double r27605365 = -r27605362;
        double r27605366 = 7.631866408902893e-240;
        bool r27605367 = r27605362 <= r27605366;
        double r27605368 = y;
        double r27605369 = x;
        double r27605370 = r27605369 * r27605369;
        double r27605371 = fma(r27605368, r27605368, r27605370);
        double r27605372 = fma(r27605362, r27605362, r27605371);
        double r27605373 = sqrt(r27605372);
        double r27605374 = 3.9603915982691884e-142;
        bool r27605375 = r27605362 <= r27605374;
        double r27605376 = 5.351693709150229e+134;
        bool r27605377 = r27605362 <= r27605376;
        double r27605378 = r27605377 ? r27605373 : r27605362;
        double r27605379 = r27605375 ? r27605369 : r27605378;
        double r27605380 = r27605367 ? r27605373 : r27605379;
        double r27605381 = r27605364 ? r27605365 : r27605380;
        return r27605381;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.1
Target24.8
Herbie26.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 4 regimes
  2. if z < -2.4101921118306453e+85

    1. Initial program 51.7

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

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

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

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

    if -2.4101921118306453e+85 < z < 7.631866408902893e-240 or 3.9603915982691884e-142 < z < 5.351693709150229e+134

    1. Initial program 28.4

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

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

    if 7.631866408902893e-240 < z < 3.9603915982691884e-142

    1. Initial program 29.0

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

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

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

    if 5.351693709150229e+134 < z

    1. Initial program 60.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.410192111830645335958391081170087462693 \cdot 10^{85}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 7.631866408902892617751544103182699118184 \cdot 10^{-240}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;z \le 3.960391598269188371406907095464583000374 \cdot 10^{-142}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \le 5.351693709150228607664599863770357835557 \cdot 10^{134}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]

Reproduce

herbie shell --seed 2019179 +o rules:numerics
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"

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