Average Error: 13.4 → 0.1
Time: 5.7s
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.9999999999979009:\\ \;\;\;\;\left|\frac{p}{x}\right|\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(0.5, \frac{x}{\mathsf{hypot}\left(\sqrt{t_0}, x\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.9999999999979009)
     (fabs (/ p x))
     (sqrt (fma 0.5 (/ x (hypot (sqrt t_0) x)) 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.9999999999979009) {
		tmp = fabs((p / x));
	} else {
		tmp = sqrt(fma(0.5, (x / hypot(sqrt(t_0), x)), 0.5));
	}
	return tmp;
}
function code(p, x)
	return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x)))))))
end
function code(p, x)
	t_0 = Float64(p * Float64(4.0 * p))
	tmp = 0.0
	if (Float64(x / sqrt(Float64(t_0 + Float64(x * x)))) <= -0.9999999999979009)
		tmp = abs(Float64(p / x));
	else
		tmp = sqrt(fma(0.5, Float64(x / hypot(sqrt(t_0), x)), 0.5));
	end
	return tmp
end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[p_, x_] := Block[{t$95$0 = N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x / N[Sqrt[N[(t$95$0 + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.9999999999979009], N[Abs[N[(p / x), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(0.5 * N[(x / N[Sqrt[N[Sqrt[t$95$0], $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]], $MachinePrecision]]]
\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.9999999999979009:\\
\;\;\;\;\left|\frac{p}{x}\right|\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(0.5, \frac{x}{\mathsf{hypot}\left(\sqrt{t_0}, x\right)}, 0.5\right)}\\


\end{array}

Error

Bits error versus p

Bits error versus x

Target

Original13.4
Target13.4
Herbie0.1
\[\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.999999999997900901

    1. Initial program 52.9

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

      \[\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 30.2

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

      \[\leadsto \sqrt{\color{blue}{\frac{p}{\frac{x \cdot x}{p}}}} \]
    5. Applied egg-rr0.1

      \[\leadsto \color{blue}{\left|\frac{p}{x}\right|} \]

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

    1. Initial program 0.1

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Simplified0.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 egg-rr0.1

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

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

Reproduce

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