?

Average Accuracy: 79.7% → 99.8%
Time: 13.5s
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.99999999:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{else}:\\ \;\;\;\;e^{0.5 \cdot \log \left(0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p + p\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.99999999)
   (/ (- (fabs p)) x)
   (exp (* 0.5 (log (+ 0.5 (* x (/ 0.5 (hypot x (+ p p))))))))))
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.99999999) {
		tmp = -fabs(p) / x;
	} else {
		tmp = exp((0.5 * log((0.5 + (x * (0.5 / hypot(x, (p + p))))))));
	}
	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.99999999) {
		tmp = -Math.abs(p) / x;
	} else {
		tmp = Math.exp((0.5 * Math.log((0.5 + (x * (0.5 / Math.hypot(x, (p + p))))))));
	}
	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.99999999:
		tmp = -math.fabs(p) / x
	else:
		tmp = math.exp((0.5 * math.log((0.5 + (x * (0.5 / math.hypot(x, (p + p))))))))
	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.99999999)
		tmp = Float64(Float64(-abs(p)) / x);
	else
		tmp = exp(Float64(0.5 * log(Float64(0.5 + Float64(x * Float64(0.5 / hypot(x, Float64(p + p))))))));
	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.99999999)
		tmp = -abs(p) / x;
	else
		tmp = exp((0.5 * log((0.5 + (x * (0.5 / hypot(x, (p + p))))))));
	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.99999999], N[((-N[Abs[p], $MachinePrecision]) / x), $MachinePrecision], N[Exp[N[(0.5 * N[Log[N[(0.5 + N[(x * N[(0.5 / N[Sqrt[x ^ 2 + N[(p + p), $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.99999999:\\
\;\;\;\;\frac{-\left|p\right|}{x}\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original79.7%
Target79.7%
Herbie99.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.99999998999999995

    1. Initial program 16.6%

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

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

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

      [Start]56.8

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

      mul-1-neg [=>]56.8

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

      *-commutative [=>]56.8

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

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

      [Start]56.8

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

      add-sqr-sqrt [=>]49.3

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

      sqrt-unprod [=>]63.0

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

      swap-sqr [=>]62.8

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

      add-sqr-sqrt [<=]63.3

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

      *-commutative [=>]63.3

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

      *-commutative [=>]63.3

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

      swap-sqr [=>]63.1

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

      add-sqr-sqrt [<=]63.6

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

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

      [Start]63.6

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

      associate-*r* [=>]63.6

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

      metadata-eval [=>]63.6

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

      *-lft-identity [=>]63.6

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

      rem-sqrt-square [=>]99.7

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

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

    1. Initial program 99.9%

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

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

      [Start]99.9

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

      pow1/2 [=>]99.9

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

      \[ \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-rr99.9%

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

      [Start]99.9

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

      fma-udef [=>]99.9

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

      +-commutative [=>]99.9

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

      clear-num [=>]99.9

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

      associate-*l/ [=>]99.9

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

      metadata-eval [=>]99.9

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

      add-log-exp [=>]90.3

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

      *-commutative [=>]90.3

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

      exp-lft-sqr [=>]90.2

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

      log-prod [=>]90.2

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

      add-log-exp [<=]95.2

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

      add-log-exp [<=]99.9

      \[ e^{\log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p + \color{blue}{p}\right)}{x}}\right) \cdot 0.5} \]
    4. Simplified99.9%

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

      [Start]99.9

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

      associate-/r/ [=>]99.9

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

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

Alternatives

Alternative 1
Accuracy99.9%
Cost20612
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.99999999:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\ \end{array} \]
Alternative 2
Accuracy67.7%
Cost7768
\[\begin{array}{l} \mathbf{if}\;p \leq -1.05 \cdot 10^{-77}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -5.5 \cdot 10^{-292}:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{elif}\;p \leq 7 \cdot 10^{-309}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9.2 \cdot 10^{-236}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 2.4 \cdot 10^{-110}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.26 \cdot 10^{-51}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(1 + \frac{x}{p} \cdot 0.25\right)\\ \mathbf{elif}\;p \leq 26000000000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 3
Accuracy67.8%
Cost7640
\[\begin{array}{l} \mathbf{if}\;p \leq -3 \cdot 10^{-75}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.6 \cdot 10^{-292}:\\ \;\;\;\;\frac{-\left|p\right|}{x}\\ \mathbf{elif}\;p \leq -3 \cdot 10^{-309}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 6 \cdot 10^{-235}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 2.4 \cdot 10^{-110}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.26 \cdot 10^{-51}:\\ \;\;\;\;\sqrt{0.5 + x \cdot \frac{0.25}{p}}\\ \mathbf{elif}\;p \leq 26000000000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Accuracy68.1%
Cost7448
\[\begin{array}{l} t_0 := \frac{-\left|p\right|}{x}\\ \mathbf{if}\;p \leq -3.9 \cdot 10^{-75}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -7.4 \cdot 10^{-293}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 3.5 \cdot 10^{-307}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.6 \cdot 10^{-235}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 5.5 \cdot 10^{-146}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.15 \cdot 10^{-28}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 26000000000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Accuracy68.6%
Cost7256
\[\begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -1.4 \cdot 10^{-79}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 5.8 \cdot 10^{-308}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.9 \cdot 10^{-235}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 6.7 \cdot 10^{-138}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 3.3 \cdot 10^{-27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 26000000000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 6
Accuracy62.6%
Cost6728
\[\begin{array}{l} \mathbf{if}\;p \leq -1 \cdot 10^{-178}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 2.6 \cdot 10^{-105}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 7
Accuracy16.4%
Cost256
\[\frac{-p}{x} \]

Error

Reproduce?

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