?

Average Error: 13.5 → 7.1
Time: 9.7s
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 -1:\\ \;\;\;\;\sqrt{2} \cdot \left(-\frac{-p}{\frac{-1}{\frac{\sqrt{0.5}}{x}}}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{\frac{2}{\sqrt{p \cdot \left(p \cdot 4\right) + x \cdot x}}}{\frac{4}{x}}}\\ \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)))) -1.0)
   (* (sqrt 2.0) (- (/ (- p) (/ -1.0 (/ (sqrt 0.5) x)))))
   (sqrt (+ 0.5 (/ (/ 2.0 (sqrt (+ (* p (* p 4.0)) (* x x)))) (/ 4.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((((4.0 * p) * p) + (x * x)))) <= -1.0) {
		tmp = sqrt(2.0) * -(-p / (-1.0 / (sqrt(0.5) / x)));
	} else {
		tmp = sqrt((0.5 + ((2.0 / sqrt(((p * (p * 4.0)) + (x * x)))) / (4.0 / 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)))) <= (-1.0d0)) then
        tmp = sqrt(2.0d0) * -(-p / ((-1.0d0) / (sqrt(0.5d0) / x)))
    else
        tmp = sqrt((0.5d0 + ((2.0d0 / sqrt(((p * (p * 4.0d0)) + (x * x)))) / (4.0d0 / 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)))) <= -1.0) {
		tmp = Math.sqrt(2.0) * -(-p / (-1.0 / (Math.sqrt(0.5) / x)));
	} else {
		tmp = Math.sqrt((0.5 + ((2.0 / Math.sqrt(((p * (p * 4.0)) + (x * x)))) / (4.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((((4.0 * p) * p) + (x * x)))) <= -1.0:
		tmp = math.sqrt(2.0) * -(-p / (-1.0 / (math.sqrt(0.5) / x)))
	else:
		tmp = math.sqrt((0.5 + ((2.0 / math.sqrt(((p * (p * 4.0)) + (x * x)))) / (4.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(Float64(4.0 * p) * p) + Float64(x * x)))) <= -1.0)
		tmp = Float64(sqrt(2.0) * Float64(-Float64(Float64(-p) / Float64(-1.0 / Float64(sqrt(0.5) / x)))));
	else
		tmp = sqrt(Float64(0.5 + Float64(Float64(2.0 / sqrt(Float64(Float64(p * Float64(p * 4.0)) + Float64(x * x)))) / Float64(4.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((((4.0 * p) * p) + (x * x)))) <= -1.0)
		tmp = sqrt(2.0) * -(-p / (-1.0 / (sqrt(0.5) / x)));
	else
		tmp = sqrt((0.5 + ((2.0 / sqrt(((p * (p * 4.0)) + (x * x)))) / (4.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[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[(N[Sqrt[2.0], $MachinePrecision] * (-N[((-p) / N[(-1.0 / N[(N[Sqrt[0.5], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision])), $MachinePrecision], N[Sqrt[N[(0.5 + N[(N[(2.0 / N[Sqrt[N[(N[(p * N[(p * 4.0), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(4.0 / 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 -1:\\
\;\;\;\;\sqrt{2} \cdot \left(-\frac{-p}{\frac{-1}{\frac{\sqrt{0.5}}{x}}}\right)\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original13.5
Target13.5
Herbie7.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 54.1

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

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

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

      [Start]28.1

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

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

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

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

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

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

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

      rational.json-simplify-9 [=>]28.1

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

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

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

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

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

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

    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)} \]
    2. Applied egg-rr0.2

      \[\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. Applied egg-rr0.2

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

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

      [Start]0.2

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

      rational.json-simplify-4 [=>]0.2

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

      metadata-eval [<=]0.2

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

      rational.json-simplify-49 [<=]0.2

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

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

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

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

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

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

Alternatives

Alternative 1
Error7.1
Cost20932
\[\begin{array}{l} t_0 := \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\\ \mathbf{if}\;t_0 \leq -1:\\ \;\;\;\;\sqrt{2} \cdot \left(-\frac{-p}{\frac{-1}{\frac{\sqrt{0.5}}{x}}}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + t_0\right)}\\ \end{array} \]
Alternative 2
Error7.1
Cost20932
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \leq -1:\\ \;\;\;\;\sqrt{2} \cdot \left(-\frac{-p}{\frac{-1}{\frac{\sqrt{0.5}}{x}}}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{\frac{\sqrt{p \cdot \left(p \cdot 4\right) + x \cdot x}}{x}}}\\ \end{array} \]
Alternative 3
Error16.8
Cost14224
\[\begin{array}{l} t_0 := \sqrt{0.5 \cdot \left(1 + \frac{x}{2 \cdot \frac{{p}^{2}}{x} + x}\right)}\\ t_1 := \sqrt{0.5} \cdot p\\ \mathbf{if}\;p \leq -5.7 \cdot 10^{-275}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 8.8 \cdot 10^{-252}:\\ \;\;\;\;-1 \cdot \frac{\sqrt{2} \cdot t_1}{x}\\ \mathbf{elif}\;p \leq 5.2 \cdot 10^{-72}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 1.45 \cdot 10^{-49}:\\ \;\;\;\;t_1 \cdot \left(-\frac{\sqrt{2}}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error20.5
Cost13772
\[\begin{array}{l} \mathbf{if}\;p \leq -1.35 \cdot 10^{-38}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{-0.25 \cdot \frac{x}{p} + -2 \cdot \frac{p}{x}}}\\ \mathbf{elif}\;p \leq -8.8 \cdot 10^{-275}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.2 \cdot 10^{-253}:\\ \;\;\;\;-1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}\\ \mathbf{elif}\;p \leq 4 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{2 \cdot \frac{p}{x} + 0.25 \cdot \frac{x}{p}}}\\ \end{array} \]
Alternative 5
Error20.4
Cost13708
\[\begin{array}{l} \mathbf{if}\;p \leq -6 \cdot 10^{-44}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{-0.25 \cdot \frac{x}{p} + -2 \cdot \frac{p}{x}}}\\ \mathbf{elif}\;p \leq -4.8 \cdot 10^{-275}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.95 \cdot 10^{-254}:\\ \;\;\;\;-\sqrt{2} \cdot \frac{p \cdot \sqrt{0.5}}{x}\\ \mathbf{elif}\;p \leq 1.05 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{2 \cdot \frac{p}{x} + 0.25 \cdot \frac{x}{p}}}\\ \end{array} \]
Alternative 6
Error20.5
Cost13708
\[\begin{array}{l} \mathbf{if}\;p \leq -1.9 \cdot 10^{-36}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{-0.25 \cdot \frac{x}{p} + -2 \cdot \frac{p}{x}}}\\ \mathbf{elif}\;p \leq -4 \cdot 10^{-275}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 8.2 \cdot 10^{-254}:\\ \;\;\;\;\left(\sqrt{0.5} \cdot p\right) \cdot \left(-\frac{\sqrt{2}}{x}\right)\\ \mathbf{elif}\;p \leq 3.9 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{2 \cdot \frac{p}{x} + 0.25 \cdot \frac{x}{p}}}\\ \end{array} \]
Alternative 7
Error20.5
Cost7888
\[\begin{array}{l} \mathbf{if}\;p \leq -1.5 \cdot 10^{-43}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{-0.25 \cdot \frac{x}{p} + -2 \cdot \frac{p}{x}}}\\ \mathbf{elif}\;p \leq -1.95 \cdot 10^{-274}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 3.5 \cdot 10^{-301}:\\ \;\;\;\;\sqrt{0}\\ \mathbf{elif}\;p \leq 3.5 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{2 \cdot \frac{p}{x} + 0.25 \cdot \frac{x}{p}}}\\ \end{array} \]
Alternative 8
Error20.7
Cost7492
\[\begin{array}{l} \mathbf{if}\;p \leq -3.8 \cdot 10^{-41}:\\ \;\;\;\;\sqrt{0.5 + \frac{0.5}{-0.25 \cdot \frac{x}{p} + -2 \cdot \frac{p}{x}}}\\ \mathbf{elif}\;p \leq -8.8 \cdot 10^{-275}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.9 \cdot 10^{-304}:\\ \;\;\;\;\sqrt{0}\\ \mathbf{elif}\;p \leq 4 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 9
Error20.9
Cost6992
\[\begin{array}{l} \mathbf{if}\;p \leq -3 \cdot 10^{-40}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -8.8 \cdot 10^{-275}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 4.5 \cdot 10^{-301}:\\ \;\;\;\;\sqrt{0}\\ \mathbf{elif}\;p \leq 3 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 10
Error20.3
Cost6728
\[\begin{array}{l} \mathbf{if}\;p \leq -4.4 \cdot 10^{-41}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 3.8 \cdot 10^{-91}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 11
Error28.7
Cost6464
\[\sqrt{0.5} \]

Error

Reproduce?

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