?

Average Error: 13.5 → 5.5
Time: 10.9s
Precision: binary64
Cost: 87364

?

\[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 := \mathsf{hypot}\left(x, p \cdot 2\right)\\ t_1 := \frac{t_0}{x}\\ t_2 := {t_1}^{3}\\ \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.999999996:\\ \;\;\;\;\sqrt{\frac{p}{x} \cdot \frac{p}{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\frac{\frac{0.001953125 + \frac{0.001953125}{{t_2}^{3}}}{{\left(\frac{x \cdot 0.5}{t_0}\right)}^{2} + \mathsf{fma}\left(\frac{-0.25}{t_0}, x, 0.25\right)}}{0.015625 + \left(\frac{0.015625}{{t_1}^{6}} + \frac{-0.015625}{t_2}\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 (hypot x (* p 2.0))) (t_1 (/ t_0 x)) (t_2 (pow t_1 3.0)))
   (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.999999996)
     (sqrt (* (/ p x) (/ p x)))
     (sqrt
      (/
       (/
        (+ 0.001953125 (/ 0.001953125 (pow t_2 3.0)))
        (+ (pow (/ (* x 0.5) t_0) 2.0) (fma (/ -0.25 t_0) x 0.25)))
       (+ 0.015625 (+ (/ 0.015625 (pow t_1 6.0)) (/ -0.015625 t_2))))))))
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 = hypot(x, (p * 2.0));
	double t_1 = t_0 / x;
	double t_2 = pow(t_1, 3.0);
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.999999996) {
		tmp = sqrt(((p / x) * (p / x)));
	} else {
		tmp = sqrt((((0.001953125 + (0.001953125 / pow(t_2, 3.0))) / (pow(((x * 0.5) / t_0), 2.0) + fma((-0.25 / t_0), x, 0.25))) / (0.015625 + ((0.015625 / pow(t_1, 6.0)) + (-0.015625 / t_2)))));
	}
	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 = hypot(x, Float64(p * 2.0))
	t_1 = Float64(t_0 / x)
	t_2 = t_1 ^ 3.0
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.999999996)
		tmp = sqrt(Float64(Float64(p / x) * Float64(p / x)));
	else
		tmp = sqrt(Float64(Float64(Float64(0.001953125 + Float64(0.001953125 / (t_2 ^ 3.0))) / Float64((Float64(Float64(x * 0.5) / t_0) ^ 2.0) + fma(Float64(-0.25 / t_0), x, 0.25))) / Float64(0.015625 + Float64(Float64(0.015625 / (t_1 ^ 6.0)) + Float64(-0.015625 / t_2)))));
	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[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 / x), $MachinePrecision]}, Block[{t$95$2 = N[Power[t$95$1, 3.0], $MachinePrecision]}, If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.999999996], N[Sqrt[N[(N[(p / x), $MachinePrecision] * N[(p / x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(N[(N[(0.001953125 + N[(0.001953125 / N[Power[t$95$2, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[Power[N[(N[(x * 0.5), $MachinePrecision] / t$95$0), $MachinePrecision], 2.0], $MachinePrecision] + N[(N[(-0.25 / t$95$0), $MachinePrecision] * x + 0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(0.015625 + N[(N[(0.015625 / N[Power[t$95$1, 6.0], $MachinePrecision]), $MachinePrecision] + N[(-0.015625 / t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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 := \mathsf{hypot}\left(x, p \cdot 2\right)\\
t_1 := \frac{t_0}{x}\\
t_2 := {t_1}^{3}\\
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.999999996:\\
\;\;\;\;\sqrt{\frac{p}{x} \cdot \frac{p}{x}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\frac{\frac{0.001953125 + \frac{0.001953125}{{t_2}^{3}}}{{\left(\frac{x \cdot 0.5}{t_0}\right)}^{2} + \mathsf{fma}\left(\frac{-0.25}{t_0}, x, 0.25\right)}}{0.015625 + \left(\frac{0.015625}{{t_1}^{6}} + \frac{-0.015625}{t_2}\right)}}\\


\end{array}

Error?

Target

Original13.5
Target13.5
Herbie5.5
\[\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.999999996000000002

    1. Initial program 53.9

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

      \[\leadsto \color{blue}{\sqrt{1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}} \cdot \sqrt{0.5}} \]
    3. Applied egg-rr53.9

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    4. Simplified53.9

      \[\leadsto \color{blue}{\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, p \cdot 2\right)}}} \]
      Proof

      [Start]53.9

      \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1 \]

      expm1-def [=>]53.9

      \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)\right)} \]

      expm1-log1p [=>]53.9

      \[ \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}} \]

      associate-*r/ [=>]53.9

      \[ \sqrt{0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}} \]

      *-commutative [=>]53.9

      \[ \sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, \color{blue}{p \cdot 2}\right)}} \]
    5. Taylor expanded in x around -inf 29.7

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

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

      [Start]29.7

      \[ \sqrt{\frac{{p}^{2}}{{x}^{2}}} \]

      unpow2 [=>]29.7

      \[ \sqrt{\frac{\color{blue}{p \cdot p}}{{x}^{2}}} \]

      unpow2 [=>]29.7

      \[ \sqrt{\frac{p \cdot p}{\color{blue}{x \cdot x}}} \]

      times-frac [=>]21.9

      \[ \sqrt{\color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]

    if -0.999999996000000002 < (/.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. Applied egg-rr0.4

      \[\leadsto \color{blue}{\sqrt{1 + \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}} \cdot \sqrt{0.5}} \]
    3. Applied egg-rr0.7

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    4. Simplified0.1

      \[\leadsto \color{blue}{\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, p \cdot 2\right)}}} \]
      Proof

      [Start]0.7

      \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1 \]

      expm1-def [=>]0.7

      \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)\right)} \]

      expm1-log1p [=>]0.1

      \[ \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}} \]

      associate-*r/ [=>]0.1

      \[ \sqrt{0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}} \]

      *-commutative [=>]0.1

      \[ \sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, \color{blue}{p \cdot 2}\right)}} \]
    5. Applied egg-rr0.1

      \[\leadsto \sqrt{\color{blue}{\frac{\left(0.001953125 + {\left(\frac{0.125}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}\right)}^{3}\right) \cdot \frac{1}{{\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}^{2} + \left(\frac{-0.25}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}} + 0.25\right)}}{0.015625 + \left(\frac{\frac{0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}} - \frac{0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}\right)}}} \]
    6. Simplified0.1

      \[\leadsto \sqrt{\color{blue}{\frac{\frac{0.001953125 + \frac{0.001953125}{{\left({\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}\right)}^{3}}}{{\left(\frac{0.5 \cdot x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}^{2} + \mathsf{fma}\left(\frac{-0.25}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.25\right)}}{0.015625 + \left(\frac{0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{6}} + \frac{-0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}\right)}}} \]
      Proof

      [Start]0.1

      \[ \sqrt{\frac{\left(0.001953125 + {\left(\frac{0.125}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}\right)}^{3}\right) \cdot \frac{1}{{\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}^{2} + \left(\frac{-0.25}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}} + 0.25\right)}}{0.015625 + \left(\frac{\frac{0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}} - \frac{0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification5.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.999999996:\\ \;\;\;\;\sqrt{\frac{p}{x} \cdot \frac{p}{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\frac{\frac{0.001953125 + \frac{0.001953125}{{\left({\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}\right)}^{3}}}{{\left(\frac{x \cdot 0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}^{2} + \mathsf{fma}\left(\frac{-0.25}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.25\right)}}{0.015625 + \left(\frac{0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{6}} + \frac{-0.015625}{{\left(\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}\right)}^{3}}\right)}}\\ \end{array} \]

Alternatives

Alternative 1
Error5.5
Cost47620
\[\begin{array}{l} t_0 := \mathsf{hypot}\left(x, p \cdot 2\right)\\ \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.999999996:\\ \;\;\;\;\sqrt{\frac{p}{x} \cdot \frac{p}{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\frac{0.125 + \frac{0.125}{{\left(\frac{t_0}{x}\right)}^{3}}}{{\left(\frac{x \cdot 0.5}{t_0}\right)}^{2} + \left(0.25 + x \cdot \frac{-0.25}{t_0}\right)}}\\ \end{array} \]
Alternative 2
Error5.6
Cost20612
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.5:\\ \;\;\;\;\sqrt{\frac{p}{x} \cdot \frac{p}{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}}\\ \end{array} \]
Alternative 3
Error20.0
Cost7176
\[\begin{array}{l} \mathbf{if}\;p \leq -7.2 \cdot 10^{-37}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.85 \cdot 10^{-255}:\\ \;\;\;\;\sqrt{\frac{\frac{1}{x}}{x}} \cdot \left(-p\right)\\ \mathbf{elif}\;p \leq 1.7 \cdot 10^{-271}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.9 \cdot 10^{-229}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 3.2 \cdot 10^{-67}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Error20.1
Cost7124
\[\begin{array}{l} \mathbf{if}\;p \leq -4.9 \cdot 10^{-49}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.4 \cdot 10^{-254}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 10^{-268}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.25 \cdot 10^{-227}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 3 \cdot 10^{-66}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Error20.5
Cost6860
\[\begin{array}{l} \mathbf{if}\;p \leq -2 \cdot 10^{-32}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 2 \cdot 10^{-305}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 4.7 \cdot 10^{-85}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 6
Error46.7
Cost388
\[\begin{array}{l} \mathbf{if}\;p \leq 2 \cdot 10^{-305}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \]
Alternative 7
Error53.5
Cost256
\[\frac{-p}{x} \]

Error

Reproduce?

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