Average Error: 35.3 → 24.3
Time: 5.5s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.6603608891381422 \cdot 10^{+118}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.4312554970818794 \cdot 10^{+131}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\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.6603608891381422 \cdot 10^{+118}:\\
\;\;\;\;-z\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r28652028 = x;
        double r28652029 = r28652028 * r28652028;
        double r28652030 = y;
        double r28652031 = r28652030 * r28652030;
        double r28652032 = r28652029 + r28652031;
        double r28652033 = z;
        double r28652034 = r28652033 * r28652033;
        double r28652035 = r28652032 + r28652034;
        double r28652036 = sqrt(r28652035);
        return r28652036;
}

double f(double x, double y, double z) {
        double r28652037 = z;
        double r28652038 = -1.6603608891381422e+118;
        bool r28652039 = r28652037 <= r28652038;
        double r28652040 = -r28652037;
        double r28652041 = 2.4312554970818794e+131;
        bool r28652042 = r28652037 <= r28652041;
        double r28652043 = y;
        double r28652044 = x;
        double r28652045 = r28652044 * r28652044;
        double r28652046 = fma(r28652043, r28652043, r28652045);
        double r28652047 = fma(r28652037, r28652037, r28652046);
        double r28652048 = sqrt(r28652047);
        double r28652049 = r28652042 ? r28652048 : r28652037;
        double r28652050 = r28652039 ? r28652040 : r28652049;
        return r28652050;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original35.3
Target24.3
Herbie24.3
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \lt 7.320293694404182 \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.6603608891381422e+118

    1. Initial program 52.4

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

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

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

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

    if -1.6603608891381422e+118 < z < 2.4312554970818794e+131

    1. Initial program 27.7

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

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

    if 2.4312554970818794e+131 < z

    1. Initial program 55.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.6603608891381422 \cdot 10^{+118}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 2.4312554970818794 \cdot 10^{+131}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array}\]

Reproduce

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