Given's Rotation SVD example

Percentage Accurate: 79.1% → 99.8%
Time: 6.0s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[10^{-150} < \left|x\right| \land \left|x\right| < 10^{+150}\]
\[\begin{array}{l} \\ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
	return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
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
public static double code(double p, double x) {
	return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x):
	return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
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 tmp = code(p, x)
	tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
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]
\begin{array}{l}

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 79.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
	return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
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
public static double code(double p, double x) {
	return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x):
	return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
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 tmp = code(p, x)
	tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
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]
\begin{array}{l}

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

Alternative 1: 99.8% accurate, 0.7× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\\ \mathbf{if}\;t_0 \leq -1:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(t_0 + 1\right)}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (/ x (sqrt (+ (* p (* 4.0 p)) (* x x))))))
   (if (<= t_0 -1.0) (/ (- p) x) (sqrt (* 0.5 (+ t_0 1.0))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = x / sqrt(((p * (4.0 * p)) + (x * x)));
	double tmp;
	if (t_0 <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = sqrt((0.5 * (t_0 + 1.0)));
	}
	return tmp;
}
NOTE: p should be positive before calling this 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(((p * (4.0d0 * p)) + (x * x)))
    if (t_0 <= (-1.0d0)) then
        tmp = -p / x
    else
        tmp = sqrt((0.5d0 * (t_0 + 1.0d0)))
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double t_0 = x / Math.sqrt(((p * (4.0 * p)) + (x * x)));
	double tmp;
	if (t_0 <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt((0.5 * (t_0 + 1.0)));
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = x / math.sqrt(((p * (4.0 * p)) + (x * x)))
	tmp = 0
	if t_0 <= -1.0:
		tmp = -p / x
	else:
		tmp = math.sqrt((0.5 * (t_0 + 1.0)))
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x))))
	tmp = 0.0
	if (t_0 <= -1.0)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(Float64(0.5 * Float64(t_0 + 1.0)));
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = x / sqrt(((p * (4.0 * p)) + (x * x)));
	tmp = 0.0;
	if (t_0 <= -1.0)
		tmp = -p / x;
	else
		tmp = sqrt((0.5 * (t_0 + 1.0)));
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $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[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}}\\
\mathbf{if}\;t_0 \leq -1:\\
\;\;\;\;\frac{-p}{x}\\

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


\end{array}
\end{array}
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 14.8%

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Taylor expanded in p around -inf 57.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    4. Step-by-step derivation
      1. associate-*r/57.4%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg57.4%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    5. Simplified57.4%

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

    if -1 < (/.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)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.7%

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

Alternative 2: 79.8% accurate, 1.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq -6.6 \cdot 10^{+42} \lor \neg \left(x \leq -3.2 \cdot 10^{-18}\right):\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (or (<= x -6.6e+42) (not (<= x -3.2e-18)))
   (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x)))))
   (/ (- p) x)))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if ((x <= -6.6e+42) || !(x <= -3.2e-18)) {
		tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
	} else {
		tmp = -p / x;
	}
	return tmp;
}
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if ((x <= -6.6e+42) || !(x <= -3.2e-18)) {
		tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
	} else {
		tmp = -p / x;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if (x <= -6.6e+42) or not (x <= -3.2e-18):
		tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x)))))
	else:
		tmp = -p / x
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if ((x <= -6.6e+42) || !(x <= -3.2e-18))
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x)))));
	else
		tmp = Float64(Float64(-p) / x);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if ((x <= -6.6e+42) || ~((x <= -3.2e-18)))
		tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
	else
		tmp = -p / x;
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[Or[LessEqual[x, -6.6e+42], N[Not[LessEqual[x, -3.2e-18]], $MachinePrecision]], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[((-p) / x), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.6 \cdot 10^{+42} \lor \neg \left(x \leq -3.2 \cdot 10^{-18}\right):\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{-p}{x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -6.5999999999999998e42 or -3.1999999999999999e-18 < x

    1. Initial program 83.8%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-sqr-sqrt83.8%

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

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(\sqrt{\left(4 \cdot p\right) \cdot p}, x\right)}}\right)} \]
      3. associate-*l*83.8%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}, x\right)}\right)} \]
      4. sqrt-prod83.8%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}, x\right)}\right)} \]
      5. metadata-eval83.8%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{2} \cdot \sqrt{p \cdot p}, x\right)}\right)} \]
      6. sqrt-unprod43.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}, x\right)}\right)} \]
      7. add-sqr-sqrt83.8%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)} \]
    3. Applied egg-rr83.8%

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

    if -6.5999999999999998e42 < x < -3.1999999999999999e-18

    1. Initial program 36.9%

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Taylor expanded in p around -inf 30.1%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    4. Step-by-step derivation
      1. associate-*r/30.1%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg30.1%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    5. Simplified30.1%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification76.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -6.6 \cdot 10^{+42} \lor \neg \left(x \leq -3.2 \cdot 10^{-18}\right):\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \]

Alternative 3: 67.2% accurate, 1.9× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq 3.1 \cdot 10^{-237}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9.6 \cdot 10^{-222}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 1.15 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9.2 \cdot 10^{-55} \lor \neg \left(p \leq 4.2 \cdot 10^{-24}\right) \land p \leq 350000:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (/ (- p) x)))
   (if (<= p 3.1e-237)
     1.0
     (if (<= p 9.6e-222)
       t_0
       (if (<= p 1.15e-189)
         1.0
         (if (or (<= p 9.2e-55) (and (not (<= p 4.2e-24)) (<= p 350000.0)))
           t_0
           (sqrt 0.5)))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (p <= 3.1e-237) {
		tmp = 1.0;
	} else if (p <= 9.6e-222) {
		tmp = t_0;
	} else if (p <= 1.15e-189) {
		tmp = 1.0;
	} else if ((p <= 9.2e-55) || (!(p <= 4.2e-24) && (p <= 350000.0))) {
		tmp = t_0;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
NOTE: p should be positive before calling this 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 = -p / x
    if (p <= 3.1d-237) then
        tmp = 1.0d0
    else if (p <= 9.6d-222) then
        tmp = t_0
    else if (p <= 1.15d-189) then
        tmp = 1.0d0
    else if ((p <= 9.2d-55) .or. (.not. (p <= 4.2d-24)) .and. (p <= 350000.0d0)) then
        tmp = t_0
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (p <= 3.1e-237) {
		tmp = 1.0;
	} else if (p <= 9.6e-222) {
		tmp = t_0;
	} else if (p <= 1.15e-189) {
		tmp = 1.0;
	} else if ((p <= 9.2e-55) || (!(p <= 4.2e-24) && (p <= 350000.0))) {
		tmp = t_0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = -p / x
	tmp = 0
	if p <= 3.1e-237:
		tmp = 1.0
	elif p <= 9.6e-222:
		tmp = t_0
	elif p <= 1.15e-189:
		tmp = 1.0
	elif (p <= 9.2e-55) or (not (p <= 4.2e-24) and (p <= 350000.0)):
		tmp = t_0
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(Float64(-p) / x)
	tmp = 0.0
	if (p <= 3.1e-237)
		tmp = 1.0;
	elseif (p <= 9.6e-222)
		tmp = t_0;
	elseif (p <= 1.15e-189)
		tmp = 1.0;
	elseif ((p <= 9.2e-55) || (!(p <= 4.2e-24) && (p <= 350000.0)))
		tmp = t_0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = -p / x;
	tmp = 0.0;
	if (p <= 3.1e-237)
		tmp = 1.0;
	elseif (p <= 9.6e-222)
		tmp = t_0;
	elseif (p <= 1.15e-189)
		tmp = 1.0;
	elseif ((p <= 9.2e-55) || (~((p <= 4.2e-24)) && (p <= 350000.0)))
		tmp = t_0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[((-p) / x), $MachinePrecision]}, If[LessEqual[p, 3.1e-237], 1.0, If[LessEqual[p, 9.6e-222], t$95$0, If[LessEqual[p, 1.15e-189], 1.0, If[Or[LessEqual[p, 9.2e-55], And[N[Not[LessEqual[p, 4.2e-24]], $MachinePrecision], LessEqual[p, 350000.0]]], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{-p}{x}\\
\mathbf{if}\;p \leq 3.1 \cdot 10^{-237}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 9.6 \cdot 10^{-222}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;p \leq 1.15 \cdot 10^{-189}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 9.2 \cdot 10^{-55} \lor \neg \left(p \leq 4.2 \cdot 10^{-24}\right) \land p \leq 350000:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < 3.0999999999999998e-237 or 9.59999999999999972e-222 < p < 1.1499999999999999e-189

    1. Initial program 74.9%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-sqr-sqrt74.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}} + x \cdot x}}\right)} \]
      2. hypot-def74.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(\sqrt{\left(4 \cdot p\right) \cdot p}, x\right)}}\right)} \]
      3. associate-*l*74.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}, x\right)}\right)} \]
      4. sqrt-prod74.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}, x\right)}\right)} \]
      5. metadata-eval74.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{2} \cdot \sqrt{p \cdot p}, x\right)}\right)} \]
      6. sqrt-unprod13.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}, x\right)}\right)} \]
      7. add-sqr-sqrt74.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)} \]
    3. Applied egg-rr74.9%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)} \]
    4. Step-by-step derivation
      1. add-sqr-sqrt74.3%

        \[\leadsto \color{blue}{\sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}} \cdot \sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}} \]
      2. pow274.3%

        \[\leadsto \color{blue}{{\left(\sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}\right)}^{2}} \]
      3. pow1/274.3%

        \[\leadsto {\left(\sqrt{\color{blue}{{\left(0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)\right)}^{0.5}}}\right)}^{2} \]
      4. sqrt-pow174.3%

        \[\leadsto {\color{blue}{\left({\left(0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]
      5. distribute-lft-in74.3%

        \[\leadsto {\left({\color{blue}{\left(0.5 \cdot 1 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      6. metadata-eval74.3%

        \[\leadsto {\left({\left(\color{blue}{0.5} + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      7. associate-*r/74.3%

        \[\leadsto {\left({\left(0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      8. metadata-eval74.3%

        \[\leadsto {\left({\left(0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{\color{blue}{0.25}}\right)}^{2} \]
    5. Applied egg-rr74.3%

      \[\leadsto \color{blue}{{\left({\left(0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{0.25}\right)}^{2}} \]
    6. Taylor expanded in x around inf 39.8%

      \[\leadsto \color{blue}{1} \]

    if 3.0999999999999998e-237 < p < 9.59999999999999972e-222 or 1.1499999999999999e-189 < p < 9.20000000000000046e-55 or 4.1999999999999999e-24 < p < 3.5e5

    1. Initial program 44.5%

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Taylor expanded in p around -inf 62.3%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    4. Step-by-step derivation
      1. associate-*r/62.3%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg62.3%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    5. Simplified62.3%

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

    if 9.20000000000000046e-55 < p < 4.1999999999999999e-24 or 3.5e5 < p

    1. Initial program 96.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 0 87.8%

      \[\leadsto \color{blue}{\sqrt{0.5}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification56.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 3.1 \cdot 10^{-237}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9.6 \cdot 10^{-222}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 1.15 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9.2 \cdot 10^{-55} \lor \neg \left(p \leq 4.2 \cdot 10^{-24}\right) \land p \leq 350000:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 4: 55.9% accurate, 35.5× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq -3.2 \cdot 10^{-150}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 (if (<= x -3.2e-150) (/ (- p) x) 1.0))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (x <= -3.2e-150) {
		tmp = -p / x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-3.2d-150)) then
        tmp = -p / x
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if (x <= -3.2e-150) {
		tmp = -p / x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if x <= -3.2e-150:
		tmp = -p / x
	else:
		tmp = 1.0
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (x <= -3.2e-150)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = 1.0;
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (x <= -3.2e-150)
		tmp = -p / x;
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[x, -3.2e-150], N[((-p) / x), $MachinePrecision], 1.0]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-150}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -3.1999999999999998e-150

    1. Initial program 55.4%

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Taylor expanded in p around -inf 31.5%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    4. Step-by-step derivation
      1. associate-*r/31.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg31.5%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    5. Simplified31.5%

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

    if -3.1999999999999998e-150 < 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. Step-by-step derivation
      1. add-sqr-sqrt100.0%

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

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(\sqrt{\left(4 \cdot p\right) \cdot p}, x\right)}}\right)} \]
      3. associate-*l*100.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}, x\right)}\right)} \]
      4. sqrt-prod100.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}, x\right)}\right)} \]
      5. metadata-eval100.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{2} \cdot \sqrt{p \cdot p}, x\right)}\right)} \]
      6. sqrt-unprod51.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}, x\right)}\right)} \]
      7. add-sqr-sqrt100.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)} \]
    3. Applied egg-rr100.0%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)} \]
    4. Step-by-step derivation
      1. add-sqr-sqrt99.2%

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

        \[\leadsto \color{blue}{{\left(\sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}\right)}^{2}} \]
      3. pow1/299.2%

        \[\leadsto {\left(\sqrt{\color{blue}{{\left(0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)\right)}^{0.5}}}\right)}^{2} \]
      4. sqrt-pow199.2%

        \[\leadsto {\color{blue}{\left({\left(0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]
      5. distribute-lft-in99.2%

        \[\leadsto {\left({\color{blue}{\left(0.5 \cdot 1 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      6. metadata-eval99.2%

        \[\leadsto {\left({\left(\color{blue}{0.5} + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      7. associate-*r/99.2%

        \[\leadsto {\left({\left(0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      8. metadata-eval99.2%

        \[\leadsto {\left({\left(0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{\color{blue}{0.25}}\right)}^{2} \]
    5. Applied egg-rr99.2%

      \[\leadsto \color{blue}{{\left({\left(0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{0.25}\right)}^{2}} \]
    6. Taylor expanded in x around inf 59.4%

      \[\leadsto \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -3.2 \cdot 10^{-150}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 5: 35.7% accurate, 215.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ 1 \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 1.0)
p = abs(p);
double code(double p, double x) {
	return 1.0;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = 1.0d0
end function
p = Math.abs(p);
public static double code(double p, double x) {
	return 1.0;
}
p = abs(p)
def code(p, x):
	return 1.0
p = abs(p)
function code(p, x)
	return 1.0
end
p = abs(p)
function tmp = code(p, x)
	tmp = 1.0;
end
NOTE: p should be positive before calling this function
code[p_, x_] := 1.0
\begin{array}{l}
p = |p|\\
\\
1
\end{array}
Derivation
  1. Initial program 77.4%

    \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
  2. Step-by-step derivation
    1. add-sqr-sqrt77.4%

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

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(\sqrt{\left(4 \cdot p\right) \cdot p}, x\right)}}\right)} \]
    3. associate-*l*77.3%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}, x\right)}\right)} \]
    4. sqrt-prod77.3%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}, x\right)}\right)} \]
    5. metadata-eval77.3%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{2} \cdot \sqrt{p \cdot p}, x\right)}\right)} \]
    6. sqrt-unprod40.8%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}, x\right)}\right)} \]
    7. add-sqr-sqrt77.3%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)} \]
  3. Applied egg-rr77.3%

    \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)} \]
  4. Step-by-step derivation
    1. add-sqr-sqrt76.6%

      \[\leadsto \color{blue}{\sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}} \cdot \sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}} \]
    2. pow276.6%

      \[\leadsto \color{blue}{{\left(\sqrt{\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}\right)}^{2}} \]
    3. pow1/276.6%

      \[\leadsto {\left(\sqrt{\color{blue}{{\left(0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)\right)}^{0.5}}}\right)}^{2} \]
    4. sqrt-pow176.6%

      \[\leadsto {\color{blue}{\left({\left(0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]
    5. distribute-lft-in76.6%

      \[\leadsto {\left({\color{blue}{\left(0.5 \cdot 1 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
    6. metadata-eval76.6%

      \[\leadsto {\left({\left(\color{blue}{0.5} + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
    7. associate-*r/76.6%

      \[\leadsto {\left({\left(0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
    8. metadata-eval76.6%

      \[\leadsto {\left({\left(0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{\color{blue}{0.25}}\right)}^{2} \]
  5. Applied egg-rr76.6%

    \[\leadsto \color{blue}{{\left({\left(0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}^{0.25}\right)}^{2}} \]
  6. Taylor expanded in x around inf 35.4%

    \[\leadsto \color{blue}{1} \]
  7. Final simplification35.4%

    \[\leadsto 1 \]

Developer target: 79.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x))))))
double code(double p, double x) {
	return sqrt((0.5 + (copysign(0.5, x) / hypot(1.0, ((2.0 * p) / x)))));
}
public static double code(double p, double x) {
	return Math.sqrt((0.5 + (Math.copySign(0.5, x) / Math.hypot(1.0, ((2.0 * p) / x)))));
}
def code(p, x):
	return math.sqrt((0.5 + (math.copysign(0.5, x) / math.hypot(1.0, ((2.0 * p) / x)))))
function code(p, x)
	return sqrt(Float64(0.5 + Float64(copysign(0.5, x) / hypot(1.0, Float64(Float64(2.0 * p) / x)))))
end
function tmp = code(p, x)
	tmp = sqrt((0.5 + ((sign(x) * abs(0.5)) / hypot(1.0, ((2.0 * p) / x)))));
end
code[p_, x_] := N[Sqrt[N[(0.5 + N[(N[With[{TMP1 = Abs[0.5], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] / N[Sqrt[1.0 ^ 2 + N[(N[(2.0 * p), $MachinePrecision] / x), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}
\end{array}

Reproduce

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