Average Error: 13.0 → 5.3
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.9999999999999535:\\ \;\;\;\;\sqrt{\frac{-p}{\frac{x}{\frac{-p}{x}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(0.5, \log \left(e^{\frac{x}{\sqrt{\mathsf{fma}\left(x, x, t_0\right)}}}\right), 0.5\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.9999999999999535:\\
\;\;\;\;\sqrt{\frac{-p}{\frac{x}{\frac{-p}{x}}}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(0.5, \log \left(e^{\frac{x}{\sqrt{\mathsf{fma}\left(x, x, t_0\right)}}}\right), 0.5\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.9999999999999535)
     (sqrt (/ (- p) (/ x (/ (- p) x))))
     (sqrt (fma 0.5 (log (exp (/ 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.9999999999999535) {
		tmp = sqrt((-p / (x / (-p / x))));
	} else {
		tmp = sqrt(fma(0.5, log(exp((x / sqrt(fma(x, x, t_0))))), 0.5));
	}
	return tmp;
}

Error

Bits error versus p

Bits error versus x

Target

Original13.0
Target13.0
Herbie5.3
\[\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.999999999999953482

    1. Initial program 53.5

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

      \[\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. Taylor expanded in x around -inf 29.0

      \[\leadsto \sqrt{\color{blue}{\frac{{p}^{2}}{{x}^{2}}}} \]
    4. Simplified21.4

      \[\leadsto \sqrt{\color{blue}{\frac{p}{\frac{x \cdot x}{p}}}} \]
    5. Applied frac-2neg_binary6421.4

      \[\leadsto \sqrt{\color{blue}{\frac{-p}{-\frac{x \cdot x}{p}}}} \]
    6. Simplified21.3

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

    if -0.999999999999953482 < (/.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. Simplified0.2

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

      \[\leadsto \sqrt{\mathsf{fma}\left(0.5, \color{blue}{\log \left(e^{\frac{x}{\sqrt{\mathsf{fma}\left(x, x, p \cdot \left(4 \cdot p\right)\right)}}}\right)}, 0.5\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification5.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.9999999999999535:\\ \;\;\;\;\sqrt{\frac{-p}{\frac{x}{\frac{-p}{x}}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(0.5, \log \left(e^{\frac{x}{\sqrt{\mathsf{fma}\left(x, x, p \cdot \left(4 \cdot p\right)\right)}}}\right), 0.5\right)}\\ \end{array} \]

Reproduce

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