Average Error: 38.3 → 25.8
Time: 6.2s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.801328071339668 \cdot 10^{105}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.42014104061242399 \cdot 10^{123}:\\ \;\;\;\;\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.801328071339668 \cdot 10^{105}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 2.42014104061242399 \cdot 10^{123}:\\
\;\;\;\;\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 r696634 = x;
        double r696635 = r696634 * r696634;
        double r696636 = y;
        double r696637 = r696636 * r696636;
        double r696638 = r696635 + r696637;
        double r696639 = z;
        double r696640 = r696639 * r696639;
        double r696641 = r696638 + r696640;
        double r696642 = sqrt(r696641);
        return r696642;
}

double f(double x, double y, double z) {
        double r696643 = z;
        double r696644 = -1.8013280713396678e+105;
        bool r696645 = r696643 <= r696644;
        double r696646 = -r696643;
        double r696647 = 2.420141040612424e+123;
        bool r696648 = r696643 <= r696647;
        double r696649 = x;
        double r696650 = y;
        double r696651 = r696650 * r696650;
        double r696652 = fma(r696649, r696649, r696651);
        double r696653 = fma(r696643, r696643, r696652);
        double r696654 = sqrt(r696653);
        double r696655 = r696648 ? r696654 : r696643;
        double r696656 = r696645 ? r696646 : r696655;
        return r696656;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.3
Target25.7
Herbie25.8
\[\begin{array}{l} \mathbf{if}\;z \lt -6.3964793941097758 \cdot 10^{136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \lt 7.3202936944041821 \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.8013280713396678e+105

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

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

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

    if -1.8013280713396678e+105 < z < 2.420141040612424e+123

    1. Initial program 29.9

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

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

    if 2.420141040612424e+123 < z

    1. Initial program 57.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.801328071339668 \cdot 10^{105}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.42014104061242399 \cdot 10^{123}:\\ \;\;\;\;\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 2020046 +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))))