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

Percentage Accurate: 51.1% → 96.8%
Time: 8.3s
Alternatives: 7
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 7 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: 51.1% 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: 96.8% accurate, 0.7× speedup?

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

\\
0.5 \cdot \left(\sqrt{\mathsf{hypot}\left(re, im\right) - re} \cdot \sqrt{2}\right)
\end{array}
Derivation
  1. Initial program 55.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. *-commutative55.3%

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

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

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

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

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

Alternative 2: 74.6% accurate, 1.0× speedup?

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

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

\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 < -3.99999999999999976e39

    1. Initial program 49.5%

      \[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. *-commutative49.5%

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

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

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

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

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

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

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

    if -3.99999999999999976e39 < re < 4.1000000000000003e29

    1. Initial program 64.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 0 78.4%

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

    if 4.1000000000000003e29 < re

    1. Initial program 37.5%

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

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

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

Alternative 3: 97.3% accurate, 1.0× speedup?

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

\\
0.5 \cdot \sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2}
\end{array}
Derivation
  1. Initial program 55.3%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} + \left(-re\right)\right)}} \]
    2. sqr-neg55.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-neg55.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-neg55.3%

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

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

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

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

Alternative 4: 64.1% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{if}\;re \leq -5 \cdot 10^{+39}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq -8.2 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq 1.1 \cdot 10^{+213}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))))
   (if (<= re -5e+39)
     t_0
     (if (<= re -8.2e-40)
       (* 0.5 (sqrt (* 2.0 (- im re))))
       (if (<= re -4.2e-129)
         t_0
         (if (<= re 1.1e+213)
           (* 0.5 (sqrt (* im 2.0)))
           (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double tmp;
	if (re <= -5e+39) {
		tmp = t_0;
	} else if (re <= -8.2e-40) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if (re <= -4.2e-129) {
		tmp = t_0;
	} else if (re <= 1.1e+213) {
		tmp = 0.5 * sqrt((im * 2.0));
	} else {
		tmp = 0.5 * (im / sqrt(re));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    if (re <= (-5d+39)) then
        tmp = t_0
    else if (re <= (-8.2d-40)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if (re <= (-4.2d-129)) then
        tmp = t_0
    else if (re <= 1.1d+213) then
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    else
        tmp = 0.5d0 * (im / sqrt(re))
    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 <= -5e+39) {
		tmp = t_0;
	} else if (re <= -8.2e-40) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if (re <= -4.2e-129) {
		tmp = t_0;
	} else if (re <= 1.1e+213) {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	tmp = 0
	if re <= -5e+39:
		tmp = t_0
	elif re <= -8.2e-40:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif re <= -4.2e-129:
		tmp = t_0
	elif re <= 1.1e+213:
		tmp = 0.5 * math.sqrt((im * 2.0))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	tmp = 0.0
	if (re <= -5e+39)
		tmp = t_0;
	elseif (re <= -8.2e-40)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif (re <= -4.2e-129)
		tmp = t_0;
	elseif (re <= 1.1e+213)
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	tmp = 0.0;
	if (re <= -5e+39)
		tmp = t_0;
	elseif (re <= -8.2e-40)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif (re <= -4.2e-129)
		tmp = t_0;
	elseif (re <= 1.1e+213)
		tmp = 0.5 * sqrt((im * 2.0));
	else
		tmp = 0.5 * (im / sqrt(re));
	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, -5e+39], t$95$0, If[LessEqual[re, -8.2e-40], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -4.2e-129], t$95$0, If[LessEqual[re, 1.1e+213], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{if}\;re \leq -5 \cdot 10^{+39}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -5.00000000000000015e39 or -8.19999999999999926e-40 < re < -4.2e-129

    1. Initial program 59.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 -inf 78.8%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    5. Simplified78.8%

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

    if -5.00000000000000015e39 < re < -8.19999999999999926e-40

    1. Initial program 84.1%

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

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

    if -4.2e-129 < re < 1.0999999999999999e213

    1. Initial program 56.8%

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

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

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

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

    if 1.0999999999999999e213 < re

    1. Initial program 2.2%

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

      \[\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*31.2%

        \[\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. *-commutative31.2%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
      2. expm1-udef63.8%

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \left(\color{blue}{\sqrt{2 \cdot 0.5}} \cdot \sqrt{\frac{1}{re}}\right)\right)} - 1\right) \]
      4. metadata-eval63.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \left(\sqrt{\color{blue}{1}} \cdot \sqrt{\frac{1}{re}}\right)\right)} - 1\right) \]
      5. metadata-eval63.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \left(\color{blue}{1} \cdot \sqrt{\frac{1}{re}}\right)\right)} - 1\right) \]
      6. *-un-lft-identity63.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right)} - 1\right) \]
      7. sqrt-div63.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right)} - 1\right) \]
      8. metadata-eval63.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right)} - 1\right) \]
      9. un-div-inv63.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{im}{\sqrt{re}}}\right)} - 1\right) \]
    7. Applied egg-rr63.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    8. Step-by-step derivation
      1. expm1-def30.4%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)} \]
      2. expm1-log1p31.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{+39}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -8.2 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.1 \cdot 10^{+213}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 62.2% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.4 \cdot 10^{+26} \lor \neg \left(re \leq -6.5 \cdot 10^{-39}\right) \land re \leq -1.3 \cdot 10^{-129}:\\ \;\;\;\;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 (or (<= re -1.4e+26) (and (not (<= re -6.5e-39)) (<= re -1.3e-129)))
   (* 0.5 (sqrt (* re -4.0)))
   (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
	double tmp;
	if ((re <= -1.4e+26) || (!(re <= -6.5e-39) && (re <= -1.3e-129))) {
		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 <= (-1.4d+26)) .or. (.not. (re <= (-6.5d-39))) .and. (re <= (-1.3d-129))) 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 <= -1.4e+26) || (!(re <= -6.5e-39) && (re <= -1.3e-129))) {
		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 <= -1.4e+26) or (not (re <= -6.5e-39) and (re <= -1.3e-129)):
		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 <= -1.4e+26) || (!(re <= -6.5e-39) && (re <= -1.3e-129)))
		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 <= -1.4e+26) || (~((re <= -6.5e-39)) && (re <= -1.3e-129)))
		tmp = 0.5 * sqrt((re * -4.0));
	else
		tmp = 0.5 * sqrt((im * 2.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, -1.4e+26], And[N[Not[LessEqual[re, -6.5e-39]], $MachinePrecision], LessEqual[re, -1.3e-129]]], 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 -1.4 \cdot 10^{+26} \lor \neg \left(re \leq -6.5 \cdot 10^{-39}\right) \land re \leq -1.3 \cdot 10^{-129}:\\
\;\;\;\;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 < -1.4e26 or -6.50000000000000027e-39 < re < -1.3e-129

    1. Initial program 60.7%

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

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

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

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

    if -1.4e26 < re < -6.50000000000000027e-39 or -1.3e-129 < re

    1. Initial program 53.0%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.4 \cdot 10^{+26} \lor \neg \left(re \leq -6.5 \cdot 10^{-39}\right) \land re \leq -1.3 \cdot 10^{-129}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 74.7% accurate, 1.8× speedup?

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

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

\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 < -4.3e39

    1. Initial program 49.5%

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

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

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

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

    if -4.3e39 < re < 3.70000000000000002e27

    1. Initial program 64.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 0 78.4%

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

    if 3.70000000000000002e27 < re

    1. Initial program 37.5%

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

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

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

Alternative 7: 51.9% 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 55.3%

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

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

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

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

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

Reproduce

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