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

Percentage Accurate: 41.3% → 90.2%
Time: 11.1s
Alternatives: 5
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 41.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.2% accurate, 0.7× speedup?

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

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

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


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

    1. Initial program 8.3%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot {\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{\color{blue}{\left(0.25 \cdot 2\right)}} \]
      5. pow-to-exp16.7%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \left(0.25 \cdot 2\right)}} \]
      6. *-commutative16.7%

        \[\leadsto 0.5 \cdot e^{\log \color{blue}{\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2\right)} \cdot \left(0.25 \cdot 2\right)} \]
      7. metadata-eval16.7%

        \[\leadsto 0.5 \cdot e^{\log \left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2\right) \cdot \color{blue}{0.5}} \]
    6. Applied egg-rr16.7%

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

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

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

    1. Initial program 48.9%

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

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

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

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

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

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

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

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

Alternative 2: 64.5% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;re \leq 1.05 \cdot 10^{+229}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


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

    1. Initial program 46.4%

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

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

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

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

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

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

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

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

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

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

    if -7.00000000000000046e45 < re < 1.04999999999999994e229

    1. Initial program 43.7%

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

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

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

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

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

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

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

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

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

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

    if 1.04999999999999994e229 < re

    1. Initial program 2.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7 \cdot 10^{+45}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.05 \cdot 10^{+229}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 76.1% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.1 \cdot 10^{+46}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{-37}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1.1e+46)
   (* 0.5 (sqrt (* re -4.0)))
   (if (<= re 6.2e-37)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (* im (sqrt (/ 1.0 re)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.1e+46) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 6.2e-37) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * (im * sqrt((1.0 / 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.1d+46)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 6.2d-37) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else
        tmp = 0.5d0 * (im * sqrt((1.0d0 / re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.1e+46) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 6.2e-37) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * (im * Math.sqrt((1.0 / re)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.1e+46:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 6.2e-37:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	else:
		tmp = 0.5 * (im * math.sqrt((1.0 / re)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.1e+46)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 6.2e-37)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	else
		tmp = Float64(0.5 * Float64(im * sqrt(Float64(1.0 / re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.1e+46)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 6.2e-37)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	else
		tmp = 0.5 * (im * sqrt((1.0 / re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.1e+46], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.2e-37], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\


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

    1. Initial program 46.4%

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

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

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

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

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

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

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

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

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

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

    if -1.1e46 < re < 6.19999999999999987e-37

    1. Initial program 55.9%

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

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

    if 6.19999999999999987e-37 < re

    1. Initial program 16.7%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot {\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{\color{blue}{\left(0.25 \cdot 2\right)}} \]
      5. pow-to-exp41.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \left(0.25 \cdot 2\right)}} \]
      6. *-commutative41.2%

        \[\leadsto 0.5 \cdot e^{\log \color{blue}{\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2\right)} \cdot \left(0.25 \cdot 2\right)} \]
      7. metadata-eval41.2%

        \[\leadsto 0.5 \cdot e^{\log \left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2\right) \cdot \color{blue}{0.5}} \]
    6. Applied egg-rr41.2%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{re}}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification78.3%

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

Alternative 4: 63.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -4.9 \cdot 10^{+45}:\\ \;\;\;\;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.9e+45) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
	double tmp;
	if (re <= -4.9e+45) {
		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.9d+45)) 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.9e+45) {
		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.9e+45:
		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.9e+45)
		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.9e+45)
		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.9e+45], 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.9 \cdot 10^{+45}:\\
\;\;\;\;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.9000000000000002e45

    1. Initial program 46.4%

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

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

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

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

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

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

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

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

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

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

    if -4.9000000000000002e45 < re

    1. Initial program 40.3%

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 51.6% 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 41.5%

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

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

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

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

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

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

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

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

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

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

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

Reproduce

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