Average Error: 13.0 → 13.1
Time: 4.2s
Precision: binary64
\[10^{-150} < \left|x\right| \land \left|x\right| < 10^{+150}\]
\[\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 \leq -1.2651948696730856 \cdot 10^{-107}:\\ \;\;\;\;\sqrt{0.5 \cdot \frac{1 - \frac{x \cdot x}{x \cdot x + p \cdot \left(p \cdot 4\right)}}{1 - \frac{x}{\sqrt{x \cdot x + p \cdot \left(p \cdot 4\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{\frac{x}{\left|\sqrt[3]{x \cdot x + p \cdot \left(p \cdot 4\right)}\right|}}{\sqrt{\sqrt[3]{x \cdot x + p \cdot \left(p \cdot 4\right)}}}\right)}\\ \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 \leq -1.2651948696730856 \cdot 10^{-107}:\\
\;\;\;\;\sqrt{0.5 \cdot \frac{1 - \frac{x \cdot x}{x \cdot x + p \cdot \left(p \cdot 4\right)}}{1 - \frac{x}{\sqrt{x \cdot x + p \cdot \left(p \cdot 4\right)}}}}\\

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

\end{array}
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
(FPCore (p x)
 :precision binary64
 (if (<= x -1.2651948696730856e-107)
   (sqrt
    (*
     0.5
     (/
      (- 1.0 (/ (* x x) (+ (* x x) (* p (* p 4.0)))))
      (- 1.0 (/ x (sqrt (+ (* x x) (* p (* p 4.0)))))))))
   (sqrt
    (*
     0.5
     (+
      1.0
      (/
       (/ x (fabs (cbrt (+ (* x x) (* p (* p 4.0))))))
       (sqrt (cbrt (+ (* x x) (* p (* p 4.0)))))))))))
double code(double p, double x) {
	return sqrt(0.5 * (1.0 + (x / sqrt(((4.0 * p) * p) + (x * x)))));
}
double code(double p, double x) {
	double tmp;
	if (x <= -1.2651948696730856e-107) {
		tmp = sqrt(0.5 * ((1.0 - ((x * x) / ((x * x) + (p * (p * 4.0))))) / (1.0 - (x / sqrt((x * x) + (p * (p * 4.0)))))));
	} else {
		tmp = sqrt(0.5 * (1.0 + ((x / fabs(cbrt((x * x) + (p * (p * 4.0))))) / sqrt(cbrt((x * x) + (p * (p * 4.0)))))));
	}
	return tmp;
}

Error

Bits error versus p

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original13.0
Target13.0
Herbie13.1
\[\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.26519486967308557e-107

    1. Initial program 27.5

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\]
    2. Using strategy rm
    3. Applied flip-+_binary6427.5

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

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

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

    if -1.26519486967308557e-107 < x

    1. Initial program 2.1

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\]
    2. Using strategy rm
    3. Applied add-cube-cbrt_binary642.2

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\color{blue}{\left(\sqrt[3]{\left(4 \cdot p\right) \cdot p + x \cdot x} \cdot \sqrt[3]{\left(4 \cdot p\right) \cdot p + x \cdot x}\right) \cdot \sqrt[3]{\left(4 \cdot p\right) \cdot p + x \cdot x}}}}\right)}\]
    4. Applied sqrt-prod_binary642.2

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\sqrt{\sqrt[3]{\left(4 \cdot p\right) \cdot p + x \cdot x} \cdot \sqrt[3]{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot \sqrt{\sqrt[3]{\left(4 \cdot p\right) \cdot p + x \cdot x}}}}\right)}\]
    5. Applied associate-/r*_binary642.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.2651948696730856 \cdot 10^{-107}:\\ \;\;\;\;\sqrt{0.5 \cdot \frac{1 - \frac{x \cdot x}{x \cdot x + p \cdot \left(p \cdot 4\right)}}{1 - \frac{x}{\sqrt{x \cdot x + p \cdot \left(p \cdot 4\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{\frac{x}{\left|\sqrt[3]{x \cdot x + p \cdot \left(p \cdot 4\right)}\right|}}{\sqrt{\sqrt[3]{x \cdot x + p \cdot \left(p \cdot 4\right)}}}\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2020224 
(FPCore (p x)
  :name "Given's Rotation SVD example"
  :precision binary64
  :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))))))))