?

Average Accuracy: 80.0% → 89.5%
Time: 7.3s
Precision: binary64
Cost: 20612

?

\[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.5:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\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.5)
   (/ (- p) x)
   (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x)))))))
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.5) {
		tmp = -p / x;
	} else {
		tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
	}
	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 tmp;
	if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
	}
	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):
	tmp = 0
	if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5:
		tmp = -p / x
	else:
		tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x)))))
	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.5)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x)))));
	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_2 = code(p, x)
	tmp = 0.0;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.5)
		tmp = -p / x;
	else
		tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
	end
	tmp_2 = 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.5], N[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p * 2.0), $MachinePrecision] ^ 2 + x ^ 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}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.5:\\
\;\;\;\;\frac{-p}{x}\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original80.0%
Target80.0%
Herbie89.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.5

    1. Initial program 17.1%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Taylor expanded in x around -inf 51.3%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Simplified62.4%

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

      [Start]51.3

      \[ \sqrt{0.5 \cdot \left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)} \]

      unpow2 [=>]51.3

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

      unpow2 [=>]51.3

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

      times-frac [=>]62.4

      \[ \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Applied egg-rr30.0%

      \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\sqrt[3]{{\left(\frac{p}{x}\right)}^{6}}}\right)} \]
      Proof

      [Start]62.4

      \[ \sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)} \]

      *-un-lft-identity [=>]62.4

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

      metadata-eval [<=]62.4

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

      associate-*r* [<=]62.4

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

      add-cbrt-cube [=>]30.0

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

      pow3 [=>]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{\color{blue}{{\left(0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)\right)}^{3}}}\right)} \]

      associate-*r* [=>]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\color{blue}{\left(\left(0.5 \cdot 2\right) \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}}^{3}}\right)} \]

      metadata-eval [=>]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left(\color{blue}{1} \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}^{3}}\right)} \]

      *-un-lft-identity [<=]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}}^{3}}\right)} \]

      pow2 [=>]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\color{blue}{\left({\left(\frac{p}{x}\right)}^{2}\right)}}^{3}}\right)} \]

      metadata-eval [<=]30.0

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

      pow-pow [=>]30.0

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

      metadata-eval [=>]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left(\frac{p}{x}\right)}^{\left(2 \cdot \color{blue}{3}\right)}}\right)} \]

      metadata-eval [=>]30.0

      \[ \sqrt{0.5 \cdot \left(2 \cdot \sqrt[3]{{\left(\frac{p}{x}\right)}^{\color{blue}{6}}}\right)} \]
    5. Taylor expanded in p around -inf 56.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    6. Simplified56.4%

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

      [Start]56.4

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

      associate-*r/ [=>]56.4

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

      neg-mul-1 [<=]56.4

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

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

    1. Initial program 100.0%

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

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

      [Start]100.0

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

      add-sqr-sqrt [=>]100.0

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

      hypot-def [=>]100.0

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

      associate-*l* [=>]100.0

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

      sqrt-prod [=>]100.0

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

      metadata-eval [=>]100.0

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

      sqrt-unprod [<=]49.3

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

      add-sqr-sqrt [<=]100.0

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

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

Alternatives

Alternative 1
Accuracy68.2%
Cost7892
\[\begin{array}{l} \mathbf{if}\;p \leq -130000000:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.1 \cdot 10^{-17}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq -1.7 \cdot 10^{-87}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.06 \cdot 10^{-234}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 2.7 \cdot 10^{-204}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 + -2 \cdot \frac{p \cdot p}{x \cdot x}\right)}\\ \mathbf{elif}\;p \leq 3.75 \cdot 10^{-81}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 2
Accuracy68.1%
Cost7256
\[\begin{array}{l} \mathbf{if}\;p \leq -760000000:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -5.4 \cdot 10^{-17}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq -2.1 \cdot 10^{-88}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.32 \cdot 10^{-234}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 3.2 \cdot 10^{-204}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.6 \cdot 10^{-81}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 3
Accuracy68.7%
Cost6860
\[\begin{array}{l} \mathbf{if}\;p \leq -7 \cdot 10^{-87}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2 \cdot 10^{-310}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.82 \cdot 10^{-81}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Accuracy25.9%
Cost388
\[\begin{array}{l} \mathbf{if}\;p \leq -2 \cdot 10^{-310}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \]
Alternative 5
Accuracy16.1%
Cost192
\[\frac{p}{x} \]

Error

Reproduce?

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