Average Error: 37.7 → 26.1
Time: 36.0s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;y \le -6.210791644306184960357743278744862344801 \cdot 10^{147}:\\ \;\;\;\;-y\\ \mathbf{elif}\;y \le -8.594175512162317958857032329739142749821 \cdot 10^{-256}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\ \mathbf{elif}\;y \le 5.255127935317791255390827891491265215058 \cdot 10^{-272}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \le 1.561976505627028795179813773971319961479 \cdot 10^{137}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;y\\ \end{array}\]
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}
\begin{array}{l}
\mathbf{if}\;y \le -6.210791644306184960357743278744862344801 \cdot 10^{147}:\\
\;\;\;\;-y\\

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

\mathbf{elif}\;y \le 5.255127935317791255390827891491265215058 \cdot 10^{-272}:\\
\;\;\;\;x\\

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

\mathbf{else}:\\
\;\;\;\;y\\

\end{array}
double f(double x, double y, double z) {
        double r29843385 = x;
        double r29843386 = r29843385 * r29843385;
        double r29843387 = y;
        double r29843388 = r29843387 * r29843387;
        double r29843389 = r29843386 + r29843388;
        double r29843390 = z;
        double r29843391 = r29843390 * r29843390;
        double r29843392 = r29843389 + r29843391;
        double r29843393 = sqrt(r29843392);
        return r29843393;
}

double f(double x, double y, double z) {
        double r29843394 = y;
        double r29843395 = -6.210791644306185e+147;
        bool r29843396 = r29843394 <= r29843395;
        double r29843397 = -r29843394;
        double r29843398 = -8.594175512162318e-256;
        bool r29843399 = r29843394 <= r29843398;
        double r29843400 = z;
        double r29843401 = x;
        double r29843402 = r29843401 * r29843401;
        double r29843403 = fma(r29843400, r29843400, r29843402);
        double r29843404 = fma(r29843394, r29843394, r29843403);
        double r29843405 = sqrt(r29843404);
        double r29843406 = 5.255127935317791e-272;
        bool r29843407 = r29843394 <= r29843406;
        double r29843408 = 1.5619765056270288e+137;
        bool r29843409 = r29843394 <= r29843408;
        double r29843410 = r29843409 ? r29843405 : r29843394;
        double r29843411 = r29843407 ? r29843401 : r29843410;
        double r29843412 = r29843399 ? r29843405 : r29843411;
        double r29843413 = r29843396 ? r29843397 : r29843412;
        return r29843413;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.8
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 4 regimes
  2. if y < -6.210791644306185e+147

    1. Initial program 62.6

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

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

      \[\leadsto \color{blue}{-1 \cdot y}\]
    4. Simplified14.7

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

    if -6.210791644306185e+147 < y < -8.594175512162318e-256 or 5.255127935317791e-272 < y < 1.5619765056270288e+137

    1. Initial program 28.5

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

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

    if -8.594175512162318e-256 < y < 5.255127935317791e-272

    1. Initial program 32.6

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

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

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

    if 1.5619765056270288e+137 < y

    1. Initial program 60.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -6.210791644306184960357743278744862344801 \cdot 10^{147}:\\ \;\;\;\;-y\\ \mathbf{elif}\;y \le -8.594175512162317958857032329739142749821 \cdot 10^{-256}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\ \mathbf{elif}\;y \le 5.255127935317791255390827891491265215058 \cdot 10^{-272}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \le 1.561976505627028795179813773971319961479 \cdot 10^{137}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;y\\ \end{array}\]

Reproduce

herbie shell --seed 2019200 +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))))