Average Error: 37.4 → 25.9
Time: 5.8s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -7.292754604329646237303955981134960903775 \cdot 10^{123}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le 212473366053330757829010616405658173440:\\ \;\;\;\;\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 -7.292754604329646237303955981134960903775 \cdot 10^{123}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le 212473366053330757829010616405658173440:\\
\;\;\;\;\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 r531025 = x;
        double r531026 = r531025 * r531025;
        double r531027 = y;
        double r531028 = r531027 * r531027;
        double r531029 = r531026 + r531028;
        double r531030 = z;
        double r531031 = r531030 * r531030;
        double r531032 = r531029 + r531031;
        double r531033 = sqrt(r531032);
        return r531033;
}

double f(double x, double y, double z) {
        double r531034 = z;
        double r531035 = -7.292754604329646e+123;
        bool r531036 = r531034 <= r531035;
        double r531037 = -r531034;
        double r531038 = 2.1247336605333076e+38;
        bool r531039 = r531034 <= r531038;
        double r531040 = x;
        double r531041 = y;
        double r531042 = r531041 * r531041;
        double r531043 = fma(r531040, r531040, r531042);
        double r531044 = fma(r531034, r531034, r531043);
        double r531045 = sqrt(r531044);
        double r531046 = r531039 ? r531045 : r531034;
        double r531047 = r531036 ? r531037 : r531046;
        return r531047;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.4
Target25.4
Herbie25.9
\[\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 < -7.292754604329646e+123

    1. Initial program 58.1

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

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

      \[\leadsto \color{blue}{-1 \cdot z}\]
    4. Simplified17.6

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

    if -7.292754604329646e+123 < z < 2.1247336605333076e+38

    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.1247336605333076e+38 < z

    1. Initial program 48.6

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

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

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