Given's Rotation SVD example

Percentage Accurate: 79.3% → 99.8%
Time: 9.4s
Alternatives: 5
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 5 alternatives:

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

Initial Program: 79.3% accurate, 1.0× speedup?

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

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

Alternative 1: 99.8% accurate, 0.7× speedup?

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

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


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

    1. Initial program 12.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. expm1-log1p-u12.6%

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
    8. Simplified47.7%

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

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

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

Alternative 2: 64.8% accurate, 1.9× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := -\frac{p}{x}\\ \mathbf{if}\;x \leq -14000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -8 \cdot 10^{-54}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -1.05 \cdot 10^{-81}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.45:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq 3.95 \cdot 10^{+87}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 1.9 \cdot 10^{+101}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (- (/ p x))))
   (if (<= x -14000000.0)
     t_0
     (if (<= x -8e-54)
       (sqrt 0.5)
       (if (<= x -1.05e-81)
         t_0
         (if (<= x 1.45)
           (sqrt 0.5)
           (if (<= x 3.95e+87) 1.0 (if (<= x 1.9e+101) (sqrt 0.5) 1.0))))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = -(p / x);
	double tmp;
	if (x <= -14000000.0) {
		tmp = t_0;
	} else if (x <= -8e-54) {
		tmp = sqrt(0.5);
	} else if (x <= -1.05e-81) {
		tmp = t_0;
	} else if (x <= 1.45) {
		tmp = sqrt(0.5);
	} else if (x <= 3.95e+87) {
		tmp = 1.0;
	} else if (x <= 1.9e+101) {
		tmp = sqrt(0.5);
	} else {
		tmp = 1.0;
	}
	return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = -(p / x)
    if (x <= (-14000000.0d0)) then
        tmp = t_0
    else if (x <= (-8d-54)) then
        tmp = sqrt(0.5d0)
    else if (x <= (-1.05d-81)) then
        tmp = t_0
    else if (x <= 1.45d0) then
        tmp = sqrt(0.5d0)
    else if (x <= 3.95d+87) then
        tmp = 1.0d0
    else if (x <= 1.9d+101) then
        tmp = sqrt(0.5d0)
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double t_0 = -(p / x);
	double tmp;
	if (x <= -14000000.0) {
		tmp = t_0;
	} else if (x <= -8e-54) {
		tmp = Math.sqrt(0.5);
	} else if (x <= -1.05e-81) {
		tmp = t_0;
	} else if (x <= 1.45) {
		tmp = Math.sqrt(0.5);
	} else if (x <= 3.95e+87) {
		tmp = 1.0;
	} else if (x <= 1.9e+101) {
		tmp = Math.sqrt(0.5);
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = -(p / x)
	tmp = 0
	if x <= -14000000.0:
		tmp = t_0
	elif x <= -8e-54:
		tmp = math.sqrt(0.5)
	elif x <= -1.05e-81:
		tmp = t_0
	elif x <= 1.45:
		tmp = math.sqrt(0.5)
	elif x <= 3.95e+87:
		tmp = 1.0
	elif x <= 1.9e+101:
		tmp = math.sqrt(0.5)
	else:
		tmp = 1.0
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(-Float64(p / x))
	tmp = 0.0
	if (x <= -14000000.0)
		tmp = t_0;
	elseif (x <= -8e-54)
		tmp = sqrt(0.5);
	elseif (x <= -1.05e-81)
		tmp = t_0;
	elseif (x <= 1.45)
		tmp = sqrt(0.5);
	elseif (x <= 3.95e+87)
		tmp = 1.0;
	elseif (x <= 1.9e+101)
		tmp = sqrt(0.5);
	else
		tmp = 1.0;
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = -(p / x);
	tmp = 0.0;
	if (x <= -14000000.0)
		tmp = t_0;
	elseif (x <= -8e-54)
		tmp = sqrt(0.5);
	elseif (x <= -1.05e-81)
		tmp = t_0;
	elseif (x <= 1.45)
		tmp = sqrt(0.5);
	elseif (x <= 3.95e+87)
		tmp = 1.0;
	elseif (x <= 1.9e+101)
		tmp = sqrt(0.5);
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = (-N[(p / x), $MachinePrecision])}, If[LessEqual[x, -14000000.0], t$95$0, If[LessEqual[x, -8e-54], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[x, -1.05e-81], t$95$0, If[LessEqual[x, 1.45], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[x, 3.95e+87], 1.0, If[LessEqual[x, 1.9e+101], N[Sqrt[0.5], $MachinePrecision], 1.0]]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := -\frac{p}{x}\\
\mathbf{if}\;x \leq -14000000:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -8 \cdot 10^{-54}:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;x \leq -1.05 \cdot 10^{-81}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 1.45:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;x \leq 3.95 \cdot 10^{+87}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq 1.9 \cdot 10^{+101}:\\
\;\;\;\;\sqrt{0.5}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -1.4e7 or -8.0000000000000002e-54 < x < -1.05e-81

    1. Initial program 36.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. expm1-log1p-u36.3%

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

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

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

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

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

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

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

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

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

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

    if -1.4e7 < x < -8.0000000000000002e-54 or -1.05e-81 < x < 1.44999999999999996 or 3.9499999999999998e87 < x < 1.8999999999999999e101

    1. Initial program 86.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 72.6%

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

    if 1.44999999999999996 < x < 3.9499999999999998e87 or 1.8999999999999999e101 < 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. expm1-log1p-u99.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -14000000:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{elif}\;x \leq -8 \cdot 10^{-54}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -1.05 \cdot 10^{-81}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{elif}\;x \leq 1.45:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq 3.95 \cdot 10^{+87}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 1.9 \cdot 10^{+101}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 3: 64.7% accurate, 1.9× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := -\frac{p}{x}\\ \mathbf{if}\;x \leq -950000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -1.7 \cdot 10^{-54}:\\ \;\;\;\;\sqrt{0.5 + \frac{x \cdot 0.25}{p}}\\ \mathbf{elif}\;x \leq -7 \cdot 10^{-81}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.5:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq 10^{+87}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 5.4 \cdot 10^{+101}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (- (/ p x))))
   (if (<= x -950000.0)
     t_0
     (if (<= x -1.7e-54)
       (sqrt (+ 0.5 (/ (* x 0.25) p)))
       (if (<= x -7e-81)
         t_0
         (if (<= x 0.5)
           (sqrt 0.5)
           (if (<= x 1e+87) 1.0 (if (<= x 5.4e+101) (sqrt 0.5) 1.0))))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = -(p / x);
	double tmp;
	if (x <= -950000.0) {
		tmp = t_0;
	} else if (x <= -1.7e-54) {
		tmp = sqrt((0.5 + ((x * 0.25) / p)));
	} else if (x <= -7e-81) {
		tmp = t_0;
	} else if (x <= 0.5) {
		tmp = sqrt(0.5);
	} else if (x <= 1e+87) {
		tmp = 1.0;
	} else if (x <= 5.4e+101) {
		tmp = sqrt(0.5);
	} else {
		tmp = 1.0;
	}
	return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = -(p / x)
    if (x <= (-950000.0d0)) then
        tmp = t_0
    else if (x <= (-1.7d-54)) then
        tmp = sqrt((0.5d0 + ((x * 0.25d0) / p)))
    else if (x <= (-7d-81)) then
        tmp = t_0
    else if (x <= 0.5d0) then
        tmp = sqrt(0.5d0)
    else if (x <= 1d+87) then
        tmp = 1.0d0
    else if (x <= 5.4d+101) then
        tmp = sqrt(0.5d0)
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double t_0 = -(p / x);
	double tmp;
	if (x <= -950000.0) {
		tmp = t_0;
	} else if (x <= -1.7e-54) {
		tmp = Math.sqrt((0.5 + ((x * 0.25) / p)));
	} else if (x <= -7e-81) {
		tmp = t_0;
	} else if (x <= 0.5) {
		tmp = Math.sqrt(0.5);
	} else if (x <= 1e+87) {
		tmp = 1.0;
	} else if (x <= 5.4e+101) {
		tmp = Math.sqrt(0.5);
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = -(p / x)
	tmp = 0
	if x <= -950000.0:
		tmp = t_0
	elif x <= -1.7e-54:
		tmp = math.sqrt((0.5 + ((x * 0.25) / p)))
	elif x <= -7e-81:
		tmp = t_0
	elif x <= 0.5:
		tmp = math.sqrt(0.5)
	elif x <= 1e+87:
		tmp = 1.0
	elif x <= 5.4e+101:
		tmp = math.sqrt(0.5)
	else:
		tmp = 1.0
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(-Float64(p / x))
	tmp = 0.0
	if (x <= -950000.0)
		tmp = t_0;
	elseif (x <= -1.7e-54)
		tmp = sqrt(Float64(0.5 + Float64(Float64(x * 0.25) / p)));
	elseif (x <= -7e-81)
		tmp = t_0;
	elseif (x <= 0.5)
		tmp = sqrt(0.5);
	elseif (x <= 1e+87)
		tmp = 1.0;
	elseif (x <= 5.4e+101)
		tmp = sqrt(0.5);
	else
		tmp = 1.0;
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = -(p / x);
	tmp = 0.0;
	if (x <= -950000.0)
		tmp = t_0;
	elseif (x <= -1.7e-54)
		tmp = sqrt((0.5 + ((x * 0.25) / p)));
	elseif (x <= -7e-81)
		tmp = t_0;
	elseif (x <= 0.5)
		tmp = sqrt(0.5);
	elseif (x <= 1e+87)
		tmp = 1.0;
	elseif (x <= 5.4e+101)
		tmp = sqrt(0.5);
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = (-N[(p / x), $MachinePrecision])}, If[LessEqual[x, -950000.0], t$95$0, If[LessEqual[x, -1.7e-54], N[Sqrt[N[(0.5 + N[(N[(x * 0.25), $MachinePrecision] / p), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[x, -7e-81], t$95$0, If[LessEqual[x, 0.5], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[x, 1e+87], 1.0, If[LessEqual[x, 5.4e+101], N[Sqrt[0.5], $MachinePrecision], 1.0]]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := -\frac{p}{x}\\
\mathbf{if}\;x \leq -950000:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;x \leq -7 \cdot 10^{-81}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;x \leq 10^{+87}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq 5.4 \cdot 10^{+101}:\\
\;\;\;\;\sqrt{0.5}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if x < -9.5e5 or -1.69999999999999994e-54 < x < -6.99999999999999973e-81

    1. Initial program 36.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. expm1-log1p-u36.3%

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

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

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

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

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

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

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

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

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

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

    if -9.5e5 < x < -1.69999999999999994e-54

    1. Initial program 69.2%

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{0.5 + 0.25 \cdot \frac{x}{p}}} \]
    7. Step-by-step derivation
      1. associate-*r/65.6%

        \[\leadsto \sqrt{0.5 + \color{blue}{\frac{0.25 \cdot x}{p}}} \]
    8. Simplified65.6%

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

    if -6.99999999999999973e-81 < x < 0.5 or 9.9999999999999996e86 < x < 5.40000000000000012e101

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

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

    if 0.5 < x < 9.9999999999999996e86 or 5.40000000000000012e101 < 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. expm1-log1p-u99.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -950000:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{elif}\;x \leq -1.7 \cdot 10^{-54}:\\ \;\;\;\;\sqrt{0.5 + \frac{x \cdot 0.25}{p}}\\ \mathbf{elif}\;x \leq -7 \cdot 10^{-81}:\\ \;\;\;\;-\frac{p}{x}\\ \mathbf{elif}\;x \leq 0.5:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq 10^{+87}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq 5.4 \cdot 10^{+101}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 4: 55.7% accurate, 35.5× speedup?

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

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


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

    1. Initial program 54.3%

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

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

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

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

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

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

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

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

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

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

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

    if -8.4999999999999997e-146 < 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. expm1-log1p-u99.2%

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

        \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\color{blue}{1 \cdot 0.5 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot 0.5}}\right)\right) \]
      3. metadata-eval99.2%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 36.1% accurate, 215.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{1} \]
  5. Final simplification34.2%

    \[\leadsto 1 \]

Developer target: 79.4% 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 2023301 
(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))))))))