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

Percentage Accurate: 41.6% → 87.8%
Time: 9.1s
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.6% 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.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 9.2 \cdot 10^{-54} \lor \neg \left(re \leq 2300\right) \land re \leq 3.25 \cdot 10^{+43}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re 9.2e-54) (and (not (<= re 2300.0)) (<= re 3.25e+43)))
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if ((re <= 9.2e-54) || (!(re <= 2300.0) && (re <= 3.25e+43))) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else {
		tmp = 0.5 * (im / sqrt(re));
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if ((re <= 9.2e-54) || (!(re <= 2300.0) && (re <= 3.25e+43))) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= 9.2e-54) or (not (re <= 2300.0) and (re <= 3.25e+43)):
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= 9.2e-54) || (!(re <= 2300.0) && (re <= 3.25e+43)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= 9.2e-54) || (~((re <= 2300.0)) && (re <= 3.25e+43)))
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, 9.2e-54], And[N[Not[LessEqual[re, 2300.0]], $MachinePrecision], LessEqual[re, 3.25e+43]]], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 9.2 \cdot 10^{-54} \lor \neg \left(re \leq 2300\right) \land re \leq 3.25 \cdot 10^{+43}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 9.1999999999999996e-54 or 2300 < re < 3.2499999999999999e43

    1. Initial program 51.6%

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

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

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

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

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

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

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

    if 9.1999999999999996e-54 < re < 2300 or 3.2499999999999999e43 < re

    1. Initial program 8.2%

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

      \[\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)} \]
    4. Step-by-step derivation
      1. associate-*l*88.5%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)} \]
      2. *-un-lft-identity16.4%

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right) \]
      5. add-log-exp88.5%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    8. Step-by-step derivation
      1. +-lft-identity89.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 9.2 \cdot 10^{-54} \lor \neg \left(re \leq 2300\right) \land re \leq 3.25 \cdot 10^{+43}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 75.8% accurate, 1.7× speedup?

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

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

\mathbf{elif}\;re \leq 7 \cdot 10^{-55} \lor \neg \left(re \leq 1620\right) \land re \leq 9.5 \cdot 10^{+31}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

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


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

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

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

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

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

    if -2.9e26 < re < 7.00000000000000051e-55 or 1620 < re < 9.5000000000000008e31

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    4. Step-by-step derivation
      1. add-log-exp9.2%

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
      2. *-un-lft-identity9.2%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right)} \]
      4. metadata-eval9.2%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right) \]
      5. add-log-exp81.9%

        \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im} \cdot \sqrt{2}}\right) \]
      6. sqrt-unprod82.5%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \sqrt{im \cdot 2}\right)} \]
    6. Step-by-step derivation
      1. +-lft-identity82.5%

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

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

    if 7.00000000000000051e-55 < re < 1620 or 9.5000000000000008e31 < re

    1. Initial program 9.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 inf 87.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)} \]
    4. Step-by-step derivation
      1. associate-*l*87.3%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right)} \]
      4. metadata-eval17.7%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right) \]
      5. add-log-exp87.3%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    8. Step-by-step derivation
      1. +-lft-identity88.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 7 \cdot 10^{-55} \lor \neg \left(re \leq 1620\right) \land re \leq 9.5 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 75.3% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.75 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1620 \lor \neg \left(re \leq 1.45 \cdot 10^{+32}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -5e+26)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 1.75e-122)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (if (or (<= re 1620.0) (not (<= re 1.45e+32)))
       (* 0.5 (/ im (sqrt re)))
       (* 0.5 (sqrt (* 2.0 im)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -5e+26) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 1.75e-122) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if ((re <= 1620.0) || !(re <= 1.45e+32)) {
		tmp = 0.5 * (im / sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-5d+26)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 1.75d-122) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if ((re <= 1620.0d0) .or. (.not. (re <= 1.45d+32))) then
        tmp = 0.5d0 * (im / sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -5e+26) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 1.75e-122) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if ((re <= 1620.0) || !(re <= 1.45e+32)) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -5e+26:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 1.75e-122:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif (re <= 1620.0) or not (re <= 1.45e+32):
		tmp = 0.5 * (im / math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -5e+26)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 1.75e-122)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif ((re <= 1620.0) || !(re <= 1.45e+32))
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -5e+26)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 1.75e-122)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif ((re <= 1620.0) || ~((re <= 1.45e+32)))
		tmp = 0.5 * (im / sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -5e+26], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.75e-122], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1620.0], N[Not[LessEqual[re, 1.45e+32]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

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

\mathbf{elif}\;re \leq 1620 \lor \neg \left(re \leq 1.45 \cdot 10^{+32}\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -5.0000000000000001e26

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

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

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

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

    if -5.0000000000000001e26 < re < 1.7500000000000001e-122

    1. Initial program 60.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 0 86.9%

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

    if 1.7500000000000001e-122 < re < 1620 or 1.45000000000000001e32 < re

    1. Initial program 15.6%

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

      \[\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)} \]
    4. Step-by-step derivation
      1. associate-*l*80.4%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right) \]
      5. add-log-exp80.4%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    8. Step-by-step derivation
      1. +-lft-identity81.4%

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

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

    if 1620 < re < 1.45000000000000001e32

    1. Initial program 31.6%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    4. Step-by-step derivation
      1. add-log-exp4.0%

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
      2. *-un-lft-identity4.0%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right)} \]
      4. metadata-eval4.0%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right) \]
      5. add-log-exp85.9%

        \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im} \cdot \sqrt{2}}\right) \]
      6. sqrt-unprod86.8%

        \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im \cdot 2}}\right) \]
    5. Applied egg-rr86.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \sqrt{im \cdot 2}\right)} \]
    6. Step-by-step derivation
      1. +-lft-identity86.8%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.75 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1620 \lor \neg \left(re \leq 1.45 \cdot 10^{+32}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 75.2% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.35 \cdot 10^{+27}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.5 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im \cdot \left(1 - \frac{re}{im}\right)\right)}\\ \mathbf{elif}\;re \leq 2400 \lor \neg \left(re \leq 2.15 \cdot 10^{+33}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1.35e+27)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 1.5e-122)
     (* 0.5 (sqrt (* 2.0 (* im (- 1.0 (/ re im))))))
     (if (or (<= re 2400.0) (not (<= re 2.15e+33)))
       (* 0.5 (/ im (sqrt re)))
       (* 0.5 (sqrt (* 2.0 im)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.35e+27) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 1.5e-122) {
		tmp = 0.5 * sqrt((2.0 * (im * (1.0 - (re / im)))));
	} else if ((re <= 2400.0) || !(re <= 2.15e+33)) {
		tmp = 0.5 * (im / sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	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+27)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 1.5d-122) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im * (1.0d0 - (re / im)))))
    else if ((re <= 2400.0d0) .or. (.not. (re <= 2.15d+33))) then
        tmp = 0.5d0 * (im / sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.35e+27) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 1.5e-122) {
		tmp = 0.5 * Math.sqrt((2.0 * (im * (1.0 - (re / im)))));
	} else if ((re <= 2400.0) || !(re <= 2.15e+33)) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.35e+27:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 1.5e-122:
		tmp = 0.5 * math.sqrt((2.0 * (im * (1.0 - (re / im)))))
	elif (re <= 2400.0) or not (re <= 2.15e+33):
		tmp = 0.5 * (im / math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.35e+27)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 1.5e-122)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im * Float64(1.0 - Float64(re / im))))));
	elseif ((re <= 2400.0) || !(re <= 2.15e+33))
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.35e+27)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 1.5e-122)
		tmp = 0.5 * sqrt((2.0 * (im * (1.0 - (re / im)))));
	elseif ((re <= 2400.0) || ~((re <= 2.15e+33)))
		tmp = 0.5 * (im / sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.35e+27], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.5e-122], N[(0.5 * N[Sqrt[N[(2.0 * N[(im * N[(1.0 - N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 2400.0], N[Not[LessEqual[re, 2.15e+33]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 1.5 \cdot 10^{-122}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im \cdot \left(1 - \frac{re}{im}\right)\right)}\\

\mathbf{elif}\;re \leq 2400 \lor \neg \left(re \leq 2.15 \cdot 10^{+33}\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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


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

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

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

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

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

    if -1.3499999999999999e27 < re < 1.50000000000000002e-122

    1. Initial program 60.7%

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

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

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

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

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

    if 1.50000000000000002e-122 < re < 2400 or 2.15000000000000014e33 < re

    1. Initial program 15.6%

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

      \[\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)} \]
    4. Step-by-step derivation
      1. associate-*l*80.4%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right) \]
      5. add-log-exp80.4%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    8. Step-by-step derivation
      1. +-lft-identity81.4%

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

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

    if 2400 < re < 2.15000000000000014e33

    1. Initial program 31.6%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    4. Step-by-step derivation
      1. add-log-exp4.0%

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
      2. *-un-lft-identity4.0%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right)} \]
      4. metadata-eval4.0%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right) \]
      5. add-log-exp85.9%

        \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im} \cdot \sqrt{2}}\right) \]
      6. sqrt-unprod86.8%

        \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im \cdot 2}}\right) \]
    5. Applied egg-rr86.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \sqrt{im \cdot 2}\right)} \]
    6. Step-by-step derivation
      1. +-lft-identity86.8%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.35 \cdot 10^{+27}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.5 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im \cdot \left(1 - \frac{re}{im}\right)\right)}\\ \mathbf{elif}\;re \leq 2400 \lor \neg \left(re \leq 2.15 \cdot 10^{+33}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 63.4% accurate, 1.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.2 \cdot 10^{-52} \lor \neg \left(re \leq 850\right) \land re \leq 3.9 \cdot 10^{+34}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 3.2000000000000001e-52 or 850 < re < 3.90000000000000019e34

    1. Initial program 51.6%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
    4. Step-by-step derivation
      1. add-log-exp7.6%

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
      2. *-un-lft-identity7.6%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right)} \]
      4. metadata-eval7.6%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right) \]
      5. add-log-exp66.5%

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \sqrt{im \cdot 2}\right)} \]
    6. Step-by-step derivation
      1. +-lft-identity67.0%

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

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

    if 3.2000000000000001e-52 < re < 850 or 3.90000000000000019e34 < re

    1. Initial program 9.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 inf 87.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)} \]
    4. Step-by-step derivation
      1. associate-*l*87.3%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right)} \]
      4. metadata-eval17.7%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right) \]
      5. add-log-exp87.3%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    8. Step-by-step derivation
      1. +-lft-identity88.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 3.2 \cdot 10^{-52} \lor \neg \left(re \leq 850\right) \land re \leq 3.9 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 51.8% accurate, 2.0× speedup?

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

\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Derivation
  1. Initial program 42.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 0 55.6%

    \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
  4. Step-by-step derivation
    1. add-log-exp9.6%

      \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
    2. *-un-lft-identity9.6%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right)} \]
    4. metadata-eval9.6%

      \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right) \]
    5. add-log-exp55.6%

      \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im} \cdot \sqrt{2}}\right) \]
    6. sqrt-unprod55.9%

      \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\sqrt{im \cdot 2}}\right) \]
  5. Applied egg-rr55.9%

    \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \sqrt{im \cdot 2}\right)} \]
  6. Step-by-step derivation
    1. +-lft-identity55.9%

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

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

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

Reproduce

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