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

Percentage Accurate: 41.3% → 90.0%
Time: 5.0s
Alternatives: 8
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 8 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.3% accurate, 1.0× speedup?

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

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

Alternative 1: 90.0% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\left(\left(im \cdot \sqrt{2}\right) \cdot \sqrt{0.5}\right) \cdot \left(0.5 \cdot \sqrt{\frac{1}{re}}\right)\\

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


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

    1. Initial program 9.5%

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

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

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

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

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

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

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

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

    1. Initial program 45.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt88.7%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt89.4%

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

        \[\leadsto \sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \color{blue}{0.25}} \]
    3. Applied egg-rr89.4%

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative89.4%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*89.4%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval89.4%

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

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

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

Alternative 2: 90.0% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(\sqrt{\frac{1}{re}} \cdot \left(im \cdot \sqrt{0.5}\right)\right)\right)\\

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


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

    1. Initial program 9.5%

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

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

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

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

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

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

    1. Initial program 45.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt88.7%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt89.4%

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

        \[\leadsto \sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \color{blue}{0.25}} \]
    3. Applied egg-rr89.4%

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative89.4%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*89.4%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval89.4%

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

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

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

Alternative 3: 90.0% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot \sqrt{\frac{1}{re}}\right)\right)\\

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


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

    1. Initial program 9.5%

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

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

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

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

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

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

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

    1. Initial program 45.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt88.7%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt89.4%

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

        \[\leadsto \sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \color{blue}{0.25}} \]
    3. Applied egg-rr89.4%

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative89.4%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*89.4%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval89.4%

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

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

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

Alternative 4: 84.7% accurate, 0.5× speedup?

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

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

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


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

    1. Initial program 11.4%

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

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

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

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

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

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

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

    1. Initial program 44.0%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt87.7%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt88.4%

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

        \[\leadsto \sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \color{blue}{0.25}} \]
    3. Applied egg-rr88.4%

      \[\leadsto \color{blue}{\sqrt{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.25}} \]
    4. Step-by-step derivation
      1. *-commutative88.4%

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}} \]
      2. associate-*r*88.4%

        \[\leadsto \sqrt{\color{blue}{\left(0.25 \cdot 2\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      3. metadata-eval88.4%

        \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
    5. Simplified88.4%

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

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

Alternative 5: 63.2% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{if}\;re \leq -3 \cdot 10^{+131}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{-13}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -2.8 \cdot 10^{-27}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))))
   (if (<= re -3e+131)
     t_0
     (if (<= re -2.9e-13)
       (* 0.5 (sqrt (* 2.0 (- im re))))
       (if (<= re -2.8e-27) t_0 (* 0.5 (sqrt (* im 2.0))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double tmp;
	if (re <= -3e+131) {
		tmp = t_0;
	} else if (re <= -2.9e-13) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if (re <= -2.8e-27) {
		tmp = t_0;
	} else {
		tmp = 0.5 * sqrt((im * 2.0));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    if (re <= (-3d+131)) then
        tmp = t_0
    else if (re <= (-2.9d-13)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if (re <= (-2.8d-27)) then
        tmp = t_0
    else
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double tmp;
	if (re <= -3e+131) {
		tmp = t_0;
	} else if (re <= -2.9e-13) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if (re <= -2.8e-27) {
		tmp = t_0;
	} else {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	tmp = 0
	if re <= -3e+131:
		tmp = t_0
	elif re <= -2.9e-13:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif re <= -2.8e-27:
		tmp = t_0
	else:
		tmp = 0.5 * math.sqrt((im * 2.0))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	tmp = 0.0
	if (re <= -3e+131)
		tmp = t_0;
	elseif (re <= -2.9e-13)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif (re <= -2.8e-27)
		tmp = t_0;
	else
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	tmp = 0.0;
	if (re <= -3e+131)
		tmp = t_0;
	elseif (re <= -2.9e-13)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif (re <= -2.8e-27)
		tmp = t_0;
	else
		tmp = 0.5 * sqrt((im * 2.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -3e+131], t$95$0, If[LessEqual[re, -2.9e-13], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -2.8e-27], t$95$0, N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{if}\;re \leq -3 \cdot 10^{+131}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;re \leq -2.8 \cdot 10^{-27}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -3.0000000000000001e131 or -2.8999999999999998e-13 < re < -2.8e-27

    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. hypot-def100.0%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified96.0%

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

    if -3.0000000000000001e131 < re < -2.8999999999999998e-13

    1. Initial program 64.1%

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

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

    if -2.8e-27 < re

    1. Initial program 41.2%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3 \cdot 10^{+131}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{-13}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -2.8 \cdot 10^{-27}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]

Alternative 6: 68.7% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.12 \cdot 10^{+132}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.15 \cdot 10^{+56}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1.12e+132)
   (* 0.5 (sqrt (* re -4.0)))
   (if (<= re 1.15e+56)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (sqrt (/ (* im im) re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.12e+132) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 1.15e+56) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * sqrt(((im * im) / re));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-1.12d+132)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 1.15d+56) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else
        tmp = 0.5d0 * sqrt(((im * im) / re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.12e+132) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 1.15e+56) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * Math.sqrt(((im * im) / re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.12e+132:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 1.15e+56:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	else:
		tmp = 0.5 * math.sqrt(((im * im) / re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.12e+132)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 1.15e+56)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	else
		tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.12e+132)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 1.15e+56)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	else
		tmp = 0.5 * sqrt(((im * im) / re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.12e+132], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.15e+56], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.12 \cdot 10^{+132}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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

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


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

    1. Initial program 17.3%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified95.6%

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

    if -1.12e132 < re < 1.15000000000000007e56

    1. Initial program 56.2%

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

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

    if 1.15000000000000007e56 < re

    1. Initial program 11.1%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
    6. Simplified54.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.12 \cdot 10^{+132}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.15 \cdot 10^{+56}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\ \end{array} \]

Alternative 7: 64.6% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -4.5 \cdot 10^{-28}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -4.5e-28) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
	double tmp;
	if (re <= -4.5e-28) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else {
		tmp = 0.5 * sqrt((im * 2.0));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-4.5d-28)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -4.5e-28) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -4.5e-28:
		tmp = 0.5 * math.sqrt((re * -4.0))
	else:
		tmp = 0.5 * math.sqrt((im * 2.0))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -4.5e-28)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	else
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -4.5e-28)
		tmp = 0.5 * sqrt((re * -4.0));
	else
		tmp = 0.5 * sqrt((im * 2.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -4.5e-28], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.5 \cdot 10^{-28}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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


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

    1. Initial program 37.5%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified79.2%

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

    if -4.4999999999999998e-28 < re

    1. Initial program 41.2%

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

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

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

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

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

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

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

Alternative 8: 52.3% accurate, 2.0× speedup?

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

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

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

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

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

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

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

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

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

Reproduce

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