Average Error: 37.7 → 25.7
Time: 6.7s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.015818798307589698471256123888820846637 \cdot 10^{101}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 6.823938244536720630133807708555442078876 \cdot 10^{139}:\\ \;\;\;\;\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 -2.015818798307589698471256123888820846637 \cdot 10^{101}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 6.823938244536720630133807708555442078876 \cdot 10^{139}:\\
\;\;\;\;\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 r1400995 = x;
        double r1400996 = r1400995 * r1400995;
        double r1400997 = y;
        double r1400998 = r1400997 * r1400997;
        double r1400999 = r1400996 + r1400998;
        double r1401000 = z;
        double r1401001 = r1401000 * r1401000;
        double r1401002 = r1400999 + r1401001;
        double r1401003 = sqrt(r1401002);
        return r1401003;
}

double f(double x, double y, double z) {
        double r1401004 = z;
        double r1401005 = -2.0158187983075897e+101;
        bool r1401006 = r1401004 <= r1401005;
        double r1401007 = -r1401004;
        double r1401008 = 6.823938244536721e+139;
        bool r1401009 = r1401004 <= r1401008;
        double r1401010 = y;
        double r1401011 = x;
        double r1401012 = r1401011 * r1401011;
        double r1401013 = fma(r1401010, r1401010, r1401012);
        double r1401014 = fma(r1401004, r1401004, r1401013);
        double r1401015 = sqrt(r1401014);
        double r1401016 = r1401009 ? r1401015 : r1401004;
        double r1401017 = r1401006 ? r1401007 : r1401016;
        return r1401017;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.6
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 < -2.0158187983075897e+101

    1. Initial program 54.5

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

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

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

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

    if -2.0158187983075897e+101 < z < 6.823938244536721e+139

    1. Initial program 29.3

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

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

    if 6.823938244536721e+139 < z

    1. Initial program 61.7

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.015818798307589698471256123888820846637 \cdot 10^{101}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 6.823938244536720630133807708555442078876 \cdot 10^{139}:\\ \;\;\;\;\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 2019194 +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))))