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

Percentage Accurate: 41.4% → 90.1%
Time: 7.9s
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: 90.1% 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 12.7%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{{im}^{2}}}{\sqrt{re}}} \]
      2. unpow249.8%

        \[\leadsto 0.5 \cdot \frac{\sqrt{\color{blue}{im \cdot im}}}{\sqrt{re}} \]
      3. sqrt-prod95.3%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}} \]
      4. add-sqr-sqrt95.5%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
      5. div-inv95.4%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Final simplification89.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} \]
  5. Add Preprocessing

Alternative 2: 74.6% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{if}\;re \leq -1 \cdot 10^{+24}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq -4.5 \cdot 10^{-45}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;re \leq -1 \cdot 10^{-97}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq 8.5 \cdot 10^{+44}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))) (t_1 (* 0.5 (sqrt (* im 2.0)))))
   (if (<= re -1e+24)
     t_0
     (if (<= re -4.5e-45)
       t_1
       (if (<= re -1e-97)
         t_0
         (if (<= re 8.5e+44) t_1 (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = 0.5 * sqrt((im * 2.0));
	double tmp;
	if (re <= -1e+24) {
		tmp = t_0;
	} else if (re <= -4.5e-45) {
		tmp = t_1;
	} else if (re <= -1e-97) {
		tmp = t_0;
	} else if (re <= 8.5e+44) {
		tmp = t_1;
	} 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) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = 0.5d0 * sqrt((im * 2.0d0))
    if (re <= (-1d+24)) then
        tmp = t_0
    else if (re <= (-4.5d-45)) then
        tmp = t_1
    else if (re <= (-1d-97)) then
        tmp = t_0
    else if (re <= 8.5d+44) then
        tmp = t_1
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = 0.5 * Math.sqrt((im * 2.0));
	double tmp;
	if (re <= -1e+24) {
		tmp = t_0;
	} else if (re <= -4.5e-45) {
		tmp = t_1;
	} else if (re <= -1e-97) {
		tmp = t_0;
	} else if (re <= 8.5e+44) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = 0.5 * math.sqrt((im * 2.0))
	tmp = 0
	if re <= -1e+24:
		tmp = t_0
	elif re <= -4.5e-45:
		tmp = t_1
	elif re <= -1e-97:
		tmp = t_0
	elif re <= 8.5e+44:
		tmp = t_1
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = Float64(0.5 * sqrt(Float64(im * 2.0)))
	tmp = 0.0
	if (re <= -1e+24)
		tmp = t_0;
	elseif (re <= -4.5e-45)
		tmp = t_1;
	elseif (re <= -1e-97)
		tmp = t_0;
	elseif (re <= 8.5e+44)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = 0.5 * sqrt((im * 2.0));
	tmp = 0.0;
	if (re <= -1e+24)
		tmp = t_0;
	elseif (re <= -4.5e-45)
		tmp = t_1;
	elseif (re <= -1e-97)
		tmp = t_0;
	elseif (re <= 8.5e+44)
		tmp = t_1;
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1e+24], t$95$0, If[LessEqual[re, -4.5e-45], t$95$1, If[LessEqual[re, -1e-97], t$95$0, If[LessEqual[re, 8.5e+44], t$95$1, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq -4.5 \cdot 10^{-45}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;re \leq -1 \cdot 10^{-97}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq 8.5 \cdot 10^{+44}:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -9.9999999999999998e23 or -4.4999999999999999e-45 < re < -1.00000000000000004e-97

    1. Initial program 39.3%

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

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

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

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

    if -9.9999999999999998e23 < re < -4.4999999999999999e-45 or -1.00000000000000004e-97 < re < 8.5e44

    1. Initial program 58.8%

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

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

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

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

    if 8.5e44 < re

    1. Initial program 9.5%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{{im}^{2}}}{\sqrt{re}}} \]
      2. unpow264.9%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}} \]
      4. add-sqr-sqrt83.2%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
      5. div-inv83.1%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im \cdot 1}{\sqrt{re}}} \]
      2. *-rgt-identity83.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1 \cdot 10^{+24}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -4.5 \cdot 10^{-45}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq -1 \cdot 10^{-97}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 8.5 \cdot 10^{+44}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 75.2% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{if}\;re \leq -1.35 \cdot 10^{+24}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq -1.15 \cdot 10^{-48}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -1 \cdot 10^{-97}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq 5.4 \cdot 10^{+44}:\\ \;\;\;\;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
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))))
   (if (<= re -1.35e+24)
     t_0
     (if (<= re -1.15e-48)
       (* 0.5 (sqrt (* 2.0 (- im re))))
       (if (<= re -1e-97)
         t_0
         (if (<= re 5.4e+44)
           (* 0.5 (sqrt (* im 2.0)))
           (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double tmp;
	if (re <= -1.35e+24) {
		tmp = t_0;
	} else if (re <= -1.15e-48) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if (re <= -1e-97) {
		tmp = t_0;
	} else if (re <= 5.4e+44) {
		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) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    if (re <= (-1.35d+24)) then
        tmp = t_0
    else if (re <= (-1.15d-48)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if (re <= (-1d-97)) then
        tmp = t_0
    else if (re <= 5.4d+44) 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 t_0 = 0.5 * Math.sqrt((re * -4.0));
	double tmp;
	if (re <= -1.35e+24) {
		tmp = t_0;
	} else if (re <= -1.15e-48) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if (re <= -1e-97) {
		tmp = t_0;
	} else if (re <= 5.4e+44) {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	tmp = 0
	if re <= -1.35e+24:
		tmp = t_0
	elif re <= -1.15e-48:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif re <= -1e-97:
		tmp = t_0
	elif re <= 5.4e+44:
		tmp = 0.5 * math.sqrt((im * 2.0))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	tmp = 0.0
	if (re <= -1.35e+24)
		tmp = t_0;
	elseif (re <= -1.15e-48)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif (re <= -1e-97)
		tmp = t_0;
	elseif (re <= 5.4e+44)
		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)
	t_0 = 0.5 * sqrt((re * -4.0));
	tmp = 0.0;
	if (re <= -1.35e+24)
		tmp = t_0;
	elseif (re <= -1.15e-48)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif (re <= -1e-97)
		tmp = t_0;
	elseif (re <= 5.4e+44)
		tmp = 0.5 * sqrt((im * 2.0));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1.35e+24], t$95$0, If[LessEqual[re, -1.15e-48], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -1e-97], t$95$0, If[LessEqual[re, 5.4e+44], 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}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{if}\;re \leq -1.35 \cdot 10^{+24}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;re \leq -1 \cdot 10^{-97}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq 5.4 \cdot 10^{+44}:\\
\;\;\;\;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 4 regimes
  2. if re < -1.35e24 or -1.15e-48 < re < -1.00000000000000004e-97

    1. Initial program 39.3%

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

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

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

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

    if -1.35e24 < re < -1.15e-48

    1. Initial program 72.5%

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

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

    if -1.00000000000000004e-97 < re < 5.4e44

    1. Initial program 56.9%

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

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

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

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

    if 5.4e44 < re

    1. Initial program 9.5%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{{im}^{2}}}{\sqrt{re}}} \]
      2. unpow264.9%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}} \]
      4. add-sqr-sqrt83.2%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
      5. div-inv83.1%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im \cdot 1}{\sqrt{re}}} \]
      2. *-rgt-identity83.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.35 \cdot 10^{+24}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.15 \cdot 10^{-48}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -1 \cdot 10^{-97}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5.4 \cdot 10^{+44}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 62.6% accurate, 1.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.6 \cdot 10^{+25} \lor \neg \left(re \leq -3.8 \cdot 10^{-48}\right) \land re \leq -6.8 \cdot 10^{-98}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -1.6e25 or -3.80000000000000002e-48 < re < -6.8000000000000003e-98

    1. Initial program 39.3%

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

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

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

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

    if -1.6e25 < re < -3.80000000000000002e-48 or -6.8000000000000003e-98 < re

    1. Initial program 41.5%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.6 \cdot 10^{+25} \lor \neg \left(re \leq -3.8 \cdot 10^{-48}\right) \land re \leq -6.8 \cdot 10^{-98}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 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.8%

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

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

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

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

    \[\leadsto 0.5 \cdot \sqrt{im \cdot 2} \]
  7. Add Preprocessing

Reproduce

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