?

Average Error: 13.2 → 7.0
Time: 35.9s
Precision: binary64
Cost: 20932

?

\[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} t_0 := \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\\ \mathbf{if}\;t_0 \leq -1:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + t_0\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
 (let* ((t_0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))
   (if (<= t_0 -1.0) (- (/ p x)) (sqrt (* 0.5 (+ 1.0 t_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 t_0 = x / sqrt((((4.0 * p) * p) + (x * x)));
	double tmp;
	if (t_0 <= -1.0) {
		tmp = -(p / x);
	} else {
		tmp = sqrt((0.5 * (1.0 + t_0)));
	}
	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) :: t_0
    real(8) :: tmp
    t_0 = x / sqrt((((4.0d0 * p) * p) + (x * x)))
    if (t_0 <= (-1.0d0)) then
        tmp = -(p / x)
    else
        tmp = sqrt((0.5d0 * (1.0d0 + t_0)))
    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 t_0 = x / Math.sqrt((((4.0 * p) * p) + (x * x)));
	double tmp;
	if (t_0 <= -1.0) {
		tmp = -(p / x);
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + t_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):
	t_0 = x / math.sqrt((((4.0 * p) * p) + (x * x)))
	tmp = 0
	if t_0 <= -1.0:
		tmp = -(p / x)
	else:
		tmp = math.sqrt((0.5 * (1.0 + t_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)
	t_0 = Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))
	tmp = 0.0
	if (t_0 <= -1.0)
		tmp = Float64(-Float64(p / x));
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + t_0)));
	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)
	t_0 = x / sqrt((((4.0 * p) * p) + (x * x)));
	tmp = 0.0;
	if (t_0 <= -1.0)
		tmp = -(p / x);
	else
		tmp = sqrt((0.5 * (1.0 + t_0)));
	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_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1.0], (-N[(p / x), $MachinePrecision]), N[Sqrt[N[(0.5 * N[(1.0 + t$95$0), $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}
t_0 := \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\\
\mathbf{if}\;t_0 \leq -1:\\
\;\;\;\;-\frac{p}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + t_0\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original13.2
Target13.3
Herbie7.0
\[\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 53.5

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

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

      [Start]53.5

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

      rational_best-simplify-59 [=>]53.5

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

      rational_best-simplify-11 [=>]53.5

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

      rational_best-simplify-1 [=>]53.5

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

      rational_best-simplify-1 [=>]53.5

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

      rational_best-simplify-1 [=>]53.5

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

      rational_best-simplify-50 [=>]53.5

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

      rational_best-simplify-50 [=>]53.5

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

      metadata-eval [=>]53.5

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

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

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

      [Start]28.4

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

      rational_best-simplify-1 [=>]28.4

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

      rational_best-simplify-10 [=>]28.4

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

      rational_best-simplify-50 [=>]28.5

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

      rational_best-simplify-1 [<=]28.5

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

      exponential-simplify-21 [=>]28.0

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

      metadata-eval [=>]28.0

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

      metadata-eval [=>]28.0

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

      rational_best-simplify-7 [=>]28.0

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

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

    1. Initial program 0.2

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

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

Alternatives

Alternative 1
Error20.3
Cost6980
\[\begin{array}{l} \mathbf{if}\;p \leq -1.1 \cdot 10^{-9}:\\ \;\;\;\;\sqrt{0.5 + \frac{x}{p \cdot -4}}\\ \mathbf{elif}\;p \leq 1.2 \cdot 10^{-239}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.05 \cdot 10^{-78}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 2
Error20.4
Cost6860
\[\begin{array}{l} \mathbf{if}\;p \leq -5.1 \cdot 10^{-104}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -5 \cdot 10^{-308}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 2.2 \cdot 10^{-79}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 3
Error20.2
Cost6860
\[\begin{array}{l} \mathbf{if}\;p \leq -4.8 \cdot 10^{-10}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 1.2 \cdot 10^{-239}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 10^{-76}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 4
Error46.8
Cost388
\[\begin{array}{l} \mathbf{if}\;p \leq -5 \cdot 10^{-308}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;-\frac{p}{x}\\ \end{array} \]
Alternative 5
Error53.1
Cost192
\[\frac{p}{x} \]

Error

Reproduce?

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