Average Error: 38.1 → 25.9
Time: 6.4s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.917725958087788011890119017695135051486 \cdot 10^{80}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 8.320790080049101987450207216852360941277 \cdot 10^{126}:\\ \;\;\;\;\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 -3.917725958087788011890119017695135051486 \cdot 10^{80}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 8.320790080049101987450207216852360941277 \cdot 10^{126}:\\
\;\;\;\;\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 r24008048 = x;
        double r24008049 = r24008048 * r24008048;
        double r24008050 = y;
        double r24008051 = r24008050 * r24008050;
        double r24008052 = r24008049 + r24008051;
        double r24008053 = z;
        double r24008054 = r24008053 * r24008053;
        double r24008055 = r24008052 + r24008054;
        double r24008056 = sqrt(r24008055);
        return r24008056;
}

double f(double x, double y, double z) {
        double r24008057 = z;
        double r24008058 = -3.917725958087788e+80;
        bool r24008059 = r24008057 <= r24008058;
        double r24008060 = -r24008057;
        double r24008061 = 8.320790080049102e+126;
        bool r24008062 = r24008057 <= r24008061;
        double r24008063 = y;
        double r24008064 = x;
        double r24008065 = r24008064 * r24008064;
        double r24008066 = fma(r24008063, r24008063, r24008065);
        double r24008067 = fma(r24008057, r24008057, r24008066);
        double r24008068 = sqrt(r24008067);
        double r24008069 = r24008062 ? r24008068 : r24008057;
        double r24008070 = r24008059 ? r24008060 : r24008069;
        return r24008070;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.1
Target25.7
Herbie25.9
\[\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 < -3.917725958087788e+80

    1. Initial program 53.2

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

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

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

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

    if -3.917725958087788e+80 < z < 8.320790080049102e+126

    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 8.320790080049102e+126 < z

    1. Initial program 58.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -3.917725958087788011890119017695135051486 \cdot 10^{80}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 8.320790080049101987450207216852360941277 \cdot 10^{126}:\\ \;\;\;\;\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 2019169 +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))))