
(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 6 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.2e+64) (* 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.2e+64) {
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.2e+64) {
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.2e+64: 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.2e+64) 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.2e+64) 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.2e+64], 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.2 \cdot 10^{+64}:\\
\;\;\;\;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.20000000000000019e64Initial program 9.8%
sqr-neg9.8%
+-commutative9.8%
sqr-neg9.8%
+-commutative9.8%
distribute-rgt-in9.8%
cancel-sign-sub9.8%
distribute-rgt-out--9.8%
sub-neg9.8%
remove-double-neg9.8%
+-commutative9.8%
hypot-define30.5%
Simplified30.5%
Taylor expanded in re around -inf 52.7%
mul-1-neg52.7%
distribute-neg-frac252.7%
Simplified52.7%
unpow252.7%
neg-mul-152.7%
times-frac62.7%
Applied egg-rr62.7%
if -3.20000000000000019e64 < re Initial program 49.6%
sqr-neg49.6%
+-commutative49.6%
sqr-neg49.6%
+-commutative49.6%
distribute-rgt-in49.6%
cancel-sign-sub49.6%
distribute-rgt-out--49.6%
sub-neg49.6%
remove-double-neg49.6%
+-commutative49.6%
hypot-define92.3%
Simplified92.3%
Final simplification85.5%
(FPCore (re im)
:precision binary64
(if (<= re 6.7e-50)
(* 0.5 (sqrt (* im 2.0)))
(if (or (<= re 2.05e-38) (not (<= re 0.0038)))
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (re <= 6.7e-50) {
tmp = 0.5 * sqrt((im * 2.0));
} else if ((re <= 2.05e-38) || !(re <= 0.0038)) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 6.7d-50) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else if ((re <= 2.05d-38) .or. (.not. (re <= 0.0038d0))) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 6.7e-50) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else if ((re <= 2.05e-38) || !(re <= 0.0038)) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 6.7e-50: tmp = 0.5 * math.sqrt((im * 2.0)) elif (re <= 2.05e-38) or not (re <= 0.0038): tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (re <= 6.7e-50) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); elseif ((re <= 2.05e-38) || !(re <= 0.0038)) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 6.7e-50) tmp = 0.5 * sqrt((im * 2.0)); elseif ((re <= 2.05e-38) || ~((re <= 0.0038))) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 6.7e-50], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 2.05e-38], N[Not[LessEqual[re, 0.0038]], $MachinePrecision]], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 6.7 \cdot 10^{-50}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{elif}\;re \leq 2.05 \cdot 10^{-38} \lor \neg \left(re \leq 0.0038\right):\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if re < 6.7000000000000005e-50Initial program 38.6%
sqr-neg38.6%
+-commutative38.6%
sqr-neg38.6%
+-commutative38.6%
distribute-rgt-in38.6%
cancel-sign-sub38.6%
distribute-rgt-out--38.6%
sub-neg38.6%
remove-double-neg38.6%
+-commutative38.6%
hypot-define68.4%
Simplified68.4%
Taylor expanded in re around 0 32.1%
*-commutative32.1%
Simplified32.1%
if 6.7000000000000005e-50 < re < 2.0499999999999999e-38 or 0.00379999999999999999 < re Initial program 42.0%
sqr-neg42.0%
+-commutative42.0%
sqr-neg42.0%
+-commutative42.0%
distribute-rgt-in42.0%
cancel-sign-sub42.0%
distribute-rgt-out--42.0%
sub-neg42.0%
remove-double-neg42.0%
+-commutative42.0%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 82.8%
*-commutative82.8%
unpow282.8%
rem-square-sqrt84.5%
Simplified84.5%
if 2.0499999999999999e-38 < re < 0.00379999999999999999Initial program 72.7%
sqr-neg72.7%
+-commutative72.7%
sqr-neg72.7%
+-commutative72.7%
distribute-rgt-in72.7%
cancel-sign-sub72.7%
distribute-rgt-out--72.7%
sub-neg72.7%
remove-double-neg72.7%
+-commutative72.7%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around 0 31.5%
distribute-lft-out31.5%
*-commutative31.5%
Simplified31.5%
Final simplification46.6%
(FPCore (re im) :precision binary64 (if (or (<= re 1.2e-50) (and (not (<= re 1.3e-37)) (<= re 72.0))) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
double code(double re, double im) {
double tmp;
if ((re <= 1.2e-50) || (!(re <= 1.3e-37) && (re <= 72.0))) {
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.2d-50) .or. (.not. (re <= 1.3d-37)) .and. (re <= 72.0d0)) 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.2e-50) || (!(re <= 1.3e-37) && (re <= 72.0))) {
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.2e-50) or (not (re <= 1.3e-37) and (re <= 72.0)): 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.2e-50) || (!(re <= 1.3e-37) && (re <= 72.0))) 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.2e-50) || (~((re <= 1.3e-37)) && (re <= 72.0))) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[re, 1.2e-50], And[N[Not[LessEqual[re, 1.3e-37]], $MachinePrecision], LessEqual[re, 72.0]]], 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.2 \cdot 10^{-50} \lor \neg \left(re \leq 1.3 \cdot 10^{-37}\right) \land re \leq 72:\\
\;\;\;\;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.20000000000000001e-50 or 1.2999999999999999e-37 < re < 72Initial program 39.9%
sqr-neg39.9%
+-commutative39.9%
sqr-neg39.9%
+-commutative39.9%
distribute-rgt-in39.9%
cancel-sign-sub39.9%
distribute-rgt-out--39.9%
sub-neg39.9%
remove-double-neg39.9%
+-commutative39.9%
hypot-define69.6%
Simplified69.6%
Taylor expanded in re around 0 32.0%
*-commutative32.0%
Simplified32.0%
if 1.20000000000000001e-50 < re < 1.2999999999999999e-37 or 72 < re Initial program 42.0%
sqr-neg42.0%
+-commutative42.0%
sqr-neg42.0%
+-commutative42.0%
distribute-rgt-in42.0%
cancel-sign-sub42.0%
distribute-rgt-out--42.0%
sub-neg42.0%
remove-double-neg42.0%
+-commutative42.0%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 82.8%
*-commutative82.8%
unpow282.8%
rem-square-sqrt84.5%
Simplified84.5%
Final simplification46.6%
(FPCore (re im)
:precision binary64
(if (<= re -5e+61)
(* 0.5 (sqrt (* (/ im -1.0) (/ im re))))
(if (<= re 90.0)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -5e+61) {
tmp = 0.5 * sqrt(((im / -1.0) * (im / re)));
} else if (re <= 90.0) {
tmp = 0.5 * sqrt((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 <= (-5d+61)) then
tmp = 0.5d0 * sqrt(((im / (-1.0d0)) * (im / re)))
else if (re <= 90.0d0) then
tmp = 0.5d0 * sqrt((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 <= -5e+61) {
tmp = 0.5 * Math.sqrt(((im / -1.0) * (im / re)));
} else if (re <= 90.0) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e+61: tmp = 0.5 * math.sqrt(((im / -1.0) * (im / re))) elif re <= 90.0: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e+61) tmp = Float64(0.5 * sqrt(Float64(Float64(im / -1.0) * Float64(im / re)))); elseif (re <= 90.0) tmp = Float64(0.5 * sqrt(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 <= -5e+61) tmp = 0.5 * sqrt(((im / -1.0) * (im / re))); elseif (re <= 90.0) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e+61], N[(0.5 * N[Sqrt[N[(N[(im / -1.0), $MachinePrecision] * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 90.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $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 -5 \cdot 10^{+61}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{-1} \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 90:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -5.00000000000000018e61Initial program 9.7%
sqr-neg9.7%
+-commutative9.7%
sqr-neg9.7%
+-commutative9.7%
distribute-rgt-in9.7%
cancel-sign-sub9.7%
distribute-rgt-out--9.7%
sub-neg9.7%
remove-double-neg9.7%
+-commutative9.7%
hypot-define30.1%
Simplified30.1%
Taylor expanded in re around -inf 52.0%
mul-1-neg52.0%
distribute-neg-frac252.0%
Simplified52.0%
unpow252.0%
neg-mul-152.0%
times-frac61.8%
Applied egg-rr61.8%
if -5.00000000000000018e61 < re < 90Initial program 56.1%
sqr-neg56.1%
+-commutative56.1%
sqr-neg56.1%
+-commutative56.1%
distribute-rgt-in56.1%
cancel-sign-sub56.1%
distribute-rgt-out--56.1%
sub-neg56.1%
remove-double-neg56.1%
+-commutative56.1%
hypot-define89.0%
Simplified89.0%
Taylor expanded in re around 0 41.6%
distribute-lft-out41.6%
*-commutative41.6%
Simplified41.6%
if 90 < re Initial program 37.6%
sqr-neg37.6%
+-commutative37.6%
sqr-neg37.6%
+-commutative37.6%
distribute-rgt-in37.6%
cancel-sign-sub37.6%
distribute-rgt-out--37.6%
sub-neg37.6%
remove-double-neg37.6%
+-commutative37.6%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 81.7%
*-commutative81.7%
unpow281.7%
rem-square-sqrt83.4%
Simplified83.4%
Final simplification57.1%
(FPCore (re im) :precision binary64 (if (<= im 6e-228) (* 0.5 (sqrt (* re 0.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (im <= 6e-228) {
tmp = 0.5 * sqrt((re * 0.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 (im <= 6d-228) then
tmp = 0.5d0 * sqrt((re * 0.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 (im <= 6e-228) {
tmp = 0.5 * Math.sqrt((re * 0.0));
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 6e-228: tmp = 0.5 * math.sqrt((re * 0.0)) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 6e-228) tmp = Float64(0.5 * sqrt(Float64(re * 0.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 (im <= 6e-228) tmp = 0.5 * sqrt((re * 0.0)); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 6e-228], N[(0.5 * N[Sqrt[N[(re * 0.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 6 \cdot 10^{-228}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot 0}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < 5.9999999999999999e-228Initial program 40.8%
sqr-neg40.8%
+-commutative40.8%
sqr-neg40.8%
+-commutative40.8%
distribute-rgt-in40.8%
cancel-sign-sub40.8%
distribute-rgt-out--40.8%
sub-neg40.8%
remove-double-neg40.8%
+-commutative40.8%
hypot-define77.9%
Simplified77.9%
hypot-define40.8%
+-commutative40.8%
distribute-rgt-in40.8%
add-sqr-sqrt39.8%
*-commutative39.8%
fma-define38.8%
*-commutative38.8%
hypot-define38.8%
*-commutative38.8%
hypot-define72.1%
*-commutative72.1%
Applied egg-rr72.1%
Taylor expanded in re around -inf 2.9%
mul-1-neg2.9%
distribute-rgt-neg-in2.9%
unpow22.9%
rem-square-sqrt8.4%
metadata-eval8.4%
metadata-eval8.4%
Simplified8.4%
if 5.9999999999999999e-228 < im Initial program 40.0%
sqr-neg40.0%
+-commutative40.0%
sqr-neg40.0%
+-commutative40.0%
distribute-rgt-in40.0%
cancel-sign-sub40.0%
distribute-rgt-out--40.0%
sub-neg40.0%
remove-double-neg40.0%
+-commutative40.0%
hypot-define78.2%
Simplified78.2%
Taylor expanded in re around 0 56.0%
*-commutative56.0%
Simplified56.0%
Final simplification31.1%
(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.5%
sqr-neg40.5%
+-commutative40.5%
sqr-neg40.5%
+-commutative40.5%
distribute-rgt-in40.5%
cancel-sign-sub40.5%
distribute-rgt-out--40.5%
sub-neg40.5%
remove-double-neg40.5%
+-commutative40.5%
hypot-define78.0%
Simplified78.0%
Taylor expanded in re around 0 27.0%
*-commutative27.0%
Simplified27.0%
Final simplification27.0%
(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 2024067
(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)))))