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

Percentage Accurate: 40.5% → 87.9%
Time: 6.3s
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: 40.5% 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 1.55 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - 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.55e+31)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
	double tmp;
	if (re <= 1.55e+31) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else {
		tmp = 0.5 * (im * pow(re, -0.5));
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if (re <= 1.55e+31) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= 1.55e+31:
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= 1.55e+31)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, 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.55e+31)
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, 1.55e+31], 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[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

\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 < 1.5500000000000001e31

    1. Initial program 45.2%

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

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

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

    if 1.5500000000000001e31 < re

    1. Initial program 7.2%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. sqr-pow83.0%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{re}^{-0.5}}\right) \]
    7. Simplified83.4%

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

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

Alternative 2: 74.8% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{+141}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.32 \cdot 10^{-30} \lor \neg \left(re \leq 4.8 \cdot 10^{+14}\right) \land re \leq 1.55 \cdot 10^{+31}:\\ \;\;\;\;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 -2.9e+141)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (or (<= re 1.32e-30) (and (not (<= re 4.8e+14)) (<= re 1.55e+31)))
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -2.9e+141) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 1.32e-30) || (!(re <= 4.8e+14) && (re <= 1.55e+31))) {
		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 <= (-2.9d+141)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if ((re <= 1.32d-30) .or. (.not. (re <= 4.8d+14)) .and. (re <= 1.55d+31)) 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 <= -2.9e+141) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 1.32e-30) || (!(re <= 4.8e+14) && (re <= 1.55e+31))) {
		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 <= -2.9e+141:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif (re <= 1.32e-30) or (not (re <= 4.8e+14) and (re <= 1.55e+31)):
		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 <= -2.9e+141)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif ((re <= 1.32e-30) || (!(re <= 4.8e+14) && (re <= 1.55e+31)))
		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 <= -2.9e+141)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif ((re <= 1.32e-30) || (~((re <= 4.8e+14)) && (re <= 1.55e+31)))
		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, -2.9e+141], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.32e-30], And[N[Not[LessEqual[re, 4.8e+14]], $MachinePrecision], LessEqual[re, 1.55e+31]]], 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 -2.9 \cdot 10^{+141}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq 1.32 \cdot 10^{-30} \lor \neg \left(re \leq 4.8 \cdot 10^{+14}\right) \land re \leq 1.55 \cdot 10^{+31}:\\
\;\;\;\;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 < -2.90000000000000007e141

    1. Initial program 6.4%

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

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

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

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

    if -2.90000000000000007e141 < re < 1.32e-30 or 4.8e14 < re < 1.5500000000000001e31

    1. Initial program 58.0%

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

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

    if 1.32e-30 < re < 4.8e14 or 1.5500000000000001e31 < re

    1. Initial program 8.6%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. sqr-pow82.0%

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left(e^{-\log re}\right)}}^{0.5}\right) \]
      7. exp-prod77.9%

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{re}^{-0.5}}\right) \]
    7. Simplified82.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{+141}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.32 \cdot 10^{-30} \lor \neg \left(re \leq 4.8 \cdot 10^{+14}\right) \land re \leq 1.55 \cdot 10^{+31}:\\ \;\;\;\;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: 52.5% accurate, 2.0× speedup?

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

\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 < -4.999999999999985e-310

    1. Initial program 47.3%

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

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

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

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

    if -4.999999999999985e-310 < re

    1. Initial program 26.6%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. sqr-pow51.8%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      10. metadata-eval49.2%

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

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

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

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

Alternative 4: 26.2% accurate, 2.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
    10. metadata-eval22.7%

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

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot {re}^{-0.5}\right)} \]
  8. Final simplification24.0%

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

Alternative 5: 3.0% accurate, 2.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
    10. metadata-eval22.7%

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(im \cdot {re}^{-0.5}\right)\right)} \]
    2. expm1-udef10.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(im \cdot {re}^{-0.5}\right)} - 1\right)} \]
    3. pow-to-exp10.5%

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

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

      \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right)} - 1\right) \]
    6. distribute-lft-neg-out10.5%

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

      \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \color{blue}{{\left(e^{-\log re}\right)}^{0.5}}\right)} - 1\right) \]
    8. unpow1/210.5%

      \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \color{blue}{\sqrt{e^{-\log re}}}\right)} - 1\right) \]
    9. add-sqr-sqrt1.2%

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

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

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

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

      \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \sqrt{e^{\color{blue}{\log re}}}\right)} - 1\right) \]
    14. add-exp-log6.5%

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

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

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

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

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

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

Reproduce

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