?

Average Error: 13.6 → 0.2
Time: 1.6min
Precision: binary64
Cost: 33672

?

\[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(p + p, x\right)\\ \mathbf{if}\;\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{{\left({\left(0.5 + \begin{array}{l} \mathbf{if}\;x \ne 0:\\ \;\;\;\;\frac{0.5}{\frac{t_0}{x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot x}{t_0}\\ \end{array}\right)}^{3}\right)}^{0.3333333333333333}}\\ \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 (+ p p) x)))
   (if (<= (/ x (sqrt (+ (* (* 4.0 p) p) (* x x)))) -1.0)
     (/ (- (fabs p)) x)
     (sqrt
      (pow
       (pow (+ 0.5 (if (!= x 0.0) (/ 0.5 (/ t_0 x)) (/ (* 0.5 x) t_0))) 3.0)
       0.3333333333333333)))))
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((p + p), x);
	double tmp;
	if ((x / sqrt((((4.0 * p) * p) + (x * x)))) <= -1.0) {
		tmp = -fabs(p) / x;
	} else {
		double tmp_1;
		if (x != 0.0) {
			tmp_1 = 0.5 / (t_0 / x);
		} else {
			tmp_1 = (0.5 * x) / t_0;
		}
		tmp = sqrt(pow(pow((0.5 + tmp_1), 3.0), 0.3333333333333333));
	}
	return tmp;
}
public static double code(double p, double x) {
	return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
public static double code(double p, double x) {
	double t_0 = Math.hypot((p + p), x);
	double tmp;
	if ((x / Math.sqrt((((4.0 * p) * p) + (x * x)))) <= -1.0) {
		tmp = -Math.abs(p) / x;
	} else {
		double tmp_1;
		if (x != 0.0) {
			tmp_1 = 0.5 / (t_0 / x);
		} else {
			tmp_1 = (0.5 * x) / t_0;
		}
		tmp = Math.sqrt(Math.pow(Math.pow((0.5 + tmp_1), 3.0), 0.3333333333333333));
	}
	return tmp;
}
def code(p, x):
	return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
def code(p, x):
	t_0 = math.hypot((p + p), x)
	tmp = 0
	if (x / math.sqrt((((4.0 * p) * p) + (x * x)))) <= -1.0:
		tmp = -math.fabs(p) / x
	else:
		tmp_1 = 0
		if x != 0.0:
			tmp_1 = 0.5 / (t_0 / x)
		else:
			tmp_1 = (0.5 * x) / t_0
		tmp = math.sqrt(math.pow(math.pow((0.5 + tmp_1), 3.0), 0.3333333333333333))
	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(Float64(p + p), x)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x)))) <= -1.0)
		tmp = Float64(Float64(-abs(p)) / x);
	else
		tmp_1 = 0.0
		if (x != 0.0)
			tmp_1 = Float64(0.5 / Float64(t_0 / x));
		else
			tmp_1 = Float64(Float64(0.5 * x) / t_0);
		end
		tmp = sqrt(((Float64(0.5 + tmp_1) ^ 3.0) ^ 0.3333333333333333));
	end
	return tmp
end
function tmp = code(p, x)
	tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
end
function tmp_3 = code(p, x)
	t_0 = hypot((p + p), x);
	tmp = 0.0;
	if ((x / sqrt((((4.0 * p) * p) + (x * x)))) <= -1.0)
		tmp = -abs(p) / x;
	else
		tmp_2 = 0.0;
		if (x ~= 0.0)
			tmp_2 = 0.5 / (t_0 / x);
		else
			tmp_2 = (0.5 * x) / t_0;
		end
		tmp = sqrt((((0.5 + tmp_2) ^ 3.0) ^ 0.3333333333333333));
	end
	tmp_3 = 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[N[(p + p), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]}, If[LessEqual[N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-N[Abs[p], $MachinePrecision]) / x), $MachinePrecision], N[Sqrt[N[Power[N[Power[N[(0.5 + If[Unequal[x, 0.0], N[(0.5 / N[(t$95$0 / x), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * x), $MachinePrecision] / t$95$0), $MachinePrecision]]), $MachinePrecision], 3.0], $MachinePrecision], 0.3333333333333333], $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(p + p, x\right)\\
\mathbf{if}\;\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-\left|p\right|}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{{\left({\left(0.5 + \begin{array}{l}
\mathbf{if}\;x \ne 0:\\
\;\;\;\;\frac{0.5}{\frac{t_0}{x}}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot x}{t_0}\\


\end{array}\right)}^{3}\right)}^{0.3333333333333333}}\\


\end{array}

Error?

Target

Original13.6
Target13.6
Herbie0.2
\[\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)))) < -1

    1. Initial program 53.7

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

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, \frac{0.5}{\mathsf{hypot}\left(2 \cdot p, x\right)}, 0.5\right)}} \]
      Proof
    3. Taylor expanded in x around -inf 22.9

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

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

    if -1 < (/.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(x, \frac{0.5}{\mathsf{hypot}\left(2 \cdot p, x\right)}, 0.5\right)}} \]
      Proof
    3. Applied egg-rr0.2

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

      \[\leadsto \sqrt{\color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;x \ne 0:\\ \;\;\;\;\frac{0.5}{\frac{\mathsf{hypot}\left(p + p, x\right)}{x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot x}{\mathsf{hypot}\left(p + p, x\right)}\\ } \end{array}} - -0.5} \]
    5. Applied egg-rr0.2

      \[\leadsto \sqrt{\color{blue}{{\left({\left(0.5 + \begin{array}{l} \mathbf{if}\;x \ne 0:\\ \;\;\;\;\frac{0.5}{\frac{\mathsf{hypot}\left(p + p, x\right)}{x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot x}{\mathsf{hypot}\left(p + p, x\right)}\\ \end{array}\right)}^{3}\right)}^{0.3333333333333333}}} \]
  3. Recombined 2 regimes into one program.

Alternatives

Alternative 1
Error0.2
Cost20744
\[\begin{array}{l} t_0 := \mathsf{hypot}\left(p + p, x\right)\\ \mathbf{if}\;\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\begin{array}{l} \mathbf{if}\;x \ne 0:\\ \;\;\;\;\frac{0.5}{\frac{t_0}{x}}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot x}{t_0}\\ \end{array} - -0.5}\\ \end{array} \]
Alternative 2
Error14.2
Cost14232
\[\begin{array}{l} t_0 := \frac{-\left|p\right|}{x}\\ t_1 := \sqrt{\frac{0.5 \cdot x}{\mathsf{hypot}\left(p + p, x\right)} - -0.5}\\ \mathbf{if}\;p \leq -1 \cdot 10^{-38}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;p \leq -2.8 \cdot 10^{-58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq -2.3 \cdot 10^{-125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;p \leq -2.15 \cdot 10^{-260}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 2.6 \cdot 10^{-181}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;p \leq 5.5 \cdot 10^{-160}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error20.2
Cost6980
\[\begin{array}{l} \mathbf{if}\;p \leq -1.85 \cdot 10^{-36}:\\ \;\;\;\;\sqrt{0.5 + \frac{-0.25 \cdot x}{p}}\\ \mathbf{elif}\;p \leq 8.6 \cdot 10^{-159}:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{elif}\;p \leq 3.3 \cdot 10^{-43}:\\ \;\;\;\;\sqrt{1}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Error20.1
Cost6920
\[\begin{array}{l} \mathbf{if}\;p \leq -1.02 \cdot 10^{-38}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 6.5 \cdot 10^{-158}:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{elif}\;p \leq 7.5 \cdot 10^{-47}:\\ \;\;\;\;\sqrt{1}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Error20.1
Cost6728
\[\begin{array}{l} \mathbf{if}\;p \leq -1.45 \cdot 10^{-24}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 7 \cdot 10^{-48}:\\ \;\;\;\;\sqrt{1}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 6
Error29.0
Cost6464
\[\sqrt{0.5} \]

Error

Reproduce?

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