Given's Rotation SVD example

Percentage Accurate: 79.2% → 99.8%
Time: 8.0s
Alternatives: 7
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 7 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.2% 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.3× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\\ \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 \mathsf{fma}\left(\sqrt[3]{{t_0}^{2}}, \sqrt[3]{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 (hypot x (* p 2.0)))))
   (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
     (/ p (- x))
     (sqrt (* 0.5 (fma (cbrt (pow t_0 2.0)) (cbrt t_0) 1.0))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = x / hypot(x, (p * 2.0));
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = p / -x;
	} else {
		tmp = sqrt((0.5 * fma(cbrt(pow(t_0, 2.0)), cbrt(t_0), 1.0)));
	}
	return tmp;
}
p = abs(p)
function code(p, x)
	t_0 = Float64(x / hypot(x, Float64(p * 2.0)))
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -1.0)
		tmp = Float64(p / Float64(-x));
	else
		tmp = sqrt(Float64(0.5 * fma(cbrt((t_0 ^ 2.0)), cbrt(t_0), 1.0)));
	end
	return tmp
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]}, 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[Power[N[Power[t$95$0, 2.0], $MachinePrecision], 1/3], $MachinePrecision] * N[Power[t$95$0, 1/3], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\\
\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 \mathsf{fma}\left(\sqrt[3]{{t_0}^{2}}, \sqrt[3]{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.5%

      \[\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. clear-num14.5%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/13.9%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def14.5%

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    8. Step-by-step derivation
      1. mul-1-neg61.4%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. *-lft-identity61.4%

        \[\leadsto -\color{blue}{1 \cdot \frac{p}{x}} \]
      3. distribute-rgt-neg-in61.4%

        \[\leadsto \color{blue}{1 \cdot \left(-\frac{p}{x}\right)} \]
      4. metadata-eval61.4%

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

        \[\leadsto \frac{-1}{-1} \cdot \color{blue}{\frac{-p}{x}} \]
      6. times-frac61.4%

        \[\leadsto \color{blue}{\frac{-1 \cdot \left(-p\right)}{-1 \cdot x}} \]
      7. neg-mul-161.4%

        \[\leadsto \frac{\color{blue}{-\left(-p\right)}}{-1 \cdot x} \]
      8. remove-double-neg61.4%

        \[\leadsto \frac{\color{blue}{p}}{-1 \cdot x} \]
      9. neg-mul-161.4%

        \[\leadsto \frac{p}{\color{blue}{-x}} \]
    9. Simplified61.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 99.3%

      \[\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. +-commutative99.3%

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

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

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

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

    \[\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 \mathsf{fma}\left(\sqrt[3]{{\left(\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}^{2}}, \sqrt[3]{\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}}, 1\right)}\\ \end{array} \]

Alternative 2: 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 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}}\\ \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 (/ 0.5 (hypot x (* p 2.0))))))))
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 * (0.5 / hypot(x, (p * 2.0))))));
	}
	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 * (0.5 / Math.hypot(x, (p * 2.0))))));
	}
	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 * (0.5 / math.hypot(x, (p * 2.0))))))
	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(p / Float64(-x));
	else
		tmp = sqrt(Float64(0.5 + Float64(x * Float64(0.5 / hypot(x, Float64(p * 2.0))))));
	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 * (0.5 / hypot(x, (p * 2.0))))));
	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[(x * N[(0.5 / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $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 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\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.5%

      \[\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. clear-num14.5%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/13.9%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def14.5%

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    8. Step-by-step derivation
      1. mul-1-neg61.4%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. *-lft-identity61.4%

        \[\leadsto -\color{blue}{1 \cdot \frac{p}{x}} \]
      3. distribute-rgt-neg-in61.4%

        \[\leadsto \color{blue}{1 \cdot \left(-\frac{p}{x}\right)} \]
      4. metadata-eval61.4%

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

        \[\leadsto \frac{-1}{-1} \cdot \color{blue}{\frac{-p}{x}} \]
      6. times-frac61.4%

        \[\leadsto \color{blue}{\frac{-1 \cdot \left(-p\right)}{-1 \cdot x}} \]
      7. neg-mul-161.4%

        \[\leadsto \frac{\color{blue}{-\left(-p\right)}}{-1 \cdot x} \]
      8. remove-double-neg61.4%

        \[\leadsto \frac{\color{blue}{p}}{-1 \cdot x} \]
      9. neg-mul-161.4%

        \[\leadsto \frac{p}{\color{blue}{-x}} \]
    9. Simplified61.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 99.3%

      \[\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. clear-num99.3%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/99.4%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def98.7%

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, p \cdot 2\right)}}} \]
    7. Step-by-step derivation
      1. associate-/l*99.8%

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

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

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

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

Alternative 3: 82.1% accurate, 1.0× speedup?

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

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


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

    1. Initial program 46.2%

      \[\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. clear-num46.2%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/45.6%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def46.3%

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

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

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

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

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

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

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. *-lft-identity40.5%

        \[\leadsto -\color{blue}{1 \cdot \frac{p}{x}} \]
      3. distribute-rgt-neg-in40.5%

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

        \[\leadsto \color{blue}{\frac{-1}{-1}} \cdot \left(-\frac{p}{x}\right) \]
      5. distribute-frac-neg40.5%

        \[\leadsto \frac{-1}{-1} \cdot \color{blue}{\frac{-p}{x}} \]
      6. times-frac40.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot \left(-p\right)}{-1 \cdot x}} \]
      7. neg-mul-140.5%

        \[\leadsto \frac{\color{blue}{-\left(-p\right)}}{-1 \cdot x} \]
      8. remove-double-neg40.5%

        \[\leadsto \frac{\color{blue}{p}}{-1 \cdot x} \]
      9. neg-mul-140.5%

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

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

    if -7.4999999999999996e-15 < x

    1. Initial program 89.5%

      \[\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-sqrt89.5%

        \[\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-def89.5%

        \[\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*89.5%

        \[\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-prod89.5%

        \[\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-eval89.5%

        \[\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-unprod39.2%

        \[\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-sqrt89.7%

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

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

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

Alternative 4: 68.7% accurate, 2.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{p}{-x}\\ \mathbf{if}\;p \leq 2 \cdot 10^{-158}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 3.4 \cdot 10^{-56}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 4.3 \cdot 10^{-44}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 2.2 \cdot 10^{-29}:\\ \;\;\;\;1\\ \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 2e-158)
     t_0
     (if (<= p 3.4e-56)
       1.0
       (if (<= p 4.3e-44) t_0 (if (<= p 2.2e-29) 1.0 (sqrt 0.5)))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = p / -x;
	double tmp;
	if (p <= 2e-158) {
		tmp = t_0;
	} else if (p <= 3.4e-56) {
		tmp = 1.0;
	} else if (p <= 4.3e-44) {
		tmp = t_0;
	} else if (p <= 2.2e-29) {
		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) :: t_0
    real(8) :: tmp
    t_0 = p / -x
    if (p <= 2d-158) then
        tmp = t_0
    else if (p <= 3.4d-56) then
        tmp = 1.0d0
    else if (p <= 4.3d-44) then
        tmp = t_0
    else if (p <= 2.2d-29) 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 t_0 = p / -x;
	double tmp;
	if (p <= 2e-158) {
		tmp = t_0;
	} else if (p <= 3.4e-56) {
		tmp = 1.0;
	} else if (p <= 4.3e-44) {
		tmp = t_0;
	} else if (p <= 2.2e-29) {
		tmp = 1.0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = p / -x
	tmp = 0
	if p <= 2e-158:
		tmp = t_0
	elif p <= 3.4e-56:
		tmp = 1.0
	elif p <= 4.3e-44:
		tmp = t_0
	elif p <= 2.2e-29:
		tmp = 1.0
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(p / Float64(-x))
	tmp = 0.0
	if (p <= 2e-158)
		tmp = t_0;
	elseif (p <= 3.4e-56)
		tmp = 1.0;
	elseif (p <= 4.3e-44)
		tmp = t_0;
	elseif (p <= 2.2e-29)
		tmp = 1.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 <= 2e-158)
		tmp = t_0;
	elseif (p <= 3.4e-56)
		tmp = 1.0;
	elseif (p <= 4.3e-44)
		tmp = t_0;
	elseif (p <= 2.2e-29)
		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_] := Block[{t$95$0 = N[(p / (-x)), $MachinePrecision]}, If[LessEqual[p, 2e-158], t$95$0, If[LessEqual[p, 3.4e-56], 1.0, If[LessEqual[p, 4.3e-44], t$95$0, If[LessEqual[p, 2.2e-29], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{p}{-x}\\
\mathbf{if}\;p \leq 2 \cdot 10^{-158}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;p \leq 3.4 \cdot 10^{-56}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 4.3 \cdot 10^{-44}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;p \leq 2.2 \cdot 10^{-29}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < 2.00000000000000013e-158 or 3.39999999999999982e-56 < p < 4.30000000000000013e-44

    1. Initial program 73.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. clear-num73.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/72.7%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def72.5%

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    8. Step-by-step derivation
      1. mul-1-neg19.6%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. *-lft-identity19.6%

        \[\leadsto -\color{blue}{1 \cdot \frac{p}{x}} \]
      3. distribute-rgt-neg-in19.6%

        \[\leadsto \color{blue}{1 \cdot \left(-\frac{p}{x}\right)} \]
      4. metadata-eval19.6%

        \[\leadsto \color{blue}{\frac{-1}{-1}} \cdot \left(-\frac{p}{x}\right) \]
      5. distribute-frac-neg19.6%

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

        \[\leadsto \color{blue}{\frac{-1 \cdot \left(-p\right)}{-1 \cdot x}} \]
      7. neg-mul-119.6%

        \[\leadsto \frac{\color{blue}{-\left(-p\right)}}{-1 \cdot x} \]
      8. remove-double-neg19.6%

        \[\leadsto \frac{\color{blue}{p}}{-1 \cdot x} \]
      9. neg-mul-119.6%

        \[\leadsto \frac{p}{\color{blue}{-x}} \]
    9. Simplified19.6%

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

    if 2.00000000000000013e-158 < p < 3.39999999999999982e-56 or 4.30000000000000013e-44 < p < 2.1999999999999999e-29

    1. Initial program 63.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. clear-num63.0%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/63.5%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def62.8%

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

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

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

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

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

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

    if 2.1999999999999999e-29 < p

    1. Initial program 89.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 78.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 2 \cdot 10^{-158}:\\ \;\;\;\;\frac{p}{-x}\\ \mathbf{elif}\;p \leq 3.4 \cdot 10^{-56}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 4.3 \cdot 10^{-44}:\\ \;\;\;\;\frac{p}{-x}\\ \mathbf{elif}\;p \leq 2.2 \cdot 10^{-29}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 5: 67.2% accurate, 2.1× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;p \leq 6 \cdot 10^{-35}:\\ \;\;\;\;\frac{p}{-x}\\ \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 6e-35) (/ p (- x)) (sqrt 0.5)))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (p <= 6e-35) {
		tmp = p / -x;
	} 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 <= 6d-35) then
        tmp = p / -x
    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 <= 6e-35) {
		tmp = p / -x;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if p <= 6e-35:
		tmp = p / -x
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (p <= 6e-35)
		tmp = Float64(p / Float64(-x));
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= 6e-35)
		tmp = p / -x;
	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, 6e-35], N[(p / (-x)), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 6 \cdot 10^{-35}:\\
\;\;\;\;\frac{p}{-x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if p < 5.99999999999999978e-35

    1. Initial program 71.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. clear-num71.8%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/71.6%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def71.4%

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

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

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

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

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

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

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. *-lft-identity21.8%

        \[\leadsto -\color{blue}{1 \cdot \frac{p}{x}} \]
      3. distribute-rgt-neg-in21.8%

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

        \[\leadsto \color{blue}{\frac{-1}{-1}} \cdot \left(-\frac{p}{x}\right) \]
      5. distribute-frac-neg21.8%

        \[\leadsto \frac{-1}{-1} \cdot \color{blue}{\frac{-p}{x}} \]
      6. times-frac21.8%

        \[\leadsto \color{blue}{\frac{-1 \cdot \left(-p\right)}{-1 \cdot x}} \]
      7. neg-mul-121.8%

        \[\leadsto \frac{\color{blue}{-\left(-p\right)}}{-1 \cdot x} \]
      8. remove-double-neg21.8%

        \[\leadsto \frac{\color{blue}{p}}{-1 \cdot x} \]
      9. neg-mul-121.8%

        \[\leadsto \frac{p}{\color{blue}{-x}} \]
    9. Simplified21.8%

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

    if 5.99999999999999978e-35 < p

    1. Initial program 89.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 6 \cdot 10^{-35}:\\ \;\;\;\;\frac{p}{-x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 6: 29.1% accurate, 35.5× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq -2 \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 -2e-310) (/ p (- x)) (/ p x)))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (x <= -2e-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 <= (-2d-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 <= -2e-310) {
		tmp = p / -x;
	} else {
		tmp = p / x;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if x <= -2e-310:
		tmp = p / -x
	else:
		tmp = p / x
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (x <= -2e-310)
		tmp = Float64(p / Float64(-x));
	else
		tmp = Float64(p / x);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (x <= -2e-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, -2e-310], N[(p / (-x)), $MachinePrecision], N[(p / x), $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \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 < -1.999999999999994e-310

    1. Initial program 53.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. clear-num53.5%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]
      2. associate-/r/53.3%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
    5. Step-by-step derivation
      1. expm1-def53.3%

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    8. Step-by-step derivation
      1. mul-1-neg34.6%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. *-lft-identity34.6%

        \[\leadsto -\color{blue}{1 \cdot \frac{p}{x}} \]
      3. distribute-rgt-neg-in34.6%

        \[\leadsto \color{blue}{1 \cdot \left(-\frac{p}{x}\right)} \]
      4. metadata-eval34.6%

        \[\leadsto \color{blue}{\frac{-1}{-1}} \cdot \left(-\frac{p}{x}\right) \]
      5. distribute-frac-neg34.6%

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

        \[\leadsto \color{blue}{\frac{-1 \cdot \left(-p\right)}{-1 \cdot x}} \]
      7. neg-mul-134.6%

        \[\leadsto \frac{\color{blue}{-\left(-p\right)}}{-1 \cdot x} \]
      8. remove-double-neg34.6%

        \[\leadsto \frac{\color{blue}{p}}{-1 \cdot x} \]
      9. neg-mul-134.6%

        \[\leadsto \frac{p}{\color{blue}{-x}} \]
    9. Simplified34.6%

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

    if -1.999999999999994e-310 < x

    1. Initial program 99.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 -inf 5.1%

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

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

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

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
    5. Taylor expanded in p around 0 3.2%

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

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

Alternative 7: 6.8% 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.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 19.7%

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

      \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
    2. unpow219.7%

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

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

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
  5. Taylor expanded in p around 0 16.3%

    \[\leadsto \color{blue}{\frac{p}{x}} \]
  6. Final simplification16.3%

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

Developer target: 79.3% 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 2023200 
(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))))))))