Average Error: 13.5 → 13.5
Time: 18.0s
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 -1.409903055203330977940093280577253862875 \cdot 10^{-126}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(-1, 1, \frac{x \cdot x}{\mathsf{fma}\left({p}^{2}, 4, x \cdot x\right)}\right)}{\frac{x}{\sqrt{\mathsf{fma}\left(4 \cdot p, p, x \cdot x\right)}} - 1} \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, \frac{1}{\sqrt{\mathsf{fma}\left(4 \cdot p, p, x \cdot x\right)}}, 1\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 -1.409903055203330977940093280577253862875 \cdot 10^{-126}:\\
\;\;\;\;\sqrt{\frac{\mathsf{fma}\left(-1, 1, \frac{x \cdot x}{\mathsf{fma}\left({p}^{2}, 4, x \cdot x\right)}\right)}{\frac{x}{\sqrt{\mathsf{fma}\left(4 \cdot p, p, x \cdot x\right)}} - 1} \cdot 0.5}\\

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

\end{array}
double f(double p, double x) {
        double r209423 = 0.5;
        double r209424 = 1.0;
        double r209425 = x;
        double r209426 = 4.0;
        double r209427 = p;
        double r209428 = r209426 * r209427;
        double r209429 = r209428 * r209427;
        double r209430 = r209425 * r209425;
        double r209431 = r209429 + r209430;
        double r209432 = sqrt(r209431);
        double r209433 = r209425 / r209432;
        double r209434 = r209424 + r209433;
        double r209435 = r209423 * r209434;
        double r209436 = sqrt(r209435);
        return r209436;
}

double f(double p, double x) {
        double r209437 = x;
        double r209438 = -1.409903055203331e-126;
        bool r209439 = r209437 <= r209438;
        double r209440 = 1.0;
        double r209441 = -r209440;
        double r209442 = r209437 * r209437;
        double r209443 = p;
        double r209444 = 2.0;
        double r209445 = pow(r209443, r209444);
        double r209446 = 4.0;
        double r209447 = fma(r209445, r209446, r209442);
        double r209448 = r209442 / r209447;
        double r209449 = fma(r209441, r209440, r209448);
        double r209450 = r209446 * r209443;
        double r209451 = fma(r209450, r209443, r209442);
        double r209452 = sqrt(r209451);
        double r209453 = r209437 / r209452;
        double r209454 = r209453 - r209440;
        double r209455 = r209449 / r209454;
        double r209456 = 0.5;
        double r209457 = r209455 * r209456;
        double r209458 = sqrt(r209457);
        double r209459 = 1.0;
        double r209460 = r209459 / r209452;
        double r209461 = fma(r209437, r209460, r209440);
        double r209462 = r209461 * r209456;
        double r209463 = sqrt(r209462);
        double r209464 = r209439 ? r209458 : r209463;
        return r209464;
}

Error

Bits error versus p

Bits error versus x

Target

Original13.5
Target13.5
Herbie13.5
\[\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 < -1.409903055203331e-126

    1. Initial program 27.8

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

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

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

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

    if -1.409903055203331e-126 < x

    1. Initial program 1.1

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

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

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

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

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

Reproduce

herbie shell --seed 2019305 +o rules:numerics
(FPCore (p x)
  :name "Given's Rotation SVD example"
  :precision binary64
  :pre (< 1.00000000000000001e-150 (fabs x) 9.99999999999999981e149)

  :herbie-target
  (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1 (/ (* 2 p) x)))))

  (sqrt (* 0.5 (+ 1 (/ x (sqrt (+ (* (* 4 p) p) (* x x))))))))