0.5 * sqrt(2.0 * (sqrt(xre * xre + xim * xim) + xre))

Percentage Accurate: 42.1% → 85.1%
Time: 5.1s
Alternatives: 6
Speedup: 1.7×

Specification

?
\[\left(-1.79 \cdot 10^{+308} \leq xre \land xre \leq 1.79 \cdot 10^{+308}\right) \land \left(0 \leq xim \land xim \leq 1.79 \cdot 10^{+308}\right)\]
\[\begin{array}{l} \\ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* xre xre) (* xim xim))) xre)))))
double code(double xre, double xim) {
	return 0.5 * sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre)));
}
real(8) function code(xre, xim)
    real(8), intent (in) :: xre
    real(8), intent (in) :: xim
    code = 0.5d0 * sqrt((2.0d0 * (sqrt(((xre * xre) + (xim * xim))) + xre)))
end function
public static double code(double xre, double xim) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((xre * xre) + (xim * xim))) + xre)));
}
def code(xre, xim):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((xre * xre) + (xim * xim))) + xre)))
function code(xre, xim)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(xre * xre) + Float64(xim * xim))) + xre))))
end
function tmp = code(xre, xim)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre)));
end
code[xre_, xim_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(xre * xre), $MachinePrecision] + N[(xim * xim), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\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 6 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: 42.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* xre xre) (* xim xim))) xre)))))
double code(double xre, double xim) {
	return 0.5 * sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre)));
}
real(8) function code(xre, xim)
    real(8), intent (in) :: xre
    real(8), intent (in) :: xim
    code = 0.5d0 * sqrt((2.0d0 * (sqrt(((xre * xre) + (xim * xim))) + xre)))
end function
public static double code(double xre, double xim) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((xre * xre) + (xim * xim))) + xre)));
}
def code(xre, xim):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((xre * xre) + (xim * xim))) + xre)))
function code(xre, xim)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(xre * xre) + Float64(xim * xim))) + xre))))
end
function tmp = code(xre, xim)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre)));
end
code[xre_, xim_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(xre * xre), $MachinePrecision] + N[(xim * xim), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}
\end{array}

Alternative 1: 85.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \leq 0:\\ \;\;\;\;\sqrt{\frac{\left(-xim\right) \cdot xim}{xre}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5\\ \end{array} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (if (<= (sqrt (* 2.0 (+ (sqrt (+ (* xre xre) (* xim xim))) xre))) 0.0)
   (* (sqrt (/ (* (- xim) xim) xre)) 0.5)
   (* (sqrt (* (+ (hypot xim xre) xre) 2.0)) 0.5)))
double code(double xre, double xim) {
	double tmp;
	if (sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre))) <= 0.0) {
		tmp = sqrt(((-xim * xim) / xre)) * 0.5;
	} else {
		tmp = sqrt(((hypot(xim, xre) + xre) * 2.0)) * 0.5;
	}
	return tmp;
}
public static double code(double xre, double xim) {
	double tmp;
	if (Math.sqrt((2.0 * (Math.sqrt(((xre * xre) + (xim * xim))) + xre))) <= 0.0) {
		tmp = Math.sqrt(((-xim * xim) / xre)) * 0.5;
	} else {
		tmp = Math.sqrt(((Math.hypot(xim, xre) + xre) * 2.0)) * 0.5;
	}
	return tmp;
}
def code(xre, xim):
	tmp = 0
	if math.sqrt((2.0 * (math.sqrt(((xre * xre) + (xim * xim))) + xre))) <= 0.0:
		tmp = math.sqrt(((-xim * xim) / xre)) * 0.5
	else:
		tmp = math.sqrt(((math.hypot(xim, xre) + xre) * 2.0)) * 0.5
	return tmp
function code(xre, xim)
	tmp = 0.0
	if (sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(xre * xre) + Float64(xim * xim))) + xre))) <= 0.0)
		tmp = Float64(sqrt(Float64(Float64(Float64(-xim) * xim) / xre)) * 0.5);
	else
		tmp = Float64(sqrt(Float64(Float64(hypot(xim, xre) + xre) * 2.0)) * 0.5);
	end
	return tmp
end
function tmp_2 = code(xre, xim)
	tmp = 0.0;
	if (sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre))) <= 0.0)
		tmp = sqrt(((-xim * xim) / xre)) * 0.5;
	else
		tmp = sqrt(((hypot(xim, xre) + xre) * 2.0)) * 0.5;
	end
	tmp_2 = tmp;
end
code[xre_, xim_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(xre * xre), $MachinePrecision] + N[(xim * xim), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(N[Sqrt[N[(N[((-xim) * xim), $MachinePrecision] / xre), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[Sqrt[N[(N[(N[Sqrt[xim ^ 2 + xre ^ 2], $MachinePrecision] + xre), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \leq 0:\\
\;\;\;\;\sqrt{\frac{\left(-xim\right) \cdot xim}{xre}} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 xre xre) (*.f64 xim xim))) xre))) < 0.0

    1. Initial program 8.9%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot \frac{1}{2}} \]
      3. lower-*.f648.9

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f648.9

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot 0.5 \]
      7. lift-sqrt.f64N/A

        \[\leadsto \sqrt{\left(\color{blue}{\sqrt{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      8. lift-+.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim + xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      10. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim} + xre \cdot xre} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      11. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{xim \cdot xim + \color{blue}{xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      12. lower-hypot.f648.9

        \[\leadsto \sqrt{\left(\color{blue}{\mathsf{hypot}\left(xim, xre\right)} + xre\right) \cdot 2} \cdot 0.5 \]
    4. Applied rewrites8.9%

      \[\leadsto \color{blue}{\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5} \]
    5. Taylor expanded in xre around -inf

      \[\leadsto \sqrt{\color{blue}{-1 \cdot \frac{{xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1 \cdot {xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
      2. lower-/.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1 \cdot {xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
      3. unpow2N/A

        \[\leadsto \sqrt{\frac{-1 \cdot \color{blue}{\left(xim \cdot xim\right)}}{xre}} \cdot \frac{1}{2} \]
      4. associate-*r*N/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-1 \cdot xim\right) \cdot xim}}{xre}} \cdot \frac{1}{2} \]
      5. lower-*.f64N/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-1 \cdot xim\right) \cdot xim}}{xre}} \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(\mathsf{neg}\left(xim\right)\right)} \cdot xim}{xre}} \cdot \frac{1}{2} \]
      7. lower-neg.f6453.3

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-xim\right)} \cdot xim}{xre}} \cdot 0.5 \]
    7. Applied rewrites53.3%

      \[\leadsto \sqrt{\color{blue}{\frac{\left(-xim\right) \cdot xim}{xre}}} \cdot 0.5 \]

    if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 xre xre) (*.f64 xim xim))) xre)))

    1. Initial program 42.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot \frac{1}{2}} \]
      3. lower-*.f6442.3

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f6442.3

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot 0.5 \]
      7. lift-sqrt.f64N/A

        \[\leadsto \sqrt{\left(\color{blue}{\sqrt{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      8. lift-+.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim + xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      10. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim} + xre \cdot xre} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      11. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{xim \cdot xim + \color{blue}{xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      12. lower-hypot.f6489.6

        \[\leadsto \sqrt{\left(\color{blue}{\mathsf{hypot}\left(xim, xre\right)} + xre\right) \cdot 2} \cdot 0.5 \]
    4. Applied rewrites89.6%

      \[\leadsto \color{blue}{\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 72.6% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;xre \leq -1.3 \cdot 10^{+74}:\\ \;\;\;\;\sqrt{\frac{\left(-xim\right) \cdot xim}{xre}} \cdot 0.5\\ \mathbf{elif}\;xre \leq 2.3 \cdot 10^{-100}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{xre}{xim} + 2, xre, 2 \cdot xim\right)} \cdot 0.5\\ \mathbf{elif}\;xre \leq 2.7 \cdot 10^{+86}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{xre}\\ \end{array} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (if (<= xre -1.3e+74)
   (* (sqrt (/ (* (- xim) xim) xre)) 0.5)
   (if (<= xre 2.3e-100)
     (* (sqrt (fma (+ (/ xre xim) 2.0) xre (* 2.0 xim))) 0.5)
     (if (<= xre 2.7e+86)
       (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* xre xre) (* xim xim))) xre))))
       (sqrt xre)))))
double code(double xre, double xim) {
	double tmp;
	if (xre <= -1.3e+74) {
		tmp = sqrt(((-xim * xim) / xre)) * 0.5;
	} else if (xre <= 2.3e-100) {
		tmp = sqrt(fma(((xre / xim) + 2.0), xre, (2.0 * xim))) * 0.5;
	} else if (xre <= 2.7e+86) {
		tmp = 0.5 * sqrt((2.0 * (sqrt(((xre * xre) + (xim * xim))) + xre)));
	} else {
		tmp = sqrt(xre);
	}
	return tmp;
}
function code(xre, xim)
	tmp = 0.0
	if (xre <= -1.3e+74)
		tmp = Float64(sqrt(Float64(Float64(Float64(-xim) * xim) / xre)) * 0.5);
	elseif (xre <= 2.3e-100)
		tmp = Float64(sqrt(fma(Float64(Float64(xre / xim) + 2.0), xre, Float64(2.0 * xim))) * 0.5);
	elseif (xre <= 2.7e+86)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(xre * xre) + Float64(xim * xim))) + xre))));
	else
		tmp = sqrt(xre);
	end
	return tmp
end
code[xre_, xim_] := If[LessEqual[xre, -1.3e+74], N[(N[Sqrt[N[(N[((-xim) * xim), $MachinePrecision] / xre), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[xre, 2.3e-100], N[(N[Sqrt[N[(N[(N[(xre / xim), $MachinePrecision] + 2.0), $MachinePrecision] * xre + N[(2.0 * xim), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[xre, 2.7e+86], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(xre * xre), $MachinePrecision] + N[(xim * xim), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[xre], $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;xre \leq -1.3 \cdot 10^{+74}:\\
\;\;\;\;\sqrt{\frac{\left(-xim\right) \cdot xim}{xre}} \cdot 0.5\\

\mathbf{elif}\;xre \leq 2.3 \cdot 10^{-100}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{xre}{xim} + 2, xre, 2 \cdot xim\right)} \cdot 0.5\\

\mathbf{elif}\;xre \leq 2.7 \cdot 10^{+86}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if xre < -1.3e74

    1. Initial program 5.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot \frac{1}{2}} \]
      3. lower-*.f645.4

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f645.4

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot 0.5 \]
      7. lift-sqrt.f64N/A

        \[\leadsto \sqrt{\left(\color{blue}{\sqrt{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      8. lift-+.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim + xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      10. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim} + xre \cdot xre} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      11. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{xim \cdot xim + \color{blue}{xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      12. lower-hypot.f6439.4

        \[\leadsto \sqrt{\left(\color{blue}{\mathsf{hypot}\left(xim, xre\right)} + xre\right) \cdot 2} \cdot 0.5 \]
    4. Applied rewrites39.4%

      \[\leadsto \color{blue}{\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5} \]
    5. Taylor expanded in xre around -inf

      \[\leadsto \sqrt{\color{blue}{-1 \cdot \frac{{xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1 \cdot {xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
      2. lower-/.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1 \cdot {xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
      3. unpow2N/A

        \[\leadsto \sqrt{\frac{-1 \cdot \color{blue}{\left(xim \cdot xim\right)}}{xre}} \cdot \frac{1}{2} \]
      4. associate-*r*N/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-1 \cdot xim\right) \cdot xim}}{xre}} \cdot \frac{1}{2} \]
      5. lower-*.f64N/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-1 \cdot xim\right) \cdot xim}}{xre}} \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(\mathsf{neg}\left(xim\right)\right)} \cdot xim}{xre}} \cdot \frac{1}{2} \]
      7. lower-neg.f6448.5

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-xim\right)} \cdot xim}{xre}} \cdot 0.5 \]
    7. Applied rewrites48.5%

      \[\leadsto \sqrt{\color{blue}{\frac{\left(-xim\right) \cdot xim}{xre}}} \cdot 0.5 \]

    if -1.3e74 < xre < 2.29999999999999994e-100

    1. Initial program 50.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot \frac{1}{2}} \]
      3. lower-*.f6450.3

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f6450.3

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot 0.5 \]
      7. lift-sqrt.f64N/A

        \[\leadsto \sqrt{\left(\color{blue}{\sqrt{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      8. lift-+.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim + xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      10. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim} + xre \cdot xre} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      11. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{xim \cdot xim + \color{blue}{xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      12. lower-hypot.f6483.0

        \[\leadsto \sqrt{\left(\color{blue}{\mathsf{hypot}\left(xim, xre\right)} + xre\right) \cdot 2} \cdot 0.5 \]
    4. Applied rewrites83.0%

      \[\leadsto \color{blue}{\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5} \]
    5. Taylor expanded in xre around 0

      \[\leadsto \sqrt{\color{blue}{2 \cdot xim + xre \cdot \left(2 + \frac{xre}{xim}\right)}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{xre \cdot \left(2 + \frac{xre}{xim}\right) + 2 \cdot xim}} \cdot \frac{1}{2} \]
      2. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(2 + \frac{xre}{xim}\right) \cdot xre} + 2 \cdot xim} \cdot \frac{1}{2} \]
      3. lower-fma.f64N/A

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(2 + \frac{xre}{xim}, xre, 2 \cdot xim\right)}} \cdot \frac{1}{2} \]
      4. +-commutativeN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\color{blue}{\frac{xre}{xim} + 2}, xre, 2 \cdot xim\right)} \cdot \frac{1}{2} \]
      5. lower-+.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\color{blue}{\frac{xre}{xim} + 2}, xre, 2 \cdot xim\right)} \cdot \frac{1}{2} \]
      6. lower-/.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\color{blue}{\frac{xre}{xim}} + 2, xre, 2 \cdot xim\right)} \cdot \frac{1}{2} \]
      7. lower-*.f6477.3

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{xre}{xim} + 2, xre, \color{blue}{2 \cdot xim}\right)} \cdot 0.5 \]
    7. Applied rewrites77.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\frac{xre}{xim} + 2, xre, 2 \cdot xim\right)}} \cdot 0.5 \]

    if 2.29999999999999994e-100 < xre < 2.70000000000000018e86

    1. Initial program 74.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing

    if 2.70000000000000018e86 < xre

    1. Initial program 21.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{xre} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{xre}\right)} \]
      2. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{xre}} \]
      3. unpow2N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)}\right) \cdot \sqrt{xre} \]
      4. rem-square-sqrtN/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{2}\right) \cdot \sqrt{xre} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{xre} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{xre}} \]
      7. lower-sqrt.f6480.6

        \[\leadsto \color{blue}{\sqrt{xre}} \]
    5. Applied rewrites80.6%

      \[\leadsto \color{blue}{\sqrt{xre}} \]
  3. Recombined 4 regimes into one program.
  4. Add Preprocessing

Alternative 3: 69.7% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;xre \leq -1.3 \cdot 10^{+74}:\\ \;\;\;\;\sqrt{\frac{\left(-xim\right) \cdot xim}{xre}} \cdot 0.5\\ \mathbf{elif}\;xre \leq 2.25 \cdot 10^{+86}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(xim + xre\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{xre}\\ \end{array} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (if (<= xre -1.3e+74)
   (* (sqrt (/ (* (- xim) xim) xre)) 0.5)
   (if (<= xre 2.25e+86) (* 0.5 (sqrt (* 2.0 (+ xim xre)))) (sqrt xre))))
double code(double xre, double xim) {
	double tmp;
	if (xre <= -1.3e+74) {
		tmp = sqrt(((-xim * xim) / xre)) * 0.5;
	} else if (xre <= 2.25e+86) {
		tmp = 0.5 * sqrt((2.0 * (xim + xre)));
	} else {
		tmp = sqrt(xre);
	}
	return tmp;
}
real(8) function code(xre, xim)
    real(8), intent (in) :: xre
    real(8), intent (in) :: xim
    real(8) :: tmp
    if (xre <= (-1.3d+74)) then
        tmp = sqrt(((-xim * xim) / xre)) * 0.5d0
    else if (xre <= 2.25d+86) then
        tmp = 0.5d0 * sqrt((2.0d0 * (xim + xre)))
    else
        tmp = sqrt(xre)
    end if
    code = tmp
end function
public static double code(double xre, double xim) {
	double tmp;
	if (xre <= -1.3e+74) {
		tmp = Math.sqrt(((-xim * xim) / xre)) * 0.5;
	} else if (xre <= 2.25e+86) {
		tmp = 0.5 * Math.sqrt((2.0 * (xim + xre)));
	} else {
		tmp = Math.sqrt(xre);
	}
	return tmp;
}
def code(xre, xim):
	tmp = 0
	if xre <= -1.3e+74:
		tmp = math.sqrt(((-xim * xim) / xre)) * 0.5
	elif xre <= 2.25e+86:
		tmp = 0.5 * math.sqrt((2.0 * (xim + xre)))
	else:
		tmp = math.sqrt(xre)
	return tmp
function code(xre, xim)
	tmp = 0.0
	if (xre <= -1.3e+74)
		tmp = Float64(sqrt(Float64(Float64(Float64(-xim) * xim) / xre)) * 0.5);
	elseif (xre <= 2.25e+86)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(xim + xre))));
	else
		tmp = sqrt(xre);
	end
	return tmp
end
function tmp_2 = code(xre, xim)
	tmp = 0.0;
	if (xre <= -1.3e+74)
		tmp = sqrt(((-xim * xim) / xre)) * 0.5;
	elseif (xre <= 2.25e+86)
		tmp = 0.5 * sqrt((2.0 * (xim + xre)));
	else
		tmp = sqrt(xre);
	end
	tmp_2 = tmp;
end
code[xre_, xim_] := If[LessEqual[xre, -1.3e+74], N[(N[Sqrt[N[(N[((-xim) * xim), $MachinePrecision] / xre), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[xre, 2.25e+86], N[(0.5 * N[Sqrt[N[(2.0 * N[(xim + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[xre], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;xre \leq -1.3 \cdot 10^{+74}:\\
\;\;\;\;\sqrt{\frac{\left(-xim\right) \cdot xim}{xre}} \cdot 0.5\\

\mathbf{elif}\;xre \leq 2.25 \cdot 10^{+86}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(xim + xre\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if xre < -1.3e74

    1. Initial program 5.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot \frac{1}{2}} \]
      3. lower-*.f645.4

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f645.4

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot 0.5 \]
      7. lift-sqrt.f64N/A

        \[\leadsto \sqrt{\left(\color{blue}{\sqrt{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      8. lift-+.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim + xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      10. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim} + xre \cdot xre} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      11. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{xim \cdot xim + \color{blue}{xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      12. lower-hypot.f6439.4

        \[\leadsto \sqrt{\left(\color{blue}{\mathsf{hypot}\left(xim, xre\right)} + xre\right) \cdot 2} \cdot 0.5 \]
    4. Applied rewrites39.4%

      \[\leadsto \color{blue}{\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5} \]
    5. Taylor expanded in xre around -inf

      \[\leadsto \sqrt{\color{blue}{-1 \cdot \frac{{xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1 \cdot {xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
      2. lower-/.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{-1 \cdot {xim}^{2}}{xre}}} \cdot \frac{1}{2} \]
      3. unpow2N/A

        \[\leadsto \sqrt{\frac{-1 \cdot \color{blue}{\left(xim \cdot xim\right)}}{xre}} \cdot \frac{1}{2} \]
      4. associate-*r*N/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-1 \cdot xim\right) \cdot xim}}{xre}} \cdot \frac{1}{2} \]
      5. lower-*.f64N/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-1 \cdot xim\right) \cdot xim}}{xre}} \cdot \frac{1}{2} \]
      6. mul-1-negN/A

        \[\leadsto \sqrt{\frac{\color{blue}{\left(\mathsf{neg}\left(xim\right)\right)} \cdot xim}{xre}} \cdot \frac{1}{2} \]
      7. lower-neg.f6448.5

        \[\leadsto \sqrt{\frac{\color{blue}{\left(-xim\right)} \cdot xim}{xre}} \cdot 0.5 \]
    7. Applied rewrites48.5%

      \[\leadsto \sqrt{\color{blue}{\frac{\left(-xim\right) \cdot xim}{xre}}} \cdot 0.5 \]

    if -1.3e74 < xre < 2.24999999999999996e86

    1. Initial program 56.6%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \color{blue}{\left(xim + xre\right)}} \]
    4. Step-by-step derivation
      1. lower-+.f6473.2

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(xim + xre\right)}} \]
    5. Applied rewrites73.2%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(xim + xre\right)}} \]

    if 2.24999999999999996e86 < xre

    1. Initial program 21.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{xre} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{xre}\right)} \]
      2. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{xre}} \]
      3. unpow2N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)}\right) \cdot \sqrt{xre} \]
      4. rem-square-sqrtN/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{2}\right) \cdot \sqrt{xre} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{xre} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{xre}} \]
      7. lower-sqrt.f6480.6

        \[\leadsto \color{blue}{\sqrt{xre}} \]
    5. Applied rewrites80.6%

      \[\leadsto \color{blue}{\sqrt{xre}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 4: 65.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;xre \leq -1.75 \cdot 10^{+148}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(-xre\right) + xre\right)}\\ \mathbf{elif}\;xre \leq 2.25 \cdot 10^{+86}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(xim + xre\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{xre}\\ \end{array} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (if (<= xre -1.75e+148)
   (* 0.5 (sqrt (* 2.0 (+ (- xre) xre))))
   (if (<= xre 2.25e+86) (* 0.5 (sqrt (* 2.0 (+ xim xre)))) (sqrt xre))))
double code(double xre, double xim) {
	double tmp;
	if (xre <= -1.75e+148) {
		tmp = 0.5 * sqrt((2.0 * (-xre + xre)));
	} else if (xre <= 2.25e+86) {
		tmp = 0.5 * sqrt((2.0 * (xim + xre)));
	} else {
		tmp = sqrt(xre);
	}
	return tmp;
}
real(8) function code(xre, xim)
    real(8), intent (in) :: xre
    real(8), intent (in) :: xim
    real(8) :: tmp
    if (xre <= (-1.75d+148)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (-xre + xre)))
    else if (xre <= 2.25d+86) then
        tmp = 0.5d0 * sqrt((2.0d0 * (xim + xre)))
    else
        tmp = sqrt(xre)
    end if
    code = tmp
end function
public static double code(double xre, double xim) {
	double tmp;
	if (xre <= -1.75e+148) {
		tmp = 0.5 * Math.sqrt((2.0 * (-xre + xre)));
	} else if (xre <= 2.25e+86) {
		tmp = 0.5 * Math.sqrt((2.0 * (xim + xre)));
	} else {
		tmp = Math.sqrt(xre);
	}
	return tmp;
}
def code(xre, xim):
	tmp = 0
	if xre <= -1.75e+148:
		tmp = 0.5 * math.sqrt((2.0 * (-xre + xre)))
	elif xre <= 2.25e+86:
		tmp = 0.5 * math.sqrt((2.0 * (xim + xre)))
	else:
		tmp = math.sqrt(xre)
	return tmp
function code(xre, xim)
	tmp = 0.0
	if (xre <= -1.75e+148)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(-xre) + xre))));
	elseif (xre <= 2.25e+86)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(xim + xre))));
	else
		tmp = sqrt(xre);
	end
	return tmp
end
function tmp_2 = code(xre, xim)
	tmp = 0.0;
	if (xre <= -1.75e+148)
		tmp = 0.5 * sqrt((2.0 * (-xre + xre)));
	elseif (xre <= 2.25e+86)
		tmp = 0.5 * sqrt((2.0 * (xim + xre)));
	else
		tmp = sqrt(xre);
	end
	tmp_2 = tmp;
end
code[xre_, xim_] := If[LessEqual[xre, -1.75e+148], N[(0.5 * N[Sqrt[N[(2.0 * N[((-xre) + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[xre, 2.25e+86], N[(0.5 * N[Sqrt[N[(2.0 * N[(xim + xre), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[xre], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;xre \leq -1.75 \cdot 10^{+148}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(-xre\right) + xre\right)}\\

\mathbf{elif}\;xre \leq 2.25 \cdot 10^{+86}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(xim + xre\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if xre < -1.7499999999999999e148

    1. Initial program 2.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\color{blue}{-1 \cdot xre} + xre\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{neg}\left(xre\right)\right)} + xre\right)} \]
      2. lower-neg.f6426.4

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(-xre\right)} + xre\right)} \]
    5. Applied rewrites26.4%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(-xre\right)} + xre\right)} \]

    if -1.7499999999999999e148 < xre < 2.24999999999999996e86

    1. Initial program 52.5%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \color{blue}{\left(xim + xre\right)}} \]
    4. Step-by-step derivation
      1. lower-+.f6468.9

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(xim + xre\right)}} \]
    5. Applied rewrites68.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(xim + xre\right)}} \]

    if 2.24999999999999996e86 < xre

    1. Initial program 21.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{xre} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{xre}\right)} \]
      2. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{xre}} \]
      3. unpow2N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)}\right) \cdot \sqrt{xre} \]
      4. rem-square-sqrtN/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{2}\right) \cdot \sqrt{xre} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{xre} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{xre}} \]
      7. lower-sqrt.f6480.6

        \[\leadsto \color{blue}{\sqrt{xre}} \]
    5. Applied rewrites80.6%

      \[\leadsto \color{blue}{\sqrt{xre}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 5: 64.1% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;xre \leq 1.9 \cdot 10^{+86}:\\ \;\;\;\;\sqrt{2 \cdot xim} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{xre}\\ \end{array} \end{array} \]
(FPCore (xre xim)
 :precision binary64
 (if (<= xre 1.9e+86) (* (sqrt (* 2.0 xim)) 0.5) (sqrt xre)))
double code(double xre, double xim) {
	double tmp;
	if (xre <= 1.9e+86) {
		tmp = sqrt((2.0 * xim)) * 0.5;
	} else {
		tmp = sqrt(xre);
	}
	return tmp;
}
real(8) function code(xre, xim)
    real(8), intent (in) :: xre
    real(8), intent (in) :: xim
    real(8) :: tmp
    if (xre <= 1.9d+86) then
        tmp = sqrt((2.0d0 * xim)) * 0.5d0
    else
        tmp = sqrt(xre)
    end if
    code = tmp
end function
public static double code(double xre, double xim) {
	double tmp;
	if (xre <= 1.9e+86) {
		tmp = Math.sqrt((2.0 * xim)) * 0.5;
	} else {
		tmp = Math.sqrt(xre);
	}
	return tmp;
}
def code(xre, xim):
	tmp = 0
	if xre <= 1.9e+86:
		tmp = math.sqrt((2.0 * xim)) * 0.5
	else:
		tmp = math.sqrt(xre)
	return tmp
function code(xre, xim)
	tmp = 0.0
	if (xre <= 1.9e+86)
		tmp = Float64(sqrt(Float64(2.0 * xim)) * 0.5);
	else
		tmp = sqrt(xre);
	end
	return tmp
end
function tmp_2 = code(xre, xim)
	tmp = 0.0;
	if (xre <= 1.9e+86)
		tmp = sqrt((2.0 * xim)) * 0.5;
	else
		tmp = sqrt(xre);
	end
	tmp_2 = tmp;
end
code[xre_, xim_] := If[LessEqual[xre, 1.9e+86], N[(N[Sqrt[N[(2.0 * xim), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[Sqrt[xre], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;xre \leq 1.9 \cdot 10^{+86}:\\
\;\;\;\;\sqrt{2 \cdot xim} \cdot 0.5\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if xre < 1.89999999999999989e86

    1. Initial program 43.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot \frac{1}{2}} \]
      3. lower-*.f6443.7

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f6443.7

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right) \cdot 2}} \cdot 0.5 \]
      7. lift-sqrt.f64N/A

        \[\leadsto \sqrt{\left(\color{blue}{\sqrt{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      8. lift-+.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xre \cdot xre + xim \cdot xim}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      9. +-commutativeN/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim + xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      10. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{\color{blue}{xim \cdot xim} + xre \cdot xre} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      11. lift-*.f64N/A

        \[\leadsto \sqrt{\left(\sqrt{xim \cdot xim + \color{blue}{xre \cdot xre}} + xre\right) \cdot 2} \cdot \frac{1}{2} \]
      12. lower-hypot.f6475.3

        \[\leadsto \sqrt{\left(\color{blue}{\mathsf{hypot}\left(xim, xre\right)} + xre\right) \cdot 2} \cdot 0.5 \]
    4. Applied rewrites75.3%

      \[\leadsto \color{blue}{\sqrt{\left(\mathsf{hypot}\left(xim, xre\right) + xre\right) \cdot 2} \cdot 0.5} \]
    5. Taylor expanded in xre around 0

      \[\leadsto \sqrt{\color{blue}{2 \cdot xim}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. lower-*.f6459.0

        \[\leadsto \sqrt{\color{blue}{2 \cdot xim}} \cdot 0.5 \]
    7. Applied rewrites59.0%

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

    if 1.89999999999999989e86 < xre

    1. Initial program 21.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in xre around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{xre} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{xre}\right)} \]
      2. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{xre}} \]
      3. unpow2N/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)}\right) \cdot \sqrt{xre} \]
      4. rem-square-sqrtN/A

        \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{2}\right) \cdot \sqrt{xre} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{xre} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{xre}} \]
      7. lower-sqrt.f6480.6

        \[\leadsto \color{blue}{\sqrt{xre}} \]
    5. Applied rewrites80.6%

      \[\leadsto \color{blue}{\sqrt{xre}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 26.8% accurate, 4.3× speedup?

\[\begin{array}{l} \\ \sqrt{xre} \end{array} \]
(FPCore (xre xim) :precision binary64 (sqrt xre))
double code(double xre, double xim) {
	return sqrt(xre);
}
real(8) function code(xre, xim)
    real(8), intent (in) :: xre
    real(8), intent (in) :: xim
    code = sqrt(xre)
end function
public static double code(double xre, double xim) {
	return Math.sqrt(xre);
}
def code(xre, xim):
	return math.sqrt(xre)
function code(xre, xim)
	return sqrt(xre)
end
function tmp = code(xre, xim)
	tmp = sqrt(xre);
end
code[xre_, xim_] := N[Sqrt[xre], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{xre}
\end{array}
Derivation
  1. Initial program 38.7%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{xre \cdot xre + xim \cdot xim} + xre\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in xre around inf

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{xre} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{xre}\right)} \]
    2. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{xre}} \]
    3. unpow2N/A

      \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)}\right) \cdot \sqrt{xre} \]
    4. rem-square-sqrtN/A

      \[\leadsto \left(\frac{1}{2} \cdot \color{blue}{2}\right) \cdot \sqrt{xre} \]
    5. metadata-evalN/A

      \[\leadsto \color{blue}{1} \cdot \sqrt{xre} \]
    6. *-lft-identityN/A

      \[\leadsto \color{blue}{\sqrt{xre}} \]
    7. lower-sqrt.f6428.0

      \[\leadsto \color{blue}{\sqrt{xre}} \]
  5. Applied rewrites28.0%

    \[\leadsto \color{blue}{\sqrt{xre}} \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 1 
(FPCore (xre xim)
  :name "0.5 * sqrt(2.0 * (sqrt(xre * xre + xim * xim) + xre))"
  :precision binary64
  :pre (and (and (<= -1.79e+308 xre) (<= xre 1.79e+308)) (and (<= 0.0 xim) (<= xim 1.79e+308)))
  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* xre xre) (* xim xim))) xre)))))