
(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 8 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 (sqrt (+ (* re re) (* im im)))) 0.0) (* 0.5 (sqrt (* im (/ im (- re))))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * sqrt((im * (im / -re)));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((re + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * Math.sqrt((im * (im / -re)));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if (re + math.sqrt(((re * re) + (im * im)))) <= 0.0: tmp = 0.5 * math.sqrt((im * (im / -re))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(im * Float64(im / Float64(-re))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) tmp = 0.5 * sqrt((im * (im / -re))); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[(im * N[(im / (-re)), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{-re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 3.2%
+-commutative3.2%
hypot-def16.5%
Simplified16.5%
Taylor expanded in re around -inf 31.0%
associate-*r/31.0%
neg-mul-131.0%
unpow231.0%
distribute-rgt-neg-in31.0%
Simplified31.0%
Taylor expanded in im around 0 31.0%
*-commutative31.0%
unpow231.0%
metadata-eval31.0%
times-frac31.0%
*-commutative31.0%
neg-mul-131.0%
associate-*r/30.9%
associate-*l*56.4%
associate-*r/56.4%
*-rgt-identity56.4%
Simplified56.4%
if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 47.5%
+-commutative47.5%
hypot-def92.4%
Simplified92.4%
add-sqr-sqrt91.8%
sqrt-unprod92.4%
*-commutative92.4%
*-commutative92.4%
swap-sqr92.4%
add-sqr-sqrt92.4%
*-commutative92.4%
metadata-eval92.4%
Applied egg-rr92.4%
associate-*l*92.4%
metadata-eval92.4%
Simplified92.4%
Final simplification87.8%
(FPCore (re im)
:precision binary64
(if (<= im -5.8e-118)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im -1.45e-237)
(sqrt re)
(if (<= im -1.7e-277)
(* 0.5 (sqrt (* im (/ im (- re)))))
(if (<= im 1e-305)
(sqrt re)
(if (<= im 2.55e-197)
(* 0.5 (/ im (sqrt (- re))))
(if (<= im 1.6e-112)
(sqrt re)
(* 0.5 (sqrt (* 2.0 (+ re im)))))))))))
double code(double re, double im) {
double tmp;
if (im <= -5.8e-118) {
tmp = 0.5 * sqrt((2.0 * (re - im)));
} else if (im <= -1.45e-237) {
tmp = sqrt(re);
} else if (im <= -1.7e-277) {
tmp = 0.5 * sqrt((im * (im / -re)));
} else if (im <= 1e-305) {
tmp = sqrt(re);
} else if (im <= 2.55e-197) {
tmp = 0.5 * (im / sqrt(-re));
} else if (im <= 1.6e-112) {
tmp = 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 (im <= (-5.8d-118)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
else if (im <= (-1.45d-237)) then
tmp = sqrt(re)
else if (im <= (-1.7d-277)) then
tmp = 0.5d0 * sqrt((im * (im / -re)))
else if (im <= 1d-305) then
tmp = sqrt(re)
else if (im <= 2.55d-197) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (im <= 1.6d-112) then
tmp = 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 (im <= -5.8e-118) {
tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
} else if (im <= -1.45e-237) {
tmp = Math.sqrt(re);
} else if (im <= -1.7e-277) {
tmp = 0.5 * Math.sqrt((im * (im / -re)));
} else if (im <= 1e-305) {
tmp = Math.sqrt(re);
} else if (im <= 2.55e-197) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (im <= 1.6e-112) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -5.8e-118: tmp = 0.5 * math.sqrt((2.0 * (re - im))) elif im <= -1.45e-237: tmp = math.sqrt(re) elif im <= -1.7e-277: tmp = 0.5 * math.sqrt((im * (im / -re))) elif im <= 1e-305: tmp = math.sqrt(re) elif im <= 2.55e-197: tmp = 0.5 * (im / math.sqrt(-re)) elif im <= 1.6e-112: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -5.8e-118) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im)))); elseif (im <= -1.45e-237) tmp = sqrt(re); elseif (im <= -1.7e-277) tmp = Float64(0.5 * sqrt(Float64(im * Float64(im / Float64(-re))))); elseif (im <= 1e-305) tmp = sqrt(re); elseif (im <= 2.55e-197) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (im <= 1.6e-112) tmp = 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 (im <= -5.8e-118) tmp = 0.5 * sqrt((2.0 * (re - im))); elseif (im <= -1.45e-237) tmp = sqrt(re); elseif (im <= -1.7e-277) tmp = 0.5 * sqrt((im * (im / -re))); elseif (im <= 1e-305) tmp = sqrt(re); elseif (im <= 2.55e-197) tmp = 0.5 * (im / sqrt(-re)); elseif (im <= 1.6e-112) tmp = sqrt(re); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -5.8e-118], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, -1.45e-237], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, -1.7e-277], N[(0.5 * N[Sqrt[N[(im * N[(im / (-re)), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1e-305], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 2.55e-197], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.6e-112], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -5.8 \cdot 10^{-118}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq -1.45 \cdot 10^{-237}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq -1.7 \cdot 10^{-277}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{-re}}\\
\mathbf{elif}\;im \leq 10^{-305}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq 2.55 \cdot 10^{-197}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;im \leq 1.6 \cdot 10^{-112}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -5.79999999999999961e-118Initial program 43.2%
+-commutative43.2%
hypot-def90.7%
Simplified90.7%
Taylor expanded in im around -inf 77.5%
mul-1-neg77.5%
sub-neg77.5%
Simplified77.5%
if -5.79999999999999961e-118 < im < -1.45000000000000005e-237 or -1.69999999999999991e-277 < im < 9.99999999999999996e-306 or 2.5500000000000001e-197 < im < 1.59999999999999997e-112Initial program 47.6%
+-commutative47.6%
hypot-def80.9%
Simplified80.9%
Taylor expanded in im around 0 60.2%
associate-*r*60.2%
unpow260.2%
rem-square-sqrt61.4%
metadata-eval61.4%
*-lft-identity61.4%
Simplified61.4%
if -1.45000000000000005e-237 < im < -1.69999999999999991e-277Initial program 14.2%
+-commutative14.2%
hypot-def47.5%
Simplified47.5%
Taylor expanded in re around -inf 26.1%
associate-*r/26.1%
neg-mul-126.1%
unpow226.1%
distribute-rgt-neg-in26.1%
Simplified26.1%
Taylor expanded in im around 0 26.1%
*-commutative26.1%
unpow226.1%
metadata-eval26.1%
times-frac26.1%
*-commutative26.1%
neg-mul-126.1%
associate-*r/26.1%
associate-*l*57.4%
associate-*r/57.2%
*-rgt-identity57.2%
Simplified57.2%
if 9.99999999999999996e-306 < im < 2.5500000000000001e-197Initial program 12.2%
+-commutative12.2%
hypot-def57.2%
Simplified57.2%
Taylor expanded in re around -inf 26.6%
associate-*r/26.6%
neg-mul-126.6%
unpow226.6%
distribute-rgt-neg-in26.6%
Simplified26.6%
frac-2neg26.6%
sqrt-div25.8%
distribute-rgt-neg-out25.8%
remove-double-neg25.8%
sqrt-unprod68.6%
add-sqr-sqrt68.8%
Applied egg-rr68.8%
if 1.59999999999999997e-112 < im Initial program 47.7%
+-commutative47.7%
hypot-def85.4%
Simplified85.4%
Taylor expanded in re around 0 73.7%
distribute-lft-out73.7%
+-commutative73.7%
*-commutative73.7%
+-commutative73.7%
Simplified73.7%
Final simplification71.5%
(FPCore (re im)
:precision binary64
(if (<= im -1.35e-117)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im 2.65e-306)
(sqrt re)
(if (<= im 9.5e-190)
(* 0.5 (/ im (sqrt (- re))))
(if (<= im 8.4e-113) (sqrt re) (* 0.5 (sqrt (* 2.0 (+ re im)))))))))
double code(double re, double im) {
double tmp;
if (im <= -1.35e-117) {
tmp = 0.5 * sqrt((2.0 * (re - im)));
} else if (im <= 2.65e-306) {
tmp = sqrt(re);
} else if (im <= 9.5e-190) {
tmp = 0.5 * (im / sqrt(-re));
} else if (im <= 8.4e-113) {
tmp = 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 (im <= (-1.35d-117)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
else if (im <= 2.65d-306) then
tmp = sqrt(re)
else if (im <= 9.5d-190) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (im <= 8.4d-113) then
tmp = 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 (im <= -1.35e-117) {
tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
} else if (im <= 2.65e-306) {
tmp = Math.sqrt(re);
} else if (im <= 9.5e-190) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (im <= 8.4e-113) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.35e-117: tmp = 0.5 * math.sqrt((2.0 * (re - im))) elif im <= 2.65e-306: tmp = math.sqrt(re) elif im <= 9.5e-190: tmp = 0.5 * (im / math.sqrt(-re)) elif im <= 8.4e-113: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.35e-117) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im)))); elseif (im <= 2.65e-306) tmp = sqrt(re); elseif (im <= 9.5e-190) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (im <= 8.4e-113) tmp = 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 (im <= -1.35e-117) tmp = 0.5 * sqrt((2.0 * (re - im))); elseif (im <= 2.65e-306) tmp = sqrt(re); elseif (im <= 9.5e-190) tmp = 0.5 * (im / sqrt(-re)); elseif (im <= 8.4e-113) tmp = sqrt(re); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.35e-117], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 2.65e-306], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 9.5e-190], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 8.4e-113], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.35 \cdot 10^{-117}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 2.65 \cdot 10^{-306}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq 9.5 \cdot 10^{-190}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;im \leq 8.4 \cdot 10^{-113}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -1.35000000000000001e-117Initial program 43.2%
+-commutative43.2%
hypot-def90.7%
Simplified90.7%
Taylor expanded in im around -inf 77.5%
mul-1-neg77.5%
sub-neg77.5%
Simplified77.5%
if -1.35000000000000001e-117 < im < 2.6499999999999999e-306 or 9.50000000000000055e-190 < im < 8.39999999999999999e-113Initial program 42.8%
+-commutative42.8%
hypot-def76.1%
Simplified76.1%
Taylor expanded in im around 0 53.2%
associate-*r*53.2%
unpow253.2%
rem-square-sqrt54.3%
metadata-eval54.3%
*-lft-identity54.3%
Simplified54.3%
if 2.6499999999999999e-306 < im < 9.50000000000000055e-190Initial program 12.2%
+-commutative12.2%
hypot-def57.2%
Simplified57.2%
Taylor expanded in re around -inf 26.6%
associate-*r/26.6%
neg-mul-126.6%
unpow226.6%
distribute-rgt-neg-in26.6%
Simplified26.6%
frac-2neg26.6%
sqrt-div25.8%
distribute-rgt-neg-out25.8%
remove-double-neg25.8%
sqrt-unprod68.6%
add-sqr-sqrt68.8%
Applied egg-rr68.8%
if 8.39999999999999999e-113 < im Initial program 47.7%
+-commutative47.7%
hypot-def85.4%
Simplified85.4%
Taylor expanded in re around 0 73.7%
distribute-lft-out73.7%
+-commutative73.7%
*-commutative73.7%
+-commutative73.7%
Simplified73.7%
Final simplification70.0%
(FPCore (re im)
:precision binary64
(if (<= im -1.75e-118)
(* 0.5 (sqrt (* im -2.0)))
(if (<= im 7.8e-306)
(sqrt re)
(if (<= im 5.8e-196)
(* 0.5 (/ im (sqrt (- re))))
(if (<= im 7.2e-113) (sqrt re) (* 0.5 (sqrt (* im 2.0))))))))
double code(double re, double im) {
double tmp;
if (im <= -1.75e-118) {
tmp = 0.5 * sqrt((im * -2.0));
} else if (im <= 7.8e-306) {
tmp = sqrt(re);
} else if (im <= 5.8e-196) {
tmp = 0.5 * (im / sqrt(-re));
} else if (im <= 7.2e-113) {
tmp = sqrt(re);
} 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 <= (-1.75d-118)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else if (im <= 7.8d-306) then
tmp = sqrt(re)
else if (im <= 5.8d-196) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (im <= 7.2d-113) then
tmp = sqrt(re)
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 <= -1.75e-118) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else if (im <= 7.8e-306) {
tmp = Math.sqrt(re);
} else if (im <= 5.8e-196) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (im <= 7.2e-113) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.75e-118: tmp = 0.5 * math.sqrt((im * -2.0)) elif im <= 7.8e-306: tmp = math.sqrt(re) elif im <= 5.8e-196: tmp = 0.5 * (im / math.sqrt(-re)) elif im <= 7.2e-113: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.75e-118) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); elseif (im <= 7.8e-306) tmp = sqrt(re); elseif (im <= 5.8e-196) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (im <= 7.2e-113) tmp = sqrt(re); 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 <= -1.75e-118) tmp = 0.5 * sqrt((im * -2.0)); elseif (im <= 7.8e-306) tmp = sqrt(re); elseif (im <= 5.8e-196) tmp = 0.5 * (im / sqrt(-re)); elseif (im <= 7.2e-113) tmp = sqrt(re); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.75e-118], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 7.8e-306], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 5.8e-196], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 7.2e-113], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.75 \cdot 10^{-118}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{elif}\;im \leq 7.8 \cdot 10^{-306}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq 5.8 \cdot 10^{-196}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;im \leq 7.2 \cdot 10^{-113}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < -1.75e-118Initial program 43.2%
+-commutative43.2%
hypot-def90.7%
Simplified90.7%
Taylor expanded in im around -inf 75.9%
*-commutative75.9%
Simplified75.9%
if -1.75e-118 < im < 7.799999999999999e-306 or 5.79999999999999974e-196 < im < 7.1999999999999995e-113Initial program 42.8%
+-commutative42.8%
hypot-def76.1%
Simplified76.1%
Taylor expanded in im around 0 53.2%
associate-*r*53.2%
unpow253.2%
rem-square-sqrt54.3%
metadata-eval54.3%
*-lft-identity54.3%
Simplified54.3%
if 7.799999999999999e-306 < im < 5.79999999999999974e-196Initial program 12.2%
+-commutative12.2%
hypot-def57.2%
Simplified57.2%
Taylor expanded in re around -inf 26.6%
associate-*r/26.6%
neg-mul-126.6%
unpow226.6%
distribute-rgt-neg-in26.6%
Simplified26.6%
frac-2neg26.6%
sqrt-div25.8%
distribute-rgt-neg-out25.8%
remove-double-neg25.8%
sqrt-unprod68.6%
add-sqr-sqrt68.8%
Applied egg-rr68.8%
if 7.1999999999999995e-113 < im Initial program 47.7%
+-commutative47.7%
hypot-def85.4%
Simplified85.4%
Taylor expanded in re around 0 72.7%
*-commutative72.7%
Simplified72.7%
Final simplification69.1%
(FPCore (re im)
:precision binary64
(if (<= im -1.8e-118)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im 2.6e-305)
(sqrt re)
(if (<= im 2.7e-187)
(* 0.5 (/ im (sqrt (- re))))
(if (<= im 1.5e-111) (sqrt re) (* 0.5 (sqrt (* im 2.0))))))))
double code(double re, double im) {
double tmp;
if (im <= -1.8e-118) {
tmp = 0.5 * sqrt((2.0 * (re - im)));
} else if (im <= 2.6e-305) {
tmp = sqrt(re);
} else if (im <= 2.7e-187) {
tmp = 0.5 * (im / sqrt(-re));
} else if (im <= 1.5e-111) {
tmp = sqrt(re);
} 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 <= (-1.8d-118)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
else if (im <= 2.6d-305) then
tmp = sqrt(re)
else if (im <= 2.7d-187) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (im <= 1.5d-111) then
tmp = sqrt(re)
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 <= -1.8e-118) {
tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
} else if (im <= 2.6e-305) {
tmp = Math.sqrt(re);
} else if (im <= 2.7e-187) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (im <= 1.5e-111) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.8e-118: tmp = 0.5 * math.sqrt((2.0 * (re - im))) elif im <= 2.6e-305: tmp = math.sqrt(re) elif im <= 2.7e-187: tmp = 0.5 * (im / math.sqrt(-re)) elif im <= 1.5e-111: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.8e-118) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im)))); elseif (im <= 2.6e-305) tmp = sqrt(re); elseif (im <= 2.7e-187) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (im <= 1.5e-111) tmp = sqrt(re); 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 <= -1.8e-118) tmp = 0.5 * sqrt((2.0 * (re - im))); elseif (im <= 2.6e-305) tmp = sqrt(re); elseif (im <= 2.7e-187) tmp = 0.5 * (im / sqrt(-re)); elseif (im <= 1.5e-111) tmp = sqrt(re); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.8e-118], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 2.6e-305], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 2.7e-187], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.5e-111], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.8 \cdot 10^{-118}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 2.6 \cdot 10^{-305}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq 2.7 \cdot 10^{-187}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;im \leq 1.5 \cdot 10^{-111}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < -1.8000000000000001e-118Initial program 43.2%
+-commutative43.2%
hypot-def90.7%
Simplified90.7%
Taylor expanded in im around -inf 77.5%
mul-1-neg77.5%
sub-neg77.5%
Simplified77.5%
if -1.8000000000000001e-118 < im < 2.6000000000000002e-305 or 2.7000000000000001e-187 < im < 1.50000000000000004e-111Initial program 42.8%
+-commutative42.8%
hypot-def76.1%
Simplified76.1%
Taylor expanded in im around 0 53.2%
associate-*r*53.2%
unpow253.2%
rem-square-sqrt54.3%
metadata-eval54.3%
*-lft-identity54.3%
Simplified54.3%
if 2.6000000000000002e-305 < im < 2.7000000000000001e-187Initial program 12.2%
+-commutative12.2%
hypot-def57.2%
Simplified57.2%
Taylor expanded in re around -inf 26.6%
associate-*r/26.6%
neg-mul-126.6%
unpow226.6%
distribute-rgt-neg-in26.6%
Simplified26.6%
frac-2neg26.6%
sqrt-div25.8%
distribute-rgt-neg-out25.8%
remove-double-neg25.8%
sqrt-unprod68.6%
add-sqr-sqrt68.8%
Applied egg-rr68.8%
if 1.50000000000000004e-111 < im Initial program 47.7%
+-commutative47.7%
hypot-def85.4%
Simplified85.4%
Taylor expanded in re around 0 72.7%
*-commutative72.7%
Simplified72.7%
Final simplification69.7%
(FPCore (re im) :precision binary64 (if (<= im -2e-118) (* 0.5 (sqrt (* im -2.0))) (if (<= im 3.5e-110) (sqrt re) (* 0.5 (sqrt (* im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= -2e-118) {
tmp = 0.5 * sqrt((im * -2.0));
} else if (im <= 3.5e-110) {
tmp = sqrt(re);
} 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 <= (-2d-118)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else if (im <= 3.5d-110) then
tmp = sqrt(re)
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 <= -2e-118) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else if (im <= 3.5e-110) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -2e-118: tmp = 0.5 * math.sqrt((im * -2.0)) elif im <= 3.5e-110: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= -2e-118) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); elseif (im <= 3.5e-110) tmp = sqrt(re); 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 <= -2e-118) tmp = 0.5 * sqrt((im * -2.0)); elseif (im <= 3.5e-110) tmp = sqrt(re); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -2e-118], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 3.5e-110], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -2 \cdot 10^{-118}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{elif}\;im \leq 3.5 \cdot 10^{-110}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < -1.99999999999999997e-118Initial program 43.2%
+-commutative43.2%
hypot-def90.7%
Simplified90.7%
Taylor expanded in im around -inf 75.9%
*-commutative75.9%
Simplified75.9%
if -1.99999999999999997e-118 < im < 3.49999999999999974e-110Initial program 34.8%
+-commutative34.8%
hypot-def71.1%
Simplified71.1%
Taylor expanded in im around 0 43.0%
associate-*r*43.0%
unpow243.0%
rem-square-sqrt43.9%
metadata-eval43.9%
*-lft-identity43.9%
Simplified43.9%
if 3.49999999999999974e-110 < im Initial program 47.7%
+-commutative47.7%
hypot-def85.4%
Simplified85.4%
Taylor expanded in re around 0 72.7%
*-commutative72.7%
Simplified72.7%
Final simplification64.4%
(FPCore (re im) :precision binary64 (if (<= im -1.32e-118) (* 0.5 (sqrt (* im -2.0))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (im <= -1.32e-118) {
tmp = 0.5 * sqrt((im * -2.0));
} else {
tmp = sqrt(re);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-1.32d-118)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -1.32e-118) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.32e-118: tmp = 0.5 * math.sqrt((im * -2.0)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.32e-118) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -1.32e-118) tmp = 0.5 * sqrt((im * -2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.32e-118], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.32 \cdot 10^{-118}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if im < -1.32000000000000003e-118Initial program 43.2%
+-commutative43.2%
hypot-def90.7%
Simplified90.7%
Taylor expanded in im around -inf 75.9%
*-commutative75.9%
Simplified75.9%
if -1.32000000000000003e-118 < im Initial program 41.0%
+-commutative41.0%
hypot-def78.1%
Simplified78.1%
Taylor expanded in im around 0 29.9%
associate-*r*29.9%
unpow229.9%
rem-square-sqrt30.5%
metadata-eval30.5%
*-lft-identity30.5%
Simplified30.5%
Final simplification47.0%
(FPCore (re im) :precision binary64 (sqrt re))
double code(double re, double im) {
return sqrt(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt(re)
end function
public static double code(double re, double im) {
return Math.sqrt(re);
}
def code(re, im): return math.sqrt(re)
function code(re, im) return sqrt(re) end
function tmp = code(re, im) tmp = sqrt(re); end
code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{re}
\end{array}
Initial program 41.8%
+-commutative41.8%
hypot-def82.7%
Simplified82.7%
Taylor expanded in im around 0 25.5%
associate-*r*25.5%
unpow225.5%
rem-square-sqrt26.0%
metadata-eval26.0%
*-lft-identity26.0%
Simplified26.0%
Final simplification26.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 2023174
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(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)))))