Average Error: 13.1 → 0.2
Time: 5.1s
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}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\left|\frac{p}{x}\right|\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\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}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\left|\frac{p}{x}\right|\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\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 (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
   (fabs (/ p x))
   (sqrt (* 0.5 (+ 1.0 (* x (/ 1.0 (sqrt (+ (* p (* 4.0 p)) (* x x))))))))))
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 / sqrt((p * (4.0 * p)) + (x * x))) <= -1.0) {
		tmp = fabs(p / x);
	} else {
		tmp = sqrt(0.5 * (1.0 + (x * (1.0 / sqrt((p * (4.0 * p)) + (x * x))))));
	}
	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.1
Target13.1
Herbie0.2
\[\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 (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1

    1. Initial program 53.6

      \[\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 32.1

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{p}{\frac{x \cdot x}{p}}\right)}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt_binary6423.9

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

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

      \[\leadsto \sqrt{\left|\frac{p}{x}\right|} \cdot \color{blue}{\sqrt{\left|\frac{p}{x}\right|}}\]
    8. Using strategy rm
    9. Applied pow1/2_binary640.4

      \[\leadsto \sqrt{\left|\frac{p}{x}\right|} \cdot \color{blue}{{\left(\left|\frac{p}{x}\right|\right)}^{0.5}}\]
    10. Applied pow1/2_binary640.4

      \[\leadsto \color{blue}{{\left(\left|\frac{p}{x}\right|\right)}^{0.5}} \cdot {\left(\left|\frac{p}{x}\right|\right)}^{0.5}\]
    11. Applied pow-prod-up_binary640

      \[\leadsto \color{blue}{{\left(\left|\frac{p}{x}\right|\right)}^{\left(0.5 + 0.5\right)}}\]

    if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x))))

    1. Initial program 0.2

      \[\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_binary640.2

      \[\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)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

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

Reproduce

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