?

Average Error: 13.8 → 7.3
Time: 11.1s
Precision: binary64
Cost: 21060

?

\[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{\left(4 \cdot p\right) \cdot p + x \cdot x}} \leq -0.5:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{1}{\sqrt{4 \cdot \left(p \cdot p\right) + x \cdot x}} \cdot x\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 (+ (* (* 4.0 p) p) (* x x)))) -0.5)
   (/ p x)
   (sqrt (* 0.5 (+ 1.0 (* (/ 1.0 (sqrt (+ (* 4.0 (* p p)) (* x x)))) 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((((4.0 * p) * p) + (x * x)))) <= -0.5) {
		tmp = p / x;
	} else {
		tmp = sqrt((0.5 * (1.0 + ((1.0 / sqrt(((4.0 * (p * p)) + (x * x)))) * x))));
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x / sqrt((((4.0d0 * p) * p) + (x * x)))) <= (-0.5d0)) then
        tmp = p / x
    else
        tmp = sqrt((0.5d0 * (1.0d0 + ((1.0d0 / sqrt(((4.0d0 * (p * p)) + (x * x)))) * x))))
    end if
    code = tmp
end function
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((((4.0 * p) * p) + (x * x)))) <= -0.5) {
		tmp = p / x;
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + ((1.0 / Math.sqrt(((4.0 * (p * p)) + (x * x)))) * 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((((4.0 * p) * p) + (x * x)))) <= -0.5:
		tmp = p / x
	else:
		tmp = math.sqrt((0.5 * (1.0 + ((1.0 / math.sqrt(((4.0 * (p * p)) + (x * x)))) * 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(Float64(4.0 * p) * p) + Float64(x * x)))) <= -0.5)
		tmp = Float64(p / x);
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(Float64(1.0 / sqrt(Float64(Float64(4.0 * Float64(p * p)) + Float64(x * x)))) * 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((((4.0 * p) * p) + (x * x)))) <= -0.5)
		tmp = p / x;
	else
		tmp = sqrt((0.5 * (1.0 + ((1.0 / sqrt(((4.0 * (p * p)) + (x * x)))) * 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[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.5], N[(p / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(N[(1.0 / N[Sqrt[N[(N[(4.0 * N[(p * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * x), $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{\left(4 \cdot p\right) \cdot p + x \cdot x}} \leq -0.5:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{1}{\sqrt{4 \cdot \left(p \cdot p\right) + x \cdot x}} \cdot x\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original13.8
Target13.8
Herbie7.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 53.3

      \[\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 53.6

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

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

      [Start]53.6

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

      rational.json-simplify-2 [=>]53.6

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

      rational.json-simplify-2 [=>]53.6

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

      rational.json-simplify-8 [<=]53.6

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

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

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

      [Start]28.6

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

      rational.json-simplify-2 [=>]28.6

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

      rational.json-simplify-43 [=>]28.7

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

      rational.json-simplify-49 [=>]28.7

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

      exponential.json-simplify-20 [=>]28.2

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

      metadata-eval [=>]28.2

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

      metadata-eval [=>]28.2

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

      rational.json-simplify-6 [=>]28.2

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

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

    1. Initial program 0.0

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

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\sqrt{4 \cdot \left(p \cdot p\right) + x \cdot x}} \cdot x}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification7.3

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

Alternatives

Alternative 1
Error7.3
Cost20932
\[\begin{array}{l} t_0 := \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\\ \mathbf{if}\;t_0 \leq -0.5:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + t_0\right)}\\ \end{array} \]
Alternative 2
Error16.8
Cost14092
\[\begin{array}{l} \mathbf{if}\;x \leq -4.5 \cdot 10^{+109}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;x \leq -2.65 \cdot 10^{+32}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -7.5 \cdot 10^{-7}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{2 \cdot \frac{{p}^{2}}{x} + x}\right)}\\ \end{array} \]
Alternative 3
Error20.2
Cost7256
\[\begin{array}{l} \mathbf{if}\;p \leq -2.15 \cdot 10^{-26}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -5.5 \cdot 10^{-88}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq -4.2 \cdot 10^{-98}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 10^{-254}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 3.5 \cdot 10^{-240}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.6 \cdot 10^{-59}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Error20.1
Cost6860
\[\begin{array}{l} \mathbf{if}\;p \leq -6.6 \cdot 10^{-34}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -5.2 \cdot 10^{-278}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 5.9 \cdot 10^{-114}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Error46.6
Cost388
\[\begin{array}{l} \mathbf{if}\;p \leq -5.2 \cdot 10^{-278}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;-\frac{p}{x}\\ \end{array} \]
Alternative 6
Error53.2
Cost192
\[\frac{p}{x} \]

Error

Reproduce?

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