Average Error: 38.2 → 25.7
Time: 11.0s
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(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.565989759358971209149702534705727090611 \cdot 10^{153}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 2.299090396283016055840928250753650963956 \cdot 10^{118}:\\
\;\;\;\;\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 r26338859 = x;
        double r26338860 = r26338859 * r26338859;
        double r26338861 = y;
        double r26338862 = r26338861 * r26338861;
        double r26338863 = r26338860 + r26338862;
        double r26338864 = z;
        double r26338865 = r26338864 * r26338864;
        double r26338866 = r26338863 + r26338865;
        double r26338867 = sqrt(r26338866);
        return r26338867;
}

double f(double x, double y, double z) {
        double r26338868 = z;
        double r26338869 = -1.5659897593589712e+153;
        bool r26338870 = r26338868 <= r26338869;
        double r26338871 = -r26338868;
        double r26338872 = 2.299090396283016e+118;
        bool r26338873 = r26338868 <= r26338872;
        double r26338874 = y;
        double r26338875 = x;
        double r26338876 = r26338875 * r26338875;
        double r26338877 = fma(r26338874, r26338874, r26338876);
        double r26338878 = fma(r26338868, r26338868, r26338877);
        double r26338879 = sqrt(r26338878);
        double r26338880 = r26338873 ? r26338879 : r26338868;
        double r26338881 = r26338870 ? r26338871 : r26338880;
        return r26338881;
}

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(y, y, x \cdot x\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(y, y, x \cdot x\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(y, y, x \cdot x\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(y, y, x \cdot x\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))))