math.sqrt on complex, real part

Percentage Accurate: 41.4% → 86.9%
Time: 10.7s
Alternatives: 6
Speedup: 1.9×

Specification

?
\[\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.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: 86.9% accurate, 1.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -1.7 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \left(im\_m \cdot {\left(\frac{-1}{re}\right)}^{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\_m\right)\right)}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re -1.7e-96)
   (* 0.5 (* im_m (pow (/ -1.0 re) 0.5)))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re im_m)))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= -1.7e-96) {
		tmp = 0.5 * (im_m * pow((-1.0 / re), 0.5));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im_m))));
	}
	return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= -1.7e-96) {
		tmp = 0.5 * (im_m * Math.pow((-1.0 / re), 0.5));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im_m))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= -1.7e-96:
		tmp = 0.5 * (im_m * math.pow((-1.0 / re), 0.5))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im_m))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= -1.7e-96)
		tmp = Float64(0.5 * Float64(im_m * (Float64(-1.0 / re) ^ 0.5)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im_m)))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= -1.7e-96)
		tmp = 0.5 * (im_m * ((-1.0 / re) ^ 0.5));
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im_m))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, -1.7e-96], N[(0.5 * N[(im$95$m * N[Power[N[(-1.0 / re), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im$95$m ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.7 \cdot 10^{-96}:\\
\;\;\;\;0.5 \cdot \left(im\_m \cdot {\left(\frac{-1}{re}\right)}^{0.5}\right)\\

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


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

    1. Initial program 15.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6434.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified34.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. pow1/2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left({\left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)}^{\color{blue}{\frac{1}{2}}}\right)\right) \]
      2. pow-to-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right) \cdot \frac{1}{2}}\right)\right) \]
      3. exp-lowering-exp.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\left(\log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right) \cdot \frac{1}{2}\right)\right)\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\left(\frac{1}{2} \cdot \log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      5. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right) \]
      8. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right)\right) \]
      9. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right)\right)\right) \]
      10. hypot-lowering-hypot.f6432.4%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right)\right)\right) \]
    6. Applied egg-rr32.4%

      \[\leadsto 0.5 \cdot \color{blue}{e^{0.5 \cdot \log \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}} \]
    7. Taylor expanded in re around -inf

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\color{blue}{\left(\frac{1}{2} \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)\right)}\right)\right) \]
    8. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\log \left(\frac{-1}{re}\right), \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      3. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\left(\frac{-1}{re}\right)\right), \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      4. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      5. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \mathsf{log.f64}\left(\left({im}^{2}\right)\right)\right)\right)\right)\right) \]
      6. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \mathsf{log.f64}\left(\left(im \cdot im\right)\right)\right)\right)\right)\right) \]
      7. *-lowering-*.f6453.3%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \mathsf{log.f64}\left(\mathsf{*.f64}\left(im, im\right)\right)\right)\right)\right)\right) \]
    9. Simplified53.3%

      \[\leadsto 0.5 \cdot e^{\color{blue}{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(im \cdot im\right)\right)}} \]
    10. Taylor expanded in im around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot e^{\frac{1}{2} \cdot \left(\log \left(\frac{-1}{re}\right) + -2 \cdot \log \left(\frac{1}{im}\right)\right)}} \]
    11. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(e^{\frac{1}{2} \cdot \left(\log \left(\frac{-1}{re}\right) + -2 \cdot \log \left(\frac{1}{im}\right)\right)}\right)}\right) \]
      2. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\frac{1}{2} \cdot \left(-2 \cdot \log \left(\frac{1}{im}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\frac{1}{2} \cdot \left(-2 \cdot \log \left(\frac{1}{im}\right)\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      4. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\left(\frac{1}{2} \cdot -2\right) \cdot \log \left(\frac{1}{im}\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      5. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{-1 \cdot \log \left(\frac{1}{im}\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      6. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      7. log-recN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\log im\right)\right)\right)\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      8. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log im + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log im + \log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}\right)\right) \]
      10. exp-sumN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log im} \cdot \color{blue}{e^{\log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}}\right)\right) \]
      11. rem-exp-logN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(im \cdot e^{\color{blue}{\log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}}\right)\right) \]
      12. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \color{blue}{\left(e^{\log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}\right)}\right)\right) \]
      13. exp-to-powN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \left({\left(\frac{-1}{re}\right)}^{\color{blue}{\frac{1}{2}}}\right)\right)\right) \]
      14. pow-lowering-pow.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \mathsf{pow.f64}\left(\left(\frac{-1}{re}\right), \color{blue}{\frac{1}{2}}\right)\right)\right) \]
      15. /-lowering-/.f6442.4%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \mathsf{pow.f64}\left(\mathsf{/.f64}\left(-1, re\right), \frac{1}{2}\right)\right)\right) \]
    12. Simplified42.4%

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

    if -1.7e-96 < re

    1. Initial program 57.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6498.6%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified98.6%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 75.5% accurate, 1.8× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -1.3 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \left(im\_m \cdot {\left(\frac{-1}{re}\right)}^{0.5}\right)\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{+96}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\_m\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re -1.3e-96)
   (* 0.5 (* im_m (pow (/ -1.0 re) 0.5)))
   (if (<= re 6.2e+96) (* 0.5 (sqrt (* 2.0 (+ re im_m)))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= -1.3e-96) {
		tmp = 0.5 * (im_m * pow((-1.0 / re), 0.5));
	} else if (re <= 6.2e+96) {
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	} else {
		tmp = sqrt(re);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (re <= (-1.3d-96)) then
        tmp = 0.5d0 * (im_m * (((-1.0d0) / re) ** 0.5d0))
    else if (re <= 6.2d+96) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im_m)))
    else
        tmp = sqrt(re)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= -1.3e-96) {
		tmp = 0.5 * (im_m * Math.pow((-1.0 / re), 0.5));
	} else if (re <= 6.2e+96) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im_m)));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= -1.3e-96:
		tmp = 0.5 * (im_m * math.pow((-1.0 / re), 0.5))
	elif re <= 6.2e+96:
		tmp = 0.5 * math.sqrt((2.0 * (re + im_m)))
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= -1.3e-96)
		tmp = Float64(0.5 * Float64(im_m * (Float64(-1.0 / re) ^ 0.5)));
	elseif (re <= 6.2e+96)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im_m))));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= -1.3e-96)
		tmp = 0.5 * (im_m * ((-1.0 / re) ^ 0.5));
	elseif (re <= 6.2e+96)
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, -1.3e-96], N[(0.5 * N[(im$95$m * N[Power[N[(-1.0 / re), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.2e+96], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.3 \cdot 10^{-96}:\\
\;\;\;\;0.5 \cdot \left(im\_m \cdot {\left(\frac{-1}{re}\right)}^{0.5}\right)\\

\mathbf{elif}\;re \leq 6.2 \cdot 10^{+96}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\_m\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


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

    1. Initial program 15.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6434.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified34.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. pow1/2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left({\left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)}^{\color{blue}{\frac{1}{2}}}\right)\right) \]
      2. pow-to-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right) \cdot \frac{1}{2}}\right)\right) \]
      3. exp-lowering-exp.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\left(\log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right) \cdot \frac{1}{2}\right)\right)\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\left(\frac{1}{2} \cdot \log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      5. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right) \]
      8. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right)\right) \]
      9. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right)\right)\right) \]
      10. hypot-lowering-hypot.f6432.4%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right)\right)\right) \]
    6. Applied egg-rr32.4%

      \[\leadsto 0.5 \cdot \color{blue}{e^{0.5 \cdot \log \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}} \]
    7. Taylor expanded in re around -inf

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\color{blue}{\left(\frac{1}{2} \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)\right)}\right)\right) \]
    8. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\log \left(\frac{-1}{re}\right), \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      3. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\left(\frac{-1}{re}\right)\right), \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      4. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \log \left({im}^{2}\right)\right)\right)\right)\right) \]
      5. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \mathsf{log.f64}\left(\left({im}^{2}\right)\right)\right)\right)\right)\right) \]
      6. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \mathsf{log.f64}\left(\left(im \cdot im\right)\right)\right)\right)\right)\right) \]
      7. *-lowering-*.f6453.3%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{+.f64}\left(\mathsf{log.f64}\left(\mathsf{/.f64}\left(-1, re\right)\right), \mathsf{log.f64}\left(\mathsf{*.f64}\left(im, im\right)\right)\right)\right)\right)\right) \]
    9. Simplified53.3%

      \[\leadsto 0.5 \cdot e^{\color{blue}{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(im \cdot im\right)\right)}} \]
    10. Taylor expanded in im around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot e^{\frac{1}{2} \cdot \left(\log \left(\frac{-1}{re}\right) + -2 \cdot \log \left(\frac{1}{im}\right)\right)}} \]
    11. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(e^{\frac{1}{2} \cdot \left(\log \left(\frac{-1}{re}\right) + -2 \cdot \log \left(\frac{1}{im}\right)\right)}\right)}\right) \]
      2. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\frac{1}{2} \cdot \left(-2 \cdot \log \left(\frac{1}{im}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\frac{1}{2} \cdot \left(-2 \cdot \log \left(\frac{1}{im}\right)\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      4. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\left(\frac{1}{2} \cdot -2\right) \cdot \log \left(\frac{1}{im}\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      5. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{-1 \cdot \log \left(\frac{1}{im}\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      6. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\left(\mathsf{neg}\left(\log \left(\frac{1}{im}\right)\right)\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      7. log-recN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\log im\right)\right)\right)\right) + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      8. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log im + \frac{1}{2} \cdot \log \left(\frac{-1}{re}\right)}\right)\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log im + \log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}\right)\right) \]
      10. exp-sumN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log im} \cdot \color{blue}{e^{\log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}}\right)\right) \]
      11. rem-exp-logN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(im \cdot e^{\color{blue}{\log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}}\right)\right) \]
      12. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \color{blue}{\left(e^{\log \left(\frac{-1}{re}\right) \cdot \frac{1}{2}}\right)}\right)\right) \]
      13. exp-to-powN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \left({\left(\frac{-1}{re}\right)}^{\color{blue}{\frac{1}{2}}}\right)\right)\right) \]
      14. pow-lowering-pow.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \mathsf{pow.f64}\left(\left(\frac{-1}{re}\right), \color{blue}{\frac{1}{2}}\right)\right)\right) \]
      15. /-lowering-/.f6442.4%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(im, \mathsf{pow.f64}\left(\mathsf{/.f64}\left(-1, re\right), \frac{1}{2}\right)\right)\right) \]
    12. Simplified42.4%

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

    if -1.3000000000000001e-96 < re < 6.1999999999999996e96

    1. Initial program 68.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6498.2%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified98.2%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around 0

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\color{blue}{\left(2 \cdot im + 2 \cdot re\right)}\right)\right) \]
    6. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(im + re\right)\right)\right)\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(im + re\right)\right)\right)\right) \]
      3. +-lowering-+.f6438.7%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(im, re\right)\right)\right)\right) \]
    7. Simplified38.7%

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

    if 6.1999999999999996e96 < re

    1. Initial program 24.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f64100.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left({\left(\sqrt{2}\right)}^{2} \cdot \color{blue}{\sqrt{re}}\right) \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \sqrt{\color{blue}{re}}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(2 \cdot \sqrt{\color{blue}{re}}\right) \]
      4. associate-*r*N/A

        \[\leadsto \left(\frac{1}{2} \cdot 2\right) \cdot \color{blue}{\sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto 1 \cdot \sqrt{\color{blue}{re}} \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(1, \color{blue}{\left(\sqrt{re}\right)}\right) \]
      7. sqrt-lowering-sqrt.f6487.2%

        \[\leadsto \mathsf{*.f64}\left(1, \mathsf{sqrt.f64}\left(re\right)\right) \]
    7. Simplified87.2%

      \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
    8. Step-by-step derivation
      1. *-lft-identityN/A

        \[\leadsto \sqrt{re} \]
      2. sqrt-lowering-sqrt.f6487.2%

        \[\leadsto \mathsf{sqrt.f64}\left(re\right) \]
    9. Applied egg-rr87.2%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification47.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.3 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {\left(\frac{-1}{re}\right)}^{0.5}\right)\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{+96}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 64.6% accurate, 1.8× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -2.6 \cdot 10^{+120}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{0 - im\_m}{re}}\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{+96}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\_m\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re -2.6e+120)
   (* 0.5 (sqrt (/ (- 0.0 im_m) re)))
   (if (<= re 6.2e+96) (* 0.5 (sqrt (* 2.0 (+ re im_m)))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= -2.6e+120) {
		tmp = 0.5 * sqrt(((0.0 - im_m) / re));
	} else if (re <= 6.2e+96) {
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	} else {
		tmp = sqrt(re);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (re <= (-2.6d+120)) then
        tmp = 0.5d0 * sqrt(((0.0d0 - im_m) / re))
    else if (re <= 6.2d+96) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im_m)))
    else
        tmp = sqrt(re)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= -2.6e+120) {
		tmp = 0.5 * Math.sqrt(((0.0 - im_m) / re));
	} else if (re <= 6.2e+96) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im_m)));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= -2.6e+120:
		tmp = 0.5 * math.sqrt(((0.0 - im_m) / re))
	elif re <= 6.2e+96:
		tmp = 0.5 * math.sqrt((2.0 * (re + im_m)))
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= -2.6e+120)
		tmp = Float64(0.5 * sqrt(Float64(Float64(0.0 - im_m) / re)));
	elseif (re <= 6.2e+96)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im_m))));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= -2.6e+120)
		tmp = 0.5 * sqrt(((0.0 - im_m) / re));
	elseif (re <= 6.2e+96)
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, -2.6e+120], N[(0.5 * N[Sqrt[N[(N[(0.0 - im$95$m), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.2e+96], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

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

\mathbf{elif}\;re \leq 6.2 \cdot 10^{+96}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\_m\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


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

    1. Initial program 8.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6432.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified32.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around -inf

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\color{blue}{\left(-1 \cdot \frac{{im}^{2}}{re}\right)}\right)\right) \]
    6. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)\right)\right)\right) \]
      2. neg-sub0N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(0 - \frac{{im}^{2}}{re}\right)\right)\right) \]
      3. --lowering--.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \left(\frac{{im}^{2}}{re}\right)\right)\right)\right) \]
      4. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({im}^{2}\right), re\right)\right)\right)\right) \]
      5. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(im \cdot im\right), re\right)\right)\right)\right) \]
      6. *-lowering-*.f6461.3%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\mathsf{*.f64}\left(im, im\right), re\right)\right)\right)\right) \]
    7. Simplified61.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0 - \frac{im \cdot im}{re}}} \]
    8. Step-by-step derivation
      1. pow2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({im}^{2}\right), re\right)\right)\right)\right) \]
      2. pow-to-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\log im \cdot 2}\right), re\right)\right)\right)\right) \]
      3. rem-log-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\log \left(e^{\log im \cdot 2}\right)}\right), re\right)\right)\right)\right) \]
      4. pow-to-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\log \left({im}^{2}\right)}\right), re\right)\right)\right)\right) \]
      5. pow2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\log \left(im \cdot im\right)}\right), re\right)\right)\right)\right) \]
      6. log-prodN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\log im + \log im}\right), re\right)\right)\right)\right) \]
      7. flip-+N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im}}\right), re\right)\right)\right)\right) \]
      8. +-inversesN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{0}{\log im - \log im}}\right), re\right)\right)\right)\right) \]
      9. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{0 - 0}{\log im - \log im}}\right), re\right)\right)\right)\right) \]
      10. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{0 \cdot 0 - 0}{\log im - \log im}}\right), re\right)\right)\right)\right) \]
      11. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{0 \cdot 0 - 0 \cdot 0}{\log im - \log im}}\right), re\right)\right)\right)\right) \]
      12. +-inversesN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{0 \cdot 0 - 0 \cdot 0}{0}}\right), re\right)\right)\right)\right) \]
      13. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{0 \cdot 0 - 0 \cdot 0}{0 + 0}}\right), re\right)\right)\right)\right) \]
      14. flip--N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{0 - 0}\right), re\right)\right)\right)\right) \]
      15. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{0}\right), re\right)\right)\right)\right) \]
      16. 1-exp6.3%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(1, re\right)\right)\right)\right) \]
      17. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{0}\right), re\right)\right)\right)\right) \]
      18. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(0 - 0\right)}\right), re\right)\right)\right)\right) \]
      19. flip--N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{0 \cdot 0 - 0 \cdot 0}{0 + 0}\right)}\right), re\right)\right)\right)\right) \]
      20. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{0 - 0 \cdot 0}{0 + 0}\right)}\right), re\right)\right)\right)\right) \]
      21. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{0 - 0}{0 + 0}\right)}\right), re\right)\right)\right)\right) \]
      22. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{0}{0 + 0}\right)}\right), re\right)\right)\right)\right) \]
      23. +-inversesN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{\log im \cdot \log im - \log im \cdot \log im}{0 + 0}\right)}\right), re\right)\right)\right)\right) \]
      24. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{\log im \cdot \log im - \log im \cdot \log im}{0}\right)}\right), re\right)\right)\right)\right) \]
      25. +-inversesN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im}\right)}\right), re\right)\right)\right)\right) \]
      26. flip-+N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\left(\log im + \log im\right)}\right), re\right)\right)\right)\right) \]
      27. log-prodN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left({\left(e^{\frac{1}{2}}\right)}^{\log \left(im \cdot im\right)}\right), re\right)\right)\right)\right) \]
      28. pow-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{\_.f64}\left(0, \mathsf{/.f64}\left(\left(e^{\frac{1}{2} \cdot \log \left(im \cdot im\right)}\right), re\right)\right)\right)\right) \]
    9. Applied egg-rr18.4%

      \[\leadsto 0.5 \cdot \sqrt{0 - \frac{\color{blue}{im}}{re}} \]
    10. Step-by-step derivation
      1. sub0-negN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(\mathsf{neg}\left(\frac{im}{re}\right)\right)\right)\right) \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{neg.f64}\left(\left(\frac{im}{re}\right)\right)\right)\right) \]
      3. /-lowering-/.f6418.4%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{neg.f64}\left(\mathsf{/.f64}\left(im, re\right)\right)\right)\right) \]
    11. Applied egg-rr18.4%

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

    if -2.5999999999999999e120 < re < 6.1999999999999996e96

    1. Initial program 57.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6484.6%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified84.6%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around 0

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\color{blue}{\left(2 \cdot im + 2 \cdot re\right)}\right)\right) \]
    6. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(im + re\right)\right)\right)\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(im + re\right)\right)\right)\right) \]
      3. +-lowering-+.f6433.1%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(im, re\right)\right)\right)\right) \]
    7. Simplified33.1%

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

    if 6.1999999999999996e96 < re

    1. Initial program 24.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f64100.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left({\left(\sqrt{2}\right)}^{2} \cdot \color{blue}{\sqrt{re}}\right) \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \sqrt{\color{blue}{re}}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(2 \cdot \sqrt{\color{blue}{re}}\right) \]
      4. associate-*r*N/A

        \[\leadsto \left(\frac{1}{2} \cdot 2\right) \cdot \color{blue}{\sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto 1 \cdot \sqrt{\color{blue}{re}} \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(1, \color{blue}{\left(\sqrt{re}\right)}\right) \]
      7. sqrt-lowering-sqrt.f6487.2%

        \[\leadsto \mathsf{*.f64}\left(1, \mathsf{sqrt.f64}\left(re\right)\right) \]
    7. Simplified87.2%

      \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
    8. Step-by-step derivation
      1. *-lft-identityN/A

        \[\leadsto \sqrt{re} \]
      2. sqrt-lowering-sqrt.f6487.2%

        \[\leadsto \mathsf{sqrt.f64}\left(re\right) \]
    9. Applied egg-rr87.2%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification40.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.6 \cdot 10^{+120}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{0 - im}{re}}\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{+96}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 64.5% accurate, 1.9× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq 2.6 \cdot 10^{+36}:\\ \;\;\;\;0.5 \cdot \sqrt{im\_m \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re 2.6e+36) (* 0.5 (sqrt (* im_m 2.0))) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= 2.6e+36) {
		tmp = 0.5 * sqrt((im_m * 2.0));
	} else {
		tmp = sqrt(re);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (re <= 2.6d+36) then
        tmp = 0.5d0 * sqrt((im_m * 2.0d0))
    else
        tmp = sqrt(re)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= 2.6e+36) {
		tmp = 0.5 * Math.sqrt((im_m * 2.0));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= 2.6e+36:
		tmp = 0.5 * math.sqrt((im_m * 2.0))
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= 2.6e+36)
		tmp = Float64(0.5 * sqrt(Float64(im_m * 2.0)));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= 2.6e+36)
		tmp = 0.5 * sqrt((im_m * 2.0));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, 2.6e+36], N[(0.5 * N[Sqrt[N[(im$95$m * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.6 \cdot 10^{+36}:\\
\;\;\;\;0.5 \cdot \sqrt{im\_m \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


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

    1. Initial program 49.0%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6475.3%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified75.3%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around 0

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\color{blue}{\left(2 \cdot im\right)}\right)\right) \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(im \cdot 2\right)\right)\right) \]
      2. *-lowering-*.f6427.3%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(im, 2\right)\right)\right) \]
    7. Simplified27.3%

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

    if 2.6000000000000001e36 < re

    1. Initial program 32.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f64100.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left({\left(\sqrt{2}\right)}^{2} \cdot \color{blue}{\sqrt{re}}\right) \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \sqrt{\color{blue}{re}}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(2 \cdot \sqrt{\color{blue}{re}}\right) \]
      4. associate-*r*N/A

        \[\leadsto \left(\frac{1}{2} \cdot 2\right) \cdot \color{blue}{\sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto 1 \cdot \sqrt{\color{blue}{re}} \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(1, \color{blue}{\left(\sqrt{re}\right)}\right) \]
      7. sqrt-lowering-sqrt.f6482.0%

        \[\leadsto \mathsf{*.f64}\left(1, \mathsf{sqrt.f64}\left(re\right)\right) \]
    7. Simplified82.0%

      \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
    8. Step-by-step derivation
      1. *-lft-identityN/A

        \[\leadsto \sqrt{re} \]
      2. sqrt-lowering-sqrt.f6482.0%

        \[\leadsto \mathsf{sqrt.f64}\left(re\right) \]
    9. Applied egg-rr82.0%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 29.4% accurate, 2.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq 1.7 \cdot 10^{-247}:\\ \;\;\;\;0.5 \cdot \sqrt{2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re 1.7e-247) (* 0.5 (sqrt 2.0)) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= 1.7e-247) {
		tmp = 0.5 * sqrt(2.0);
	} else {
		tmp = sqrt(re);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (re <= 1.7d-247) then
        tmp = 0.5d0 * sqrt(2.0d0)
    else
        tmp = sqrt(re)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= 1.7e-247) {
		tmp = 0.5 * Math.sqrt(2.0);
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= 1.7e-247:
		tmp = 0.5 * math.sqrt(2.0)
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= 1.7e-247)
		tmp = Float64(0.5 * sqrt(2.0));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= 1.7e-247)
		tmp = 0.5 * sqrt(2.0);
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, 1.7e-247], N[(0.5 * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.7 \cdot 10^{-247}:\\
\;\;\;\;0.5 \cdot \sqrt{2}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


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

    1. Initial program 39.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f6463.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified63.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. pow1/2N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left({\left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)}^{\color{blue}{\frac{1}{2}}}\right)\right) \]
      2. pow-to-expN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \left(e^{\log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right) \cdot \frac{1}{2}}\right)\right) \]
      3. exp-lowering-exp.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\left(\log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right) \cdot \frac{1}{2}\right)\right)\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\left(\frac{1}{2} \cdot \log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      5. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \log \left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\left(2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right) \]
      8. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right)\right)\right) \]
      9. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right)\right)\right) \]
      10. hypot-lowering-hypot.f6459.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right)\right)\right) \]
    6. Applied egg-rr59.0%

      \[\leadsto 0.5 \cdot \color{blue}{e^{0.5 \cdot \log \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}} \]
    7. Taylor expanded in re around 0

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\color{blue}{\left(\frac{1}{2} \cdot \log \left(2 \cdot im\right)\right)}\right)\right) \]
    8. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \log \left(2 \cdot im\right)\right)\right)\right) \]
      2. log-lowering-log.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\left(2 \cdot im\right)\right)\right)\right)\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\left(im \cdot 2\right)\right)\right)\right)\right) \]
      4. *-lowering-*.f6421.6%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{exp.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{log.f64}\left(\mathsf{*.f64}\left(im, 2\right)\right)\right)\right)\right) \]
    9. Simplified21.6%

      \[\leadsto 0.5 \cdot e^{\color{blue}{0.5 \cdot \log \left(im \cdot 2\right)}} \]
    10. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto e^{\frac{1}{2} \cdot \log \left(im \cdot 2\right)} \cdot \color{blue}{\frac{1}{2}} \]
    11. Applied egg-rr6.5%

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

    if 1.7000000000000001e-247 < re

    1. Initial program 53.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
      2. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
      6. hypot-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
      7. hypot-lowering-hypot.f64100.0%

        \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \left({\left(\sqrt{2}\right)}^{2} \cdot \color{blue}{\sqrt{re}}\right) \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \sqrt{\color{blue}{re}}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(2 \cdot \sqrt{\color{blue}{re}}\right) \]
      4. associate-*r*N/A

        \[\leadsto \left(\frac{1}{2} \cdot 2\right) \cdot \color{blue}{\sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto 1 \cdot \sqrt{\color{blue}{re}} \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(1, \color{blue}{\left(\sqrt{re}\right)}\right) \]
      7. sqrt-lowering-sqrt.f6453.1%

        \[\leadsto \mathsf{*.f64}\left(1, \mathsf{sqrt.f64}\left(re\right)\right) \]
    7. Simplified53.1%

      \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
    8. Step-by-step derivation
      1. *-lft-identityN/A

        \[\leadsto \sqrt{re} \]
      2. sqrt-lowering-sqrt.f6453.1%

        \[\leadsto \mathsf{sqrt.f64}\left(re\right) \]
    9. Applied egg-rr53.1%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification28.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.7 \cdot 10^{-247}:\\ \;\;\;\;0.5 \cdot \sqrt{2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 26.4% accurate, 2.1× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \sqrt{re} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 (sqrt re))
im_m = fabs(im);
double code(double re, double im_m) {
	return sqrt(re);
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = sqrt(re)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return Math.sqrt(re);
}
im_m = math.fabs(im)
def code(re, im_m):
	return math.sqrt(re)
im_m = abs(im)
function code(re, im_m)
	return sqrt(re)
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = sqrt(re);
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
\sqrt{re}
\end{array}
Derivation
  1. Initial program 45.7%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
  2. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}\right) \]
    2. sqrt-lowering-sqrt.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\left(2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(\sqrt{re \cdot re + im \cdot im} + re\right)\right)\right)\right) \]
    4. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \left(re + \sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right) \]
    5. +-lowering-+.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)\right)\right) \]
    6. hypot-defineN/A

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)\right)\right) \]
    7. hypot-lowering-hypot.f6480.2%

      \[\leadsto \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{sqrt.f64}\left(\mathsf{*.f64}\left(2, \mathsf{+.f64}\left(re, \mathsf{hypot.f64}\left(re, im\right)\right)\right)\right)\right) \]
  3. Simplified80.2%

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
  4. Add Preprocessing
  5. Taylor expanded in re around inf

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
  6. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \left({\left(\sqrt{2}\right)}^{2} \cdot \color{blue}{\sqrt{re}}\right) \]
    2. unpow2N/A

      \[\leadsto \frac{1}{2} \cdot \left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \sqrt{\color{blue}{re}}\right) \]
    3. rem-square-sqrtN/A

      \[\leadsto \frac{1}{2} \cdot \left(2 \cdot \sqrt{\color{blue}{re}}\right) \]
    4. associate-*r*N/A

      \[\leadsto \left(\frac{1}{2} \cdot 2\right) \cdot \color{blue}{\sqrt{re}} \]
    5. metadata-evalN/A

      \[\leadsto 1 \cdot \sqrt{\color{blue}{re}} \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(1, \color{blue}{\left(\sqrt{re}\right)}\right) \]
    7. sqrt-lowering-sqrt.f6425.1%

      \[\leadsto \mathsf{*.f64}\left(1, \mathsf{sqrt.f64}\left(re\right)\right) \]
  7. Simplified25.1%

    \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
  8. Step-by-step derivation
    1. *-lft-identityN/A

      \[\leadsto \sqrt{re} \]
    2. sqrt-lowering-sqrt.f6425.1%

      \[\leadsto \mathsf{sqrt.f64}\left(re\right) \]
  9. Applied egg-rr25.1%

    \[\leadsto \color{blue}{\sqrt{re}} \]
  10. Add Preprocessing

Developer Target 1: 48.5% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im}\\ \mathbf{if}\;re < 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t\_0 - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t\_0 + re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (sqrt (+ (* re re) (* im im)))))
   (if (< re 0.0)
     (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
     (* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
	double t_0 = sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * sqrt((2.0 * (t_0 + 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 = sqrt(((re * re) + (im * im)))
    if (re < 0.0d0) then
        tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = Math.sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
	}
	return tmp;
}
def code(re, im):
	t_0 = math.sqrt(((re * re) + (im * im)))
	tmp = 0
	if re < 0.0:
		tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re))))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (t_0 + re)))
	return tmp
function code(re, im)
	t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im)))
	tmp = 0.0
	if (re < 0.0)
		tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re)))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = sqrt(((re * re) + (im * im)));
	tmp = 0.0;
	if (re < 0.0)
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	else
		tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t\_0 - re}}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t\_0 + re\right)}\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2024150 
(FPCore (re im)
  :name "math.sqrt on complex, real part"
  :precision binary64

  :alt
  (! :herbie-platform default (if (< re 0) (* 1/2 (* (sqrt 2) (sqrt (/ (* im im) (- (modulus re im) re))))) (* 1/2 (sqrt (* 2 (+ (modulus re im) re))))))

  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))