
(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:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (re im) :precision binary64 (if (<= re -3.7e+129) (* 0.5 (sqrt (* (/ im -1.0) (/ im re)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -3.7e+129) {
tmp = 0.5 * sqrt(((im / -1.0) * (im / re)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -3.7e+129) {
tmp = 0.5 * Math.sqrt(((im / -1.0) * (im / re)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.7e+129: tmp = 0.5 * math.sqrt(((im / -1.0) * (im / re))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.7e+129) tmp = Float64(0.5 * sqrt(Float64(Float64(im / -1.0) * Float64(im / re)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.7e+129) tmp = 0.5 * sqrt(((im / -1.0) * (im / re))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.7e+129], N[(0.5 * N[Sqrt[N[(N[(im / -1.0), $MachinePrecision] * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.7 \cdot 10^{+129}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{-1} \cdot \frac{im}{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -3.69999999999999978e129Initial program 5.6%
sqr-neg5.6%
+-commutative5.6%
sqr-neg5.6%
+-commutative5.6%
distribute-rgt-in5.6%
cancel-sign-sub5.6%
distribute-rgt-out--5.6%
sub-neg5.6%
remove-double-neg5.6%
+-commutative5.6%
Simplified23.0%
Taylor expanded in re around -inf 59.2%
mul-1-neg59.2%
distribute-neg-frac259.2%
Simplified59.2%
unpow259.2%
neg-mul-159.2%
times-frac67.4%
Applied egg-rr67.4%
if -3.69999999999999978e129 < re Initial program 46.6%
sqr-neg46.6%
+-commutative46.6%
sqr-neg46.6%
+-commutative46.6%
distribute-rgt-in46.6%
cancel-sign-sub46.6%
distribute-rgt-out--46.6%
sub-neg46.6%
remove-double-neg46.6%
+-commutative46.6%
Simplified88.3%
Final simplification85.4%
(FPCore (re im)
:precision binary64
(if (<= re -1.1e+129)
(* 0.5 (sqrt (* (/ im -1.0) (/ im re))))
(if (<= re 3.5e-55)
(* 0.5 (sqrt (+ (* im 2.0) (* re (+ 2.0 (/ re im))))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.1e+129) {
tmp = 0.5 * sqrt(((im / -1.0) * (im / re)));
} else if (re <= 3.5e-55) {
tmp = 0.5 * sqrt(((im * 2.0) + (re * (2.0 + (re / im)))));
} else {
tmp = 0.5 * (2.0 * 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 <= (-1.1d+129)) then
tmp = 0.5d0 * sqrt(((im / (-1.0d0)) * (im / re)))
else if (re <= 3.5d-55) then
tmp = 0.5d0 * sqrt(((im * 2.0d0) + (re * (2.0d0 + (re / im)))))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.1e+129) {
tmp = 0.5 * Math.sqrt(((im / -1.0) * (im / re)));
} else if (re <= 3.5e-55) {
tmp = 0.5 * Math.sqrt(((im * 2.0) + (re * (2.0 + (re / im)))));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.1e+129: tmp = 0.5 * math.sqrt(((im / -1.0) * (im / re))) elif re <= 3.5e-55: tmp = 0.5 * math.sqrt(((im * 2.0) + (re * (2.0 + (re / im))))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.1e+129) tmp = Float64(0.5 * sqrt(Float64(Float64(im / -1.0) * Float64(im / re)))); elseif (re <= 3.5e-55) tmp = Float64(0.5 * sqrt(Float64(Float64(im * 2.0) + Float64(re * Float64(2.0 + Float64(re / im)))))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.1e+129) tmp = 0.5 * sqrt(((im / -1.0) * (im / re))); elseif (re <= 3.5e-55) tmp = 0.5 * sqrt(((im * 2.0) + (re * (2.0 + (re / im))))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.1e+129], N[(0.5 * N[Sqrt[N[(N[(im / -1.0), $MachinePrecision] * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.5e-55], N[(0.5 * N[Sqrt[N[(N[(im * 2.0), $MachinePrecision] + N[(re * N[(2.0 + N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.1 \cdot 10^{+129}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{-1} \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 3.5 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2 + re \cdot \left(2 + \frac{re}{im}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.1e129Initial program 5.6%
sqr-neg5.6%
+-commutative5.6%
sqr-neg5.6%
+-commutative5.6%
distribute-rgt-in5.6%
cancel-sign-sub5.6%
distribute-rgt-out--5.6%
sub-neg5.6%
remove-double-neg5.6%
+-commutative5.6%
Simplified23.0%
Taylor expanded in re around -inf 59.2%
mul-1-neg59.2%
distribute-neg-frac259.2%
Simplified59.2%
unpow259.2%
neg-mul-159.2%
times-frac67.4%
Applied egg-rr67.4%
if -1.1e129 < re < 3.50000000000000025e-55Initial program 48.0%
sqr-neg48.0%
+-commutative48.0%
sqr-neg48.0%
+-commutative48.0%
distribute-rgt-in48.0%
cancel-sign-sub48.0%
distribute-rgt-out--48.0%
sub-neg48.0%
remove-double-neg48.0%
+-commutative48.0%
Simplified81.8%
Taylor expanded in re around 0 37.7%
if 3.50000000000000025e-55 < re Initial program 44.2%
sqr-neg44.2%
+-commutative44.2%
sqr-neg44.2%
+-commutative44.2%
distribute-rgt-in44.2%
cancel-sign-sub44.2%
distribute-rgt-out--44.2%
sub-neg44.2%
remove-double-neg44.2%
+-commutative44.2%
Simplified100.0%
Taylor expanded in re around inf 73.8%
*-commutative73.8%
unpow273.8%
rem-square-sqrt75.3%
Simplified75.3%
Final simplification53.5%
(FPCore (re im) :precision binary64 (if (<= re -7.8e+128) (* 0.5 (sqrt (* (/ im -1.0) (/ im re)))) (if (<= re 3.6e-55) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -7.8e+128) {
tmp = 0.5 * sqrt(((im / -1.0) * (im / re)));
} else if (re <= 3.6e-55) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * 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 <= (-7.8d+128)) then
tmp = 0.5d0 * sqrt(((im / (-1.0d0)) * (im / re)))
else if (re <= 3.6d-55) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -7.8e+128) {
tmp = 0.5 * Math.sqrt(((im / -1.0) * (im / re)));
} else if (re <= 3.6e-55) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7.8e+128: tmp = 0.5 * math.sqrt(((im / -1.0) * (im / re))) elif re <= 3.6e-55: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -7.8e+128) tmp = Float64(0.5 * sqrt(Float64(Float64(im / -1.0) * Float64(im / re)))); elseif (re <= 3.6e-55) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -7.8e+128) tmp = 0.5 * sqrt(((im / -1.0) * (im / re))); elseif (re <= 3.6e-55) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7.8e+128], N[(0.5 * N[Sqrt[N[(N[(im / -1.0), $MachinePrecision] * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.6e-55], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7.8 \cdot 10^{+128}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{-1} \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 3.6 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -7.7999999999999994e128Initial program 5.6%
sqr-neg5.6%
+-commutative5.6%
sqr-neg5.6%
+-commutative5.6%
distribute-rgt-in5.6%
cancel-sign-sub5.6%
distribute-rgt-out--5.6%
sub-neg5.6%
remove-double-neg5.6%
+-commutative5.6%
Simplified23.0%
Taylor expanded in re around -inf 59.2%
mul-1-neg59.2%
distribute-neg-frac259.2%
Simplified59.2%
unpow259.2%
neg-mul-159.2%
times-frac67.4%
Applied egg-rr67.4%
if -7.7999999999999994e128 < re < 3.6000000000000001e-55Initial program 48.0%
sqr-neg48.0%
+-commutative48.0%
sqr-neg48.0%
+-commutative48.0%
distribute-rgt-in48.0%
cancel-sign-sub48.0%
distribute-rgt-out--48.0%
sub-neg48.0%
remove-double-neg48.0%
+-commutative48.0%
Simplified81.8%
Taylor expanded in re around 0 37.1%
*-commutative37.1%
Simplified37.1%
if 3.6000000000000001e-55 < re Initial program 44.2%
sqr-neg44.2%
+-commutative44.2%
sqr-neg44.2%
+-commutative44.2%
distribute-rgt-in44.2%
cancel-sign-sub44.2%
distribute-rgt-out--44.2%
sub-neg44.2%
remove-double-neg44.2%
+-commutative44.2%
Simplified100.0%
Taylor expanded in re around inf 73.8%
*-commutative73.8%
unpow273.8%
rem-square-sqrt75.3%
Simplified75.3%
Final simplification53.2%
(FPCore (re im) :precision binary64 (if (<= re 1.9e-55) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 1.9e-55) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * 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 <= 1.9d-55) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1.9e-55) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.9e-55: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.9e-55) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.9e-55) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.9e-55], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.9 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 1.8999999999999998e-55Initial program 39.4%
sqr-neg39.4%
+-commutative39.4%
sqr-neg39.4%
+-commutative39.4%
distribute-rgt-in39.4%
cancel-sign-sub39.4%
distribute-rgt-out--39.4%
sub-neg39.4%
remove-double-neg39.4%
+-commutative39.4%
Simplified69.8%
Taylor expanded in re around 0 32.5%
*-commutative32.5%
Simplified32.5%
if 1.8999999999999998e-55 < re Initial program 44.2%
sqr-neg44.2%
+-commutative44.2%
sqr-neg44.2%
+-commutative44.2%
distribute-rgt-in44.2%
cancel-sign-sub44.2%
distribute-rgt-out--44.2%
sub-neg44.2%
remove-double-neg44.2%
+-commutative44.2%
Simplified100.0%
Taylor expanded in re around inf 73.8%
*-commutative73.8%
unpow273.8%
rem-square-sqrt75.3%
Simplified75.3%
Final simplification45.7%
(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}
Initial program 40.9%
sqr-neg40.9%
+-commutative40.9%
sqr-neg40.9%
+-commutative40.9%
distribute-rgt-in40.9%
cancel-sign-sub40.9%
distribute-rgt-out--40.9%
sub-neg40.9%
remove-double-neg40.9%
+-commutative40.9%
Simplified79.1%
Taylor expanded in re around 0 28.6%
*-commutative28.6%
Simplified28.6%
Final simplification28.6%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + 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 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t\_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t\_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2024130
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))