?

Average Accuracy: 79.3% → 89.3%
Time: 13.8s
Precision: binary64
Cost: 27140

?

\[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} + {\left(\frac{p}{x}\right)}^{3} \cdot -1.5\\ \mathbf{else}:\\ \;\;\;\;e^{0.5 \cdot \mathsf{log1p}\left(-0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\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) (* (pow (/ p x) 3.0) -1.5))
   (exp (* 0.5 (log1p (+ -0.5 (* 0.5 (/ x (hypot x (* p 2.0))))))))))
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) + (pow((p / x), 3.0) * -1.5);
	} else {
		tmp = exp((0.5 * log1p((-0.5 + (0.5 * (x / hypot(x, (p * 2.0))))))));
	}
	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) + (Math.pow((p / x), 3.0) * -1.5);
	} else {
		tmp = Math.exp((0.5 * Math.log1p((-0.5 + (0.5 * (x / Math.hypot(x, (p * 2.0))))))));
	}
	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) + (math.pow((p / x), 3.0) * -1.5)
	else:
		tmp = math.exp((0.5 * math.log1p((-0.5 + (0.5 * (x / math.hypot(x, (p * 2.0))))))))
	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) + Float64((Float64(p / x) ^ 3.0) * -1.5));
	else
		tmp = exp(Float64(0.5 * log1p(Float64(-0.5 + Float64(0.5 * Float64(x / hypot(x, Float64(p * 2.0))))))));
	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.5], N[(N[(p / x), $MachinePrecision] + N[(N[Power[N[(p / x), $MachinePrecision], 3.0], $MachinePrecision] * -1.5), $MachinePrecision]), $MachinePrecision], N[Exp[N[(0.5 * N[Log[1 + N[(-0.5 + N[(0.5 * N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $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} + {\left(\frac{p}{x}\right)}^{3} \cdot -1.5\\

\mathbf{else}:\\
\;\;\;\;e^{0.5 \cdot \mathsf{log1p}\left(-0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original79.3%
Target79.3%
Herbie89.3%
\[\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.0%

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

      \[\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} \]
      Proof

      [Start]17.0

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

      expm1-log1p-u [=>]17.0

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

      expm1-udef [=>]17.0

      \[ \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)} - 1} \]
    3. Simplified17.0%

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

      [Start]15.7

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

      expm1-def [=>]15.7

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

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

      associate-*r/ [=>]17.0

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

      \[\leadsto \sqrt{\color{blue}{\frac{{p}^{2}}{{x}^{2}} + 0.5 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{{x}^{4}}}} \]
    5. Simplified45.9%

      \[\leadsto \sqrt{\color{blue}{\frac{p \cdot p}{x \cdot x} + 0.5 \cdot \frac{{p}^{4} \cdot -6}{{x}^{4}}}} \]
      Proof

      [Start]45.9

      \[ \sqrt{\frac{{p}^{2}}{{x}^{2}} + 0.5 \cdot \frac{-2 \cdot {p}^{4} + -4 \cdot {p}^{4}}{{x}^{4}}} \]

      unpow2 [=>]45.9

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

      unpow2 [=>]45.9

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

      distribute-rgt-out [=>]45.9

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

      metadata-eval [=>]45.9

      \[ \sqrt{\frac{p \cdot p}{x \cdot x} + 0.5 \cdot \frac{{p}^{4} \cdot \color{blue}{-6}}{{x}^{4}}} \]
    6. Taylor expanded in p around 0 52.4%

      \[\leadsto \color{blue}{\frac{p}{x} + -1.5 \cdot \frac{{p}^{3}}{{x}^{3}}} \]
    7. Simplified52.4%

      \[\leadsto \color{blue}{\frac{p}{x} + \frac{-1.5 \cdot {p}^{3}}{{x}^{3}}} \]
      Proof

      [Start]52.4

      \[ \frac{p}{x} + -1.5 \cdot \frac{{p}^{3}}{{x}^{3}} \]

      associate-*r/ [=>]52.4

      \[ \frac{p}{x} + \color{blue}{\frac{-1.5 \cdot {p}^{3}}{{x}^{3}}} \]
    8. Applied egg-rr57.0%

      \[\leadsto \frac{p}{x} + \color{blue}{{\left(\frac{p}{x}\right)}^{3} \cdot -1.5} \]
      Proof

      [Start]52.4

      \[ \frac{p}{x} + \frac{-1.5 \cdot {p}^{3}}{{x}^{3}} \]

      *-commutative [=>]52.4

      \[ \frac{p}{x} + \frac{\color{blue}{{p}^{3} \cdot -1.5}}{{x}^{3}} \]

      associate-/l* [=>]52.4

      \[ \frac{p}{x} + \color{blue}{\frac{{p}^{3}}{\frac{{x}^{3}}{-1.5}}} \]

      associate-/r/ [=>]52.4

      \[ \frac{p}{x} + \color{blue}{\frac{{p}^{3}}{{x}^{3}} \cdot -1.5} \]

      cube-div [<=]57.0

      \[ \frac{p}{x} + \color{blue}{{\left(\frac{p}{x}\right)}^{3}} \cdot -1.5 \]

    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 \color{blue}{e^{\left(\log 0.5 + \mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right)\right) \cdot 0.5}} \]
      Proof

      [Start]100.0

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

      pow1/2 [=>]100.0

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

      pow-to-exp [=>]100.0

      \[ \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}} \]
    3. Applied egg-rr100.0%

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

      [Start]100.0

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

      log1p-expm1-u [=>]100.0

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

      log1p-udef [=>]100.0

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

      expm1-udef [=>]100.0

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

      exp-sum [=>]100.0

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

      add-exp-log [<=]100.0

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

      log1p-udef [=>]100.0

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

      add-exp-log [<=]100.0

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

      distribute-lft-in [=>]100.0

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

      metadata-eval [=>]100.0

      \[ e^{\log \left(1 + \left(\left(\color{blue}{0.5} + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}\right) - 1\right)\right) \cdot 0.5} \]
    4. Simplified100.0%

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

      [Start]100.0

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

      log1p-def [=>]100.0

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

      +-commutative [=>]100.0

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

      associate--l+ [=>]100.0

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

      *-commutative [=>]100.0

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

      metadata-eval [=>]100.0

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

    \[\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} + {\left(\frac{p}{x}\right)}^{3} \cdot -1.5\\ \mathbf{else}:\\ \;\;\;\;e^{0.5 \cdot \mathsf{log1p}\left(-0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy89.3%
Cost20612
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.5:\\ \;\;\;\;\frac{p}{x} + {\left(\frac{p}{x}\right)}^{3} \cdot -1.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{x \cdot 0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}}\\ \end{array} \]
Alternative 2
Accuracy68.4%
Cost7388
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -6 \cdot 10^{-19}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.5 \cdot 10^{-273}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.6 \cdot 10^{-292}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 8.8 \cdot 10^{-259}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 2.5 \cdot 10^{-220}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.7 \cdot 10^{-194}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 1.76 \cdot 10^{-41}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 3
Accuracy68.3%
Cost7388
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -8.8 \cdot 10^{-20}:\\ \;\;\;\;\sqrt{0.5 + \frac{x}{p} \cdot -0.25}\\ \mathbf{elif}\;p \leq -9.5 \cdot 10^{-274}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.6 \cdot 10^{-292}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.7 \cdot 10^{-258}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 1.35 \cdot 10^{-223}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.2 \cdot 10^{-194}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 1.4 \cdot 10^{-41}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Accuracy68.2%
Cost7388
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -8.8 \cdot 10^{-20}:\\ \;\;\;\;\sqrt{0.5 + \frac{x}{p} \cdot -0.25}\\ \mathbf{elif}\;p \leq -4.8 \cdot 10^{-273}:\\ \;\;\;\;\frac{p}{x} + {\left(\frac{p}{x}\right)}^{3} \cdot -1.5\\ \mathbf{elif}\;p \leq 1.35 \cdot 10^{-292}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 7.6 \cdot 10^{-258}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 1.1 \cdot 10^{-223}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 6 \cdot 10^{-194}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 2.5 \cdot 10^{-40}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Accuracy45.2%
Cost324
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-149}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 6
Accuracy36.4%
Cost64
\[1 \]

Error

Reproduce?

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