Average Error: 38.2 → 25.7
Time: 9.8s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.565989759358971209149702534705727090611 \cdot 10^{153}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.299090396283016055840928250753650963956 \cdot 10^{118}:\\ \;\;\;\;\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.565989759358971209149702534705727090611 \cdot 10^{153}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 2.299090396283016055840928250753650963956 \cdot 10^{118}:\\
\;\;\;\;\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 r40684492 = x;
        double r40684493 = r40684492 * r40684492;
        double r40684494 = y;
        double r40684495 = r40684494 * r40684494;
        double r40684496 = r40684493 + r40684495;
        double r40684497 = z;
        double r40684498 = r40684497 * r40684497;
        double r40684499 = r40684496 + r40684498;
        double r40684500 = sqrt(r40684499);
        return r40684500;
}

double f(double x, double y, double z) {
        double r40684501 = z;
        double r40684502 = -1.5659897593589712e+153;
        bool r40684503 = r40684501 <= r40684502;
        double r40684504 = -r40684501;
        double r40684505 = 2.299090396283016e+118;
        bool r40684506 = r40684501 <= r40684505;
        double r40684507 = x;
        double r40684508 = y;
        double r40684509 = r40684508 * r40684508;
        double r40684510 = fma(r40684507, r40684507, r40684509);
        double r40684511 = fma(r40684501, r40684501, r40684510);
        double r40684512 = sqrt(r40684511);
        double r40684513 = r40684506 ? r40684512 : r40684501;
        double r40684514 = r40684503 ? r40684504 : r40684513;
        return r40684514;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.7
Herbie25.7
\[\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 < -1.5659897593589712e+153

    1. Initial program 63.8

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

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

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

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

    if -1.5659897593589712e+153 < z < 2.299090396283016e+118

    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 2.299090396283016e+118 < z

    1. Initial program 56.4

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.565989759358971209149702534705727090611 \cdot 10^{153}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.299090396283016055840928250753650963956 \cdot 10^{118}:\\ \;\;\;\;\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 2019174 +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))))