Average Error: 37.8 → 25.5
Time: 4.6s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -6.648716719064588023982305394807437197062 \cdot 10^{123}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 1.431866699493512367666877608258188926282 \cdot 10^{82}:\\ \;\;\;\;\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 -6.648716719064588023982305394807437197062 \cdot 10^{123}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 1.431866699493512367666877608258188926282 \cdot 10^{82}:\\
\;\;\;\;\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 r543083 = x;
        double r543084 = r543083 * r543083;
        double r543085 = y;
        double r543086 = r543085 * r543085;
        double r543087 = r543084 + r543086;
        double r543088 = z;
        double r543089 = r543088 * r543088;
        double r543090 = r543087 + r543089;
        double r543091 = sqrt(r543090);
        return r543091;
}

double f(double x, double y, double z) {
        double r543092 = z;
        double r543093 = -6.648716719064588e+123;
        bool r543094 = r543092 <= r543093;
        double r543095 = -r543092;
        double r543096 = 1.4318666994935124e+82;
        bool r543097 = r543092 <= r543096;
        double r543098 = x;
        double r543099 = y;
        double r543100 = r543099 * r543099;
        double r543101 = fma(r543098, r543098, r543100);
        double r543102 = fma(r543092, r543092, r543101);
        double r543103 = sqrt(r543102);
        double r543104 = r543097 ? r543103 : r543092;
        double r543105 = r543094 ? r543095 : r543104;
        return r543105;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.3
Herbie25.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 < -6.648716719064588e+123

    1. Initial program 57.6

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

      \[\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}{-1 \cdot z}\]
    4. Simplified15.9

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

    if -6.648716719064588e+123 < z < 1.4318666994935124e+82

    1. Initial program 29.2

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

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

    if 1.4318666994935124e+82 < z

    1. Initial program 52.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -6.648716719064588023982305394807437197062 \cdot 10^{123}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 1.431866699493512367666877608258188926282 \cdot 10^{82}:\\ \;\;\;\;\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 2019351 +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))))