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

Percentage Accurate: 41.0% → 90.0%
Time: 9.9s
Alternatives: 8
Speedup: 2.1×

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 8 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.0% 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.0% 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:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \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)
   (* im (sqrt (/ 0.25 re)))
   (sqrt (* 0.5 (- (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 = im * sqrt((0.25 / re));
	} else {
		tmp = sqrt((0.5 * (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 = im * Math.sqrt((0.25 / re));
	} else {
		tmp = Math.sqrt((0.5 * (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 = im * math.sqrt((0.25 / re))
	else:
		tmp = math.sqrt((0.5 * (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(im * sqrt(Float64(0.25 / re)));
	else
		tmp = sqrt(Float64(0.5 * 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 = im * sqrt((0.25 / re));
	else
		tmp = sqrt((0.5 * (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[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $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:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \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 15.7%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Step-by-step derivation
      1. associate-*l*98.4%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. associate-*l*98.7%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*98.4%

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    7. Step-by-step derivation
      1. associate-*r*99.5%

        \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}} \]
      2. un-div-inv99.5%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Applied egg-rr99.5%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    9. Step-by-step derivation
      1. *-commutative99.5%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
      2. *-lft-identity99.5%

        \[\leadsto \frac{im \cdot 0.5}{\color{blue}{1 \cdot \sqrt{re}}} \]
      3. times-frac99.5%

        \[\leadsto \color{blue}{\frac{im}{1} \cdot \frac{0.5}{\sqrt{re}}} \]
      4. /-rgt-identity99.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 44.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative90.0%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*90.4%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval90.4%

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

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

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

Alternative 2: 74.5% accurate, 1.7× speedup?

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

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -2 + \left(2 \cdot im + re \cdot \frac{re}{im}\right)}\\
\mathbf{if}\;re \leq -1.05 \cdot 10^{-72}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 1.7 \cdot 10^{-9}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 3.05 \cdot 10^{+23}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\

\mathbf{elif}\;re \leq 3.6 \cdot 10^{+69}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -1.05e-72

    1. Initial program 47.6%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left(-im\right) \cdot \left(-im\right)}} - re\right)} \]
      2. sqr-neg47.6%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Taylor expanded in re around -inf 74.9%

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

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

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

    if -1.05e-72 < re < 1.6999999999999999e-9 or 3.0499999999999999e23 < re < 3.6000000000000003e69

    1. Initial program 53.1%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left(-im\right) \cdot \left(-im\right)}} - re\right)} \]
      2. sqr-neg53.1%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{im \cdot im}} - re\right)} \]
      3. hypot-def91.3%

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Taylor expanded in re around 0 82.5%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-2 \cdot re + \left(2 \cdot im + \frac{{re}^{2}}{im}\right)}} \]
    5. Step-by-step derivation
      1. unpow282.5%

        \[\leadsto 0.5 \cdot \sqrt{-2 \cdot re + \left(2 \cdot im + \frac{\color{blue}{re \cdot re}}{im}\right)} \]
      2. *-un-lft-identity82.5%

        \[\leadsto 0.5 \cdot \sqrt{-2 \cdot re + \left(2 \cdot im + \frac{re \cdot re}{\color{blue}{1 \cdot im}}\right)} \]
      3. times-frac83.0%

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

        \[\leadsto 0.5 \cdot \sqrt{-2 \cdot re + \left(2 \cdot im + \color{blue}{re} \cdot \frac{re}{im}\right)} \]
    6. Applied egg-rr83.0%

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

    if 1.6999999999999999e-9 < re < 3.0499999999999999e23

    1. Initial program 6.4%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Step-by-step derivation
      1. associate-*l*85.4%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. associate-*l*86.0%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*85.4%

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      5. *-un-lft-identity86.4%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right) \]
      6. sqrt-div86.0%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right) \]
      7. metadata-eval86.0%

        \[\leadsto 0.5 \cdot \left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right) \]
    6. Applied egg-rr86.0%

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    7. Step-by-step derivation
      1. associate-*r*86.0%

        \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}} \]
      2. un-div-inv86.0%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Applied egg-rr86.0%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    9. Step-by-step derivation
      1. *-commutative86.0%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
      2. *-lft-identity86.0%

        \[\leadsto \frac{im \cdot 0.5}{\color{blue}{1 \cdot \sqrt{re}}} \]
      3. times-frac86.0%

        \[\leadsto \color{blue}{\frac{im}{1} \cdot \frac{0.5}{\sqrt{re}}} \]
      4. /-rgt-identity86.0%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto im \cdot \color{blue}{\sqrt{\frac{0.25}{re}}} \]
    14. Simplified86.4%

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

    if 3.6000000000000003e69 < 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 im around 0 77.0%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Step-by-step derivation
      1. associate-*l*77.0%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. associate-*l*77.2%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*77.0%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\sqrt{2 \cdot 0.5}} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. metadata-eval77.8%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      5. *-un-lft-identity77.8%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right) \]
      6. sqrt-div77.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right) \]
      7. metadata-eval77.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right) \]
    6. Applied egg-rr77.9%

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    7. Step-by-step derivation
      1. associate-*r*77.9%

        \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}} \]
      2. un-div-inv77.9%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Applied egg-rr77.9%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    9. Step-by-step derivation
      1. *-commutative77.9%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
      2. *-lft-identity77.9%

        \[\leadsto \frac{im \cdot 0.5}{\color{blue}{1 \cdot \sqrt{re}}} \]
      3. times-frac77.9%

        \[\leadsto \color{blue}{\frac{im}{1} \cdot \frac{0.5}{\sqrt{re}}} \]
      4. /-rgt-identity77.9%

        \[\leadsto \color{blue}{im} \cdot \frac{0.5}{\sqrt{re}} \]
    10. Simplified77.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.05 \cdot 10^{-72}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.7 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -2 + \left(2 \cdot im + re \cdot \frac{re}{im}\right)}\\ \mathbf{elif}\;re \leq 3.05 \cdot 10^{+23}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \mathbf{elif}\;re \leq 3.6 \cdot 10^{+69}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -2 + \left(2 \cdot im + re \cdot \frac{re}{im}\right)}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 74.6% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.7 \cdot 10^{-57}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 1.7 \cdot 10^{-9} \lor \neg \left(re \leq 1.42 \cdot 10^{+23}\right) \land re \leq 3.6 \cdot 10^{+66}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -1.70000000000000008e-57

    1. Initial program 46.4%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left(-im\right) \cdot \left(-im\right)}} - re\right)} \]
      2. sqr-neg46.4%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Taylor expanded in re around -inf 75.3%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified75.3%

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

    if -1.70000000000000008e-57 < re < 1.6999999999999999e-9 or 1.42000000000000004e23 < re < 3.6e66

    1. Initial program 53.9%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
      2. add-sqr-sqrt90.7%

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

        \[\leadsto \color{blue}{\sqrt{\left(0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right)}} \]
      4. *-commutative91.5%

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

        \[\leadsto \sqrt{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5\right) \cdot \color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5\right)}} \]
      6. swap-sqr91.5%

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      7. add-sqr-sqrt91.5%

        \[\leadsto \sqrt{\color{blue}{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)} \cdot \left(0.5 \cdot 0.5\right)} \]
      8. metadata-eval91.5%

        \[\leadsto \sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \color{blue}{0.25}} \]
    3. Applied egg-rr91.5%

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative91.5%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*91.5%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval91.5%

        \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
    5. Simplified91.5%

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 81.7%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-181.7%

        \[\leadsto \sqrt{0.5 \cdot \left(im + \color{blue}{\left(-re\right)}\right)} \]
      2. unsub-neg81.7%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im - re\right)}} \]
    8. Simplified81.7%

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

    if 1.6999999999999999e-9 < re < 1.42000000000000004e23 or 3.6e66 < re

    1. Initial program 9.8%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Step-by-step derivation
      1. associate-*l*78.1%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. associate-*l*78.2%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*78.1%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\sqrt{2 \cdot 0.5}} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. metadata-eval78.9%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      5. *-un-lft-identity78.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right) \]
      6. sqrt-div78.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right) \]
      7. metadata-eval78.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right) \]
    6. Applied egg-rr78.9%

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    7. Step-by-step derivation
      1. associate-*r*78.9%

        \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}} \]
      2. un-div-inv78.9%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Applied egg-rr78.9%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    9. Step-by-step derivation
      1. *-commutative78.9%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
      2. *-lft-identity78.9%

        \[\leadsto \frac{im \cdot 0.5}{\color{blue}{1 \cdot \sqrt{re}}} \]
      3. times-frac78.9%

        \[\leadsto \color{blue}{\frac{im}{1} \cdot \frac{0.5}{\sqrt{re}}} \]
      4. /-rgt-identity78.9%

        \[\leadsto \color{blue}{im} \cdot \frac{0.5}{\sqrt{re}} \]
    10. Simplified78.9%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto im \cdot \color{blue}{\sqrt{\frac{0.25}{re}}} \]
    14. Simplified78.9%

      \[\leadsto im \cdot \color{blue}{\sqrt{\frac{0.25}{re}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification78.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.7 \cdot 10^{-57}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.7 \cdot 10^{-9} \lor \neg \left(re \leq 1.42 \cdot 10^{+23}\right) \land re \leq 3.6 \cdot 10^{+66}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \end{array} \]

Alternative 4: 74.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -7.5 \cdot 10^{-64}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.6 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.36 \cdot 10^{+25}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \mathbf{elif}\;re \leq 2.95 \cdot 10^{+66}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (sqrt (* 0.5 (- im re)))))
   (if (<= re -7.5e-64)
     (* 0.5 (sqrt (* re -4.0)))
     (if (<= re 5.6e-9)
       t_0
       (if (<= re 1.36e+25)
         (* im (sqrt (/ 0.25 re)))
         (if (<= re 2.95e+66) t_0 (* im (/ 0.5 (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -7.5e-64) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 5.6e-9) {
		tmp = t_0;
	} else if (re <= 1.36e+25) {
		tmp = im * sqrt((0.25 / re));
	} else if (re <= 2.95e+66) {
		tmp = t_0;
	} else {
		tmp = im * (0.5 / sqrt(re));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = sqrt((0.5d0 * (im - re)))
    if (re <= (-7.5d-64)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 5.6d-9) then
        tmp = t_0
    else if (re <= 1.36d+25) then
        tmp = im * sqrt((0.25d0 / re))
    else if (re <= 2.95d+66) then
        tmp = t_0
    else
        tmp = im * (0.5d0 / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = Math.sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -7.5e-64) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 5.6e-9) {
		tmp = t_0;
	} else if (re <= 1.36e+25) {
		tmp = im * Math.sqrt((0.25 / re));
	} else if (re <= 2.95e+66) {
		tmp = t_0;
	} else {
		tmp = im * (0.5 / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = math.sqrt((0.5 * (im - re)))
	tmp = 0
	if re <= -7.5e-64:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 5.6e-9:
		tmp = t_0
	elif re <= 1.36e+25:
		tmp = im * math.sqrt((0.25 / re))
	elif re <= 2.95e+66:
		tmp = t_0
	else:
		tmp = im * (0.5 / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = sqrt(Float64(0.5 * Float64(im - re)))
	tmp = 0.0
	if (re <= -7.5e-64)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 5.6e-9)
		tmp = t_0;
	elseif (re <= 1.36e+25)
		tmp = Float64(im * sqrt(Float64(0.25 / re)));
	elseif (re <= 2.95e+66)
		tmp = t_0;
	else
		tmp = Float64(im * Float64(0.5 / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = sqrt((0.5 * (im - re)));
	tmp = 0.0;
	if (re <= -7.5e-64)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 5.6e-9)
		tmp = t_0;
	elseif (re <= 1.36e+25)
		tmp = im * sqrt((0.25 / re));
	elseif (re <= 2.95e+66)
		tmp = t_0;
	else
		tmp = im * (0.5 / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[re, -7.5e-64], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.6e-9], t$95$0, If[LessEqual[re, 1.36e+25], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.95e+66], t$95$0, N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -7.5 \cdot 10^{-64}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 5.6 \cdot 10^{-9}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 1.36 \cdot 10^{+25}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\

\mathbf{elif}\;re \leq 2.95 \cdot 10^{+66}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -7.49999999999999949e-64

    1. Initial program 46.4%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left(-im\right) \cdot \left(-im\right)}} - re\right)} \]
      2. sqr-neg46.4%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Taylor expanded in re around -inf 75.3%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified75.3%

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

    if -7.49999999999999949e-64 < re < 5.59999999999999969e-9 or 1.36e25 < re < 2.94999999999999994e66

    1. Initial program 53.9%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
      2. add-sqr-sqrt90.7%

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

        \[\leadsto \color{blue}{\sqrt{\left(0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right)}} \]
      4. *-commutative91.5%

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

        \[\leadsto \sqrt{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5\right) \cdot \color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5\right)}} \]
      6. swap-sqr91.5%

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      7. add-sqr-sqrt91.5%

        \[\leadsto \sqrt{\color{blue}{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)} \cdot \left(0.5 \cdot 0.5\right)} \]
      8. metadata-eval91.5%

        \[\leadsto \sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \color{blue}{0.25}} \]
    3. Applied egg-rr91.5%

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative91.5%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*91.5%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval91.5%

        \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
    5. Simplified91.5%

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 81.7%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-181.7%

        \[\leadsto \sqrt{0.5 \cdot \left(im + \color{blue}{\left(-re\right)}\right)} \]
      2. unsub-neg81.7%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im - re\right)}} \]
    8. Simplified81.7%

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

    if 5.59999999999999969e-9 < re < 1.36e25

    1. Initial program 6.4%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Step-by-step derivation
      1. associate-*l*85.4%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. associate-*l*86.0%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*85.4%

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      5. *-un-lft-identity86.4%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right) \]
      6. sqrt-div86.0%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right) \]
      7. metadata-eval86.0%

        \[\leadsto 0.5 \cdot \left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right) \]
    6. Applied egg-rr86.0%

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    7. Step-by-step derivation
      1. associate-*r*86.0%

        \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}} \]
      2. un-div-inv86.0%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Applied egg-rr86.0%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    9. Step-by-step derivation
      1. *-commutative86.0%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
      2. *-lft-identity86.0%

        \[\leadsto \frac{im \cdot 0.5}{\color{blue}{1 \cdot \sqrt{re}}} \]
      3. times-frac86.0%

        \[\leadsto \color{blue}{\frac{im}{1} \cdot \frac{0.5}{\sqrt{re}}} \]
      4. /-rgt-identity86.0%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto im \cdot \color{blue}{\sqrt{\frac{0.25}{re}}} \]
    14. Simplified86.4%

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

    if 2.94999999999999994e66 < 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 im around 0 77.0%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    3. Step-by-step derivation
      1. associate-*l*77.0%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. associate-*l*77.2%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*77.0%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\sqrt{2 \cdot 0.5}} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      3. metadata-eval77.8%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
      5. *-un-lft-identity77.8%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right) \]
      6. sqrt-div77.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right) \]
      7. metadata-eval77.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right) \]
    6. Applied egg-rr77.9%

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    7. Step-by-step derivation
      1. associate-*r*77.9%

        \[\leadsto \color{blue}{\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}} \]
      2. un-div-inv77.9%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Applied egg-rr77.9%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    9. Step-by-step derivation
      1. *-commutative77.9%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
      2. *-lft-identity77.9%

        \[\leadsto \frac{im \cdot 0.5}{\color{blue}{1 \cdot \sqrt{re}}} \]
      3. times-frac77.9%

        \[\leadsto \color{blue}{\frac{im}{1} \cdot \frac{0.5}{\sqrt{re}}} \]
      4. /-rgt-identity77.9%

        \[\leadsto \color{blue}{im} \cdot \frac{0.5}{\sqrt{re}} \]
    10. Simplified77.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7.5 \cdot 10^{-64}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.6 \cdot 10^{-9}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.36 \cdot 10^{+25}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \mathbf{elif}\;re \leq 2.95 \cdot 10^{+66}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \]

Alternative 5: 63.6% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.8 \cdot 10^{-72}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -1.8e-72

    1. Initial program 47.6%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left(-im\right) \cdot \left(-im\right)}} - re\right)} \]
      2. sqr-neg47.6%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Taylor expanded in re around -inf 74.9%

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

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

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

    if -1.8e-72 < re

    1. Initial program 37.7%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
      2. add-sqr-sqrt72.5%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative73.0%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*73.6%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval73.6%

        \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
    5. Simplified73.6%

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 61.5%

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

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

Alternative 6: 55.5% accurate, 2.0× speedup?

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

\\
\sqrt{0.5 \cdot \left(im - re\right)}
\end{array}
Derivation
  1. Initial program 41.4%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
    2. add-sqr-sqrt82.4%

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
  4. Step-by-step derivation
    1. *-commutative83.0%

      \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
    2. associate-*r*83.4%

      \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    3. metadata-eval83.4%

      \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
  5. Simplified83.4%

    \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  6. Taylor expanded in re around 0 53.6%

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
  7. Step-by-step derivation
    1. neg-mul-153.6%

      \[\leadsto \sqrt{0.5 \cdot \left(im + \color{blue}{\left(-re\right)}\right)} \]
    2. unsub-neg53.6%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im - re\right)}} \]
  8. Simplified53.6%

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im - re\right)}} \]
  9. Final simplification53.6%

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

Alternative 7: 53.3% accurate, 2.1× speedup?

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
    2. add-sqr-sqrt82.4%

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
  4. Step-by-step derivation
    1. *-commutative83.0%

      \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
    2. associate-*r*83.4%

      \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    3. metadata-eval83.4%

      \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
  5. Simplified83.4%

    \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  6. Taylor expanded in re around 0 50.0%

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{im}} \]
  7. Final simplification50.0%

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

Alternative 8: 6.6% accurate, 71.0× speedup?

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

\\
im \cdot 0.5
\end{array}
Derivation
  1. Initial program 41.4%

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
  3. Step-by-step derivation
    1. associate-*l*22.7%

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

      \[\leadsto 0.5 \cdot \left(im \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right) \]
    3. associate-*l*22.8%

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
  5. Step-by-step derivation
    1. add-sqr-sqrt22.7%

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

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt{im \cdot \left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)}^{2}} \]
    3. associate-*r*22.7%

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

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

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

      \[\leadsto 0.5 \cdot {\left(\sqrt{im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)}\right)}^{2} \]
    7. *-un-lft-identity22.9%

      \[\leadsto 0.5 \cdot {\left(\sqrt{im \cdot \color{blue}{\sqrt{\frac{1}{re}}}}\right)}^{2} \]
    8. inv-pow22.9%

      \[\leadsto 0.5 \cdot {\left(\sqrt{im \cdot \sqrt{\color{blue}{{re}^{-1}}}}\right)}^{2} \]
    9. sqrt-pow122.9%

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

      \[\leadsto 0.5 \cdot {\left(\sqrt{im \cdot {re}^{\color{blue}{-0.5}}}\right)}^{2} \]
  6. Applied egg-rr22.9%

    \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt{im \cdot {re}^{-0.5}}\right)}^{2}} \]
  7. Step-by-step derivation
    1. unpow222.9%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im \cdot {re}^{-0.5}} \cdot \sqrt{im \cdot {re}^{-0.5}}\right)} \]
    2. add-sqr-sqrt23.0%

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot {re}^{-0.5}\right)} \]
    3. metadata-eval23.0%

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

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\sqrt{re}\right)}^{-1}}\right) \]
    5. inv-pow22.9%

      \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\frac{1}{\sqrt{re}}}\right) \]
    6. un-div-inv22.9%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. *-un-lft-identity22.9%

      \[\leadsto 0.5 \cdot \frac{\color{blue}{1 \cdot im}}{\sqrt{re}} \]
    8. add-sqr-sqrt22.9%

      \[\leadsto 0.5 \cdot \frac{1 \cdot im}{\color{blue}{\sqrt{\sqrt{re}} \cdot \sqrt{\sqrt{re}}}} \]
    9. times-frac22.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{1}{\sqrt{\sqrt{re}}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right)} \]
    10. metadata-eval22.8%

      \[\leadsto 0.5 \cdot \left(\frac{\color{blue}{\sqrt{1}}}{\sqrt{\sqrt{re}}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right) \]
    11. sqrt-div22.9%

      \[\leadsto 0.5 \cdot \left(\color{blue}{\sqrt{\frac{1}{\sqrt{re}}}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right) \]
    12. inv-pow22.9%

      \[\leadsto 0.5 \cdot \left(\sqrt{\color{blue}{{\left(\sqrt{re}\right)}^{-1}}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right) \]
    13. sqrt-pow222.9%

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

      \[\leadsto 0.5 \cdot \left(\sqrt{{re}^{\color{blue}{-0.5}}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right) \]
    15. sqrt-pow122.9%

      \[\leadsto 0.5 \cdot \left(\color{blue}{{re}^{\left(\frac{-0.5}{2}\right)}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right) \]
    16. metadata-eval22.9%

      \[\leadsto 0.5 \cdot \left({re}^{\color{blue}{-0.25}} \cdot \frac{im}{\sqrt{\sqrt{re}}}\right) \]
    17. add-exp-log22.0%

      \[\leadsto 0.5 \cdot \left({re}^{-0.25} \cdot \frac{im}{\sqrt{\sqrt{\color{blue}{e^{\log re}}}}}\right) \]
    18. add-sqr-sqrt17.2%

      \[\leadsto 0.5 \cdot \left({re}^{-0.25} \cdot \frac{im}{\sqrt{\sqrt{e^{\color{blue}{\sqrt{\log re} \cdot \sqrt{\log re}}}}}}\right) \]
    19. sqrt-unprod18.8%

      \[\leadsto 0.5 \cdot \left({re}^{-0.25} \cdot \frac{im}{\sqrt{\sqrt{e^{\color{blue}{\sqrt{\log re \cdot \log re}}}}}}\right) \]
    20. sqr-neg18.8%

      \[\leadsto 0.5 \cdot \left({re}^{-0.25} \cdot \frac{im}{\sqrt{\sqrt{e^{\sqrt{\color{blue}{\left(-\log re\right) \cdot \left(-\log re\right)}}}}}}\right) \]
    21. sqrt-unprod1.4%

      \[\leadsto 0.5 \cdot \left({re}^{-0.25} \cdot \frac{im}{\sqrt{\sqrt{e^{\color{blue}{\sqrt{-\log re} \cdot \sqrt{-\log re}}}}}}\right) \]
    22. add-sqr-sqrt3.1%

      \[\leadsto 0.5 \cdot \left({re}^{-0.25} \cdot \frac{im}{\sqrt{\sqrt{e^{\color{blue}{-\log re}}}}}\right) \]
  8. Applied egg-rr3.1%

    \[\leadsto 0.5 \cdot \color{blue}{\left({re}^{-0.25} \cdot \frac{im}{{re}^{-0.25}}\right)} \]
  9. Step-by-step derivation
    1. *-commutative3.1%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{{re}^{-0.25}} \cdot {re}^{-0.25}\right)} \]
    2. associate-*l/3.5%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im \cdot {re}^{-0.25}}{{re}^{-0.25}}} \]
    3. associate-/l*3.1%

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\frac{{re}^{-0.25}}{{re}^{-0.25}}}} \]
    4. *-inverses6.3%

      \[\leadsto 0.5 \cdot \frac{im}{\color{blue}{1}} \]
    5. /-rgt-identity6.3%

      \[\leadsto 0.5 \cdot \color{blue}{im} \]
  10. Simplified6.3%

    \[\leadsto 0.5 \cdot \color{blue}{im} \]
  11. Final simplification6.3%

    \[\leadsto im \cdot 0.5 \]

Reproduce

?
herbie shell --seed 2023320 
(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)))))