Average Error: 37.7 → 26.6
Time: 14.8s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.255797641178115091184855102985974791186 \cdot 10^{124}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -6.671259440587564170012615816152882462551 \cdot 10^{-115}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -1.177006063301380770204460022706249484831 \cdot 10^{-191}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \le 1.262162961903328808240912941168874780852 \cdot 10^{67}:\\ \;\;\;\;\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 -1.255797641178115091184855102985974791186 \cdot 10^{124}:\\
\;\;\;\;-z\\

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

\mathbf{elif}\;z \le -1.177006063301380770204460022706249484831 \cdot 10^{-191}:\\
\;\;\;\;y\\

\mathbf{elif}\;z \le 1.262162961903328808240912941168874780852 \cdot 10^{67}:\\
\;\;\;\;\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 r519299 = x;
        double r519300 = r519299 * r519299;
        double r519301 = y;
        double r519302 = r519301 * r519301;
        double r519303 = r519300 + r519302;
        double r519304 = z;
        double r519305 = r519304 * r519304;
        double r519306 = r519303 + r519305;
        double r519307 = sqrt(r519306);
        return r519307;
}

double f(double x, double y, double z) {
        double r519308 = z;
        double r519309 = -1.255797641178115e+124;
        bool r519310 = r519308 <= r519309;
        double r519311 = -r519308;
        double r519312 = -6.671259440587564e-115;
        bool r519313 = r519308 <= r519312;
        double r519314 = x;
        double r519315 = y;
        double r519316 = r519315 * r519315;
        double r519317 = fma(r519314, r519314, r519316);
        double r519318 = fma(r519308, r519308, r519317);
        double r519319 = sqrt(r519318);
        double r519320 = -1.1770060633013808e-191;
        bool r519321 = r519308 <= r519320;
        double r519322 = 1.2621629619033288e+67;
        bool r519323 = r519308 <= r519322;
        double r519324 = r519323 ? r519319 : r519308;
        double r519325 = r519321 ? r519315 : r519324;
        double r519326 = r519313 ? r519319 : r519325;
        double r519327 = r519310 ? r519311 : r519326;
        return r519327;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.2
Herbie26.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 4 regimes
  2. if z < -1.255797641178115e+124

    1. Initial program 56.5

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

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

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

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

    if -1.255797641178115e+124 < z < -6.671259440587564e-115 or -1.1770060633013808e-191 < z < 1.2621629619033288e+67

    1. Initial program 29.5

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

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

    if -6.671259440587564e-115 < z < -1.1770060633013808e-191

    1. Initial program 27.3

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

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

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

    if 1.2621629619033288e+67 < z

    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 20.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.255797641178115091184855102985974791186 \cdot 10^{124}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -6.671259440587564170012615816152882462551 \cdot 10^{-115}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -1.177006063301380770204460022706249484831 \cdot 10^{-191}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \le 1.262162961903328808240912941168874780852 \cdot 10^{67}:\\ \;\;\;\;\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 2019212 +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))))