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

Percentage Accurate: 41.8% → 88.8%
Time: 10.7s
Alternatives: 4
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 4 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.8% 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: 88.8% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 55000000000000:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 0.5}\\

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


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

    1. Initial program 42.9%

      \[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. add-sqr-sqrt42.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 5.5e13 < re

    1. Initial program 12.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\frac{im}{\sqrt{re}}} \cdot 0.5 \]
      3. associate-*l/81.9%

        \[\leadsto \color{blue}{\frac{im \cdot 0.5}{\sqrt{re}}} \]
    9. Applied egg-rr81.9%

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

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

Alternative 2: 77.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -15500000000000:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 35000000000000:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -15500000000000.0)
   (sqrt (- re))
   (if (<= re 35000000000000.0)
     (sqrt (* 0.5 (- im re)))
     (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if (re <= -15500000000000.0) {
		tmp = sqrt(-re);
	} else if (re <= 35000000000000.0) {
		tmp = sqrt((0.5 * (im - re)));
	} else {
		tmp = (im * 0.5) / sqrt(re);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-15500000000000.0d0)) then
        tmp = sqrt(-re)
    else if (re <= 35000000000000.0d0) then
        tmp = sqrt((0.5d0 * (im - re)))
    else
        tmp = (im * 0.5d0) / sqrt(re)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -15500000000000.0) {
		tmp = Math.sqrt(-re);
	} else if (re <= 35000000000000.0) {
		tmp = Math.sqrt((0.5 * (im - re)));
	} else {
		tmp = (im * 0.5) / Math.sqrt(re);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -15500000000000.0:
		tmp = math.sqrt(-re)
	elif re <= 35000000000000.0:
		tmp = math.sqrt((0.5 * (im - re)))
	else:
		tmp = (im * 0.5) / math.sqrt(re)
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -15500000000000.0)
		tmp = sqrt(Float64(-re));
	elseif (re <= 35000000000000.0)
		tmp = sqrt(Float64(0.5 * Float64(im - re)));
	else
		tmp = Float64(Float64(im * 0.5) / sqrt(re));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -15500000000000.0)
		tmp = sqrt(-re);
	elseif (re <= 35000000000000.0)
		tmp = sqrt((0.5 * (im - re)));
	else
		tmp = (im * 0.5) / sqrt(re);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -15500000000000.0], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 35000000000000.0], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -15500000000000:\\
\;\;\;\;\sqrt{-re}\\

\mathbf{elif}\;re \leq 35000000000000:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\

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


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

    1. Initial program 23.4%

      \[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. add-sqr-sqrt23.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    8. Step-by-step derivation
      1. neg-mul-177.2%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified77.2%

      \[\leadsto \sqrt{\color{blue}{-re}} \]

    if -1.55e13 < re < 3.5e13

    1. Initial program 51.8%

      \[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. add-sqr-sqrt51.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\left(im + -1 \cdot re\right)} \cdot 0.5} \]
    8. Step-by-step derivation
      1. neg-mul-174.3%

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

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

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

    if 3.5e13 < re

    1. Initial program 12.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\frac{im}{\sqrt{re}}} \cdot 0.5 \]
      3. associate-*l/81.9%

        \[\leadsto \color{blue}{\frac{im \cdot 0.5}{\sqrt{re}}} \]
    9. Applied egg-rr81.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -15500000000000:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 35000000000000:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 64.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.085:\\
\;\;\;\;\sqrt{-re}\\

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


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

    1. Initial program 28.2%

      \[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. add-sqr-sqrt28.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    8. Step-by-step derivation
      1. neg-mul-175.7%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified75.7%

      \[\leadsto \sqrt{\color{blue}{-re}} \]

    if -0.0850000000000000061 < re

    1. Initial program 37.6%

      \[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. add-sqr-sqrt37.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -0.085:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 26.3% accurate, 2.1× speedup?

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

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

    \[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. add-sqr-sqrt35.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
  8. Step-by-step derivation
    1. neg-mul-125.3%

      \[\leadsto \sqrt{\color{blue}{-re}} \]
  9. Simplified25.3%

    \[\leadsto \sqrt{\color{blue}{-re}} \]
  10. Final simplification25.3%

    \[\leadsto \sqrt{-re} \]
  11. Add Preprocessing

Reproduce

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