Average Error: 38.2 → 26.1
Time: 6.7s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.722077932407332897914292742062327594505 \cdot 10^{118}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -9.284378369405581674635859258893819774821 \cdot 10^{-211}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;z \le -1.242347267252565005698596962527611511272 \cdot 10^{-225}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \le 5.001396056530513724211604175576022569559 \cdot 10^{92}:\\ \;\;\;\;\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 -1.722077932407332897914292742062327594505 \cdot 10^{118}:\\
\;\;\;\;-z\\

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

\mathbf{elif}\;z \le -1.242347267252565005698596962527611511272 \cdot 10^{-225}:\\
\;\;\;\;x\\

\mathbf{elif}\;z \le 5.001396056530513724211604175576022569559 \cdot 10^{92}:\\
\;\;\;\;\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 r25571429 = x;
        double r25571430 = r25571429 * r25571429;
        double r25571431 = y;
        double r25571432 = r25571431 * r25571431;
        double r25571433 = r25571430 + r25571432;
        double r25571434 = z;
        double r25571435 = r25571434 * r25571434;
        double r25571436 = r25571433 + r25571435;
        double r25571437 = sqrt(r25571436);
        return r25571437;
}

double f(double x, double y, double z) {
        double r25571438 = z;
        double r25571439 = -1.722077932407333e+118;
        bool r25571440 = r25571438 <= r25571439;
        double r25571441 = -r25571438;
        double r25571442 = -9.284378369405582e-211;
        bool r25571443 = r25571438 <= r25571442;
        double r25571444 = y;
        double r25571445 = x;
        double r25571446 = r25571445 * r25571445;
        double r25571447 = fma(r25571444, r25571444, r25571446);
        double r25571448 = fma(r25571438, r25571438, r25571447);
        double r25571449 = sqrt(r25571448);
        double r25571450 = -1.242347267252565e-225;
        bool r25571451 = r25571438 <= r25571450;
        double r25571452 = 5.001396056530514e+92;
        bool r25571453 = r25571438 <= r25571452;
        double r25571454 = r25571453 ? r25571449 : r25571438;
        double r25571455 = r25571451 ? r25571445 : r25571454;
        double r25571456 = r25571443 ? r25571449 : r25571455;
        double r25571457 = r25571440 ? r25571441 : r25571456;
        return r25571457;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.9
Herbie26.1
\[\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.722077932407333e+118

    1. Initial program 56.3

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

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

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

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

    if -1.722077932407333e+118 < z < -9.284378369405582e-211 or -1.242347267252565e-225 < z < 5.001396056530514e+92

    1. Initial program 29.7

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

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

    if -9.284378369405582e-211 < z < -1.242347267252565e-225

    1. Initial program 34.5

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

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

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

    if 5.001396056530514e+92 < z

    1. Initial program 54.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.722077932407332897914292742062327594505 \cdot 10^{118}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -9.284378369405581674635859258893819774821 \cdot 10^{-211}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;z \le -1.242347267252565005698596962527611511272 \cdot 10^{-225}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \le 5.001396056530513724211604175576022569559 \cdot 10^{92}:\\ \;\;\;\;\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 2019170 +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))))