Average Error: 13.3 → 7.8
Time: 5.2s
Precision: 64
\[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}\;\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \le -1:\\ \;\;\;\;\sqrt{0.5 \cdot \frac{{\left(\frac{-1}{x}\right)}^{2}}{\frac{{\left(\frac{-1}{p}\right)}^{2}}{2}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + x \cdot \frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + 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{\left(4 \cdot p\right) \cdot p + x \cdot x}} \le -1:\\
\;\;\;\;\sqrt{0.5 \cdot \frac{{\left(\frac{-1}{x}\right)}^{2}}{\frac{{\left(\frac{-1}{p}\right)}^{2}}{2}}}\\

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

\end{array}
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 VAR;
	if (((x / sqrt((((4.0 * p) * p) + (x * x)))) <= -1.0)) {
		VAR = sqrt((0.5 * (pow((-1.0 / x), 2.0) / (pow((-1.0 / p), 2.0) / 2.0))));
	} else {
		VAR = sqrt((0.5 * (1.0 + (x * (1.0 / sqrt((((4.0 * p) * p) + (x * x))))))));
	}
	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.3
Target13.3
Herbie7.8
\[\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 (sqrt (+ (* (* 4.0 p) p) (* x x)))) < -1.0

    1. Initial program 53.8

      \[\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-exp-log53.8

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{e^{\log \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}}\]
    4. Using strategy rm
    5. Applied pow153.8

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

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

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

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

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

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

    if -1.0 < (/ x (sqrt (+ (* (* 4.0 p) p) (* 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-inv0.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 simplification7.8

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

Reproduce

herbie shell --seed 2020102 +o rules:numerics
(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 (/ (* 2 p) x)))))

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