Average Error: 37.7 → 26.1
Time: 5.4s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.029763652061646458925883779889462879994 \cdot 10^{81}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 1.250292644869176057387329263248829167286 \cdot 10^{85}:\\ \;\;\;\;\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.029763652061646458925883779889462879994 \cdot 10^{81}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 1.250292644869176057387329263248829167286 \cdot 10^{85}:\\
\;\;\;\;\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 r26952523 = x;
        double r26952524 = r26952523 * r26952523;
        double r26952525 = y;
        double r26952526 = r26952525 * r26952525;
        double r26952527 = r26952524 + r26952526;
        double r26952528 = z;
        double r26952529 = r26952528 * r26952528;
        double r26952530 = r26952527 + r26952529;
        double r26952531 = sqrt(r26952530);
        return r26952531;
}

double f(double x, double y, double z) {
        double r26952532 = z;
        double r26952533 = -1.0297636520616465e+81;
        bool r26952534 = r26952532 <= r26952533;
        double r26952535 = -r26952532;
        double r26952536 = 1.250292644869176e+85;
        bool r26952537 = r26952532 <= r26952536;
        double r26952538 = y;
        double r26952539 = x;
        double r26952540 = r26952539 * r26952539;
        double r26952541 = fma(r26952538, r26952538, r26952540);
        double r26952542 = fma(r26952532, r26952532, r26952541);
        double r26952543 = sqrt(r26952542);
        double r26952544 = r26952537 ? r26952543 : r26952532;
        double r26952545 = r26952534 ? r26952535 : r26952544;
        return r26952545;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.6
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 3 regimes
  2. if z < -1.0297636520616465e+81

    1. Initial program 52.1

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

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

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

    if -1.0297636520616465e+81 < z < 1.250292644869176e+85

    1. Initial program 29.1

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

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

    if 1.250292644869176e+85 < z

    1. Initial program 54.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.029763652061646458925883779889462879994 \cdot 10^{81}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 1.250292644869176057387329263248829167286 \cdot 10^{85}:\\ \;\;\;\;\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 2019192 +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))))