Average Error: 37.6 → 26.5
Time: 5.3s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -5.634597593114443456705073370449663857349 \cdot 10^{55}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 264911678537884152394416128:\\ \;\;\;\;\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 -5.634597593114443456705073370449663857349 \cdot 10^{55}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 264911678537884152394416128:\\
\;\;\;\;\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 r373545 = x;
        double r373546 = r373545 * r373545;
        double r373547 = y;
        double r373548 = r373547 * r373547;
        double r373549 = r373546 + r373548;
        double r373550 = z;
        double r373551 = r373550 * r373550;
        double r373552 = r373549 + r373551;
        double r373553 = sqrt(r373552);
        return r373553;
}

double f(double x, double y, double z) {
        double r373554 = z;
        double r373555 = -5.634597593114443e+55;
        bool r373556 = r373554 <= r373555;
        double r373557 = -r373554;
        double r373558 = 2.6491167853788415e+26;
        bool r373559 = r373554 <= r373558;
        double r373560 = x;
        double r373561 = y;
        double r373562 = r373561 * r373561;
        double r373563 = fma(r373560, r373560, r373562);
        double r373564 = fma(r373554, r373554, r373563);
        double r373565 = sqrt(r373564);
        double r373566 = r373559 ? r373565 : r373554;
        double r373567 = r373556 ? r373557 : r373566;
        return r373567;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.6
Target25.3
Herbie26.5
\[\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 < -5.634597593114443e+55

    1. Initial program 50.5

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

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

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

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

    if -5.634597593114443e+55 < z < 2.6491167853788415e+26

    1. Initial program 29.0

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

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

    if 2.6491167853788415e+26 < z

    1. Initial program 48.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -5.634597593114443456705073370449663857349 \cdot 10^{55}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 264911678537884152394416128:\\ \;\;\;\;\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 2019235 +o rules:numerics
(FPCore (x y z)
  :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
  :precision binary64

  :herbie-target
  (if (< z -6.3964793941097758e136) (- z) (if (< z 7.3202936944041821e117) (sqrt (+ (+ (* z z) (* x x)) (* y y))) z))

  (sqrt (+ (+ (* x x) (* y y)) (* z z))))