Average Error: 12.7 → 5.7
Time: 5.6s
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 := p \cdot \left(4 \cdot p\right)\\ \mathbf{if}\;\frac{x}{\sqrt{t_0 + x \cdot x}} \leq -0.9999850286817621:\\ \;\;\;\;\sqrt{\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)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{{e}^{\log \left(\mathsf{fma}\left(0.5, \frac{x}{\sqrt{\mathsf{fma}\left(x, x, t_0\right)}}, 0.5\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}
t_0 := p \cdot \left(4 \cdot p\right)\\
\mathbf{if}\;\frac{x}{\sqrt{t_0 + x \cdot x}} \leq -0.9999850286817621:\\
\;\;\;\;\sqrt{\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)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{{e}^{\log \left(\mathsf{fma}\left(0.5, \frac{x}{\sqrt{\mathsf{fma}\left(x, x, t_0\right)}}, 0.5\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
 (let* ((t_0 (* p (* 4.0 p))))
   (if (<= (/ x (sqrt (+ t_0 (* x x)))) -0.9999850286817621)
     (sqrt
      (fma
       (pow (/ p x) 4.0)
       -3.0
       (fma (/ p x) (/ p x) (/ 10.0 (pow (/ x p) 6.0)))))
     (sqrt (pow E (log (fma 0.5 (/ x (sqrt (fma x x t_0))) 0.5)))))))
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 = p * (4.0 * p);
	double tmp;
	if ((x / sqrt(t_0 + (x * x))) <= -0.9999850286817621) {
		tmp = sqrt(fma(pow((p / x), 4.0), -3.0, fma((p / x), (p / x), (10.0 / pow((x / p), 6.0)))));
	} else {
		tmp = sqrt(pow(((double) M_E), log(fma(0.5, (x / sqrt(fma(x, x, t_0))), 0.5))));
	}
	return tmp;
}

Error

Bits error versus p

Bits error versus x

Target

Original12.7
Target12.7
Herbie5.7
\[\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.999985028681762089

    1. Initial program 53.1

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Simplified53.1

      \[\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 add-exp-log_binary6453.1

      \[\leadsto \sqrt{\color{blue}{e^{\log \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)}}} \]
    4. Taylor expanded in x around -inf 38.8

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

      \[\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)}} \]

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

    1. Initial program 0.0

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Simplified0.0

      \[\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 add-exp-log_binary640.0

      \[\leadsto \sqrt{\color{blue}{e^{\log \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)}}} \]
    4. Applied pow1_binary640.0

      \[\leadsto \sqrt{e^{\log \color{blue}{\left({\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)}^{1}\right)}}} \]
    5. Applied log-pow_binary640.0

      \[\leadsto \sqrt{e^{\color{blue}{1 \cdot \log \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)}}} \]
    6. Applied exp-prod_binary640.0

      \[\leadsto \sqrt{\color{blue}{{\left(e^{1}\right)}^{\log \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)}}} \]
    7. Simplified0.0

      \[\leadsto \sqrt{{\color{blue}{e}}^{\log \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)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification5.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.9999850286817621:\\ \;\;\;\;\sqrt{\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)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{{e}^{\log \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)}}\\ \end{array} \]

Reproduce

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