?

Average Accuracy: 78.8% → 91.1%
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 -1:\\ \;\;\;\;\frac{-\sqrt{p \cdot 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)))) -1.0)
   (/ (- (sqrt (* p 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)))) <= -1.0) {
		tmp = -sqrt((p * 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)))) <= -1.0) {
		tmp = -Math.sqrt((p * 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)))) <= -1.0:
		tmp = -math.sqrt((p * 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)))) <= -1.0)
		tmp = Float64(Float64(-sqrt(Float64(p * 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)))) <= -1.0)
		tmp = -sqrt((p * 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], -1.0], N[((-N[Sqrt[N[(p * p), $MachinePrecision]], $MachinePrecision]) / 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 -1:\\
\;\;\;\;\frac{-\sqrt{p \cdot 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

Original78.8%
Target78.8%
Herbie91.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)))) < -1

    1. Initial program 15.0%

      \[\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 55.2%

      \[\leadsto \color{blue}{-1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}} \]
    3. Simplified55.2%

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

      [Start]55.2

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

      mul-1-neg [=>]55.2

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

      associate-/l* [=>]55.0

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

      associate-/r/ [=>]55.2

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

      distribute-lft-neg-in [=>]55.2

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

      *-commutative [=>]55.2

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

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

      [Start]55.2

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

      add-sqr-sqrt [=>]48.2

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

      sqrt-unprod [=>]64.1

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

      pow1/2 [=>]64.1

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

      *-commutative [=>]64.1

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

      *-commutative [=>]64.1

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

      swap-sqr [=>]64.0

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

      add-sqr-sqrt [<=]64.4

      \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot {\left(\color{blue}{0.5} \cdot \left(p \cdot p\right)\right)}^{0.5} \]
    5. Simplified64.4%

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

      [Start]64.4

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

      unpow1/2 [=>]64.4

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

      associate-*r* [=>]64.4

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

      *-commutative [=>]64.4

      \[ \left(-\frac{\sqrt{2}}{x}\right) \cdot \sqrt{\color{blue}{p \cdot \left(0.5 \cdot p\right)}} \]
    6. Applied egg-rr64.8%

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

      [Start]64.4

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

      distribute-lft-neg-out [=>]64.4

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

      neg-sub0 [=>]64.4

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

      associate-*l/ [=>]64.5

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

      pow1/2 [=>]64.5

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

      pow1/2 [=>]64.5

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

      pow-prod-down [=>]64.8

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

      *-commutative [=>]64.8

      \[ 0 - \frac{{\left(2 \cdot \left(p \cdot \color{blue}{\left(p \cdot 0.5\right)}\right)\right)}^{0.5}}{x} \]
    7. Simplified64.8%

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

      [Start]64.8

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

      neg-sub0 [<=]64.8

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

      distribute-neg-frac [=>]64.8

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

      unpow1/2 [=>]64.8

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

      *-commutative [=>]64.8

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

      associate-*r* [=>]64.8

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

      associate-*l* [=>]64.8

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

      metadata-eval [=>]64.8

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

      *-rgt-identity [=>]64.8

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

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

    1. Initial program 99.7%

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

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

      [Start]99.7

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

      add-sqr-sqrt [=>]99.7

      \[ \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 [=>]99.7

      \[ \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* [=>]99.7

      \[ \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 [=>]99.7

      \[ \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 [=>]99.7

      \[ \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.6

      \[ \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 [<=]99.7

      \[ \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 simplification91.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\sqrt{p \cdot 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.4%
Cost7124
\[\begin{array}{l} \mathbf{if}\;p \leq -1.45 \cdot 10^{-18}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -3.7 \cdot 10^{-104}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq -5.3 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.65 \cdot 10^{-192}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.8 \cdot 10^{-81}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 2
Accuracy67.9%
Cost6860
\[\begin{array}{l} \mathbf{if}\;p \leq -5.5 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 9.5 \cdot 10^{-303}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 10^{-74}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 3
Accuracy27.0%
Cost388
\[\begin{array}{l} \mathbf{if}\;p \leq 9.5 \cdot 10^{-303}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \]
Alternative 4
Accuracy16.9%
Cost192
\[\frac{p}{x} \]

Error

Reproduce?

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