Average Error: 38.4 → 26.0
Time: 5.0s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.413158811260579489343167230304798124904 \cdot 10^{73}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 1.021837622190480599653779155515833653726 \cdot 10^{112}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\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.413158811260579489343167230304798124904 \cdot 10^{73}:\\
\;\;\;\;-z\\

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

\mathbf{else}:\\
\;\;\;\;z\\

\end{array}
double f(double x, double y, double z) {
        double r415247 = x;
        double r415248 = r415247 * r415247;
        double r415249 = y;
        double r415250 = r415249 * r415249;
        double r415251 = r415248 + r415250;
        double r415252 = z;
        double r415253 = r415252 * r415252;
        double r415254 = r415251 + r415253;
        double r415255 = sqrt(r415254);
        return r415255;
}

double f(double x, double y, double z) {
        double r415256 = z;
        double r415257 = -1.4131588112605795e+73;
        bool r415258 = r415256 <= r415257;
        double r415259 = -r415256;
        double r415260 = 1.0218376221904806e+112;
        bool r415261 = r415256 <= r415260;
        double r415262 = x;
        double r415263 = y;
        double r415264 = r415263 * r415263;
        double r415265 = fma(r415262, r415262, r415264);
        double r415266 = fma(r415256, r415256, r415265);
        double r415267 = sqrt(r415266);
        double r415268 = r415261 ? r415267 : r415256;
        double r415269 = r415258 ? r415259 : r415268;
        return r415269;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.4
Target25.8
Herbie26.0
\[\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.4131588112605795e+73

    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(x, x, y \cdot y\right)\right)}}\]
    3. Taylor expanded around -inf 20.1

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

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

    if -1.4131588112605795e+73 < z < 1.0218376221904806e+112

    1. Initial program 29.8

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

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

    if 1.0218376221904806e+112 < z

    1. Initial program 56.7

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.413158811260579489343167230304798124904 \cdot 10^{73}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 1.021837622190480599653779155515833653726 \cdot 10^{112}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]

Reproduce

herbie shell --seed 2019322 +o rules:numerics
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
  :precision binary64

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