Given's Rotation SVD example

Percentage Accurate: 79.0% → 99.8%
Time: 7.4s
Alternatives: 8
Speedup: 0.7×

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 8 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.0% 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} \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 + \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)} \cdot 0.5}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
   (/ (- p) x)
   (sqrt (+ 0.5 (* (/ x (hypot x (* p 2.0))) 0.5)))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = sqrt((0.5 + ((x / hypot(x, (p * 2.0))) * 0.5)));
	}
	return tmp;
}
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt((0.5 + ((x / Math.hypot(x, (p * 2.0))) * 0.5)));
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0:
		tmp = -p / x
	else:
		tmp = math.sqrt((0.5 + ((x / math.hypot(x, (p * 2.0))) * 0.5)))
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -1.0)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(Float64(0.5 + Float64(Float64(x / hypot(x, Float64(p * 2.0))) * 0.5)));
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0)
		tmp = -p / x;
	else
		tmp = sqrt((0.5 + ((x / hypot(x, (p * 2.0))) * 0.5)));
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
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], -1.0], N[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 + N[(N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\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 + \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)} \cdot 0.5}\\


\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. Step-by-step derivation
      1. +-commutative14.8%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + 0.5}} \]
    4. Taylor expanded in x around -inf 50.3%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-150.3%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac50.3%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    6. Simplified50.3%

      \[\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)} \]
    2. Step-by-step derivation
      1. +-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\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 + \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)} \cdot 0.5}\\ \end{array} \]

Alternative 2: 77.0% accurate, 1.7× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;x \leq -3.8 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -1.2:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -1.5 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -2.3 \cdot 10^{-82}:\\ \;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\ \mathbf{elif}\;x \leq -3.2 \cdot 10^{-137}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{x + 2 \cdot \frac{p \cdot p}{x}}\right)}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (/ (- p) x)))
   (if (<= x -3.8e+49)
     t_0
     (if (<= x -1.2)
       (sqrt 0.5)
       (if (<= x -1.5e-9)
         t_0
         (if (<= x -2.3e-82)
           (sqrt (+ 0.5 (* 0.25 (/ x p))))
           (if (<= x -3.2e-137)
             t_0
             (sqrt (* 0.5 (+ 1.0 (/ x (+ x (* 2.0 (/ (* p p) x))))))))))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (x <= -3.8e+49) {
		tmp = t_0;
	} else if (x <= -1.2) {
		tmp = sqrt(0.5);
	} else if (x <= -1.5e-9) {
		tmp = t_0;
	} else if (x <= -2.3e-82) {
		tmp = sqrt((0.5 + (0.25 * (x / p))));
	} else if (x <= -3.2e-137) {
		tmp = t_0;
	} else {
		tmp = sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x)))))));
	}
	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 (x <= (-3.8d+49)) then
        tmp = t_0
    else if (x <= (-1.2d0)) then
        tmp = sqrt(0.5d0)
    else if (x <= (-1.5d-9)) then
        tmp = t_0
    else if (x <= (-2.3d-82)) then
        tmp = sqrt((0.5d0 + (0.25d0 * (x / p))))
    else if (x <= (-3.2d-137)) then
        tmp = t_0
    else
        tmp = sqrt((0.5d0 * (1.0d0 + (x / (x + (2.0d0 * ((p * p) / x)))))))
    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 (x <= -3.8e+49) {
		tmp = t_0;
	} else if (x <= -1.2) {
		tmp = Math.sqrt(0.5);
	} else if (x <= -1.5e-9) {
		tmp = t_0;
	} else if (x <= -2.3e-82) {
		tmp = Math.sqrt((0.5 + (0.25 * (x / p))));
	} else if (x <= -3.2e-137) {
		tmp = t_0;
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x)))))));
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = -p / x
	tmp = 0
	if x <= -3.8e+49:
		tmp = t_0
	elif x <= -1.2:
		tmp = math.sqrt(0.5)
	elif x <= -1.5e-9:
		tmp = t_0
	elif x <= -2.3e-82:
		tmp = math.sqrt((0.5 + (0.25 * (x / p))))
	elif x <= -3.2e-137:
		tmp = t_0
	else:
		tmp = math.sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x)))))))
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(Float64(-p) / x)
	tmp = 0.0
	if (x <= -3.8e+49)
		tmp = t_0;
	elseif (x <= -1.2)
		tmp = sqrt(0.5);
	elseif (x <= -1.5e-9)
		tmp = t_0;
	elseif (x <= -2.3e-82)
		tmp = sqrt(Float64(0.5 + Float64(0.25 * Float64(x / p))));
	elseif (x <= -3.2e-137)
		tmp = t_0;
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / Float64(x + Float64(2.0 * Float64(Float64(p * p) / x)))))));
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = -p / x;
	tmp = 0.0;
	if (x <= -3.8e+49)
		tmp = t_0;
	elseif (x <= -1.2)
		tmp = sqrt(0.5);
	elseif (x <= -1.5e-9)
		tmp = t_0;
	elseif (x <= -2.3e-82)
		tmp = sqrt((0.5 + (0.25 * (x / p))));
	elseif (x <= -3.2e-137)
		tmp = t_0;
	else
		tmp = sqrt((0.5 * (1.0 + (x / (x + (2.0 * ((p * p) / x)))))));
	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[x, -3.8e+49], t$95$0, If[LessEqual[x, -1.2], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[x, -1.5e-9], t$95$0, If[LessEqual[x, -2.3e-82], N[Sqrt[N[(0.5 + N[(0.25 * N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[x, -3.2e-137], t$95$0, N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[(x + N[(2.0 * N[(N[(p * p), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{-p}{x}\\
\mathbf{if}\;x \leq -3.8 \cdot 10^{+49}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -1.2:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;x \leq -1.5 \cdot 10^{-9}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -2.3 \cdot 10^{-82}:\\
\;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\

\mathbf{elif}\;x \leq -3.2 \cdot 10^{-137}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if x < -3.7999999999999999e49 or -1.19999999999999996 < x < -1.49999999999999999e-9 or -2.29999999999999997e-82 < x < -3.20000000000000021e-137

    1. Initial program 43.1%

      \[\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. +-commutative43.1%

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

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt43.1%

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + 0.5}} \]
    4. Taylor expanded in x around -inf 36.1%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-136.1%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac36.1%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    6. Simplified36.1%

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

    if -3.7999999999999999e49 < x < -1.19999999999999996

    1. Initial program 74.0%

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

      \[\leadsto \color{blue}{\sqrt{0.5}} \]

    if -1.49999999999999999e-9 < x < -2.29999999999999997e-82

    1. Initial program 65.7%

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

      \[\leadsto \sqrt{\color{blue}{0.5 + 0.25 \cdot \frac{x}{p}}} \]

    if -3.20000000000000021e-137 < x

    1. Initial program 98.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 p around 0 98.3%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{x + 2 \cdot \frac{{p}^{2}}{x}}}\right)} \]
    3. Step-by-step derivation
      1. unpow298.3%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -3.8 \cdot 10^{+49}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;x \leq -1.2:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -1.5 \cdot 10^{-9}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;x \leq -2.3 \cdot 10^{-82}:\\ \;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\ \mathbf{elif}\;x \leq -3.2 \cdot 10^{-137}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{x + 2 \cdot \frac{p \cdot p}{x}}\right)}\\ \end{array} \]

Alternative 3: 64.2% accurate, 1.9× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;p \leq 5 \cdot 10^{-180}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{elif}\;p \leq 6.2 \cdot 10^{-31}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 3.8 \cdot 10^{-5} \lor \neg \left(p \leq 1.05 \cdot 10^{+68}\right):\\ \;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= p 5e-180)
   (+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))
   (if (<= p 6.2e-31)
     (/ (- p) x)
     (if (or (<= p 3.8e-5) (not (<= p 1.05e+68)))
       (sqrt (+ 0.5 (* 0.25 (/ x p))))
       1.0))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (p <= 5e-180) {
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	} else if (p <= 6.2e-31) {
		tmp = -p / x;
	} else if ((p <= 3.8e-5) || !(p <= 1.05e+68)) {
		tmp = sqrt((0.5 + (0.25 * (x / p))));
	} 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 (p <= 5d-180) then
        tmp = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
    else if (p <= 6.2d-31) then
        tmp = -p / x
    else if ((p <= 3.8d-5) .or. (.not. (p <= 1.05d+68))) then
        tmp = sqrt((0.5d0 + (0.25d0 * (x / p))))
    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 (p <= 5e-180) {
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	} else if (p <= 6.2e-31) {
		tmp = -p / x;
	} else if ((p <= 3.8e-5) || !(p <= 1.05e+68)) {
		tmp = Math.sqrt((0.5 + (0.25 * (x / p))));
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if p <= 5e-180:
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)))
	elif p <= 6.2e-31:
		tmp = -p / x
	elif (p <= 3.8e-5) or not (p <= 1.05e+68):
		tmp = math.sqrt((0.5 + (0.25 * (x / p))))
	else:
		tmp = 1.0
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (p <= 5e-180)
		tmp = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p))));
	elseif (p <= 6.2e-31)
		tmp = Float64(Float64(-p) / x);
	elseif ((p <= 3.8e-5) || !(p <= 1.05e+68))
		tmp = sqrt(Float64(0.5 + Float64(0.25 * Float64(x / p))));
	else
		tmp = 1.0;
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= 5e-180)
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	elseif (p <= 6.2e-31)
		tmp = -p / x;
	elseif ((p <= 3.8e-5) || ~((p <= 1.05e+68)))
		tmp = sqrt((0.5 + (0.25 * (x / p))));
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[p, 5e-180], N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 6.2e-31], N[((-p) / x), $MachinePrecision], If[Or[LessEqual[p, 3.8e-5], N[Not[LessEqual[p, 1.05e+68]], $MachinePrecision]], N[Sqrt[N[(0.5 + N[(0.25 * N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 1.0]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 5 \cdot 10^{-180}:\\
\;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\

\mathbf{elif}\;p \leq 6.2 \cdot 10^{-31}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{elif}\;p \leq 3.8 \cdot 10^{-5} \lor \neg \left(p \leq 1.05 \cdot 10^{+68}\right):\\
\;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if p < 5.0000000000000001e-180

    1. Initial program 74.6%

      \[\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. +-commutative74.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in74.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt74.6%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot \frac{{p}^{2}}{{x}^{2}}} \]
    5. Step-by-step derivation
      1. unpow234.5%

        \[\leadsto 1 + -0.5 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}} \]
      2. unpow234.5%

        \[\leadsto 1 + -0.5 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}} \]
      3. times-frac34.5%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)} \]
      4. unpow234.5%

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot {\left(\frac{p}{x}\right)}^{2}} \]
    7. Step-by-step derivation
      1. unpow234.5%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)} \]
      2. clear-num34.5%

        \[\leadsto 1 + -0.5 \cdot \left(\frac{p}{x} \cdot \color{blue}{\frac{1}{\frac{x}{p}}}\right) \]
      3. un-div-inv34.5%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\frac{\frac{p}{x}}{\frac{x}{p}}} \]
    8. Applied egg-rr34.5%

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

    if 5.0000000000000001e-180 < p < 6.19999999999999999e-31

    1. Initial program 42.6%

      \[\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. +-commutative42.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in42.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt42.6%

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + 0.5}} \]
    4. Taylor expanded in x around -inf 61.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-161.4%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac61.4%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    6. Simplified61.4%

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

    if 6.19999999999999999e-31 < p < 3.8000000000000002e-5 or 1.05e68 < p

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

      \[\leadsto \sqrt{\color{blue}{0.5 + 0.25 \cdot \frac{x}{p}}} \]

    if 3.8000000000000002e-5 < p < 1.05e68

    1. Initial program 67.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. +-commutative67.8%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{1}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification52.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 5 \cdot 10^{-180}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{elif}\;p \leq 6.2 \cdot 10^{-31}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 3.8 \cdot 10^{-5} \lor \neg \left(p \leq 1.05 \cdot 10^{+68}\right):\\ \;\;\;\;\sqrt{0.5 + 0.25 \cdot \frac{x}{p}}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 4: 66.2% accurate, 2.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := 1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{if}\;p \leq 4.3 \cdot 10^{-182}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 5.6 \cdot 10^{-35}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 0.00048:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\ \;\;\;\;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 (+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))))
   (if (<= p 4.3e-182)
     t_0
     (if (<= p 5.6e-35)
       (/ (- p) x)
       (if (<= p 0.00048) (sqrt 0.5) (if (<= p 4.75e+33) t_0 (sqrt 0.5)))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = 1.0 + (-0.5 * ((p / x) / (x / p)));
	double tmp;
	if (p <= 4.3e-182) {
		tmp = t_0;
	} else if (p <= 5.6e-35) {
		tmp = -p / x;
	} else if (p <= 0.00048) {
		tmp = sqrt(0.5);
	} else if (p <= 4.75e+33) {
		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 = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
    if (p <= 4.3d-182) then
        tmp = t_0
    else if (p <= 5.6d-35) then
        tmp = -p / x
    else if (p <= 0.00048d0) then
        tmp = sqrt(0.5d0)
    else if (p <= 4.75d+33) 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 = 1.0 + (-0.5 * ((p / x) / (x / p)));
	double tmp;
	if (p <= 4.3e-182) {
		tmp = t_0;
	} else if (p <= 5.6e-35) {
		tmp = -p / x;
	} else if (p <= 0.00048) {
		tmp = Math.sqrt(0.5);
	} else if (p <= 4.75e+33) {
		tmp = t_0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = 1.0 + (-0.5 * ((p / x) / (x / p)))
	tmp = 0
	if p <= 4.3e-182:
		tmp = t_0
	elif p <= 5.6e-35:
		tmp = -p / x
	elif p <= 0.00048:
		tmp = math.sqrt(0.5)
	elif p <= 4.75e+33:
		tmp = t_0
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p))))
	tmp = 0.0
	if (p <= 4.3e-182)
		tmp = t_0;
	elseif (p <= 5.6e-35)
		tmp = Float64(Float64(-p) / x);
	elseif (p <= 0.00048)
		tmp = sqrt(0.5);
	elseif (p <= 4.75e+33)
		tmp = t_0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = 1.0 + (-0.5 * ((p / x) / (x / p)));
	tmp = 0.0;
	if (p <= 4.3e-182)
		tmp = t_0;
	elseif (p <= 5.6e-35)
		tmp = -p / x;
	elseif (p <= 0.00048)
		tmp = sqrt(0.5);
	elseif (p <= 4.75e+33)
		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[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p, 4.3e-182], t$95$0, If[LessEqual[p, 5.6e-35], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 0.00048], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, 4.75e+33], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := 1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\
\mathbf{if}\;p \leq 4.3 \cdot 10^{-182}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;p \leq 5.6 \cdot 10^{-35}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{elif}\;p \leq 0.00048:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < 4.3e-182 or 4.80000000000000012e-4 < p < 4.7500000000000002e33

    1. Initial program 74.6%

      \[\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. +-commutative74.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in74.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt74.6%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot \frac{{p}^{2}}{{x}^{2}}} \]
    5. Step-by-step derivation
      1. unpow236.0%

        \[\leadsto 1 + -0.5 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}} \]
      2. unpow236.0%

        \[\leadsto 1 + -0.5 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}} \]
      3. times-frac36.0%

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

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot {\left(\frac{p}{x}\right)}^{2}} \]
    7. Step-by-step derivation
      1. unpow236.0%

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

        \[\leadsto 1 + -0.5 \cdot \left(\frac{p}{x} \cdot \color{blue}{\frac{1}{\frac{x}{p}}}\right) \]
      3. un-div-inv36.0%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\frac{\frac{p}{x}}{\frac{x}{p}}} \]
    8. Applied egg-rr36.0%

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

    if 4.3e-182 < p < 5.5999999999999999e-35

    1. Initial program 42.6%

      \[\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. +-commutative42.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in42.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt42.6%

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + 0.5}} \]
    4. Taylor expanded in x around -inf 61.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-161.4%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac61.4%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    6. Simplified61.4%

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

    if 5.5999999999999999e-35 < p < 4.80000000000000012e-4 or 4.7500000000000002e33 < p

    1. Initial program 92.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 0 88.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 4.3 \cdot 10^{-182}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{elif}\;p \leq 5.6 \cdot 10^{-35}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 0.00048:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 5: 66.8% accurate, 2.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;p \leq 3.5 \cdot 10^{-180}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{elif}\;p \leq 2 \cdot 10^{-34}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 0.135:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= p 3.5e-180)
   (+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))
   (if (<= p 2e-34)
     (/ (- p) x)
     (if (<= p 0.135) (sqrt 0.5) (if (<= p 4.75e+33) 1.0 (sqrt 0.5))))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (p <= 3.5e-180) {
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	} else if (p <= 2e-34) {
		tmp = -p / x;
	} else if (p <= 0.135) {
		tmp = sqrt(0.5);
	} else if (p <= 4.75e+33) {
		tmp = 1.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) :: tmp
    if (p <= 3.5d-180) then
        tmp = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
    else if (p <= 2d-34) then
        tmp = -p / x
    else if (p <= 0.135d0) then
        tmp = sqrt(0.5d0)
    else if (p <= 4.75d+33) then
        tmp = 1.0d0
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if (p <= 3.5e-180) {
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	} else if (p <= 2e-34) {
		tmp = -p / x;
	} else if (p <= 0.135) {
		tmp = Math.sqrt(0.5);
	} else if (p <= 4.75e+33) {
		tmp = 1.0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if p <= 3.5e-180:
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)))
	elif p <= 2e-34:
		tmp = -p / x
	elif p <= 0.135:
		tmp = math.sqrt(0.5)
	elif p <= 4.75e+33:
		tmp = 1.0
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (p <= 3.5e-180)
		tmp = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p))));
	elseif (p <= 2e-34)
		tmp = Float64(Float64(-p) / x);
	elseif (p <= 0.135)
		tmp = sqrt(0.5);
	elseif (p <= 4.75e+33)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= 3.5e-180)
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	elseif (p <= 2e-34)
		tmp = -p / x;
	elseif (p <= 0.135)
		tmp = sqrt(0.5);
	elseif (p <= 4.75e+33)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[p, 3.5e-180], N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[p, 2e-34], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 0.135], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, 4.75e+33], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 3.5 \cdot 10^{-180}:\\
\;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\

\mathbf{elif}\;p \leq 2 \cdot 10^{-34}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{elif}\;p \leq 0.135:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if p < 3.5000000000000001e-180

    1. Initial program 74.6%

      \[\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. +-commutative74.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in74.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt74.6%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot \frac{{p}^{2}}{{x}^{2}}} \]
    5. Step-by-step derivation
      1. unpow234.5%

        \[\leadsto 1 + -0.5 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}} \]
      2. unpow234.5%

        \[\leadsto 1 + -0.5 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}} \]
      3. times-frac34.5%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)} \]
      4. unpow234.5%

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot {\left(\frac{p}{x}\right)}^{2}} \]
    7. Step-by-step derivation
      1. unpow234.5%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)} \]
      2. clear-num34.5%

        \[\leadsto 1 + -0.5 \cdot \left(\frac{p}{x} \cdot \color{blue}{\frac{1}{\frac{x}{p}}}\right) \]
      3. un-div-inv34.5%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\frac{\frac{p}{x}}{\frac{x}{p}}} \]
    8. Applied egg-rr34.5%

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

    if 3.5000000000000001e-180 < p < 1.99999999999999986e-34

    1. Initial program 42.6%

      \[\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. +-commutative42.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in42.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt42.6%

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + 0.5}} \]
    4. Taylor expanded in x around -inf 61.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-161.4%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac61.4%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    6. Simplified61.4%

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

    if 1.99999999999999986e-34 < p < 0.13500000000000001 or 4.7500000000000002e33 < p

    1. Initial program 92.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 0 88.9%

      \[\leadsto \color{blue}{\sqrt{0.5}} \]

    if 0.13500000000000001 < p < 4.7500000000000002e33

    1. Initial program 75.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. +-commutative75.9%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{1}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification52.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 3.5 \cdot 10^{-180}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \mathbf{elif}\;p \leq 2 \cdot 10^{-34}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 0.135:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 4.75 \cdot 10^{+33}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 6: 51.5% accurate, 16.5× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 3.9 \cdot 10^{-228}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= x 3.9e-228) (/ (- p) x) (+ 1.0 (* -0.5 (/ (/ p x) (/ x p))))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (x <= 3.9e-228) {
		tmp = -p / x;
	} else {
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	}
	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.9d-228) then
        tmp = -p / x
    else
        tmp = 1.0d0 + ((-0.5d0) * ((p / x) / (x / p)))
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if (x <= 3.9e-228) {
		tmp = -p / x;
	} else {
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if x <= 3.9e-228:
		tmp = -p / x
	else:
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)))
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (x <= 3.9e-228)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = Float64(1.0 + Float64(-0.5 * Float64(Float64(p / x) / Float64(x / p))));
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (x <= 3.9e-228)
		tmp = -p / x;
	else
		tmp = 1.0 + (-0.5 * ((p / x) / (x / p)));
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[x, 3.9e-228], N[((-p) / x), $MachinePrecision], N[(1.0 + N[(-0.5 * N[(N[(p / x), $MachinePrecision] / N[(x / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.9 \cdot 10^{-228}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{else}:\\
\;\;\;\;1 + -0.5 \cdot \frac{\frac{p}{x}}{\frac{x}{p}}\\


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

    1. Initial program 51.6%

      \[\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. +-commutative51.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in51.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt51.6%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-130.1%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac30.1%

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

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

    if 3.90000000000000029e-228 < 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. +-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot \frac{{p}^{2}}{{x}^{2}}} \]
    5. Step-by-step derivation
      1. unpow248.8%

        \[\leadsto 1 + -0.5 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}} \]
      2. unpow248.8%

        \[\leadsto 1 + -0.5 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}} \]
      3. times-frac48.8%

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

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

      \[\leadsto \color{blue}{1 + -0.5 \cdot {\left(\frac{p}{x}\right)}^{2}} \]
    7. Step-by-step derivation
      1. unpow248.8%

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

        \[\leadsto 1 + -0.5 \cdot \left(\frac{p}{x} \cdot \color{blue}{\frac{1}{\frac{x}{p}}}\right) \]
      3. un-div-inv48.8%

        \[\leadsto 1 + -0.5 \cdot \color{blue}{\frac{\frac{p}{x}}{\frac{x}{p}}} \]
    8. Applied egg-rr48.8%

      \[\leadsto 1 + -0.5 \cdot \color{blue}{\frac{\frac{p}{x}}{\frac{x}{p}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification39.7%

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

Alternative 7: 28.8% accurate, 35.5× speedup?

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

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


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

    1. Initial program 51.6%

      \[\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. +-commutative51.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. distribute-rgt-in51.6%

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

        \[\leadsto \sqrt{\frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot 0.5 + 1 \cdot 0.5} \]
      4. add-sqr-sqrt51.6%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    5. Step-by-step derivation
      1. neg-mul-130.1%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-neg-frac30.1%

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

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

    if -4.999999999999985e-310 < 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. Taylor expanded in x around -inf 4.3%

      \[\leadsto \sqrt{\color{blue}{\frac{{p}^{2}}{{x}^{2}}}} \]
    3. Step-by-step derivation
      1. unpow24.3%

        \[\leadsto \sqrt{\frac{\color{blue}{p \cdot p}}{{x}^{2}}} \]
      2. unpow24.3%

        \[\leadsto \sqrt{\frac{p \cdot p}{\color{blue}{x \cdot x}}} \]
      3. times-frac4.6%

        \[\leadsto \sqrt{\color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    4. Simplified4.6%

      \[\leadsto \sqrt{\color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    5. Taylor expanded in p around 0 3.3%

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

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

Alternative 8: 6.2% accurate, 71.7× speedup?

\[\begin{array}{l} p = |p|\\ \\ \frac{p}{x} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 (/ p x))
p = abs(p);
double code(double p, double x) {
	return p / x;
}
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 = p / x
end function
p = Math.abs(p);
public static double code(double p, double x) {
	return p / x;
}
p = abs(p)
def code(p, x):
	return p / x
p = abs(p)
function code(p, x)
	return Float64(p / x)
end
p = abs(p)
function tmp = code(p, x)
	tmp = p / x;
end
NOTE: p should be positive before calling this function
code[p_, x_] := N[(p / x), $MachinePrecision]
\begin{array}{l}
p = |p|\\
\\
\frac{p}{x}
\end{array}
Derivation
  1. Initial program 76.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 18.2%

    \[\leadsto \sqrt{\color{blue}{\frac{{p}^{2}}{{x}^{2}}}} \]
  3. Step-by-step derivation
    1. unpow218.2%

      \[\leadsto \sqrt{\frac{\color{blue}{p \cdot p}}{{x}^{2}}} \]
    2. unpow218.2%

      \[\leadsto \sqrt{\frac{p \cdot p}{\color{blue}{x \cdot x}}} \]
    3. times-frac23.7%

      \[\leadsto \sqrt{\color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
  4. Simplified23.7%

    \[\leadsto \sqrt{\color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
  5. Taylor expanded in p around 0 19.8%

    \[\leadsto \color{blue}{\frac{p}{x}} \]
  6. Final simplification19.8%

    \[\leadsto \frac{p}{x} \]

Developer target: 79.0% 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 2023271 
(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))))))))