Average Error: 13.3 → 13.3
Time: 12.2s
Precision: 64
\[1.000000000000000006295358232172963997211 \cdot 10^{-150} \lt \left|x\right| \lt 9.999999999999999808355961724373745905731 \cdot 10^{149}\]
\[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\]
\[\begin{array}{l} \mathbf{if}\;x \le -2.325695664003728561652602965367804820151 \cdot 10^{-106}:\\ \;\;\;\;\frac{\sqrt{\left(1 \cdot 1 - \frac{x \cdot x}{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}\right) \cdot 0.5}}{\sqrt{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt{\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \left(\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}}\right)}}\right) \cdot 0.5}\\ \end{array}\]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\begin{array}{l}
\mathbf{if}\;x \le -2.325695664003728561652602965367804820151 \cdot 10^{-106}:\\
\;\;\;\;\frac{\sqrt{\left(1 \cdot 1 - \frac{x \cdot x}{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}\right) \cdot 0.5}}{\sqrt{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt{\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \left(\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}}\right)}}\right) \cdot 0.5}\\

\end{array}
double f(double p, double x) {
        double r372760 = 0.5;
        double r372761 = 1.0;
        double r372762 = x;
        double r372763 = 4.0;
        double r372764 = p;
        double r372765 = r372763 * r372764;
        double r372766 = r372765 * r372764;
        double r372767 = r372762 * r372762;
        double r372768 = r372766 + r372767;
        double r372769 = sqrt(r372768);
        double r372770 = r372762 / r372769;
        double r372771 = r372761 + r372770;
        double r372772 = r372760 * r372771;
        double r372773 = sqrt(r372772);
        return r372773;
}

double f(double p, double x) {
        double r372774 = x;
        double r372775 = -2.3256956640037286e-106;
        bool r372776 = r372774 <= r372775;
        double r372777 = 1.0;
        double r372778 = r372777 * r372777;
        double r372779 = r372774 * r372774;
        double r372780 = p;
        double r372781 = r372780 * r372780;
        double r372782 = 4.0;
        double r372783 = fma(r372781, r372782, r372779);
        double r372784 = r372779 / r372783;
        double r372785 = r372778 - r372784;
        double r372786 = 0.5;
        double r372787 = r372785 * r372786;
        double r372788 = sqrt(r372787);
        double r372789 = sqrt(r372783);
        double r372790 = r372774 / r372789;
        double r372791 = r372777 - r372790;
        double r372792 = sqrt(r372791);
        double r372793 = r372788 / r372792;
        double r372794 = sqrt(r372789);
        double r372795 = r372780 * r372782;
        double r372796 = fma(r372780, r372795, r372779);
        double r372797 = sqrt(r372796);
        double r372798 = cbrt(r372797);
        double r372799 = r372798 * r372798;
        double r372800 = r372798 * r372799;
        double r372801 = sqrt(r372800);
        double r372802 = r372794 * r372801;
        double r372803 = r372774 / r372802;
        double r372804 = r372777 + r372803;
        double r372805 = r372804 * r372786;
        double r372806 = sqrt(r372805);
        double r372807 = r372776 ? r372793 : r372806;
        return r372807;
}

Error

Bits error versus p

Bits error versus x

Target

Original13.3
Target13.3
Herbie13.3
\[\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}\]

Derivation

  1. Split input into 2 regimes
  2. if x < -2.3256956640037286e-106

    1. Initial program 28.0

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\]
    2. Simplified28.0

      \[\leadsto \color{blue}{\sqrt{\left(1 + \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}\right) \cdot 0.5}}\]
    3. Using strategy rm
    4. Applied flip-+28.0

      \[\leadsto \sqrt{\color{blue}{\frac{1 \cdot 1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}} \cdot \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}} \cdot 0.5}\]
    5. Applied associate-*l/28.0

      \[\leadsto \sqrt{\color{blue}{\frac{\left(1 \cdot 1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}} \cdot \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}\right) \cdot 0.5}{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}}}\]
    6. Applied sqrt-div28.1

      \[\leadsto \color{blue}{\frac{\sqrt{\left(1 \cdot 1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}} \cdot \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}\right) \cdot 0.5}}{\sqrt{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}}}\]
    7. Simplified28.0

      \[\leadsto \frac{\color{blue}{\sqrt{\left(1 \cdot 1 - \frac{x \cdot x}{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}\right) \cdot 0.5}}}{\sqrt{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}}\]
    8. Simplified28.0

      \[\leadsto \frac{\sqrt{\left(1 \cdot 1 - \frac{x \cdot x}{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}\right) \cdot 0.5}}{\color{blue}{\sqrt{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}}\]

    if -2.3256956640037286e-106 < x

    1. Initial program 2.3

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\]
    2. Simplified2.3

      \[\leadsto \color{blue}{\sqrt{\left(1 + \frac{x}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}\right) \cdot 0.5}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt2.3

      \[\leadsto \sqrt{\left(1 + \frac{x}{\sqrt{\color{blue}{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)} \cdot \sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}}\right) \cdot 0.5}\]
    5. Applied sqrt-prod2.4

      \[\leadsto \sqrt{\left(1 + \frac{x}{\color{blue}{\sqrt{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}} \cdot \sqrt{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}}\right) \cdot 0.5}\]
    6. Simplified2.4

      \[\leadsto \sqrt{\left(1 + \frac{x}{\color{blue}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}} \cdot \sqrt{\sqrt{\mathsf{fma}\left(p, 4 \cdot p, x \cdot x\right)}}}\right) \cdot 0.5}\]
    7. Simplified2.4

      \[\leadsto \sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \color{blue}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}\right) \cdot 0.5}\]
    8. Using strategy rm
    9. Applied add-cube-cbrt2.4

      \[\leadsto \sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt{\color{blue}{\left(\sqrt[3]{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}\right) \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}}\right) \cdot 0.5}\]
    10. Simplified2.4

      \[\leadsto \sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt{\color{blue}{\left(\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}}\right)} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}\right) \cdot 0.5}\]
    11. Simplified2.4

      \[\leadsto \sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt{\left(\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}}\right) \cdot \color{blue}{\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}}}}}\right) \cdot 0.5}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification13.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -2.325695664003728561652602965367804820151 \cdot 10^{-106}:\\ \;\;\;\;\frac{\sqrt{\left(1 \cdot 1 - \frac{x \cdot x}{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}\right) \cdot 0.5}}{\sqrt{1 - \frac{x}{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(1 + \frac{x}{\sqrt{\sqrt{\mathsf{fma}\left(p \cdot p, 4, x \cdot x\right)}} \cdot \sqrt{\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \left(\sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}} \cdot \sqrt[3]{\sqrt{\mathsf{fma}\left(p, p \cdot 4, x \cdot x\right)}}\right)}}\right) \cdot 0.5}\\ \end{array}\]

Reproduce

herbie shell --seed 2019174 +o rules:numerics
(FPCore (p x)
  :name "Given's Rotation SVD example"
  :pre (< 1e-150 (fabs x) 1e+150)

  :herbie-target
  (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x)))))

  (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))