Given's Rotation SVD example

Percentage Accurate: 79.7% → 99.7%
Time: 9.3s
Alternatives: 4
Speedup: 0.7×

Specification

?
\[10^{-150} < \left|x\right| \land \left|x\right| < 10^{+150}\]
\[\begin{array}{l} \\ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
	return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
	return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x):
	return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
function code(p, x)
	return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x)))))))
end
function tmp = code(p, x)
	tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 4 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.7% 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.7% accurate, 0.7× speedup?

\[\begin{array}{l} p_m = \left|p\right| \\ \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p\_m \cdot \left(4 \cdot p\_m\right) + x \cdot x}} \leq -0.6:\\ \;\;\;\;\frac{p\_m}{-x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p\_m \cdot 2\right)}}\\ \end{array} \end{array} \]
p_m = (fabs.f64 p)
(FPCore (p_m x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -0.6)
   (/ p_m (- x))
   (sqrt (+ 0.5 (* 0.5 (/ x (hypot x (* p_m 2.0))))))))
p_m = fabs(p);
double code(double p_m, double x) {
	double tmp;
	if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) {
		tmp = p_m / -x;
	} else {
		tmp = sqrt((0.5 + (0.5 * (x / hypot(x, (p_m * 2.0))))));
	}
	return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
	double tmp;
	if ((x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6) {
		tmp = p_m / -x;
	} else {
		tmp = Math.sqrt((0.5 + (0.5 * (x / Math.hypot(x, (p_m * 2.0))))));
	}
	return tmp;
}
p_m = math.fabs(p)
def code(p_m, x):
	tmp = 0
	if (x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6:
		tmp = p_m / -x
	else:
		tmp = math.sqrt((0.5 + (0.5 * (x / math.hypot(x, (p_m * 2.0))))))
	return tmp
p_m = abs(p)
function code(p_m, x)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -0.6)
		tmp = Float64(p_m / Float64(-x));
	else
		tmp = sqrt(Float64(0.5 + Float64(0.5 * Float64(x / hypot(x, Float64(p_m * 2.0))))));
	end
	return tmp
end
p_m = abs(p);
function tmp_2 = code(p_m, x)
	tmp = 0.0;
	if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -0.6)
		tmp = p_m / -x;
	else
		tmp = sqrt((0.5 + (0.5 * (x / hypot(x, (p_m * 2.0))))));
	end
	tmp_2 = tmp;
end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.6], N[(p$95$m / (-x)), $MachinePrecision], N[Sqrt[N[(0.5 + N[(0.5 * N[(x / N[Sqrt[x ^ 2 + N[(p$95$m * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|

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

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


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

    1. Initial program 19.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. +-commutative19.5%

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(\frac{x}{\sqrt{\color{blue}{\mathsf{fma}\left(4, p \cdot p, x \cdot x\right)}}} + 1\right)} \]
      6. sqr-neg19.5%

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around -inf 57.0%

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

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

        \[\leadsto \color{blue}{\frac{p}{-x}} \]
    7. Simplified57.0%

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

    if -0.599999999999999978 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x))))

    1. Initial program 99.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. +-commutative99.9%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. div-inv99.9%

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

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

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

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \left(x \cdot \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}}}}\right)} \]
      5. hypot-define99.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 67.8% accurate, 1.9× speedup?

\[\begin{array}{l} p_m = \left|p\right| \\ \begin{array}{l} \mathbf{if}\;p\_m \leq 2.05 \cdot 10^{-272}:\\ \;\;\;\;1\\ \mathbf{elif}\;p\_m \leq 1.9 \cdot 10^{-249}:\\ \;\;\;\;\frac{p\_m}{-x}\\ \mathbf{elif}\;p\_m \leq 3.8 \cdot 10^{-96}:\\ \;\;\;\;1 + \left(\frac{p\_m}{x} \cdot \frac{p\_m}{x}\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
p_m = (fabs.f64 p)
(FPCore (p_m x)
 :precision binary64
 (if (<= p_m 2.05e-272)
   1.0
   (if (<= p_m 1.9e-249)
     (/ p_m (- x))
     (if (<= p_m 3.8e-96)
       (+ 1.0 (* (* (/ p_m x) (/ p_m x)) -0.5))
       (sqrt 0.5)))))
p_m = fabs(p);
double code(double p_m, double x) {
	double tmp;
	if (p_m <= 2.05e-272) {
		tmp = 1.0;
	} else if (p_m <= 1.9e-249) {
		tmp = p_m / -x;
	} else if (p_m <= 3.8e-96) {
		tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
    real(8), intent (in) :: p_m
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p_m <= 2.05d-272) then
        tmp = 1.0d0
    else if (p_m <= 1.9d-249) then
        tmp = p_m / -x
    else if (p_m <= 3.8d-96) then
        tmp = 1.0d0 + (((p_m / x) * (p_m / x)) * (-0.5d0))
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
	double tmp;
	if (p_m <= 2.05e-272) {
		tmp = 1.0;
	} else if (p_m <= 1.9e-249) {
		tmp = p_m / -x;
	} else if (p_m <= 3.8e-96) {
		tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p_m = math.fabs(p)
def code(p_m, x):
	tmp = 0
	if p_m <= 2.05e-272:
		tmp = 1.0
	elif p_m <= 1.9e-249:
		tmp = p_m / -x
	elif p_m <= 3.8e-96:
		tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5)
	else:
		tmp = math.sqrt(0.5)
	return tmp
p_m = abs(p)
function code(p_m, x)
	tmp = 0.0
	if (p_m <= 2.05e-272)
		tmp = 1.0;
	elseif (p_m <= 1.9e-249)
		tmp = Float64(p_m / Float64(-x));
	elseif (p_m <= 3.8e-96)
		tmp = Float64(1.0 + Float64(Float64(Float64(p_m / x) * Float64(p_m / x)) * -0.5));
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p_m = abs(p);
function tmp_2 = code(p_m, x)
	tmp = 0.0;
	if (p_m <= 2.05e-272)
		tmp = 1.0;
	elseif (p_m <= 1.9e-249)
		tmp = p_m / -x;
	elseif (p_m <= 3.8e-96)
		tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := If[LessEqual[p$95$m, 2.05e-272], 1.0, If[LessEqual[p$95$m, 1.9e-249], N[(p$95$m / (-x)), $MachinePrecision], If[LessEqual[p$95$m, 3.8e-96], N[(1.0 + N[(N[(N[(p$95$m / x), $MachinePrecision] * N[(p$95$m / x), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}
p_m = \left|p\right|

\\
\begin{array}{l}
\mathbf{if}\;p\_m \leq 2.05 \cdot 10^{-272}:\\
\;\;\;\;1\\

\mathbf{elif}\;p\_m \leq 1.9 \cdot 10^{-249}:\\
\;\;\;\;\frac{p\_m}{-x}\\

\mathbf{elif}\;p\_m \leq 3.8 \cdot 10^{-96}:\\
\;\;\;\;1 + \left(\frac{p\_m}{x} \cdot \frac{p\_m}{x}\right) \cdot -0.5\\

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


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

    1. Initial program 83.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. +-commutative83.5%

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(\frac{x}{\sqrt{\color{blue}{\mathsf{fma}\left(4, p \cdot p, x \cdot x\right)}}} + 1\right)} \]
      6. sqr-neg83.5%

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. div-inv83.4%

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

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

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

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \left(x \cdot \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}}}}\right)} \]
      5. hypot-define83.4%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{1 + -1 \cdot \frac{{p}^{2}}{{x}^{2}}}} \]
    10. Step-by-step derivation
      1. mul-1-neg32.8%

        \[\leadsto \sqrt{1 + \color{blue}{\left(-\frac{{p}^{2}}{{x}^{2}}\right)}} \]
      2. unsub-neg32.8%

        \[\leadsto \sqrt{\color{blue}{1 - \frac{{p}^{2}}{{x}^{2}}}} \]
      3. unpow232.8%

        \[\leadsto \sqrt{1 - \frac{\color{blue}{p \cdot p}}{{x}^{2}}} \]
      4. unpow232.8%

        \[\leadsto \sqrt{1 - \frac{p \cdot p}{\color{blue}{x \cdot x}}} \]
      5. times-frac32.8%

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

        \[\leadsto \sqrt{1 - \color{blue}{{\left(\frac{p}{x}\right)}^{2}}} \]
    11. Simplified32.8%

      \[\leadsto \sqrt{\color{blue}{1 - {\left(\frac{p}{x}\right)}^{2}}} \]
    12. Step-by-step derivation
      1. pow232.8%

        \[\leadsto \sqrt{1 - \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    13. Applied egg-rr32.8%

      \[\leadsto \sqrt{1 - \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    14. Taylor expanded in p around 0 41.8%

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

    if 2.0499999999999999e-272 < p < 1.9e-249

    1. Initial program 68.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. +-commutative68.1%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around -inf 51.3%

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

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

        \[\leadsto \color{blue}{\frac{p}{-x}} \]
    7. Simplified51.3%

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

    if 1.9e-249 < p < 3.8000000000000001e-96

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 71.6%

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

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

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

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

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

        \[\leadsto 1 + \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)} \cdot -0.5 \]
    9. Applied egg-rr71.6%

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

    if 3.8000000000000001e-96 < p

    1. Initial program 90.2%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 83.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 2.05 \cdot 10^{-272}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.9 \cdot 10^{-249}:\\ \;\;\;\;\frac{p}{-x}\\ \mathbf{elif}\;p \leq 3.8 \cdot 10^{-96}:\\ \;\;\;\;1 + \left(\frac{p}{x} \cdot \frac{p}{x}\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 55.1% accurate, 23.8× speedup?

\[\begin{array}{l} p_m = \left|p\right| \\ \begin{array}{l} \mathbf{if}\;x \leq -7.4 \cdot 10^{-109}:\\ \;\;\;\;\frac{p\_m}{-x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
p_m = (fabs.f64 p)
(FPCore (p_m x) :precision binary64 (if (<= x -7.4e-109) (/ p_m (- x)) 1.0))
p_m = fabs(p);
double code(double p_m, double x) {
	double tmp;
	if (x <= -7.4e-109) {
		tmp = p_m / -x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
    real(8), intent (in) :: p_m
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-7.4d-109)) then
        tmp = p_m / -x
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
	double tmp;
	if (x <= -7.4e-109) {
		tmp = p_m / -x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p_m = math.fabs(p)
def code(p_m, x):
	tmp = 0
	if x <= -7.4e-109:
		tmp = p_m / -x
	else:
		tmp = 1.0
	return tmp
p_m = abs(p)
function code(p_m, x)
	tmp = 0.0
	if (x <= -7.4e-109)
		tmp = Float64(p_m / Float64(-x));
	else
		tmp = 1.0;
	end
	return tmp
end
p_m = abs(p);
function tmp_2 = code(p_m, x)
	tmp = 0.0;
	if (x <= -7.4e-109)
		tmp = p_m / -x;
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := If[LessEqual[x, -7.4e-109], N[(p$95$m / (-x)), $MachinePrecision], 1.0]
\begin{array}{l}
p_m = \left|p\right|

\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.4 \cdot 10^{-109}:\\
\;\;\;\;\frac{p\_m}{-x}\\

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


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

    1. Initial program 63.4%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around -inf 27.2%

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

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

        \[\leadsto \color{blue}{\frac{p}{-x}} \]
    7. Simplified27.2%

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

    if -7.39999999999999961e-109 < x

    1. Initial program 98.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. +-commutative98.9%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. div-inv98.9%

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

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

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

        \[\leadsto \sqrt{0.5 + 0.5 \cdot \left(x \cdot \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}}}}\right)} \]
      5. hypot-define98.9%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{1 + -1 \cdot \frac{{p}^{2}}{{x}^{2}}}} \]
    10. Step-by-step derivation
      1. mul-1-neg46.8%

        \[\leadsto \sqrt{1 + \color{blue}{\left(-\frac{{p}^{2}}{{x}^{2}}\right)}} \]
      2. unsub-neg46.8%

        \[\leadsto \sqrt{\color{blue}{1 - \frac{{p}^{2}}{{x}^{2}}}} \]
      3. unpow246.8%

        \[\leadsto \sqrt{1 - \frac{\color{blue}{p \cdot p}}{{x}^{2}}} \]
      4. unpow246.8%

        \[\leadsto \sqrt{1 - \frac{p \cdot p}{\color{blue}{x \cdot x}}} \]
      5. times-frac46.8%

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

        \[\leadsto \sqrt{1 - \color{blue}{{\left(\frac{p}{x}\right)}^{2}}} \]
    11. Simplified46.8%

      \[\leadsto \sqrt{\color{blue}{1 - {\left(\frac{p}{x}\right)}^{2}}} \]
    12. Step-by-step derivation
      1. pow246.8%

        \[\leadsto \sqrt{1 - \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    13. Applied egg-rr46.8%

      \[\leadsto \sqrt{1 - \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
    14. Taylor expanded in p around 0 56.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -7.4 \cdot 10^{-109}:\\ \;\;\;\;\frac{p}{-x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 36.8% accurate, 215.0× speedup?

\[\begin{array}{l} p_m = \left|p\right| \\ 1 \end{array} \]
p_m = (fabs.f64 p)
(FPCore (p_m x) :precision binary64 1.0)
p_m = fabs(p);
double code(double p_m, double x) {
	return 1.0;
}
p_m = abs(p)
real(8) function code(p_m, x)
    real(8), intent (in) :: p_m
    real(8), intent (in) :: x
    code = 1.0d0
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
	return 1.0;
}
p_m = math.fabs(p)
def code(p_m, x):
	return 1.0
p_m = abs(p)
function code(p_m, x)
	return 1.0
end
p_m = abs(p);
function tmp = code(p_m, x)
	tmp = 1.0;
end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := 1.0
\begin{array}{l}
p_m = \left|p\right|

\\
1
\end{array}
Derivation
  1. Initial program 84.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. +-commutative84.5%

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

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

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

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

      \[\leadsto \sqrt{0.5 \cdot \left(\frac{x}{\sqrt{\color{blue}{\mathsf{fma}\left(4, p \cdot p, x \cdot x\right)}}} + 1\right)} \]
    6. sqr-neg84.5%

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

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

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

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

    \[\leadsto \color{blue}{\sqrt{0.5 + 0.5 \cdot \frac{x}{\sqrt{\mathsf{fma}\left(x, x, 4 \cdot \left(p \cdot p\right)\right)}}}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. div-inv84.1%

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

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

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

      \[\leadsto \sqrt{0.5 + 0.5 \cdot \left(x \cdot \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}}}}\right)} \]
    5. hypot-define84.1%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \sqrt{\color{blue}{1 + -1 \cdot \frac{{p}^{2}}{{x}^{2}}}} \]
  10. Step-by-step derivation
    1. mul-1-neg28.8%

      \[\leadsto \sqrt{1 + \color{blue}{\left(-\frac{{p}^{2}}{{x}^{2}}\right)}} \]
    2. unsub-neg28.8%

      \[\leadsto \sqrt{\color{blue}{1 - \frac{{p}^{2}}{{x}^{2}}}} \]
    3. unpow228.8%

      \[\leadsto \sqrt{1 - \frac{\color{blue}{p \cdot p}}{{x}^{2}}} \]
    4. unpow228.8%

      \[\leadsto \sqrt{1 - \frac{p \cdot p}{\color{blue}{x \cdot x}}} \]
    5. times-frac28.8%

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

      \[\leadsto \sqrt{1 - \color{blue}{{\left(\frac{p}{x}\right)}^{2}}} \]
  11. Simplified28.8%

    \[\leadsto \sqrt{\color{blue}{1 - {\left(\frac{p}{x}\right)}^{2}}} \]
  12. Step-by-step derivation
    1. pow228.8%

      \[\leadsto \sqrt{1 - \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
  13. Applied egg-rr28.8%

    \[\leadsto \sqrt{1 - \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}} \]
  14. Taylor expanded in p around 0 39.0%

    \[\leadsto \color{blue}{1} \]
  15. Final simplification39.0%

    \[\leadsto 1 \]
  16. Add Preprocessing

Developer target: 79.7% 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 2024077 
(FPCore (p x)
  :name "Given's Rotation SVD example"
  :precision binary64
  :pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))

  :alt
  (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))))))))