Average Error: 13.3 → 7.0
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} t_0 := \frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\\ \mathbf{if}\;t_0 \leq -0.9999999999999999:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(t_0 + 1\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}
t_0 := \frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\\
\mathbf{if}\;t_0 \leq -0.9999999999999999:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(t_0 + 1\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
 (let* ((t_0 (/ x (sqrt (+ (* p (* 4.0 p)) (* x x))))))
   (if (<= t_0 -0.9999999999999999) (/ p x) (sqrt (* 0.5 (+ t_0 1.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 t_0 = x / sqrt((p * (4.0 * p)) + (x * x));
	double tmp;
	if (t_0 <= -0.9999999999999999) {
		tmp = p / x;
	} else {
		tmp = sqrt(0.5 * (t_0 + 1.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.3
Target13.3
Herbie7.0
\[\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)))) < -0.999999999999999889

    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. Simplified53.8

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(0.5, \frac{x}{\sqrt{\mathsf{fma}\left(x, x, p \cdot \left(4 \cdot p\right)\right)}}, 0.5\right)}} \]
    3. Applied expm1-log1p-u_binary6453.8

      \[\leadsto \sqrt{\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.5, \frac{x}{\sqrt{\mathsf{fma}\left(x, x, p \cdot \left(4 \cdot p\right)\right)}}, 0.5\right)\right)\right)}} \]
    4. Taylor expanded in x around -inf 37.3

      \[\leadsto \sqrt{\color{blue}{\left(10 \cdot \frac{{p}^{6}}{{x}^{6}} + \frac{{p}^{2}}{{x}^{2}}\right) - 3 \cdot \frac{{p}^{4}}{{x}^{4}}}} \]
    5. Simplified22.2

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left({\left(\frac{p}{x}\right)}^{4}, -3, \mathsf{fma}\left(\frac{p}{x}, \frac{p}{x}, \frac{10}{{\left(\frac{x}{p}\right)}^{6}}\right)\right)}} \]
    6. Taylor expanded in p around 0 28.0

      \[\leadsto \color{blue}{\frac{p}{x}} \]

    if -0.999999999999999889 < (/.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)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification7.0

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

Reproduce

herbie shell --seed 2022081 
(FPCore (p x)
  :name "Given's Rotation SVD example"
  :precision binary64
  :pre (and (< 1e-150 (fabs x)) (< (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))))))))