Average Error: 38.0 → 26.0
Time: 14.5s
Precision: 64
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.668643408655414716179248580097977706017 \cdot 10^{122}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -2.672713142726911514272253621456748121583 \cdot 10^{-197}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -2.003356476000291454828377115726675914901 \cdot 10^{-248}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \le 1.313897985272073697158734222693046230491 \cdot 10^{67}:\\ \;\;\;\;\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 -2.668643408655414716179248580097977706017 \cdot 10^{122}:\\
\;\;\;\;-z\\

\mathbf{elif}\;z \le -2.672713142726911514272253621456748121583 \cdot 10^{-197}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\

\mathbf{elif}\;z \le -2.003356476000291454828377115726675914901 \cdot 10^{-248}:\\
\;\;\;\;y\\

\mathbf{elif}\;z \le 1.313897985272073697158734222693046230491 \cdot 10^{67}:\\
\;\;\;\;\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 r451630 = x;
        double r451631 = r451630 * r451630;
        double r451632 = y;
        double r451633 = r451632 * r451632;
        double r451634 = r451631 + r451633;
        double r451635 = z;
        double r451636 = r451635 * r451635;
        double r451637 = r451634 + r451636;
        double r451638 = sqrt(r451637);
        return r451638;
}

double f(double x, double y, double z) {
        double r451639 = z;
        double r451640 = -2.6686434086554147e+122;
        bool r451641 = r451639 <= r451640;
        double r451642 = -r451639;
        double r451643 = -2.6727131427269115e-197;
        bool r451644 = r451639 <= r451643;
        double r451645 = x;
        double r451646 = y;
        double r451647 = r451646 * r451646;
        double r451648 = fma(r451645, r451645, r451647);
        double r451649 = fma(r451639, r451639, r451648);
        double r451650 = sqrt(r451649);
        double r451651 = -2.0033564760002915e-248;
        bool r451652 = r451639 <= r451651;
        double r451653 = 1.3138979852720737e+67;
        bool r451654 = r451639 <= r451653;
        double r451655 = r451654 ? r451650 : r451639;
        double r451656 = r451652 ? r451646 : r451655;
        double r451657 = r451644 ? r451650 : r451656;
        double r451658 = r451641 ? r451642 : r451657;
        return r451658;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.0
Target25.1
Herbie26.0
\[\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 4 regimes
  2. if z < -2.6686434086554147e+122

    1. Initial program 58.3

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

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

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

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

    if -2.6686434086554147e+122 < z < -2.6727131427269115e-197 or -2.0033564760002915e-248 < z < 1.3138979852720737e+67

    1. Initial program 28.9

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

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

    if -2.6727131427269115e-197 < z < -2.0033564760002915e-248

    1. Initial program 30.9

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

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

      \[\leadsto \color{blue}{y}\]

    if 1.3138979852720737e+67 < z

    1. Initial program 52.2

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

      \[\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.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.668643408655414716179248580097977706017 \cdot 10^{122}:\\ \;\;\;\;-z\\ \mathbf{elif}\;z \le -2.672713142726911514272253621456748121583 \cdot 10^{-197}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -2.003356476000291454828377115726675914901 \cdot 10^{-248}:\\ \;\;\;\;y\\ \mathbf{elif}\;z \le 1.313897985272073697158734222693046230491 \cdot 10^{67}:\\ \;\;\;\;\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 2019305 +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))))