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

Percentage Accurate: 41.4% → 87.9%
Time: 7.0s
Alternatives: 5
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 5 alternatives:

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

Initial Program: 41.4% 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: 87.9% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 0.35:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

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


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

    1. Initial program 59.0%

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

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

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

    if 0.34999999999999998 < re

    1. Initial program 10.9%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im \cdot im}{re}\right)}} \]
    5. Taylor expanded in im around 0 85.2%

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{e^{\log im}} \cdot \sqrt{\frac{1}{re}}\right) \]
      3. unpow1/280.2%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      4. rem-exp-log79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\left(\frac{1}{\color{blue}{e^{\log re}}}\right)}^{0.5}\right) \]
      5. exp-neg79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\color{blue}{\left(e^{-\log re}\right)}}^{0.5}\right) \]
      6. exp-prod79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      7. distribute-lft-neg-out79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      8. distribute-rgt-neg-in79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      9. metadata-eval79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      10. *-commutative79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-0.5 \cdot \log re}}\right) \]
      11. log-pow79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log \left({re}^{-0.5}\right)}}\right) \]
      12. exp-to-pow79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(e^{\log re \cdot -0.5}\right)}}\right) \]
      13. metadata-eval79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\log re \cdot \color{blue}{\left(-0.5\right)}}\right)}\right) \]
      14. distribute-rgt-neg-in79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\color{blue}{-\log re \cdot 0.5}}\right)}\right) \]
      15. exp-neg79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(\frac{1}{e^{\log re \cdot 0.5}}\right)}}\right) \]
      16. log-rec79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log \left(e^{\log re \cdot 0.5}\right)}}\right) \]
      17. exp-to-pow79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left({re}^{0.5}\right)}}\right) \]
      18. unpow1/279.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left(\sqrt{re}\right)}}\right) \]
      19. exp-sum79.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log im + \left(-\log \left(\sqrt{re}\right)\right)}} \]
      20. sub-neg79.2%

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

        \[\leadsto 0.5 \cdot e^{\color{blue}{\log \left(\frac{im}{\sqrt{re}}\right)}} \]
    7. Simplified85.3%

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

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

Alternative 2: 76.3% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -9.4 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 12:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -9.4e-30)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 12.0)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -9.4e-30) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 12.0) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} 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 <= (-9.4d-30)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 12.0d0) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    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 <= -9.4e-30) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 12.0) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -9.4e-30:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 12.0:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -9.4e-30)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 12.0)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -9.4e-30)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 12.0)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -9.4e-30], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 12.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -9.4 \cdot 10^{-30}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq 12:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\

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


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

    1. Initial program 48.5%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-2 \cdot re\right)}} \]
    3. Step-by-step derivation
      1. *-commutative77.6%

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

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

    if -9.39999999999999938e-30 < re < 12

    1. Initial program 64.4%

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

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

    if 12 < re

    1. Initial program 10.9%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im \cdot im}{re}\right)}} \]
    5. Taylor expanded in im around 0 85.2%

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{e^{\log im}} \cdot \sqrt{\frac{1}{re}}\right) \]
      3. unpow1/280.2%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      4. rem-exp-log79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\left(\frac{1}{\color{blue}{e^{\log re}}}\right)}^{0.5}\right) \]
      5. exp-neg79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\color{blue}{\left(e^{-\log re}\right)}}^{0.5}\right) \]
      6. exp-prod79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      7. distribute-lft-neg-out79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      8. distribute-rgt-neg-in79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      9. metadata-eval79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      10. *-commutative79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-0.5 \cdot \log re}}\right) \]
      11. log-pow79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log \left({re}^{-0.5}\right)}}\right) \]
      12. exp-to-pow79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(e^{\log re \cdot -0.5}\right)}}\right) \]
      13. metadata-eval79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\log re \cdot \color{blue}{\left(-0.5\right)}}\right)}\right) \]
      14. distribute-rgt-neg-in79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\color{blue}{-\log re \cdot 0.5}}\right)}\right) \]
      15. exp-neg79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(\frac{1}{e^{\log re \cdot 0.5}}\right)}}\right) \]
      16. log-rec79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log \left(e^{\log re \cdot 0.5}\right)}}\right) \]
      17. exp-to-pow79.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left({re}^{0.5}\right)}}\right) \]
      18. unpow1/279.5%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left(\sqrt{re}\right)}}\right) \]
      19. exp-sum79.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log im + \left(-\log \left(\sqrt{re}\right)\right)}} \]
      20. sub-neg79.2%

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

        \[\leadsto 0.5 \cdot e^{\color{blue}{\log \left(\frac{im}{\sqrt{re}}\right)}} \]
    7. Simplified85.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -9.4 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 12:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 75.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -7.5 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 27000000000000:\\ \;\;\;\;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 -7.5e-30)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 27000000000000.0)
     (* 0.5 (sqrt (* 2.0 im)))
     (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -7.5e-30) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 27000000000000.0) {
		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 <= (-7.5d-30)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 27000000000000.0d0) 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 <= -7.5e-30) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 27000000000000.0) {
		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 <= -7.5e-30:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 27000000000000.0:
		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 <= -7.5e-30)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 27000000000000.0)
		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 <= -7.5e-30)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 27000000000000.0)
		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, -7.5e-30], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 27000000000000.0], 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 -7.5 \cdot 10^{-30}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq 27000000000000:\\
\;\;\;\;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 < -7.5000000000000006e-30

    1. Initial program 48.5%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-2 \cdot re\right)}} \]
    3. Step-by-step derivation
      1. *-commutative77.6%

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

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

    if -7.5000000000000006e-30 < re < 2.7e13

    1. Initial program 63.5%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - \color{blue}{\left(-\sqrt{re}\right)} \cdot \sqrt{re}\right)} \]
      8. distribute-lft-neg-in42.9%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right) \cdot 1} - \left(-re\right)\right)} \]
      12. neg-mul-182.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(-re, -1, re \cdot -1\right)\right)}} \]
      14. add-sqr-sqrt42.9%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(-\color{blue}{\sqrt{re} \cdot \sqrt{re}}, -1, re \cdot -1\right)\right)} \]
      15. distribute-rgt-neg-in42.9%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\color{blue}{\sqrt{re} \cdot \left(-\sqrt{re}\right)}, -1, re \cdot -1\right)\right)} \]
      16. add-sqr-sqrt0.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \color{blue}{\left(\sqrt{-\sqrt{re}} \cdot \sqrt{-\sqrt{re}}\right)}, -1, re \cdot -1\right)\right)} \]
      17. sqrt-unprod44.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \color{blue}{\sqrt{\left(-\sqrt{re}\right) \cdot \left(-\sqrt{re}\right)}}, -1, re \cdot -1\right)\right)} \]
      18. sqr-neg44.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \sqrt{\color{blue}{\sqrt{re} \cdot \sqrt{re}}}, -1, re \cdot -1\right)\right)} \]
      19. add-sqr-sqrt44.0%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\color{blue}{re}, -1, re \cdot -1\right)\right)} \]
    5. Applied egg-rr91.7%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)}} \]
    6. Step-by-step derivation
      1. fma-udef91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + \left(-re \cdot -1\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      2. distribute-rgt-neg-in91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + \color{blue}{re \cdot \left(--1\right)}\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      3. metadata-eval91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + re \cdot \color{blue}{1}\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      4. distribute-rgt-in91.7%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      6. *-lft-identity91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      7. *-lft-identity91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      8. +-commutative91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(\mathsf{hypot}\left(re, im\right) + re\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      9. distribute-rgt-in91.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + re \cdot 1\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      10. *-rgt-identity91.7%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) + re \cdot 1\right) + \mathsf{fma}\left(re, -1, \color{blue}{-1 \cdot re}\right)\right)} \]
      12. mul-1-neg91.7%

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

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

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

    if 2.7e13 < re

    1. Initial program 11.1%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im \cdot im}{re}\right)}} \]
    5. Taylor expanded in im around 0 86.3%

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{e^{\log im}} \cdot \sqrt{\frac{1}{re}}\right) \]
      3. unpow1/281.1%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      4. rem-exp-log80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\left(\frac{1}{\color{blue}{e^{\log re}}}\right)}^{0.5}\right) \]
      5. exp-neg80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\color{blue}{\left(e^{-\log re}\right)}}^{0.5}\right) \]
      6. exp-prod80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      7. distribute-lft-neg-out80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      8. distribute-rgt-neg-in80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      9. metadata-eval80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      10. *-commutative80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-0.5 \cdot \log re}}\right) \]
      11. log-pow80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log \left({re}^{-0.5}\right)}}\right) \]
      12. exp-to-pow80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(e^{\log re \cdot -0.5}\right)}}\right) \]
      13. metadata-eval80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\log re \cdot \color{blue}{\left(-0.5\right)}}\right)}\right) \]
      14. distribute-rgt-neg-in80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\color{blue}{-\log re \cdot 0.5}}\right)}\right) \]
      15. exp-neg80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(\frac{1}{e^{\log re \cdot 0.5}}\right)}}\right) \]
      16. log-rec80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log \left(e^{\log re \cdot 0.5}\right)}}\right) \]
      17. exp-to-pow80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left({re}^{0.5}\right)}}\right) \]
      18. unpow1/280.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left(\sqrt{re}\right)}}\right) \]
      19. exp-sum80.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log im + \left(-\log \left(\sqrt{re}\right)\right)}} \]
      20. sub-neg80.2%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7.5 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 27000000000000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 4: 63.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 30000000000000:\\ \;\;\;\;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 30000000000000.0)
   (* 0.5 (sqrt (* 2.0 im)))
   (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if (re <= 30000000000000.0) {
		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 <= 30000000000000.0d0) 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 <= 30000000000000.0) {
		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 <= 30000000000000.0:
		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 <= 30000000000000.0)
		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 <= 30000000000000.0)
		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, 30000000000000.0], 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 30000000000000:\\
\;\;\;\;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 2 regimes
  2. if re < 3e13

    1. Initial program 58.4%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{{\left(\sqrt{\mathsf{hypot}\left(re, im\right)}\right)}^{2}} - re\right)} \]
    4. Step-by-step derivation
      1. unpow294.3%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - \left(-\color{blue}{re}\right)\right)} \]
      10. *-un-lft-identity62.9%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right) \cdot 1} - \left(-re\right)\right)} \]
      12. neg-mul-162.9%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) \cdot 1 - \color{blue}{-1 \cdot re}\right)} \]
      13. prod-diff62.9%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(-\color{blue}{\sqrt{re} \cdot \sqrt{re}}, -1, re \cdot -1\right)\right)} \]
      15. distribute-rgt-neg-in28.5%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\color{blue}{\sqrt{re} \cdot \left(-\sqrt{re}\right)}, -1, re \cdot -1\right)\right)} \]
      16. add-sqr-sqrt0.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \color{blue}{\left(\sqrt{-\sqrt{re}} \cdot \sqrt{-\sqrt{re}}\right)}, -1, re \cdot -1\right)\right)} \]
      17. sqrt-unprod29.1%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \color{blue}{\sqrt{\left(-\sqrt{re}\right) \cdot \left(-\sqrt{re}\right)}}, -1, re \cdot -1\right)\right)} \]
      18. sqr-neg29.1%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \sqrt{\color{blue}{\sqrt{re} \cdot \sqrt{re}}}, -1, re \cdot -1\right)\right)} \]
      19. add-sqr-sqrt29.1%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\color{blue}{re}, -1, re \cdot -1\right)\right)} \]
    5. Applied egg-rr94.5%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)}} \]
    6. Step-by-step derivation
      1. fma-udef94.5%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + \left(-re \cdot -1\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      2. distribute-rgt-neg-in94.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + re \cdot \color{blue}{1}\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      4. distribute-rgt-in94.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      6. *-lft-identity94.5%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      7. *-lft-identity94.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(\mathsf{hypot}\left(re, im\right) + re\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      9. distribute-rgt-in94.5%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + re \cdot 1\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
      10. *-rgt-identity94.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) + re \cdot 1\right) + \mathsf{fma}\left(re, -1, \color{blue}{-1 \cdot re}\right)\right)} \]
      12. mul-1-neg94.5%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) + re \cdot 1\right) + \mathsf{fma}\left(re, -1, \color{blue}{-re}\right)\right)} \]
    7. Simplified94.5%

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

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

    if 3e13 < re

    1. Initial program 11.1%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im \cdot im}{re}\right)}} \]
    5. Taylor expanded in im around 0 86.3%

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{e^{\log im}} \cdot \sqrt{\frac{1}{re}}\right) \]
      3. unpow1/281.1%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      4. rem-exp-log80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\left(\frac{1}{\color{blue}{e^{\log re}}}\right)}^{0.5}\right) \]
      5. exp-neg80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot {\color{blue}{\left(e^{-\log re}\right)}}^{0.5}\right) \]
      6. exp-prod80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      7. distribute-lft-neg-out80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      8. distribute-rgt-neg-in80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      9. metadata-eval80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      10. *-commutative80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-0.5 \cdot \log re}}\right) \]
      11. log-pow80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{\log \left({re}^{-0.5}\right)}}\right) \]
      12. exp-to-pow80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(e^{\log re \cdot -0.5}\right)}}\right) \]
      13. metadata-eval80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\log re \cdot \color{blue}{\left(-0.5\right)}}\right)}\right) \]
      14. distribute-rgt-neg-in80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \left(e^{\color{blue}{-\log re \cdot 0.5}}\right)}\right) \]
      15. exp-neg80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\log \color{blue}{\left(\frac{1}{e^{\log re \cdot 0.5}}\right)}}\right) \]
      16. log-rec80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{\color{blue}{-\log \left(e^{\log re \cdot 0.5}\right)}}\right) \]
      17. exp-to-pow80.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left({re}^{0.5}\right)}}\right) \]
      18. unpow1/280.4%

        \[\leadsto 0.5 \cdot \left(e^{\log im} \cdot e^{-\log \color{blue}{\left(\sqrt{re}\right)}}\right) \]
      19. exp-sum80.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log im + \left(-\log \left(\sqrt{re}\right)\right)}} \]
      20. sub-neg80.2%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 30000000000000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 5: 52.1% 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 46.8%

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

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

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

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

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{{\left(\sqrt{\mathsf{hypot}\left(re, im\right)}\right)}^{2}} - re\right)} \]
  4. Step-by-step derivation
    1. unpow276.3%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right) \cdot 1} - \left(-re\right)\right)} \]
    12. neg-mul-151.4%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) \cdot 1 - \color{blue}{-1 \cdot re}\right)} \]
    13. prod-diff51.4%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(-\color{blue}{\sqrt{re} \cdot \sqrt{re}}, -1, re \cdot -1\right)\right)} \]
    15. distribute-rgt-neg-in25.5%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\color{blue}{\sqrt{re} \cdot \left(-\sqrt{re}\right)}, -1, re \cdot -1\right)\right)} \]
    16. add-sqr-sqrt0.0%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \color{blue}{\left(\sqrt{-\sqrt{re}} \cdot \sqrt{-\sqrt{re}}\right)}, -1, re \cdot -1\right)\right)} \]
    17. sqrt-unprod28.7%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \color{blue}{\sqrt{\left(-\sqrt{re}\right) \cdot \left(-\sqrt{re}\right)}}, -1, re \cdot -1\right)\right)} \]
    18. sqr-neg28.7%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\sqrt{re} \cdot \sqrt{\color{blue}{re}}, -1, re \cdot -1\right)\right)} \]
    20. add-sqr-sqrt78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(\color{blue}{re}, -1, re \cdot -1\right)\right)} \]
  5. Applied egg-rr78.9%

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{hypot}\left(re, im\right), 1, -re \cdot -1\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)}} \]
  6. Step-by-step derivation
    1. fma-udef78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + \left(-re \cdot -1\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    2. distribute-rgt-neg-in78.9%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + re \cdot \color{blue}{1}\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    4. distribute-rgt-in78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1 \cdot \left(\mathsf{hypot}\left(re, im\right) + re\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    5. +-commutative78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    6. *-lft-identity78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    7. *-lft-identity78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    8. +-commutative78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(\mathsf{hypot}\left(re, im\right) + re\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    9. distribute-rgt-in78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) \cdot 1 + re \cdot 1\right)} + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    10. *-rgt-identity78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\color{blue}{\mathsf{hypot}\left(re, im\right)} + re \cdot 1\right) + \mathsf{fma}\left(re, -1, re \cdot -1\right)\right)} \]
    11. *-commutative78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) + re \cdot 1\right) + \mathsf{fma}\left(re, -1, \color{blue}{-1 \cdot re}\right)\right)} \]
    12. mul-1-neg78.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(\mathsf{hypot}\left(re, im\right) + re \cdot 1\right) + \mathsf{fma}\left(re, -1, \color{blue}{-re}\right)\right)} \]
  7. Simplified78.9%

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

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{im}} \]
  9. Final simplification52.7%

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

Reproduce

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