Average Error: 13.1 → 13.9
Time: 4.6s
Precision: binary64
\[1.00000000000000001 \cdot 10^{-150} \lt \left|x\right| \lt 9.99999999999999981 \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}\;p \le -6.21904064190429138 \cdot 10^{-253}:\\ \;\;\;\;\log \left(e^{\sqrt{0.5 \cdot \left(1 + \left(x \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right) \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right)}}\right)\\ \mathbf{elif}\;p \le -3.5755809718925854 \cdot 10^{-305}:\\ \;\;\;\;e^{\left(\log \left(\sqrt{2} \cdot \sqrt{0.5}\right) + \log \left(\frac{-1}{x}\right)\right) - \log \left(\frac{-1}{p}\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\log \left(\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\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}\;p \le -6.21904064190429138 \cdot 10^{-253}:\\
\;\;\;\;\log \left(e^{\sqrt{0.5 \cdot \left(1 + \left(x \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right) \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right)}}\right)\\

\mathbf{elif}\;p \le -3.5755809718925854 \cdot 10^{-305}:\\
\;\;\;\;e^{\left(\log \left(\sqrt{2} \cdot \sqrt{0.5}\right) + \log \left(\frac{-1}{x}\right)\right) - \log \left(\frac{-1}{p}\right)}\\

\mathbf{else}:\\
\;\;\;\;e^{\log \left(\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\right)\right)}\\

\end{array}
double code(double p, double x) {
	return ((double) sqrt(((double) (0.5 * ((double) (1.0 + ((double) (x / ((double) sqrt(((double) (((double) (((double) (4.0 * p)) * p)) + ((double) (x * x))))))))))))));
}
double code(double p, double x) {
	double VAR;
	if ((p <= -6.219040641904291e-253)) {
		VAR = ((double) log(((double) exp(((double) sqrt(((double) (0.5 * ((double) (1.0 + ((double) (((double) (x * ((double) sqrt(((double) (1.0 / ((double) sqrt(((double) (((double) (((double) (4.0 * p)) * p)) + ((double) (x * x)))))))))))) * ((double) sqrt(((double) (1.0 / ((double) sqrt(((double) (((double) (((double) (4.0 * p)) * p)) + ((double) (x * x))))))))))))))))))))));
	} else {
		double VAR_1;
		if ((p <= -3.5755809718925854e-305)) {
			VAR_1 = ((double) exp(((double) (((double) (((double) log(((double) (((double) sqrt(2.0)) * ((double) sqrt(0.5)))))) + ((double) log(((double) (-1.0 / x)))))) - ((double) log(((double) (-1.0 / p))))))));
		} else {
			VAR_1 = ((double) exp(((double) log(((double) log(((double) exp(((double) sqrt(((double) (0.5 * ((double) (1.0 + ((double) (x * ((double) (1.0 / ((double) sqrt(((double) (((double) (((double) (4.0 * p)) * p)) + ((double) (x * x))))))))))))))))))))))));
		}
		VAR = VAR_1;
	}
	return VAR;
}

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
Herbie13.9
\[\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 3 regimes
  2. if p < -6.21904064190429138e-253

    1. Initial program 12.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 div-inv12.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)}\]
    4. Using strategy rm
    5. Applied add-log-exp12.2

      \[\leadsto \color{blue}{\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\right)}\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt12.6

      \[\leadsto \log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \color{blue}{\left(\sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}} \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right)}\right)}}\right)\]
    8. Applied associate-*r*12.6

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

    if -6.21904064190429138e-253 < p < -3.5755809718925854e-305

    1. Initial program 20.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 div-inv21.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. Using strategy rm
    5. Applied add-log-exp21.4

      \[\leadsto \color{blue}{\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\right)}\]
    6. Using strategy rm
    7. Applied add-exp-log21.4

      \[\leadsto \color{blue}{e^{\log \left(\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\right)\right)}}\]
    8. Taylor expanded around -inf 32.2

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

    if -3.5755809718925854e-305 < p

    1. Initial program 13.3

      \[\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-inv13.5

      \[\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. Using strategy rm
    5. Applied add-log-exp13.6

      \[\leadsto \color{blue}{\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\right)}\]
    6. Using strategy rm
    7. Applied add-exp-log13.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \le -6.21904064190429138 \cdot 10^{-253}:\\ \;\;\;\;\log \left(e^{\sqrt{0.5 \cdot \left(1 + \left(x \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right) \cdot \sqrt{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}\right)}}\right)\\ \mathbf{elif}\;p \le -3.5755809718925854 \cdot 10^{-305}:\\ \;\;\;\;e^{\left(\log \left(\sqrt{2} \cdot \sqrt{0.5}\right) + \log \left(\frac{-1}{x}\right)\right) - \log \left(\frac{-1}{p}\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\log \left(\log \left(e^{\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}\right)\right)}\\ \end{array}\]

Reproduce

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