Given's Rotation SVD example

Percentage Accurate: 79.5% → 91.1%
Time: 10.9s
Alternatives: 10
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 10 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.5% 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: 91.1% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.8:\\ \;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left({\left(\frac{p}{x}\right)}^{4}, -6, 2 \cdot {\left(\frac{p}{x}\right)}^{2}\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{0.5 \cdot \log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}\right)}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -0.8)
   (sqrt (* 0.5 (fma (pow (/ p x) 4.0) -6.0 (* 2.0 (pow (/ p x) 2.0)))))
   (exp (* 0.5 (log (+ 0.5 (/ 0.5 (/ (hypot x (* p 2.0)) x))))))))
double code(double p, double x) {
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -0.8) {
		tmp = sqrt((0.5 * fma(pow((p / x), 4.0), -6.0, (2.0 * pow((p / x), 2.0)))));
	} else {
		tmp = exp((0.5 * log((0.5 + (0.5 / (hypot(x, (p * 2.0)) / x))))));
	}
	return tmp;
}
function code(p, x)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -0.8)
		tmp = sqrt(Float64(0.5 * fma((Float64(p / x) ^ 4.0), -6.0, Float64(2.0 * (Float64(p / x) ^ 2.0)))));
	else
		tmp = exp(Float64(0.5 * log(Float64(0.5 + Float64(0.5 / Float64(hypot(x, Float64(p * 2.0)) / x))))));
	end
	return tmp
end
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], -0.8], N[Sqrt[N[(0.5 * N[(N[Power[N[(p / x), $MachinePrecision], 4.0], $MachinePrecision] * -6.0 + N[(2.0 * N[Power[N[(p / x), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Exp[N[(0.5 * N[Log[N[(0.5 + N[(0.5 / N[(N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.8:\\
\;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left({\left(\frac{p}{x}\right)}^{4}, -6, 2 \cdot {\left(\frac{p}{x}\right)}^{2}\right)}\\

\mathbf{else}:\\
\;\;\;\;e^{0.5 \cdot \log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}\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)))) < -0.80000000000000004

    1. Initial program 15.8%

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \log \left(e^{\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}}}}}\right)\right)} \]
      4. hypot-def15.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\mathsf{fma}\left(\frac{{p}^{4}}{{x}^{4}}, -2 + -4, 2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    8. Simplified65.9%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\mathsf{fma}\left({\left(\frac{p}{x}\right)}^{4}, -6, 2 \cdot {\left(\frac{p}{x}\right)}^{2}\right)}} \]

    if -0.80000000000000004 < (/.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. pow1/2100.0%

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

        \[\leadsto \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}} \]
    3. Applied egg-rr100.0%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}} \]
    4. Step-by-step derivation
      1. fma-udef100.0%

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

        \[\leadsto e^{\log \left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot 0.5 + \color{blue}{1 \cdot 0.5}\right) \cdot 0.5} \]
      3. distribute-rgt-in100.0%

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

        \[\leadsto e^{\log \color{blue}{\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} + 0.5 \cdot 1\right)} \cdot 0.5} \]
      5. clear-num100.0%

        \[\leadsto e^{\log \left(0.5 \cdot \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}} + 0.5 \cdot 1\right) \cdot 0.5} \]
      6. un-div-inv100.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.8:\\ \;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left({\left(\frac{p}{x}\right)}^{4}, -6, 2 \cdot {\left(\frac{p}{x}\right)}^{2}\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{0.5 \cdot \log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}\right)}\\ \end{array} \]

Alternative 2: 90.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{0.5 \cdot \log \left(0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}\right)}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
   (sqrt (* 0.5 (* 2.0 (* (/ p x) (/ p x)))))
   (exp (* 0.5 (log (+ 0.5 (/ 0.5 (/ (hypot x (* p 2.0)) x))))))))
double code(double p, double x) {
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = sqrt((0.5 * (2.0 * ((p / x) * (p / x)))));
	} else {
		tmp = exp((0.5 * log((0.5 + (0.5 / (hypot(x, (p * 2.0)) / x))))));
	}
	return tmp;
}
public static double code(double p, double x) {
	double tmp;
	if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = Math.sqrt((0.5 * (2.0 * ((p / x) * (p / x)))));
	} else {
		tmp = Math.exp((0.5 * Math.log((0.5 + (0.5 / (Math.hypot(x, (p * 2.0)) / x))))));
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0:
		tmp = math.sqrt((0.5 * (2.0 * ((p / x) * (p / x)))))
	else:
		tmp = math.exp((0.5 * math.log((0.5 + (0.5 / (math.hypot(x, (p * 2.0)) / x))))))
	return tmp
function code(p, x)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -1.0)
		tmp = sqrt(Float64(0.5 * Float64(2.0 * Float64(Float64(p / x) * Float64(p / x)))));
	else
		tmp = exp(Float64(0.5 * log(Float64(0.5 + Float64(0.5 / Float64(hypot(x, Float64(p * 2.0)) / x))))));
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0)
		tmp = sqrt((0.5 * (2.0 * ((p / x) * (p / x)))));
	else
		tmp = exp((0.5 * log((0.5 + (0.5 / (hypot(x, (p * 2.0)) / x))))));
	end
	tmp_2 = tmp;
end
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[Sqrt[N[(0.5 * N[(2.0 * N[(N[(p / x), $MachinePrecision] * N[(p / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Exp[N[(0.5 * N[Log[N[(0.5 + N[(0.5 / N[(N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1

    1. Initial program 14.8%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Taylor expanded in x around -inf 48.7%

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

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

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

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

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

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

    1. Initial program 99.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. pow1/299.8%

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

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

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}} \]
    4. Step-by-step derivation
      1. fma-udef99.8%

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

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

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

        \[\leadsto e^{\log \color{blue}{\left(0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)} + 0.5 \cdot 1\right)} \cdot 0.5} \]
      5. clear-num99.8%

        \[\leadsto e^{\log \left(0.5 \cdot \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}} + 0.5 \cdot 1\right) \cdot 0.5} \]
      6. un-div-inv99.8%

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

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

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

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

Alternative 3: 90.9% accurate, 0.7× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1

    1. Initial program 14.8%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Taylor expanded in x around -inf 48.7%

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

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

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

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

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

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

    1. Initial program 99.8%

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \log \left(e^{\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}}}}}\right)\right)} \]
      4. hypot-def99.8%

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 79.5% accurate, 1.0× speedup?

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

\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}
\end{array}
Derivation
  1. Initial program 81.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. add-sqr-sqrt81.6%

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

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

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

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

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

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

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

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

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

Alternative 5: 68.6% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq -2.6 \cdot 10^{-62}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -3.5 \cdot 10^{-196}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 6.6 \cdot 10^{-232}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 7 \cdot 10^{-155}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 3.2 \cdot 10^{-24}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.4 \cdot 10^{-5}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x \cdot 0.5}{p}\right)}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (/ (- p) x)))
   (if (<= p -2.6e-62)
     (sqrt 0.5)
     (if (<= p -3.5e-196)
       (/ p x)
       (if (<= p 6.6e-232)
         1.0
         (if (<= p 7e-155)
           t_0
           (if (<= p 3.2e-24)
             1.0
             (if (<= p 2.4e-5)
               t_0
               (sqrt (* 0.5 (+ 1.0 (/ (* x 0.5) p))))))))))))
double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (p <= -2.6e-62) {
		tmp = sqrt(0.5);
	} else if (p <= -3.5e-196) {
		tmp = p / x;
	} else if (p <= 6.6e-232) {
		tmp = 1.0;
	} else if (p <= 7e-155) {
		tmp = t_0;
	} else if (p <= 3.2e-24) {
		tmp = 1.0;
	} else if (p <= 2.4e-5) {
		tmp = t_0;
	} else {
		tmp = sqrt((0.5 * (1.0 + ((x * 0.5) / p))));
	}
	return tmp;
}
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 <= (-2.6d-62)) then
        tmp = sqrt(0.5d0)
    else if (p <= (-3.5d-196)) then
        tmp = p / x
    else if (p <= 6.6d-232) then
        tmp = 1.0d0
    else if (p <= 7d-155) then
        tmp = t_0
    else if (p <= 3.2d-24) then
        tmp = 1.0d0
    else if (p <= 2.4d-5) then
        tmp = t_0
    else
        tmp = sqrt((0.5d0 * (1.0d0 + ((x * 0.5d0) / p))))
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (p <= -2.6e-62) {
		tmp = Math.sqrt(0.5);
	} else if (p <= -3.5e-196) {
		tmp = p / x;
	} else if (p <= 6.6e-232) {
		tmp = 1.0;
	} else if (p <= 7e-155) {
		tmp = t_0;
	} else if (p <= 3.2e-24) {
		tmp = 1.0;
	} else if (p <= 2.4e-5) {
		tmp = t_0;
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + ((x * 0.5) / p))));
	}
	return tmp;
}
def code(p, x):
	t_0 = -p / x
	tmp = 0
	if p <= -2.6e-62:
		tmp = math.sqrt(0.5)
	elif p <= -3.5e-196:
		tmp = p / x
	elif p <= 6.6e-232:
		tmp = 1.0
	elif p <= 7e-155:
		tmp = t_0
	elif p <= 3.2e-24:
		tmp = 1.0
	elif p <= 2.4e-5:
		tmp = t_0
	else:
		tmp = math.sqrt((0.5 * (1.0 + ((x * 0.5) / p))))
	return tmp
function code(p, x)
	t_0 = Float64(Float64(-p) / x)
	tmp = 0.0
	if (p <= -2.6e-62)
		tmp = sqrt(0.5);
	elseif (p <= -3.5e-196)
		tmp = Float64(p / x);
	elseif (p <= 6.6e-232)
		tmp = 1.0;
	elseif (p <= 7e-155)
		tmp = t_0;
	elseif (p <= 3.2e-24)
		tmp = 1.0;
	elseif (p <= 2.4e-5)
		tmp = t_0;
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(Float64(x * 0.5) / p))));
	end
	return tmp
end
function tmp_2 = code(p, x)
	t_0 = -p / x;
	tmp = 0.0;
	if (p <= -2.6e-62)
		tmp = sqrt(0.5);
	elseif (p <= -3.5e-196)
		tmp = p / x;
	elseif (p <= 6.6e-232)
		tmp = 1.0;
	elseif (p <= 7e-155)
		tmp = t_0;
	elseif (p <= 3.2e-24)
		tmp = 1.0;
	elseif (p <= 2.4e-5)
		tmp = t_0;
	else
		tmp = sqrt((0.5 * (1.0 + ((x * 0.5) / p))));
	end
	tmp_2 = tmp;
end
code[p_, x_] := Block[{t$95$0 = N[((-p) / x), $MachinePrecision]}, If[LessEqual[p, -2.6e-62], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -3.5e-196], N[(p / x), $MachinePrecision], If[LessEqual[p, 6.6e-232], 1.0, If[LessEqual[p, 7e-155], t$95$0, If[LessEqual[p, 3.2e-24], 1.0, If[LessEqual[p, 2.4e-5], t$95$0, N[Sqrt[N[(0.5 * N[(1.0 + N[(N[(x * 0.5), $MachinePrecision] / p), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{-p}{x}\\
\mathbf{if}\;p \leq -2.6 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq -3.5 \cdot 10^{-196}:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{elif}\;p \leq 6.6 \cdot 10^{-232}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 7 \cdot 10^{-155}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;p \leq 3.2 \cdot 10^{-24}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 2.4 \cdot 10^{-5}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 5 regimes
  2. if p < -2.5999999999999999e-62

    1. Initial program 95.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 0 83.4%

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

    if -2.5999999999999999e-62 < p < -3.50000000000000004e-196

    1. Initial program 41.3%

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified41.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 64.4%

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

    if -3.50000000000000004e-196 < p < 6.5999999999999997e-232 or 7.00000000000000031e-155 < p < 3.20000000000000012e-24

    1. Initial program 78.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 x around inf 64.2%

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

    if 6.5999999999999997e-232 < p < 7.00000000000000031e-155 or 3.20000000000000012e-24 < p < 2.4000000000000001e-5

    1. Initial program 24.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. pow1/224.1%

        \[\leadsto \color{blue}{{\left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right)}^{0.5}} \]
      2. pow-to-exp24.1%

        \[\leadsto \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}} \]
    3. Applied egg-rr24.1%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in x around -inf 81.8%

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    6. Simplified81.8%

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

    if 2.4000000000000001e-5 < p

    1. Initial program 99.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 0 96.1%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -2.6 \cdot 10^{-62}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -3.5 \cdot 10^{-196}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 6.6 \cdot 10^{-232}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 7 \cdot 10^{-155}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 3.2 \cdot 10^{-24}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.4 \cdot 10^{-5}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x \cdot 0.5}{p}\right)}\\ \end{array} \]

Alternative 6: 76.6% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2.15 \cdot 10^{+58}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{1}{1 + 2 \cdot \left(p \cdot \frac{p}{x \cdot x}\right)}\right)}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= x -2.15e+58)
   (/ p x)
   (sqrt (* 0.5 (+ 1.0 (/ 1.0 (+ 1.0 (* 2.0 (* p (/ p (* x x)))))))))))
double code(double p, double x) {
	double tmp;
	if (x <= -2.15e+58) {
		tmp = p / x;
	} else {
		tmp = sqrt((0.5 * (1.0 + (1.0 / (1.0 + (2.0 * (p * (p / (x * x)))))))));
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-2.15d+58)) then
        tmp = p / x
    else
        tmp = sqrt((0.5d0 * (1.0d0 + (1.0d0 / (1.0d0 + (2.0d0 * (p * (p / (x * x)))))))))
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (x <= -2.15e+58) {
		tmp = p / x;
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + (1.0 / (1.0 + (2.0 * (p * (p / (x * x)))))))));
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if x <= -2.15e+58:
		tmp = p / x
	else:
		tmp = math.sqrt((0.5 * (1.0 + (1.0 / (1.0 + (2.0 * (p * (p / (x * x)))))))))
	return tmp
function code(p, x)
	tmp = 0.0
	if (x <= -2.15e+58)
		tmp = Float64(p / x);
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(1.0 / Float64(1.0 + Float64(2.0 * Float64(p * Float64(p / Float64(x * x)))))))));
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (x <= -2.15e+58)
		tmp = p / x;
	else
		tmp = sqrt((0.5 * (1.0 + (1.0 / (1.0 + (2.0 * (p * (p / (x * x)))))))));
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[x, -2.15e+58], N[(p / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(1.0 / N[(1.0 + N[(2.0 * N[(p * N[(p / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.15 \cdot 10^{+58}:\\
\;\;\;\;\frac{p}{x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2.14999999999999996e58

    1. Initial program 46.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 40.3%

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

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

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

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

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

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

    if -2.14999999999999996e58 < x

    1. Initial program 88.0%

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \log \left(e^{\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}}}}}\right)\right)} \]
      4. hypot-def88.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 7: 68.9% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -2.8 \cdot 10^{-63}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.25 \cdot 10^{-195}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 4.95 \cdot 10^{-231}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.06 \cdot 10^{-151}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 4.2 \cdot 10^{-62}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= p -2.8e-63)
   (sqrt 0.5)
   (if (<= p -2.25e-195)
     (/ p x)
     (if (<= p 4.95e-231)
       1.0
       (if (<= p 1.06e-151) (/ (- p) x) (if (<= p 4.2e-62) 1.0 (sqrt 0.5)))))))
double code(double p, double x) {
	double tmp;
	if (p <= -2.8e-63) {
		tmp = sqrt(0.5);
	} else if (p <= -2.25e-195) {
		tmp = p / x;
	} else if (p <= 4.95e-231) {
		tmp = 1.0;
	} else if (p <= 1.06e-151) {
		tmp = -p / x;
	} else if (p <= 4.2e-62) {
		tmp = 1.0;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p <= (-2.8d-63)) then
        tmp = sqrt(0.5d0)
    else if (p <= (-2.25d-195)) then
        tmp = p / x
    else if (p <= 4.95d-231) then
        tmp = 1.0d0
    else if (p <= 1.06d-151) then
        tmp = -p / x
    else if (p <= 4.2d-62) then
        tmp = 1.0d0
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (p <= -2.8e-63) {
		tmp = Math.sqrt(0.5);
	} else if (p <= -2.25e-195) {
		tmp = p / x;
	} else if (p <= 4.95e-231) {
		tmp = 1.0;
	} else if (p <= 1.06e-151) {
		tmp = -p / x;
	} else if (p <= 4.2e-62) {
		tmp = 1.0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -2.8e-63:
		tmp = math.sqrt(0.5)
	elif p <= -2.25e-195:
		tmp = p / x
	elif p <= 4.95e-231:
		tmp = 1.0
	elif p <= 1.06e-151:
		tmp = -p / x
	elif p <= 4.2e-62:
		tmp = 1.0
	else:
		tmp = math.sqrt(0.5)
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -2.8e-63)
		tmp = sqrt(0.5);
	elseif (p <= -2.25e-195)
		tmp = Float64(p / x);
	elseif (p <= 4.95e-231)
		tmp = 1.0;
	elseif (p <= 1.06e-151)
		tmp = Float64(Float64(-p) / x);
	elseif (p <= 4.2e-62)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -2.8e-63)
		tmp = sqrt(0.5);
	elseif (p <= -2.25e-195)
		tmp = p / x;
	elseif (p <= 4.95e-231)
		tmp = 1.0;
	elseif (p <= 1.06e-151)
		tmp = -p / x;
	elseif (p <= 4.2e-62)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -2.8e-63], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -2.25e-195], N[(p / x), $MachinePrecision], If[LessEqual[p, 4.95e-231], 1.0, If[LessEqual[p, 1.06e-151], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 4.2e-62], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;p \leq -2.8 \cdot 10^{-63}:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq -2.25 \cdot 10^{-195}:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{elif}\;p \leq 4.95 \cdot 10^{-231}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 1.06 \cdot 10^{-151}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{elif}\;p \leq 4.2 \cdot 10^{-62}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if p < -2.8000000000000002e-63 or 4.1999999999999998e-62 < p

    1. Initial program 93.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 83.8%

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

    if -2.8000000000000002e-63 < p < -2.25e-195

    1. Initial program 41.3%

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified41.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 64.4%

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

    if -2.25e-195 < p < 4.9500000000000001e-231 or 1.06e-151 < p < 4.1999999999999998e-62

    1. Initial program 78.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 x around inf 67.7%

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

    if 4.9500000000000001e-231 < p < 1.06e-151

    1. Initial program 30.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. pow1/230.9%

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

        \[\leadsto \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}} \]
    3. Applied egg-rr30.9%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in x around -inf 75.7%

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    6. Simplified75.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -2.8 \cdot 10^{-63}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.25 \cdot 10^{-195}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 4.95 \cdot 10^{-231}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.06 \cdot 10^{-151}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 4.2 \cdot 10^{-62}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 8: 67.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -6.2 \cdot 10^{-65}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.85 \cdot 10^{-278}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 5 \cdot 10^{-10}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= p -6.2e-65)
   (sqrt 0.5)
   (if (<= p -1.85e-278) (/ p x) (if (<= p 5e-10) (/ (- p) x) (sqrt 0.5)))))
double code(double p, double x) {
	double tmp;
	if (p <= -6.2e-65) {
		tmp = sqrt(0.5);
	} else if (p <= -1.85e-278) {
		tmp = p / x;
	} else if (p <= 5e-10) {
		tmp = -p / x;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p <= (-6.2d-65)) then
        tmp = sqrt(0.5d0)
    else if (p <= (-1.85d-278)) then
        tmp = p / x
    else if (p <= 5d-10) then
        tmp = -p / x
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (p <= -6.2e-65) {
		tmp = Math.sqrt(0.5);
	} else if (p <= -1.85e-278) {
		tmp = p / x;
	} else if (p <= 5e-10) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -6.2e-65:
		tmp = math.sqrt(0.5)
	elif p <= -1.85e-278:
		tmp = p / x
	elif p <= 5e-10:
		tmp = -p / x
	else:
		tmp = math.sqrt(0.5)
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -6.2e-65)
		tmp = sqrt(0.5);
	elseif (p <= -1.85e-278)
		tmp = Float64(p / x);
	elseif (p <= 5e-10)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -6.2e-65)
		tmp = sqrt(0.5);
	elseif (p <= -1.85e-278)
		tmp = p / x;
	elseif (p <= 5e-10)
		tmp = -p / x;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -6.2e-65], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -1.85e-278], N[(p / x), $MachinePrecision], If[LessEqual[p, 5e-10], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;p \leq -6.2 \cdot 10^{-65}:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq -1.85 \cdot 10^{-278}:\\
\;\;\;\;\frac{p}{x}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < -6.20000000000000032e-65 or 5.00000000000000031e-10 < p

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

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

    if -6.20000000000000032e-65 < p < -1.85000000000000011e-278

    1. Initial program 54.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 19.6%

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

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

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

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

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

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

    if -1.85000000000000011e-278 < p < 5.00000000000000031e-10

    1. Initial program 64.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. pow1/264.9%

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

        \[\leadsto \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}} \]
    3. Applied egg-rr64.9%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in x around -inf 47.4%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -6.2 \cdot 10^{-65}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.85 \cdot 10^{-278}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 5 \cdot 10^{-10}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 9: 26.1% accurate, 35.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -1.85 \cdot 10^{-278}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \end{array} \]
(FPCore (p x) :precision binary64 (if (<= p -1.85e-278) (/ p x) (/ (- p) x)))
double code(double p, double x) {
	double tmp;
	if (p <= -1.85e-278) {
		tmp = p / x;
	} else {
		tmp = -p / x;
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p <= (-1.85d-278)) then
        tmp = p / x
    else
        tmp = -p / x
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (p <= -1.85e-278) {
		tmp = p / x;
	} else {
		tmp = -p / x;
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -1.85e-278:
		tmp = p / x
	else:
		tmp = -p / x
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -1.85e-278)
		tmp = Float64(p / x);
	else
		tmp = Float64(Float64(-p) / x);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -1.85e-278)
		tmp = p / x;
	else
		tmp = -p / x;
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -1.85e-278], N[(p / x), $MachinePrecision], N[((-p) / x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;p \leq -1.85 \cdot 10^{-278}:\\
\;\;\;\;\frac{p}{x}\\

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


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

    1. Initial program 80.3%

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

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

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

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

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

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

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

    if -1.85000000000000011e-278 < p

    1. Initial program 82.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. pow1/282.9%

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

        \[\leadsto \color{blue}{e^{\log \left(0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)\right) \cdot 0.5}} \]
    3. Applied egg-rr82.9%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in x around -inf 24.5%

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

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

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

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

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

Alternative 10: 17.1% accurate, 71.7× speedup?

\[\begin{array}{l} \\ \frac{p}{x} \end{array} \]
(FPCore (p x) :precision binary64 (/ p x))
double code(double p, double x) {
	return p / x;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = p / x
end function
public static double code(double p, double x) {
	return p / x;
}
def code(p, x):
	return p / x
function code(p, x)
	return Float64(p / x)
end
function tmp = code(p, x)
	tmp = p / x;
end
code[p_, x_] := N[(p / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{p}{x}
\end{array}
Derivation
  1. Initial program 81.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 x around -inf 14.5%

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

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

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

      \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
  4. Simplified18.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 15.9%

    \[\leadsto \color{blue}{\frac{p}{x}} \]
  6. Final simplification15.9%

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

Developer target: 79.5% 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 2023194 
(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))))))))