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

Percentage Accurate: 41.6% → 85.8%
Time: 4.2s
Alternatives: 5
Speedup: 2.6×

Specification

?
\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
(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)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    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]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}

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.6% accurate, 1.0× speedup?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
(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)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    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]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}

Alternative 1: 85.8% accurate, 0.4× speedup?

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

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


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

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Taylor expanded in re around inf

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    6. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{\color{blue}{re}}} \]
      2. lower-pow.f6415.3%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{2}}{re}} \]
    7. Applied rewrites15.3%

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{\color{blue}{re}}} \]
      2. lift-pow.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{re}} \]
      3. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{im \cdot im}{re}} \]
      4. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      6. lower-/.f6418.0%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{im}{\color{blue}{re}}} \]
    9. Applied rewrites18.0%

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{im}{\color{blue}{re}}} \]
      2. frac-2negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{\mathsf{neg}\left(im\right)}{\color{blue}{\mathsf{neg}\left(re\right)}}} \]
      3. div-flipN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\color{blue}{\frac{\mathsf{neg}\left(re\right)}{\mathsf{neg}\left(im\right)}}}} \]
      4. lower-unsound-/.f32N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{\mathsf{neg}\left(re\right)}{\color{blue}{\mathsf{neg}\left(im\right)}}}} \]
      5. lower-/.f32N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{\mathsf{neg}\left(re\right)}{\color{blue}{\mathsf{neg}\left(im\right)}}}} \]
      6. frac-2negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{re}{\color{blue}{im}}}} \]
      7. lift-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{re}{\color{blue}{im}}}} \]
      8. lower-unsound-/.f6417.9%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{1}{\color{blue}{\frac{re}{im}}}} \]
    11. Applied rewrites17.9%

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

    if 0.0 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))))

    1. Initial program 41.6%

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

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re + im \cdot im}} - re\right)} \]
      3. add-flipN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re - \left(\mathsf{neg}\left(im \cdot im\right)\right)}} - re\right)} \]
      4. sub-flipN/A

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{im \cdot im}\right)\right)\right)\right)} - re\right)} \]
      6. sqr-abs-revN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{\left|im\right| \cdot \left|im\right|}\right)\right)\right)\right)} - re\right)} \]
      7. distribute-rgt-neg-inN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \left(\mathsf{neg}\left(\color{blue}{\left|im\right| \cdot \left(\mathsf{neg}\left(\left|im\right|\right)\right)}\right)\right)} - re\right)} \]
      8. distribute-lft-neg-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left(\mathsf{neg}\left(\left|im\right|\right)\right) \cdot \left(\mathsf{neg}\left(\left|im\right|\right)\right)}} - re\right)} \]
      9. fp-cancel-sign-sub-invN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re - \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left|im\right|\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\left|im\right|\right)\right)}} - re\right)} \]
      10. fp-cancel-sub-sign-invN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left|im\right|\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\left|im\right|\right)\right)}} - re\right)} \]
      11. lift-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{re \cdot re} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left|im\right|\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(\left|im\right|\right)\right)} - re\right)} \]
      12. remove-double-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \left(\mathsf{neg}\left(\color{blue}{\left|im\right|}\right)\right) \cdot \left(\mathsf{neg}\left(\left|im\right|\right)\right)} - re\right)} \]
      13. sqr-neg-revN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + \color{blue}{\left|im\right| \cdot \left|im\right|}} - re\right)} \]
      14. sqr-abs-revN/A

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

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

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

Alternative 2: 71.2% accurate, 0.3× speedup?

\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{if}\;t\_0 \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{1}{\frac{re}{im}}}\\ \mathbf{elif}\;t\_0 \leq 10^{+73}:\\ \;\;\;\;\sqrt{\left(\sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)} - re\right) \cdot 2} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot -2} \cdot 0.5\\ \end{array} \]
(FPCore (re im)
  :precision binary64
  (let* ((t_0
        (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))))
  (if (<= t_0 0.0)
    (* 0.5 (sqrt (* im (/ 1.0 (/ re im)))))
    (if (<= t_0 1e+73)
      (* (sqrt (* (- (sqrt (fma im im (* re re))) re) 2.0)) 0.5)
      (* (sqrt (* (- re im) -2.0)) 0.5)))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
	double tmp;
	if (t_0 <= 0.0) {
		tmp = 0.5 * sqrt((im * (1.0 / (re / im))));
	} else if (t_0 <= 1e+73) {
		tmp = sqrt(((sqrt(fma(im, im, (re * re))) - re) * 2.0)) * 0.5;
	} else {
		tmp = sqrt(((re - im) * -2.0)) * 0.5;
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))))
	tmp = 0.0
	if (t_0 <= 0.0)
		tmp = Float64(0.5 * sqrt(Float64(im * Float64(1.0 / Float64(re / im)))));
	elseif (t_0 <= 1e+73)
		tmp = Float64(sqrt(Float64(Float64(sqrt(fma(im, im, Float64(re * re))) - re) * 2.0)) * 0.5);
	else
		tmp = Float64(sqrt(Float64(Float64(re - im) * -2.0)) * 0.5);
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = 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]}, If[LessEqual[t$95$0, 0.0], N[(0.5 * N[Sqrt[N[(im * N[(1.0 / N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 1e+73], N[(N[Sqrt[N[(N[(N[Sqrt[N[(im * im + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[Sqrt[N[(N[(re - im), $MachinePrecision] * -2.0), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]]]
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\
\mathbf{if}\;t\_0 \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{1}{\frac{re}{im}}}\\

\mathbf{elif}\;t\_0 \leq 10^{+73}:\\
\;\;\;\;\sqrt{\left(\sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)} - re\right) \cdot 2} \cdot 0.5\\

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


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

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Taylor expanded in re around inf

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    6. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{\color{blue}{re}}} \]
      2. lower-pow.f6415.3%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{2}}{re}} \]
    7. Applied rewrites15.3%

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{\color{blue}{re}}} \]
      2. lift-pow.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{re}} \]
      3. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{im \cdot im}{re}} \]
      4. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      6. lower-/.f6418.0%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{im}{\color{blue}{re}}} \]
    9. Applied rewrites18.0%

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{im}{\color{blue}{re}}} \]
      2. frac-2negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{\mathsf{neg}\left(im\right)}{\color{blue}{\mathsf{neg}\left(re\right)}}} \]
      3. div-flipN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\color{blue}{\frac{\mathsf{neg}\left(re\right)}{\mathsf{neg}\left(im\right)}}}} \]
      4. lower-unsound-/.f32N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{\mathsf{neg}\left(re\right)}{\color{blue}{\mathsf{neg}\left(im\right)}}}} \]
      5. lower-/.f32N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{\mathsf{neg}\left(re\right)}{\color{blue}{\mathsf{neg}\left(im\right)}}}} \]
      6. frac-2negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{re}{\color{blue}{im}}}} \]
      7. lift-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \frac{1}{\frac{re}{\color{blue}{im}}}} \]
      8. lower-unsound-/.f6417.9%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{1}{\color{blue}{\frac{re}{im}}}} \]
    11. Applied rewrites17.9%

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

    if 0.0 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))) < 9.9999999999999998e72

    1. Initial program 41.6%

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

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

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

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

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

    if 9.9999999999999998e72 < (*.f64 #s(literal 1/2 binary64) (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))))

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Taylor expanded in im around inf

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

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

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \left(2 + -2 \cdot \color{blue}{\frac{re}{im}}\right)} \]
      4. lower-/.f6454.1%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(2 + -2 \cdot \frac{re}{im}\right)}} \]
    8. Step-by-step derivation
      1. lift-*.f64N/A

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

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

        \[\leadsto \color{blue}{\sqrt{im \cdot \left(2 + -2 \cdot \frac{re}{im}\right)} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{im \cdot \color{blue}{\left(2 + -2 \cdot \frac{re}{im}\right)}} \cdot \frac{1}{2} \]
      5. *-commutativeN/A

        \[\leadsto \sqrt{\left(2 + -2 \cdot \frac{re}{im}\right) \cdot \color{blue}{im}} \cdot \frac{1}{2} \]
      6. lower-*.f6454.1%

        \[\leadsto \sqrt{\left(2 + -2 \cdot \frac{re}{im}\right) \cdot \color{blue}{im}} \cdot 0.5 \]
      7. lift-+.f64N/A

        \[\leadsto \sqrt{\left(2 + -2 \cdot \frac{re}{im}\right) \cdot im} \cdot \frac{1}{2} \]
      8. +-commutativeN/A

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

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

        \[\leadsto \sqrt{\left(\frac{re}{im} \cdot -2 + 2\right) \cdot im} \cdot \frac{1}{2} \]
      11. lower-fma.f6454.1%

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im}, -2, 2\right) \cdot im} \cdot 0.5 \]
    9. Applied rewrites54.1%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\frac{re}{im}, -2, 2\right) \cdot im} \cdot 0.5} \]
    10. Applied rewrites55.1%

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

Alternative 3: 70.3% accurate, 1.1× speedup?

\[\begin{array}{l} \mathbf{if}\;re \leq -1.18 \cdot 10^{-77}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 6 \cdot 10^{+131}:\\ \;\;\;\;\sqrt{im + im} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{re}}\\ \end{array} \]
(FPCore (re im)
  :precision binary64
  (if (<= re -1.18e-77)
  (* 0.5 (sqrt (* -4.0 re)))
  (if (<= re 6e+131)
    (* (sqrt (+ im im)) 0.5)
    (* 0.5 (sqrt (* im (/ im re)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.18e-77) {
		tmp = 0.5 * sqrt((-4.0 * re));
	} else if (re <= 6e+131) {
		tmp = sqrt((im + im)) * 0.5;
	} else {
		tmp = 0.5 * sqrt((im * (im / re)));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-1.18d-77)) then
        tmp = 0.5d0 * sqrt(((-4.0d0) * re))
    else if (re <= 6d+131) then
        tmp = sqrt((im + im)) * 0.5d0
    else
        tmp = 0.5d0 * sqrt((im * (im / re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.18e-77) {
		tmp = 0.5 * Math.sqrt((-4.0 * re));
	} else if (re <= 6e+131) {
		tmp = Math.sqrt((im + im)) * 0.5;
	} else {
		tmp = 0.5 * Math.sqrt((im * (im / re)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.18e-77:
		tmp = 0.5 * math.sqrt((-4.0 * re))
	elif re <= 6e+131:
		tmp = math.sqrt((im + im)) * 0.5
	else:
		tmp = 0.5 * math.sqrt((im * (im / re)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.18e-77)
		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
	elseif (re <= 6e+131)
		tmp = Float64(sqrt(Float64(im + im)) * 0.5);
	else
		tmp = Float64(0.5 * sqrt(Float64(im * Float64(im / re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.18e-77)
		tmp = 0.5 * sqrt((-4.0 * re));
	elseif (re <= 6e+131)
		tmp = sqrt((im + im)) * 0.5;
	else
		tmp = 0.5 * sqrt((im * (im / re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.18e-77], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6e+131], N[(N[Sqrt[N[(im + im), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;re \leq -1.18 \cdot 10^{-77}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\

\mathbf{elif}\;re \leq 6 \cdot 10^{+131}:\\
\;\;\;\;\sqrt{im + im} \cdot 0.5\\

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


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

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

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

    if -1.1800000000000001e-77 < re < 6.0000000000000003e131

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Taylor expanded in im around inf

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    8. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot im}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot im} \cdot \frac{1}{2}} \]
      3. lower-*.f6452.7%

        \[\leadsto \color{blue}{\sqrt{2 \cdot im} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{im}} \cdot \frac{1}{2} \]
      5. count-2-revN/A

        \[\leadsto \sqrt{im + \color{blue}{im}} \cdot \frac{1}{2} \]
      6. lower-+.f6452.7%

        \[\leadsto \sqrt{im + \color{blue}{im}} \cdot 0.5 \]
    9. Applied rewrites52.7%

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

    if 6.0000000000000003e131 < re

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Taylor expanded in re around inf

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    6. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{\color{blue}{re}}} \]
      2. lower-pow.f6415.3%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{2}}{re}} \]
    7. Applied rewrites15.3%

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{\color{blue}{re}}} \]
      2. lift-pow.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{{im}^{2}}{re}} \]
      3. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{im \cdot im}{re}} \]
      4. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      5. lower-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      6. lower-/.f6418.0%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{im}{\color{blue}{re}}} \]
    9. Applied rewrites18.0%

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

Alternative 4: 64.0% accurate, 1.7× speedup?

\[\begin{array}{l} \mathbf{if}\;re \leq -1.18 \cdot 10^{-77}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im + im} \cdot 0.5\\ \end{array} \]
(FPCore (re im)
  :precision binary64
  (if (<= re -1.18e-77)
  (* 0.5 (sqrt (* -4.0 re)))
  (* (sqrt (+ im im)) 0.5)))
double code(double re, double im) {
	double tmp;
	if (re <= -1.18e-77) {
		tmp = 0.5 * sqrt((-4.0 * re));
	} else {
		tmp = sqrt((im + im)) * 0.5;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

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


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

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

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

    if -1.1800000000000001e-77 < re

    1. Initial program 41.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    4. Applied rewrites26.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Taylor expanded in im around inf

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    8. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot im}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot im} \cdot \frac{1}{2}} \]
      3. lower-*.f6452.7%

        \[\leadsto \color{blue}{\sqrt{2 \cdot im} \cdot 0.5} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{im}} \cdot \frac{1}{2} \]
      5. count-2-revN/A

        \[\leadsto \sqrt{im + \color{blue}{im}} \cdot \frac{1}{2} \]
      6. lower-+.f6452.7%

        \[\leadsto \sqrt{im + \color{blue}{im}} \cdot 0.5 \]
    9. Applied rewrites52.7%

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

Alternative 5: 52.7% accurate, 2.6× speedup?

\[\sqrt{im + im} \cdot 0.5 \]
(FPCore (re im)
  :precision binary64
  (* (sqrt (+ im im)) 0.5))
double code(double re, double im) {
	return sqrt((im + im)) * 0.5;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = sqrt((im + im)) * 0.5d0
end function
public static double code(double re, double im) {
	return Math.sqrt((im + im)) * 0.5;
}
def code(re, im):
	return math.sqrt((im + im)) * 0.5
function code(re, im)
	return Float64(sqrt(Float64(im + im)) * 0.5)
end
function tmp = code(re, im)
	tmp = sqrt((im + im)) * 0.5;
end
code[re_, im_] := N[(N[Sqrt[N[(im + im), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]
\sqrt{im + im} \cdot 0.5
Derivation
  1. Initial program 41.6%

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

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

      \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
  4. Applied rewrites26.3%

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
  5. Taylor expanded in im around inf

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

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

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
  8. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot im}} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{\sqrt{2 \cdot im} \cdot \frac{1}{2}} \]
    3. lower-*.f6452.7%

      \[\leadsto \color{blue}{\sqrt{2 \cdot im} \cdot 0.5} \]
    4. lift-*.f64N/A

      \[\leadsto \sqrt{2 \cdot \color{blue}{im}} \cdot \frac{1}{2} \]
    5. count-2-revN/A

      \[\leadsto \sqrt{im + \color{blue}{im}} \cdot \frac{1}{2} \]
    6. lower-+.f6452.7%

      \[\leadsto \sqrt{im + \color{blue}{im}} \cdot 0.5 \]
  9. Applied rewrites52.7%

    \[\leadsto \color{blue}{\sqrt{im + im} \cdot 0.5} \]
  10. Add Preprocessing

Reproduce

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