math.sqrt on complex, imaginary part, im greater than 0 branch

Percentage Accurate: 41.9% → 90.7%
Time: 10.6s
Alternatives: 6
Speedup: 2.0×

Specification

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

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\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: 41.9% accurate, 1.0× speedup?

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

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\end{array}

Alternative 1: 90.7% accurate, 0.5× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 12.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around inf 34.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    3. Step-by-step derivation
      1. unpow234.8%

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    4. Simplified34.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. expm1-log1p-u34.8%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)\right)} \]
      2. expm1-udef12.4%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)} - 1\right)} \]
      3. sqrt-div12.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{im \cdot im}}{\sqrt{re}}}\right)} - 1\right) \]
      4. sqrt-prod12.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right)} - 1\right) \]
      5. add-sqr-sqrt12.4%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    7. Step-by-step derivation
      1. expm1-def99.7%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)} \]
      2. expm1-log1p99.7%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    8. Simplified99.7%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]

    if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 45.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Step-by-step derivation
      1. hypot-def90.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
    3. Simplified90.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.1%

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

Alternative 2: 74.8% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -5.5 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.5 \cdot 10^{-104} \lor \neg \left(re \leq 3.9 \cdot 10^{-70}\right) \land \left(re \leq 3 \cdot 10^{+66} \lor \neg \left(re \leq 2.05 \cdot 10^{+97}\right) \land re \leq 2 \cdot 10^{+113}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -5.5e+32)
   (* 0.5 (sqrt (* re -4.0)))
   (if (or (<= re 5.5e-104)
           (and (not (<= re 3.9e-70))
                (or (<= re 3e+66)
                    (and (not (<= re 2.05e+97)) (<= re 2e+113)))))
     (* 0.5 (sqrt (* 2.0 im)))
     (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -5.5e+32) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if ((re <= 5.5e-104) || (!(re <= 3.9e-70) && ((re <= 3e+66) || (!(re <= 2.05e+97) && (re <= 2e+113))))) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im / sqrt(re));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-5.5d+32)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if ((re <= 5.5d-104) .or. (.not. (re <= 3.9d-70)) .and. (re <= 3d+66) .or. (.not. (re <= 2.05d+97)) .and. (re <= 2d+113)) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -5.5e+32) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if ((re <= 5.5e-104) || (!(re <= 3.9e-70) && ((re <= 3e+66) || (!(re <= 2.05e+97) && (re <= 2e+113))))) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -5.5e+32:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif (re <= 5.5e-104) or (not (re <= 3.9e-70) and ((re <= 3e+66) or (not (re <= 2.05e+97) and (re <= 2e+113)))):
		tmp = 0.5 * math.sqrt((2.0 * im))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -5.5e+32)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif ((re <= 5.5e-104) || (!(re <= 3.9e-70) && ((re <= 3e+66) || (!(re <= 2.05e+97) && (re <= 2e+113)))))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -5.5e+32)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif ((re <= 5.5e-104) || (~((re <= 3.9e-70)) && ((re <= 3e+66) || (~((re <= 2.05e+97)) && (re <= 2e+113)))))
		tmp = 0.5 * sqrt((2.0 * im));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -5.5e+32], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 5.5e-104], And[N[Not[LessEqual[re, 3.9e-70]], $MachinePrecision], Or[LessEqual[re, 3e+66], And[N[Not[LessEqual[re, 2.05e+97]], $MachinePrecision], LessEqual[re, 2e+113]]]]], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.5 \cdot 10^{+32}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 5.5 \cdot 10^{-104} \lor \neg \left(re \leq 3.9 \cdot 10^{-70}\right) \land \left(re \leq 3 \cdot 10^{+66} \lor \neg \left(re \leq 2.05 \cdot 10^{+97}\right) \land re \leq 2 \cdot 10^{+113}\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -5.49999999999999984e32

    1. Initial program 38.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around -inf 82.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    3. Step-by-step derivation
      1. *-commutative82.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified82.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -5.49999999999999984e32 < re < 5.4999999999999998e-104 or 3.90000000000000019e-70 < re < 3.00000000000000002e66 or 2.04999999999999994e97 < re < 2e113

    1. Initial program 54.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around 0 79.1%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    3. Step-by-step derivation
      1. *-commutative79.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    4. Simplified79.1%

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

    if 5.4999999999999998e-104 < re < 3.90000000000000019e-70 or 3.00000000000000002e66 < re < 2.04999999999999994e97 or 2e113 < re

    1. Initial program 11.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around inf 42.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    3. Step-by-step derivation
      1. unpow242.8%

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    4. Simplified42.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. expm1-log1p-u42.5%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)\right)} \]
      2. expm1-udef21.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)} - 1\right)} \]
      3. sqrt-div21.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{im \cdot im}}{\sqrt{re}}}\right)} - 1\right) \]
      4. sqrt-prod29.1%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right)} - 1\right) \]
      5. add-sqr-sqrt29.1%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    6. Applied egg-rr29.1%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    7. Step-by-step derivation
      1. expm1-def86.2%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)} \]
      2. expm1-log1p87.0%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    8. Simplified87.0%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification81.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5.5 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.5 \cdot 10^{-104} \lor \neg \left(re \leq 3.9 \cdot 10^{-70}\right) \land \left(re \leq 3 \cdot 10^{+66} \lor \neg \left(re \leq 2.05 \cdot 10^{+97}\right) \land re \leq 2 \cdot 10^{+113}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 74.7% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \frac{im}{\sqrt{re}}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -2 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.5 \cdot 10^{-104}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 5.2 \cdot 10^{-68}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.25 \cdot 10^{+69}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 3.2 \cdot 10^{+97}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{elif}\;re \leq 2 \cdot 10^{+113}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (/ im (sqrt re)))) (t_1 (* 0.5 (sqrt (* 2.0 im)))))
   (if (<= re -2e+32)
     (* 0.5 (sqrt (* re -4.0)))
     (if (<= re 5.5e-104)
       t_1
       (if (<= re 5.2e-68)
         t_0
         (if (<= re 1.25e+69)
           t_1
           (if (<= re 3.2e+97)
             (* 0.5 (* im (pow re -0.5)))
             (if (<= re 2e+113) t_1 t_0))))))))
double code(double re, double im) {
	double t_0 = 0.5 * (im / sqrt(re));
	double t_1 = 0.5 * sqrt((2.0 * im));
	double tmp;
	if (re <= -2e+32) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 5.5e-104) {
		tmp = t_1;
	} else if (re <= 5.2e-68) {
		tmp = t_0;
	} else if (re <= 1.25e+69) {
		tmp = t_1;
	} else if (re <= 3.2e+97) {
		tmp = 0.5 * (im * pow(re, -0.5));
	} else if (re <= 2e+113) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * (im / sqrt(re))
    t_1 = 0.5d0 * sqrt((2.0d0 * im))
    if (re <= (-2d+32)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 5.5d-104) then
        tmp = t_1
    else if (re <= 5.2d-68) then
        tmp = t_0
    else if (re <= 1.25d+69) then
        tmp = t_1
    else if (re <= 3.2d+97) then
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    else if (re <= 2d+113) then
        tmp = t_1
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * (im / Math.sqrt(re));
	double t_1 = 0.5 * Math.sqrt((2.0 * im));
	double tmp;
	if (re <= -2e+32) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 5.5e-104) {
		tmp = t_1;
	} else if (re <= 5.2e-68) {
		tmp = t_0;
	} else if (re <= 1.25e+69) {
		tmp = t_1;
	} else if (re <= 3.2e+97) {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	} else if (re <= 2e+113) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * (im / math.sqrt(re))
	t_1 = 0.5 * math.sqrt((2.0 * im))
	tmp = 0
	if re <= -2e+32:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 5.5e-104:
		tmp = t_1
	elif re <= 5.2e-68:
		tmp = t_0
	elif re <= 1.25e+69:
		tmp = t_1
	elif re <= 3.2e+97:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	elif re <= 2e+113:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * Float64(im / sqrt(re)))
	t_1 = Float64(0.5 * sqrt(Float64(2.0 * im)))
	tmp = 0.0
	if (re <= -2e+32)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 5.5e-104)
		tmp = t_1;
	elseif (re <= 5.2e-68)
		tmp = t_0;
	elseif (re <= 1.25e+69)
		tmp = t_1;
	elseif (re <= 3.2e+97)
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	elseif (re <= 2e+113)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * (im / sqrt(re));
	t_1 = 0.5 * sqrt((2.0 * im));
	tmp = 0.0;
	if (re <= -2e+32)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 5.5e-104)
		tmp = t_1;
	elseif (re <= 5.2e-68)
		tmp = t_0;
	elseif (re <= 1.25e+69)
		tmp = t_1;
	elseif (re <= 3.2e+97)
		tmp = 0.5 * (im * (re ^ -0.5));
	elseif (re <= 2e+113)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -2e+32], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.5e-104], t$95$1, If[LessEqual[re, 5.2e-68], t$95$0, If[LessEqual[re, 1.25e+69], t$95$1, If[LessEqual[re, 3.2e+97], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2e+113], t$95$1, t$95$0]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \frac{im}{\sqrt{re}}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{if}\;re \leq -2 \cdot 10^{+32}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 5.5 \cdot 10^{-104}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq 5.2 \cdot 10^{-68}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 1.25 \cdot 10^{+69}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq 3.2 \cdot 10^{+97}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\

\mathbf{elif}\;re \leq 2 \cdot 10^{+113}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -2.00000000000000011e32

    1. Initial program 38.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around -inf 82.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    3. Step-by-step derivation
      1. *-commutative82.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified82.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -2.00000000000000011e32 < re < 5.4999999999999998e-104 or 5.1999999999999996e-68 < re < 1.25000000000000009e69 or 3.20000000000000016e97 < re < 2e113

    1. Initial program 54.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around 0 79.1%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    3. Step-by-step derivation
      1. *-commutative79.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    4. Simplified79.1%

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

    if 5.4999999999999998e-104 < re < 5.1999999999999996e-68 or 2e113 < re

    1. Initial program 10.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around inf 44.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    3. Step-by-step derivation
      1. unpow244.7%

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    4. Simplified44.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. expm1-log1p-u44.4%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)\right)} \]
      2. expm1-udef21.3%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)} - 1\right)} \]
      3. sqrt-div21.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{im \cdot im}}{\sqrt{re}}}\right)} - 1\right) \]
      4. sqrt-prod29.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right)} - 1\right) \]
      5. add-sqr-sqrt29.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    6. Applied egg-rr29.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    7. Step-by-step derivation
      1. expm1-def84.7%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)} \]
      2. expm1-log1p85.6%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    8. Simplified85.6%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]

    if 1.25000000000000009e69 < re < 3.20000000000000016e97

    1. Initial program 25.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around inf 25.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    3. Step-by-step derivation
      1. unpow225.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    4. Simplified25.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. div-inv25.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(im \cdot im\right) \cdot \frac{1}{re}}} \]
      2. sqrt-prod25.4%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im \cdot im} \cdot \sqrt{\frac{1}{re}}\right)} \]
      3. sqrt-prod99.5%

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{im} \cdot \sqrt{\frac{1}{re}}\right) \]
      5. inv-pow99.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\color{blue}{{re}^{-1}}}\right) \]
      6. sqrt-pow199.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{re}^{\left(\frac{-1}{2}\right)}}\right) \]
      7. metadata-eval99.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {re}^{\color{blue}{-0.5}}\right) \]
    6. Applied egg-rr99.7%

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot {re}^{-0.5}\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification81.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.5 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 5.2 \cdot 10^{-68}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 1.25 \cdot 10^{+69}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 3.2 \cdot 10^{+97}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{elif}\;re \leq 2 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 4: 75.7% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \frac{im}{\sqrt{re}}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -6.1 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.35 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.05 \cdot 10^{-69}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 8 \cdot 10^{+68}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 2.4 \cdot 10^{+97}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{elif}\;re \leq 8 \cdot 10^{+114}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (/ im (sqrt re)))) (t_1 (* 0.5 (sqrt (* 2.0 im)))))
   (if (<= re -6.1e+34)
     (* 0.5 (sqrt (* re -4.0)))
     (if (<= re 2.35e-104)
       (* 0.5 (sqrt (* 2.0 (- im re))))
       (if (<= re 1.05e-69)
         t_0
         (if (<= re 8e+68)
           t_1
           (if (<= re 2.4e+97)
             (* 0.5 (* im (pow re -0.5)))
             (if (<= re 8e+114) t_1 t_0))))))))
double code(double re, double im) {
	double t_0 = 0.5 * (im / sqrt(re));
	double t_1 = 0.5 * sqrt((2.0 * im));
	double tmp;
	if (re <= -6.1e+34) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 2.35e-104) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if (re <= 1.05e-69) {
		tmp = t_0;
	} else if (re <= 8e+68) {
		tmp = t_1;
	} else if (re <= 2.4e+97) {
		tmp = 0.5 * (im * pow(re, -0.5));
	} else if (re <= 8e+114) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * (im / sqrt(re))
    t_1 = 0.5d0 * sqrt((2.0d0 * im))
    if (re <= (-6.1d+34)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 2.35d-104) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if (re <= 1.05d-69) then
        tmp = t_0
    else if (re <= 8d+68) then
        tmp = t_1
    else if (re <= 2.4d+97) then
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    else if (re <= 8d+114) then
        tmp = t_1
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * (im / Math.sqrt(re));
	double t_1 = 0.5 * Math.sqrt((2.0 * im));
	double tmp;
	if (re <= -6.1e+34) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 2.35e-104) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if (re <= 1.05e-69) {
		tmp = t_0;
	} else if (re <= 8e+68) {
		tmp = t_1;
	} else if (re <= 2.4e+97) {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	} else if (re <= 8e+114) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * (im / math.sqrt(re))
	t_1 = 0.5 * math.sqrt((2.0 * im))
	tmp = 0
	if re <= -6.1e+34:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 2.35e-104:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif re <= 1.05e-69:
		tmp = t_0
	elif re <= 8e+68:
		tmp = t_1
	elif re <= 2.4e+97:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	elif re <= 8e+114:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * Float64(im / sqrt(re)))
	t_1 = Float64(0.5 * sqrt(Float64(2.0 * im)))
	tmp = 0.0
	if (re <= -6.1e+34)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 2.35e-104)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif (re <= 1.05e-69)
		tmp = t_0;
	elseif (re <= 8e+68)
		tmp = t_1;
	elseif (re <= 2.4e+97)
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	elseif (re <= 8e+114)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * (im / sqrt(re));
	t_1 = 0.5 * sqrt((2.0 * im));
	tmp = 0.0;
	if (re <= -6.1e+34)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 2.35e-104)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif (re <= 1.05e-69)
		tmp = t_0;
	elseif (re <= 8e+68)
		tmp = t_1;
	elseif (re <= 2.4e+97)
		tmp = 0.5 * (im * (re ^ -0.5));
	elseif (re <= 8e+114)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -6.1e+34], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.35e-104], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.05e-69], t$95$0, If[LessEqual[re, 8e+68], t$95$1, If[LessEqual[re, 2.4e+97], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8e+114], t$95$1, t$95$0]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \frac{im}{\sqrt{re}}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{if}\;re \leq -6.1 \cdot 10^{+34}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 2.35 \cdot 10^{-104}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\

\mathbf{elif}\;re \leq 1.05 \cdot 10^{-69}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 8 \cdot 10^{+68}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq 2.4 \cdot 10^{+97}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\

\mathbf{elif}\;re \leq 8 \cdot 10^{+114}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 5 regimes
  2. if re < -6.09999999999999996e34

    1. Initial program 36.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around -inf 83.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    3. Step-by-step derivation
      1. *-commutative83.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified83.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -6.09999999999999996e34 < re < 2.35e-104

    1. Initial program 59.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around 0 81.5%

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

    if 2.35e-104 < re < 1.05e-69 or 8e114 < re

    1. Initial program 10.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around inf 44.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    3. Step-by-step derivation
      1. unpow244.7%

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    4. Simplified44.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. expm1-log1p-u44.4%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)\right)} \]
      2. expm1-udef21.3%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\frac{im \cdot im}{re}}\right)} - 1\right)} \]
      3. sqrt-div21.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{im \cdot im}}{\sqrt{re}}}\right)} - 1\right) \]
      4. sqrt-prod29.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right)} - 1\right) \]
      5. add-sqr-sqrt29.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    6. Applied egg-rr29.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    7. Step-by-step derivation
      1. expm1-def84.7%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)} \]
      2. expm1-log1p85.6%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    8. Simplified85.6%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]

    if 1.05e-69 < re < 7.99999999999999962e68 or 2.4e97 < re < 8e114

    1. Initial program 40.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around 0 76.5%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    3. Step-by-step derivation
      1. *-commutative76.5%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    4. Simplified76.5%

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

    if 7.99999999999999962e68 < re < 2.4e97

    1. Initial program 25.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around inf 25.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    3. Step-by-step derivation
      1. unpow225.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    4. Simplified25.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. div-inv25.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(im \cdot im\right) \cdot \frac{1}{re}}} \]
      2. sqrt-prod25.4%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im \cdot im} \cdot \sqrt{\frac{1}{re}}\right)} \]
      3. sqrt-prod99.5%

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{im} \cdot \sqrt{\frac{1}{re}}\right) \]
      5. inv-pow99.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\color{blue}{{re}^{-1}}}\right) \]
      6. sqrt-pow199.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{re}^{\left(\frac{-1}{2}\right)}}\right) \]
      7. metadata-eval99.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {re}^{\color{blue}{-0.5}}\right) \]
    6. Applied egg-rr99.7%

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot {re}^{-0.5}\right)} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification82.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -6.1 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.35 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.05 \cdot 10^{-69}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 8 \cdot 10^{+68}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 2.4 \cdot 10^{+97}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{elif}\;re \leq 8 \cdot 10^{+114}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 5: 64.8% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.5 \cdot 10^{+31}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -4.4999999999999996e31

    1. Initial program 38.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around -inf 82.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    3. Step-by-step derivation
      1. *-commutative82.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified82.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -4.4999999999999996e31 < re

    1. Initial program 43.0%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in re around 0 63.1%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    3. Step-by-step derivation
      1. *-commutative63.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    4. Simplified63.1%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.5 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 6: 52.9% accurate, 2.0× speedup?

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

\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Derivation
  1. Initial program 41.8%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
  2. Taylor expanded in re around 0 53.0%

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
  3. Step-by-step derivation
    1. *-commutative53.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  4. Simplified53.0%

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  5. Final simplification53.0%

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot im} \]

Reproduce

?
herbie shell --seed 2023257 
(FPCore (re im)
  :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
  :precision binary64
  :pre (> im 0.0)
  (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))