Average Error: 38.2 → 25.7
Time: 9.6s
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 r470782 = x;
        double r470783 = r470782 * r470782;
        double r470784 = y;
        double r470785 = r470784 * r470784;
        double r470786 = r470783 + r470785;
        double r470787 = z;
        double r470788 = r470787 * r470787;
        double r470789 = r470786 + r470788;
        double r470790 = sqrt(r470789);
        return r470790;
}

double f(double x, double y, double z) {
        double r470791 = z;
        double r470792 = -1.5659897593589712e+153;
        bool r470793 = r470791 <= r470792;
        double r470794 = -r470791;
        double r470795 = 2.299090396283016e+118;
        bool r470796 = r470791 <= r470795;
        double r470797 = y;
        double r470798 = x;
        double r470799 = r470798 * r470798;
        double r470800 = fma(r470797, r470797, r470799);
        double r470801 = fma(r470791, r470791, r470800);
        double r470802 = sqrt(r470801);
        double r470803 = r470796 ? r470802 : r470791;
        double r470804 = r470793 ? r470794 : r470803;
        return r470804;
}

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