Average Error: 13.2 → 14.6
Time: 7.8s
Precision: binary64
Cost: 14209
\[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 -5.541137639902676 \cdot 10^{+37}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{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 -5.541137639902676 \cdot 10^{+37}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{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 -5.541137639902676e+37)
   (sqrt (* 0.5 (* 2.0 (/ p (/ (* x x) p)))))
   (sqrt (* 0.5 (+ 1.0 (* x (/ 1.0 (sqrt (+ (* 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 <= -5.541137639902676e+37) {
		tmp = sqrt(0.5 * (2.0 * (p / ((x * x) / p))));
	} else {
		tmp = sqrt(0.5 * (1.0 + (x * (1.0 / sqrt((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.2
Target13.2
Herbie14.6
\[\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}\]

Alternatives

Alternative 1
Error14.5
Cost14081
\[\begin{array}{l} \mathbf{if}\;x \leq -3.4879622713358363 \cdot 10^{+40}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{x \cdot x + p \cdot \left(p \cdot 4\right)}}\right)}\\ \end{array}\]
Alternative 2
Error15.3
Cost7681
\[\begin{array}{l} \mathbf{if}\;x \leq -2.23931280946362 \cdot 10^{+36}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{x + 2 \cdot \frac{p \cdot p}{x}}\right)}\\ \end{array}\]
Alternative 3
Error20.1
Cost7297
\[\begin{array}{l} \mathbf{if}\;p \leq -5.81188855795806 \cdot 10^{+20}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{p \cdot -2}\right)}\\ \mathbf{elif}\;p \leq 7.734268629689336 \cdot 10^{-22}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array}\]
Alternative 4
Error19.9
Cost6792
\[\begin{array}{l} \mathbf{if}\;p \leq -4.411606632062893 \cdot 10^{-38} \lor \neg \left(p \leq 5.7469143280990995 \cdot 10^{-18}\right):\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array}\]
Alternative 5
Error39.5
Cost385
\[\begin{array}{l} \mathbf{if}\;x \leq -2.3222646024626815 \cdot 10^{+36}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array}\]
Alternative 6
Error40.6
Cost64
\[1\]

Error

Derivation

  1. Split input into 2 regimes
  2. if x < -5.54113763990267597e37

    1. Initial program 29.4

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\]
    2. Taylor expanded around -inf 37.5

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}}\]
    3. Simplified36.9

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}}\]
    4. Simplified36.9

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}}\]

    if -5.54113763990267597e37 < x

    1. Initial program 9.4

      \[\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 div-inv_binary64_31449.4

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

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

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

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

Reproduce

herbie shell --seed 2021044 
(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))))))))