
(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 (<= (sqrt (* 2.0 (+ 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 (sqrt((2.0 * (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 (Math.sqrt((2.0 * (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 math.sqrt((2.0 * (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 (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(im * Float64(-Float64(im / 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 (sqrt((2.0 * (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[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \left(-\frac{im}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 11.9%
+-commutative11.9%
hypot-def11.9%
Simplified11.9%
Taylor expanded in re around -inf 47.4%
associate-*r/47.4%
neg-mul-147.4%
unpow247.4%
distribute-rgt-neg-in47.4%
Simplified47.4%
Taylor expanded in im around 0 47.4%
mul-1-neg47.4%
unpow247.4%
associate-*l/59.3%
distribute-rgt-neg-in59.3%
Simplified59.3%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 47.8%
+-commutative47.8%
hypot-def87.1%
Simplified87.1%
add-sqr-sqrt86.4%
sqrt-unprod87.1%
*-commutative87.1%
*-commutative87.1%
swap-sqr87.1%
add-sqr-sqrt87.1%
*-commutative87.1%
metadata-eval87.1%
Applied egg-rr87.1%
associate-*l*87.1%
metadata-eval87.1%
Simplified87.1%
Final simplification83.3%
(FPCore (re im)
:precision binary64
(if (<= im -1.6e-62)
(sqrt (* 0.5 (- re im)))
(if (<= im -6.2e-193)
(sqrt re)
(if (<= im -9e-199)
(* 0.5 (sqrt (* im (- (/ im re)))))
(if (<= im 5.6e-286)
(sqrt re)
(if (<= im 1.3e-101)
(* 0.5 (/ im (sqrt (- re))))
(sqrt (* 0.5 (+ re im)))))))))
double code(double re, double im) {
double tmp;
if (im <= -1.6e-62) {
tmp = sqrt((0.5 * (re - im)));
} else if (im <= -6.2e-193) {
tmp = sqrt(re);
} else if (im <= -9e-199) {
tmp = 0.5 * sqrt((im * -(im / re)));
} else if (im <= 5.6e-286) {
tmp = sqrt(re);
} else if (im <= 1.3e-101) {
tmp = 0.5 * (im / sqrt(-re));
} else {
tmp = sqrt((0.5 * (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.6d-62)) then
tmp = sqrt((0.5d0 * (re - im)))
else if (im <= (-6.2d-193)) then
tmp = sqrt(re)
else if (im <= (-9d-199)) then
tmp = 0.5d0 * sqrt((im * -(im / re)))
else if (im <= 5.6d-286) then
tmp = sqrt(re)
else if (im <= 1.3d-101) then
tmp = 0.5d0 * (im / sqrt(-re))
else
tmp = sqrt((0.5d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -1.6e-62) {
tmp = Math.sqrt((0.5 * (re - im)));
} else if (im <= -6.2e-193) {
tmp = Math.sqrt(re);
} else if (im <= -9e-199) {
tmp = 0.5 * Math.sqrt((im * -(im / re)));
} else if (im <= 5.6e-286) {
tmp = Math.sqrt(re);
} else if (im <= 1.3e-101) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else {
tmp = Math.sqrt((0.5 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.6e-62: tmp = math.sqrt((0.5 * (re - im))) elif im <= -6.2e-193: tmp = math.sqrt(re) elif im <= -9e-199: tmp = 0.5 * math.sqrt((im * -(im / re))) elif im <= 5.6e-286: tmp = math.sqrt(re) elif im <= 1.3e-101: tmp = 0.5 * (im / math.sqrt(-re)) else: tmp = math.sqrt((0.5 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.6e-62) tmp = sqrt(Float64(0.5 * Float64(re - im))); elseif (im <= -6.2e-193) tmp = sqrt(re); elseif (im <= -9e-199) tmp = Float64(0.5 * sqrt(Float64(im * Float64(-Float64(im / re))))); elseif (im <= 5.6e-286) tmp = sqrt(re); elseif (im <= 1.3e-101) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); else tmp = sqrt(Float64(0.5 * Float64(re + im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -1.6e-62) tmp = sqrt((0.5 * (re - im))); elseif (im <= -6.2e-193) tmp = sqrt(re); elseif (im <= -9e-199) tmp = 0.5 * sqrt((im * -(im / re))); elseif (im <= 5.6e-286) tmp = sqrt(re); elseif (im <= 1.3e-101) tmp = 0.5 * (im / sqrt(-re)); else tmp = sqrt((0.5 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.6e-62], N[Sqrt[N[(0.5 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[im, -6.2e-193], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, -9e-199], N[(0.5 * N[Sqrt[N[(im * (-N[(im / re), $MachinePrecision])), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5.6e-286], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 1.3e-101], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.6 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq -6.2 \cdot 10^{-193}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq -9 \cdot 10^{-199}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \left(-\frac{im}{re}\right)}\\
\mathbf{elif}\;im \leq 5.6 \cdot 10^{-286}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq 1.3 \cdot 10^{-101}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -1.60000000000000011e-62Initial program 41.5%
+-commutative41.5%
hypot-def81.8%
Simplified81.8%
add-sqr-sqrt80.9%
sqrt-unprod81.8%
*-commutative81.8%
*-commutative81.8%
swap-sqr81.8%
add-sqr-sqrt81.8%
*-commutative81.8%
metadata-eval81.8%
Applied egg-rr81.8%
associate-*l*81.8%
metadata-eval81.8%
Simplified81.8%
Taylor expanded in im around -inf 71.9%
mul-1-neg71.9%
sub-neg71.9%
Simplified71.9%
if -1.60000000000000011e-62 < im < -6.2000000000000004e-193 or -8.99999999999999995e-199 < im < 5.6e-286Initial program 43.4%
+-commutative43.4%
hypot-def70.4%
Simplified70.4%
Taylor expanded in im around 0 53.2%
associate-*r*53.2%
unpow253.2%
rem-square-sqrt54.4%
metadata-eval54.4%
*-lft-identity54.4%
Simplified54.4%
if -6.2000000000000004e-193 < im < -8.99999999999999995e-199Initial program 3.3%
+-commutative3.3%
hypot-def24.9%
Simplified24.9%
Taylor expanded in re around -inf 24.9%
associate-*r/24.9%
neg-mul-124.9%
unpow224.9%
distribute-rgt-neg-in24.9%
Simplified24.9%
Taylor expanded in im around 0 24.9%
mul-1-neg24.9%
unpow224.9%
associate-*l/81.9%
distribute-rgt-neg-in81.9%
Simplified81.9%
if 5.6e-286 < im < 1.3000000000000001e-101Initial program 34.0%
+-commutative34.0%
hypot-def53.8%
Simplified53.8%
Taylor expanded in re around -inf 18.9%
associate-*r/18.9%
neg-mul-118.9%
unpow218.9%
distribute-rgt-neg-in18.9%
Simplified18.9%
frac-2neg18.9%
sqrt-div32.2%
distribute-rgt-neg-out32.2%
remove-double-neg32.2%
sqrt-unprod58.6%
add-sqr-sqrt59.0%
Applied egg-rr59.0%
if 1.3000000000000001e-101 < im Initial program 49.5%
+-commutative49.5%
hypot-def88.7%
Simplified88.7%
add-sqr-sqrt88.0%
sqrt-unprod88.7%
*-commutative88.7%
*-commutative88.7%
swap-sqr88.7%
add-sqr-sqrt88.7%
*-commutative88.7%
metadata-eval88.7%
Applied egg-rr88.7%
associate-*l*88.7%
metadata-eval88.7%
Simplified88.7%
Taylor expanded in re around 0 73.1%
Final simplification67.3%
(FPCore (re im)
:precision binary64
(if (<= im -3.2e-61)
(sqrt (* 0.5 (- re im)))
(if (<= im 9e-290)
(sqrt re)
(if (<= im 3.8e-101)
(* 0.5 (/ im (sqrt (- re))))
(sqrt (* 0.5 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (im <= -3.2e-61) {
tmp = sqrt((0.5 * (re - im)));
} else if (im <= 9e-290) {
tmp = sqrt(re);
} else if (im <= 3.8e-101) {
tmp = 0.5 * (im / sqrt(-re));
} else {
tmp = sqrt((0.5 * (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 <= (-3.2d-61)) then
tmp = sqrt((0.5d0 * (re - im)))
else if (im <= 9d-290) then
tmp = sqrt(re)
else if (im <= 3.8d-101) then
tmp = 0.5d0 * (im / sqrt(-re))
else
tmp = sqrt((0.5d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -3.2e-61) {
tmp = Math.sqrt((0.5 * (re - im)));
} else if (im <= 9e-290) {
tmp = Math.sqrt(re);
} else if (im <= 3.8e-101) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else {
tmp = Math.sqrt((0.5 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -3.2e-61: tmp = math.sqrt((0.5 * (re - im))) elif im <= 9e-290: tmp = math.sqrt(re) elif im <= 3.8e-101: tmp = 0.5 * (im / math.sqrt(-re)) else: tmp = math.sqrt((0.5 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -3.2e-61) tmp = sqrt(Float64(0.5 * Float64(re - im))); elseif (im <= 9e-290) tmp = sqrt(re); elseif (im <= 3.8e-101) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); else tmp = sqrt(Float64(0.5 * Float64(re + im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -3.2e-61) tmp = sqrt((0.5 * (re - im))); elseif (im <= 9e-290) tmp = sqrt(re); elseif (im <= 3.8e-101) tmp = 0.5 * (im / sqrt(-re)); else tmp = sqrt((0.5 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -3.2e-61], N[Sqrt[N[(0.5 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[im, 9e-290], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 3.8e-101], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -3.2 \cdot 10^{-61}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 9 \cdot 10^{-290}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;im \leq 3.8 \cdot 10^{-101}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -3.2000000000000001e-61Initial program 41.5%
+-commutative41.5%
hypot-def81.8%
Simplified81.8%
add-sqr-sqrt80.9%
sqrt-unprod81.8%
*-commutative81.8%
*-commutative81.8%
swap-sqr81.8%
add-sqr-sqrt81.8%
*-commutative81.8%
metadata-eval81.8%
Applied egg-rr81.8%
associate-*l*81.8%
metadata-eval81.8%
Simplified81.8%
Taylor expanded in im around -inf 71.9%
mul-1-neg71.9%
sub-neg71.9%
Simplified71.9%
if -3.2000000000000001e-61 < im < 9e-290Initial program 39.6%
+-commutative39.6%
hypot-def66.1%
Simplified66.1%
Taylor expanded in im around 0 48.2%
associate-*r*48.2%
unpow248.2%
rem-square-sqrt49.2%
metadata-eval49.2%
*-lft-identity49.2%
Simplified49.2%
if 9e-290 < im < 3.8000000000000001e-101Initial program 34.0%
+-commutative34.0%
hypot-def53.8%
Simplified53.8%
Taylor expanded in re around -inf 18.9%
associate-*r/18.9%
neg-mul-118.9%
unpow218.9%
distribute-rgt-neg-in18.9%
Simplified18.9%
frac-2neg18.9%
sqrt-div32.2%
distribute-rgt-neg-out32.2%
remove-double-neg32.2%
sqrt-unprod58.6%
add-sqr-sqrt59.0%
Applied egg-rr59.0%
if 3.8000000000000001e-101 < im Initial program 49.5%
+-commutative49.5%
hypot-def88.7%
Simplified88.7%
add-sqr-sqrt88.0%
sqrt-unprod88.7%
*-commutative88.7%
*-commutative88.7%
swap-sqr88.7%
add-sqr-sqrt88.7%
*-commutative88.7%
metadata-eval88.7%
Applied egg-rr88.7%
associate-*l*88.7%
metadata-eval88.7%
Simplified88.7%
Taylor expanded in re around 0 73.1%
Final simplification65.7%
(FPCore (re im) :precision binary64 (if (<= im -1.68e-62) (sqrt (* 0.5 (- im))) (if (<= im 2.8e-187) (sqrt re) (sqrt (* 0.5 (+ re im))))))
double code(double re, double im) {
double tmp;
if (im <= -1.68e-62) {
tmp = sqrt((0.5 * -im));
} else if (im <= 2.8e-187) {
tmp = sqrt(re);
} else {
tmp = sqrt((0.5 * (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.68d-62)) then
tmp = sqrt((0.5d0 * -im))
else if (im <= 2.8d-187) then
tmp = sqrt(re)
else
tmp = sqrt((0.5d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -1.68e-62) {
tmp = Math.sqrt((0.5 * -im));
} else if (im <= 2.8e-187) {
tmp = Math.sqrt(re);
} else {
tmp = Math.sqrt((0.5 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.68e-62: tmp = math.sqrt((0.5 * -im)) elif im <= 2.8e-187: tmp = math.sqrt(re) else: tmp = math.sqrt((0.5 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.68e-62) tmp = sqrt(Float64(0.5 * Float64(-im))); elseif (im <= 2.8e-187) tmp = sqrt(re); else tmp = sqrt(Float64(0.5 * Float64(re + im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -1.68e-62) tmp = sqrt((0.5 * -im)); elseif (im <= 2.8e-187) tmp = sqrt(re); else tmp = sqrt((0.5 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.68e-62], N[Sqrt[N[(0.5 * (-im)), $MachinePrecision]], $MachinePrecision], If[LessEqual[im, 2.8e-187], N[Sqrt[re], $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.68 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(-im\right)}\\
\mathbf{elif}\;im \leq 2.8 \cdot 10^{-187}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -1.68e-62Initial program 41.5%
+-commutative41.5%
hypot-def81.8%
Simplified81.8%
add-sqr-sqrt80.9%
sqrt-unprod81.8%
*-commutative81.8%
*-commutative81.8%
swap-sqr81.8%
add-sqr-sqrt81.8%
*-commutative81.8%
metadata-eval81.8%
Applied egg-rr81.8%
associate-*l*81.8%
metadata-eval81.8%
Simplified81.8%
Taylor expanded in im around -inf 71.1%
mul-1-neg71.1%
Simplified71.1%
if -1.68e-62 < im < 2.8e-187Initial program 39.2%
+-commutative39.2%
hypot-def64.3%
Simplified64.3%
Taylor expanded in im around 0 43.6%
associate-*r*43.6%
unpow243.6%
rem-square-sqrt44.6%
metadata-eval44.6%
*-lft-identity44.6%
Simplified44.6%
if 2.8e-187 < im Initial program 46.4%
+-commutative46.4%
hypot-def82.6%
Simplified82.6%
add-sqr-sqrt81.8%
sqrt-unprod82.6%
*-commutative82.6%
*-commutative82.6%
swap-sqr82.6%
add-sqr-sqrt82.6%
*-commutative82.6%
metadata-eval82.6%
Applied egg-rr82.6%
associate-*l*82.6%
metadata-eval82.6%
Simplified82.6%
Taylor expanded in re around 0 67.1%
Final simplification61.4%
(FPCore (re im) :precision binary64 (if (<= im -6.1e-62) (sqrt (* 0.5 (- re im))) (if (<= im 2.8e-187) (sqrt re) (sqrt (* 0.5 (+ re im))))))
double code(double re, double im) {
double tmp;
if (im <= -6.1e-62) {
tmp = sqrt((0.5 * (re - im)));
} else if (im <= 2.8e-187) {
tmp = sqrt(re);
} else {
tmp = sqrt((0.5 * (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 <= (-6.1d-62)) then
tmp = sqrt((0.5d0 * (re - im)))
else if (im <= 2.8d-187) then
tmp = sqrt(re)
else
tmp = sqrt((0.5d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -6.1e-62) {
tmp = Math.sqrt((0.5 * (re - im)));
} else if (im <= 2.8e-187) {
tmp = Math.sqrt(re);
} else {
tmp = Math.sqrt((0.5 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -6.1e-62: tmp = math.sqrt((0.5 * (re - im))) elif im <= 2.8e-187: tmp = math.sqrt(re) else: tmp = math.sqrt((0.5 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -6.1e-62) tmp = sqrt(Float64(0.5 * Float64(re - im))); elseif (im <= 2.8e-187) tmp = sqrt(re); else tmp = sqrt(Float64(0.5 * Float64(re + im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -6.1e-62) tmp = sqrt((0.5 * (re - im))); elseif (im <= 2.8e-187) tmp = sqrt(re); else tmp = sqrt((0.5 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -6.1e-62], N[Sqrt[N[(0.5 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[im, 2.8e-187], N[Sqrt[re], $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -6.1 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 2.8 \cdot 10^{-187}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < -6.1e-62Initial program 41.5%
+-commutative41.5%
hypot-def81.8%
Simplified81.8%
add-sqr-sqrt80.9%
sqrt-unprod81.8%
*-commutative81.8%
*-commutative81.8%
swap-sqr81.8%
add-sqr-sqrt81.8%
*-commutative81.8%
metadata-eval81.8%
Applied egg-rr81.8%
associate-*l*81.8%
metadata-eval81.8%
Simplified81.8%
Taylor expanded in im around -inf 71.9%
mul-1-neg71.9%
sub-neg71.9%
Simplified71.9%
if -6.1e-62 < im < 2.8e-187Initial program 39.2%
+-commutative39.2%
hypot-def64.3%
Simplified64.3%
Taylor expanded in im around 0 43.6%
associate-*r*43.6%
unpow243.6%
rem-square-sqrt44.6%
metadata-eval44.6%
*-lft-identity44.6%
Simplified44.6%
if 2.8e-187 < im Initial program 46.4%
+-commutative46.4%
hypot-def82.6%
Simplified82.6%
add-sqr-sqrt81.8%
sqrt-unprod82.6%
*-commutative82.6%
*-commutative82.6%
swap-sqr82.6%
add-sqr-sqrt82.6%
*-commutative82.6%
metadata-eval82.6%
Applied egg-rr82.6%
associate-*l*82.6%
metadata-eval82.6%
Simplified82.6%
Taylor expanded in re around 0 67.1%
Final simplification61.6%
(FPCore (re im) :precision binary64 (if (<= im -1.48e-62) (sqrt (* 0.5 (- im))) (if (<= im 1.1e-168) (sqrt re) (sqrt (* im 0.5)))))
double code(double re, double im) {
double tmp;
if (im <= -1.48e-62) {
tmp = sqrt((0.5 * -im));
} else if (im <= 1.1e-168) {
tmp = sqrt(re);
} else {
tmp = sqrt((im * 0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-1.48d-62)) then
tmp = sqrt((0.5d0 * -im))
else if (im <= 1.1d-168) then
tmp = sqrt(re)
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -1.48e-62) {
tmp = Math.sqrt((0.5 * -im));
} else if (im <= 1.1e-168) {
tmp = Math.sqrt(re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -1.48e-62: tmp = math.sqrt((0.5 * -im)) elif im <= 1.1e-168: tmp = math.sqrt(re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (im <= -1.48e-62) tmp = sqrt(Float64(0.5 * Float64(-im))); elseif (im <= 1.1e-168) tmp = sqrt(re); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -1.48e-62) tmp = sqrt((0.5 * -im)); elseif (im <= 1.1e-168) tmp = sqrt(re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -1.48e-62], N[Sqrt[N[(0.5 * (-im)), $MachinePrecision]], $MachinePrecision], If[LessEqual[im, 1.1e-168], N[Sqrt[re], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.48 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(-im\right)}\\
\mathbf{elif}\;im \leq 1.1 \cdot 10^{-168}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if im < -1.48000000000000005e-62Initial program 41.5%
+-commutative41.5%
hypot-def81.8%
Simplified81.8%
add-sqr-sqrt80.9%
sqrt-unprod81.8%
*-commutative81.8%
*-commutative81.8%
swap-sqr81.8%
add-sqr-sqrt81.8%
*-commutative81.8%
metadata-eval81.8%
Applied egg-rr81.8%
associate-*l*81.8%
metadata-eval81.8%
Simplified81.8%
Taylor expanded in im around -inf 71.1%
mul-1-neg71.1%
Simplified71.1%
if -1.48000000000000005e-62 < im < 1.0999999999999999e-168Initial program 39.6%
+-commutative39.6%
hypot-def65.2%
Simplified65.2%
Taylor expanded in im around 0 43.9%
associate-*r*43.9%
unpow243.9%
rem-square-sqrt44.8%
metadata-eval44.8%
*-lft-identity44.8%
Simplified44.8%
if 1.0999999999999999e-168 < im Initial program 46.2%
+-commutative46.2%
hypot-def82.2%
Simplified82.2%
add-sqr-sqrt81.5%
sqrt-unprod82.2%
*-commutative82.2%
*-commutative82.2%
swap-sqr82.2%
add-sqr-sqrt82.2%
*-commutative82.2%
metadata-eval82.2%
Applied egg-rr82.2%
associate-*l*82.2%
metadata-eval82.2%
Simplified82.2%
Taylor expanded in re around 0 65.5%
Final simplification60.6%
(FPCore (re im) :precision binary64 (if (<= re 4.4e-80) (sqrt (* im 0.5)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 4.4e-80) {
tmp = sqrt((im * 0.5));
} 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 (re <= 4.4d-80) then
tmp = sqrt((im * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 4.4e-80) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 4.4e-80: tmp = math.sqrt((im * 0.5)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 4.4e-80) tmp = sqrt(Float64(im * 0.5)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 4.4e-80) tmp = sqrt((im * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 4.4e-80], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4.4 \cdot 10^{-80}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 4.4000000000000002e-80Initial program 36.0%
+-commutative36.0%
hypot-def66.3%
Simplified66.3%
add-sqr-sqrt65.8%
sqrt-unprod66.3%
*-commutative66.3%
*-commutative66.3%
swap-sqr66.3%
add-sqr-sqrt66.3%
*-commutative66.3%
metadata-eval66.3%
Applied egg-rr66.3%
associate-*l*66.3%
metadata-eval66.3%
Simplified66.3%
Taylor expanded in re around 0 35.4%
if 4.4000000000000002e-80 < re Initial program 58.1%
+-commutative58.1%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 69.8%
associate-*r*69.8%
unpow269.8%
rem-square-sqrt71.1%
metadata-eval71.1%
*-lft-identity71.1%
Simplified71.1%
Final simplification46.6%
(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 42.9%
+-commutative42.9%
hypot-def76.8%
Simplified76.8%
Taylor expanded in im around 0 25.1%
associate-*r*25.1%
unpow225.1%
rem-square-sqrt25.6%
metadata-eval25.6%
*-lft-identity25.6%
Simplified25.6%
Final simplification25.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 2023194
(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)))))