?

Average Error: 13.2 → 6.8
Time: 10.3s
Precision: binary64
Cost: 26884

?

\[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} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.95:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, \frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\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
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.95)
   (/ (- p) x)
   (sqrt (fma x (/ 0.5 (hypot x (* p 2.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 tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.95) {
		tmp = -p / x;
	} else {
		tmp = sqrt(fma(x, (0.5 / hypot(x, (p * 2.0))), 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)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.95)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(fma(x, Float64(0.5 / hypot(x, Float64(p * 2.0))), 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_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.95], N[((-p) / x), $MachinePrecision], N[Sqrt[N[(x * N[(0.5 / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 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}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.95:\\
\;\;\;\;\frac{-p}{x}\\

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


\end{array}

Error?

Target

Original13.2
Target13.2
Herbie6.8
\[\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.94999999999999996

    1. Initial program 52.7

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

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

      [Start]52.7

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

      distribute-lft-in [=>]52.7

      \[ \sqrt{\color{blue}{0.5 \cdot 1 + 0.5 \cdot \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}} \]

      metadata-eval [=>]52.7

      \[ \sqrt{\color{blue}{0.5} + 0.5 \cdot \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}} \]

      associate-*r/ [=>]52.7

      \[ \sqrt{0.5 + \color{blue}{\frac{0.5 \cdot x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}}} \]

      +-commutative [=>]52.7

      \[ \sqrt{0.5 + \frac{0.5 \cdot x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}}} \]

      fma-def [=>]52.6

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

      associate-*l* [=>]52.6

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

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

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

      [Start]30.7

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

      unpow2 [=>]30.7

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

      unpow2 [=>]30.7

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

      times-frac [=>]23.1

      \[ \sqrt{\color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    5. Taylor expanded in p around -inf 27.3

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

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

      [Start]27.3

      \[ -1 \cdot \frac{p}{x} \]

      mul-1-neg [=>]27.3

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

      distribute-neg-frac [=>]27.3

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

    if -0.94999999999999996 < (/.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. Applied egg-rr0.6

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

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

      [Start]0.6

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

      expm1-def [=>]0.6

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

      expm1-log1p [=>]0.0

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

      +-commutative [=>]0.0

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

      fma-def [=>]0.0

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

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

Alternatives

Alternative 1
Error6.8
Cost20612
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.95:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}}\\ \end{array} \]
Alternative 2
Error20.5
Cost7636
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -9.6 \cdot 10^{-39}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.3 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 4 \cdot 10^{-297}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 2.45 \cdot 10^{-92}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 2.85 \cdot 10^{-61}:\\ \;\;\;\;\sqrt{1 - \frac{p}{x} \cdot \frac{p}{x}}\\ \mathbf{elif}\;p \leq 2.5 \cdot 10^{-48}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 3
Error20.5
Cost7636
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -2.2 \cdot 10^{-38}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.35 \cdot 10^{-164}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 4 \cdot 10^{-297}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.08 \cdot 10^{-92}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 8 \cdot 10^{-60}:\\ \;\;\;\;\mathsf{fma}\left(-0.5, \frac{p \cdot p}{x \cdot x}, 1\right)\\ \mathbf{elif}\;p \leq 7.5 \cdot 10^{-48}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Error20.3
Cost7256
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -1.55 \cdot 10^{-40}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -3.5 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 4 \cdot 10^{-297}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.22 \cdot 10^{-93}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 3.1 \cdot 10^{-56}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 3.7 \cdot 10^{-41}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Error34.6
Cost652
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;x \leq -1.8 \cdot 10^{-72}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-137}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;x \leq -1.55 \cdot 10^{-146}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 6
Error34.8
Cost324
\[\begin{array}{l} \mathbf{if}\;x \leq -9 \cdot 10^{-137}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 7
Error40.5
Cost64
\[1 \]

Error

Reproduce?

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