Average Error: 37.6 → 25.6
Time: 5.7s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -5.607777933618982400249526983093859150209 \cdot 10^{130}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -8.416686067673292902204396712771845348161 \cdot 10^{-209}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;z \le -7.84491391625119897641243420224290104273 \cdot 10^{-234}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \le 5.773813342145198544396573435452629550473 \cdot 10^{107}:\\ \;\;\;\;\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 -5.607777933618982400249526983093859150209 \cdot 10^{130}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le -8.416686067673292902204396712771845348161 \cdot 10^{-209}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\

\mathbf{elif}\;z \le -7.84491391625119897641243420224290104273 \cdot 10^{-234}:\\
\;\;\;\;x\\

\mathbf{elif}\;z \le 5.773813342145198544396573435452629550473 \cdot 10^{107}:\\
\;\;\;\;\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 r23844319 = x;
        double r23844320 = r23844319 * r23844319;
        double r23844321 = y;
        double r23844322 = r23844321 * r23844321;
        double r23844323 = r23844320 + r23844322;
        double r23844324 = z;
        double r23844325 = r23844324 * r23844324;
        double r23844326 = r23844323 + r23844325;
        double r23844327 = sqrt(r23844326);
        return r23844327;
}

double f(double x, double y, double z) {
        double r23844328 = z;
        double r23844329 = -5.607777933618982e+130;
        bool r23844330 = r23844328 <= r23844329;
        double r23844331 = -r23844328;
        double r23844332 = -8.416686067673293e-209;
        bool r23844333 = r23844328 <= r23844332;
        double r23844334 = y;
        double r23844335 = x;
        double r23844336 = r23844335 * r23844335;
        double r23844337 = fma(r23844334, r23844334, r23844336);
        double r23844338 = fma(r23844328, r23844328, r23844337);
        double r23844339 = sqrt(r23844338);
        double r23844340 = -7.844913916251199e-234;
        bool r23844341 = r23844328 <= r23844340;
        double r23844342 = 5.7738133421451985e+107;
        bool r23844343 = r23844328 <= r23844342;
        double r23844344 = r23844343 ? r23844339 : r23844328;
        double r23844345 = r23844341 ? r23844335 : r23844344;
        double r23844346 = r23844333 ? r23844339 : r23844345;
        double r23844347 = r23844330 ? r23844331 : r23844346;
        return r23844347;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.6
Target25.3
Herbie25.6
\[\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 4 regimes
  2. if z < -5.607777933618982e+130

    1. Initial program 59.3

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

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

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

    if -5.607777933618982e+130 < z < -8.416686067673293e-209 or -7.844913916251199e-234 < z < 5.7738133421451985e+107

    1. Initial program 28.7

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

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

    if -8.416686067673293e-209 < z < -7.844913916251199e-234

    1. Initial program 32.0

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

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

      \[\leadsto \color{blue}{x}\]

    if 5.7738133421451985e+107 < z

    1. Initial program 55.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -5.607777933618982400249526983093859150209 \cdot 10^{130}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -8.416686067673292902204396712771845348161 \cdot 10^{-209}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{elif}\;z \le -7.84491391625119897641243420224290104273 \cdot 10^{-234}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \le 5.773813342145198544396573435452629550473 \cdot 10^{107}:\\ \;\;\;\;\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 2019171 +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))))