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

Percentage Accurate: 41.2% → 89.6%
Time: 7.4s
Alternatives: 6
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

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

Initial Program: 41.2% 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: 89.6% accurate, 0.7× speedup?

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

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

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


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

    1. Initial program 4.0%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Taylor expanded in im around 0 91.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. *-commutative91.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 46.7%

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

        \[\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.7%

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

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

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

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

Alternative 2: 75.1% accurate, 1.9× speedup?

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

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\


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

    1. Initial program 17.1%

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

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

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

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

    if -1.90000000000000007e136 < re < 1.29999999999999992e-5

    1. Initial program 59.9%

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

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

    if 1.29999999999999992e-5 < re

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

      \[\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. *-commutative78.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left({re}^{\color{blue}{-0.5}} \cdot im\right) \]
    10. Applied egg-rr79.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.9 \cdot 10^{+136}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.3 \cdot 10^{-5}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 3: 73.5% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;re \leq 1.5 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\


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

    1. Initial program 17.1%

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

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

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

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

    if -1.4000000000000001e136 < re < 1.50000000000000004e-5

    1. Initial program 59.9%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u69.3%

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{im \cdot 2}\right)\right)} \]
      2. expm1-log1p73.8%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{im \cdot 2}} \]
    6. Simplified73.8%

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

    if 1.50000000000000004e-5 < re

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

      \[\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. *-commutative78.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left({re}^{\color{blue}{-0.5}} \cdot im\right) \]
    10. Applied egg-rr79.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.4 \cdot 10^{+136}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.5 \cdot 10^{-5}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 4: 64.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.1 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\


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

    1. Initial program 53.1%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u59.5%

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

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{im \cdot 2}}\right)} - 1\right) \]
    4. Applied egg-rr47.7%

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

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

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

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

    if 2.09999999999999988e-5 < re

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

      \[\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. *-commutative78.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left({re}^{\color{blue}{-0.5}} \cdot im\right) \]
    10. Applied egg-rr79.0%

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

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

Alternative 5: 64.6% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.35 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


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

    1. Initial program 53.1%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u59.5%

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

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{im \cdot 2}}\right)} - 1\right) \]
    4. Applied egg-rr47.7%

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

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

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

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

    if 1.3499999999999999e-5 < re

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

      \[\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. *-commutative78.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.35 \cdot 10^{-5}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 6: 52.3% accurate, 2.0× speedup?

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

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

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
  3. Step-by-step derivation
    1. expm1-log1p-u50.1%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{im} \cdot \sqrt{2}\right)\right)} \]
    2. expm1-udef45.9%

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

      \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{im \cdot 2}}\right)} - 1\right) \]
  4. Applied egg-rr45.9%

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

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

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

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

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

Reproduce

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