
(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 -8.2e+64) (* 0.5 (sqrt (* im (/ (- im) re)))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (re <= -8.2e+64) {
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 <= -8.2e+64) {
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 <= -8.2e+64: 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 (re <= -8.2e+64) 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 (re <= -8.2e+64) 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[re, -8.2e+64], 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 \leq -8.2 \cdot 10^{+64}:\\
\;\;\;\;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 re < -8.19999999999999956e64Initial program 6.7%
+-commutative6.7%
hypot-def26.6%
Simplified26.6%
Taylor expanded in re around -inf 54.4%
*-commutative54.4%
unpow254.4%
associate-/l*59.3%
Simplified59.3%
expm1-log1p-u58.7%
expm1-udef30.6%
*-commutative30.6%
associate-*l*30.6%
associate-/r/30.6%
*-commutative30.6%
metadata-eval30.6%
Applied egg-rr30.6%
expm1-def58.9%
expm1-log1p59.5%
*-commutative59.5%
neg-mul-159.5%
distribute-rgt-neg-in59.5%
distribute-frac-neg59.5%
Simplified59.5%
if -8.19999999999999956e64 < re Initial program 48.1%
+-commutative48.1%
hypot-def91.7%
Simplified91.7%
add-sqr-sqrt90.9%
sqrt-unprod91.7%
*-commutative91.7%
*-commutative91.7%
swap-sqr91.7%
add-sqr-sqrt91.7%
*-commutative91.7%
metadata-eval91.7%
Applied egg-rr91.7%
associate-*l*91.7%
metadata-eval91.7%
Simplified91.7%
Final simplification84.0%
(FPCore (re im)
:precision binary64
(if (<= re -7.8e+205)
(* 0.5 (sqrt (/ im (/ re im))))
(if (<= re 3.9e-101)
(* 0.5 (sqrt (* im 2.0)))
(if (<= re 3e-76)
(sqrt re)
(if (<= re 2.05e+101) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -7.8e+205) {
tmp = 0.5 * sqrt((im / (re / im)));
} else if (re <= 3.9e-101) {
tmp = 0.5 * sqrt((im * 2.0));
} else if (re <= 3e-76) {
tmp = sqrt(re);
} else if (re <= 2.05e+101) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} 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 <= (-7.8d+205)) then
tmp = 0.5d0 * sqrt((im / (re / im)))
else if (re <= 3.9d-101) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else if (re <= 3d-76) then
tmp = sqrt(re)
else if (re <= 2.05d+101) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -7.8e+205) {
tmp = 0.5 * Math.sqrt((im / (re / im)));
} else if (re <= 3.9e-101) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else if (re <= 3e-76) {
tmp = Math.sqrt(re);
} else if (re <= 2.05e+101) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7.8e+205: tmp = 0.5 * math.sqrt((im / (re / im))) elif re <= 3.9e-101: tmp = 0.5 * math.sqrt((im * 2.0)) elif re <= 3e-76: tmp = math.sqrt(re) elif re <= 2.05e+101: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -7.8e+205) tmp = Float64(0.5 * sqrt(Float64(im / Float64(re / im)))); elseif (re <= 3.9e-101) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); elseif (re <= 3e-76) tmp = sqrt(re); elseif (re <= 2.05e+101) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -7.8e+205) tmp = 0.5 * sqrt((im / (re / im))); elseif (re <= 3.9e-101) tmp = 0.5 * sqrt((im * 2.0)); elseif (re <= 3e-76) tmp = sqrt(re); elseif (re <= 2.05e+101) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7.8e+205], N[(0.5 * N[Sqrt[N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.9e-101], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3e-76], N[Sqrt[re], $MachinePrecision], If[LessEqual[re, 2.05e+101], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7.8 \cdot 10^{+205}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{\frac{re}{im}}}\\
\mathbf{elif}\;re \leq 3.9 \cdot 10^{-101}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{elif}\;re \leq 3 \cdot 10^{-76}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;re \leq 2.05 \cdot 10^{+101}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -7.7999999999999997e205Initial program 2.3%
+-commutative2.3%
hypot-def23.5%
Simplified23.5%
Taylor expanded in re around -inf 61.9%
*-commutative61.9%
unpow261.9%
associate-/l*73.7%
Simplified73.7%
expm1-log1p-u72.6%
expm1-udef38.0%
*-commutative38.0%
associate-*l*38.0%
associate-/r/38.0%
*-commutative38.0%
metadata-eval38.0%
Applied egg-rr38.0%
expm1-def72.4%
expm1-log1p73.5%
rem-square-sqrt73.5%
fabs-sqr73.5%
rem-square-sqrt73.5%
fabs-mul73.5%
rem-square-sqrt21.7%
fabs-sqr21.7%
metadata-eval21.7%
rem-square-sqrt21.7%
*-rgt-identity21.7%
*-commutative21.7%
associate-/r/21.7%
Simplified21.7%
if -7.7999999999999997e205 < re < 3.90000000000000015e-101Initial program 41.1%
+-commutative41.1%
hypot-def71.9%
Simplified71.9%
Taylor expanded in re around 0 33.5%
*-commutative33.5%
Simplified33.5%
if 3.90000000000000015e-101 < re < 3.00000000000000024e-76 or 2.05e101 < re Initial program 35.0%
+-commutative35.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 91.3%
associate-*r*91.3%
unpow291.3%
rem-square-sqrt93.1%
metadata-eval93.1%
*-lft-identity93.1%
Simplified93.1%
if 3.00000000000000024e-76 < re < 2.05e101Initial program 60.3%
+-commutative60.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around 0 27.7%
distribute-lft-out27.7%
+-commutative27.7%
*-commutative27.7%
+-commutative27.7%
Simplified27.7%
Final simplification44.0%
(FPCore (re im)
:precision binary64
(if (<= re -2.3e+61)
(* 0.5 (sqrt (* im (/ (- im) re))))
(if (<= re 3.9e-101)
(* 0.5 (sqrt (* im 2.0)))
(if (<= re 1e-75)
(sqrt re)
(if (<= re 1.36e+101) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.3e+61) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if (re <= 3.9e-101) {
tmp = 0.5 * sqrt((im * 2.0));
} else if (re <= 1e-75) {
tmp = sqrt(re);
} else if (re <= 1.36e+101) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} 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 <= (-2.3d+61)) then
tmp = 0.5d0 * sqrt((im * (-im / re)))
else if (re <= 3.9d-101) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else if (re <= 1d-75) then
tmp = sqrt(re)
else if (re <= 1.36d+101) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.3e+61) {
tmp = 0.5 * Math.sqrt((im * (-im / re)));
} else if (re <= 3.9e-101) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else if (re <= 1e-75) {
tmp = Math.sqrt(re);
} else if (re <= 1.36e+101) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.3e+61: tmp = 0.5 * math.sqrt((im * (-im / re))) elif re <= 3.9e-101: tmp = 0.5 * math.sqrt((im * 2.0)) elif re <= 1e-75: tmp = math.sqrt(re) elif re <= 1.36e+101: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.3e+61) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif (re <= 3.9e-101) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); elseif (re <= 1e-75) tmp = sqrt(re); elseif (re <= 1.36e+101) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.3e+61) tmp = 0.5 * sqrt((im * (-im / re))); elseif (re <= 3.9e-101) tmp = 0.5 * sqrt((im * 2.0)); elseif (re <= 1e-75) tmp = sqrt(re); elseif (re <= 1.36e+101) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.3e+61], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.9e-101], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1e-75], N[Sqrt[re], $MachinePrecision], If[LessEqual[re, 1.36e+101], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.3 \cdot 10^{+61}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq 3.9 \cdot 10^{-101}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{elif}\;re \leq 10^{-75}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;re \leq 1.36 \cdot 10^{+101}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.3e61Initial program 6.7%
+-commutative6.7%
hypot-def26.6%
Simplified26.6%
Taylor expanded in re around -inf 54.4%
*-commutative54.4%
unpow254.4%
associate-/l*59.3%
Simplified59.3%
expm1-log1p-u58.7%
expm1-udef30.6%
*-commutative30.6%
associate-*l*30.6%
associate-/r/30.6%
*-commutative30.6%
metadata-eval30.6%
Applied egg-rr30.6%
expm1-def58.9%
expm1-log1p59.5%
*-commutative59.5%
neg-mul-159.5%
distribute-rgt-neg-in59.5%
distribute-frac-neg59.5%
Simplified59.5%
if -2.3e61 < re < 3.90000000000000015e-101Initial program 51.2%
+-commutative51.2%
hypot-def85.7%
Simplified85.7%
Taylor expanded in re around 0 39.8%
*-commutative39.8%
Simplified39.8%
if 3.90000000000000015e-101 < re < 9.9999999999999996e-76 or 1.35999999999999998e101 < re Initial program 35.0%
+-commutative35.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 91.3%
associate-*r*91.3%
unpow291.3%
rem-square-sqrt93.1%
metadata-eval93.1%
*-lft-identity93.1%
Simplified93.1%
if 9.9999999999999996e-76 < re < 1.35999999999999998e101Initial program 60.3%
+-commutative60.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around 0 27.7%
distribute-lft-out27.7%
+-commutative27.7%
*-commutative27.7%
+-commutative27.7%
Simplified27.7%
Final simplification54.1%
(FPCore (re im)
:precision binary64
(if (<= re 3.2e-101)
(* 0.5 (sqrt (* im 2.0)))
(if (<= re 2.8e-76)
(sqrt re)
(if (<= re 1.25e+101) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 3.2e-101) {
tmp = 0.5 * sqrt((im * 2.0));
} else if (re <= 2.8e-76) {
tmp = sqrt(re);
} else if (re <= 1.25e+101) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} 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 <= 3.2d-101) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else if (re <= 2.8d-76) then
tmp = sqrt(re)
else if (re <= 1.25d+101) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 3.2e-101) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else if (re <= 2.8e-76) {
tmp = Math.sqrt(re);
} else if (re <= 1.25e+101) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 3.2e-101: tmp = 0.5 * math.sqrt((im * 2.0)) elif re <= 2.8e-76: tmp = math.sqrt(re) elif re <= 1.25e+101: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 3.2e-101) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); elseif (re <= 2.8e-76) tmp = sqrt(re); elseif (re <= 1.25e+101) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 3.2e-101) tmp = 0.5 * sqrt((im * 2.0)); elseif (re <= 2.8e-76) tmp = sqrt(re); elseif (re <= 1.25e+101) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 3.2e-101], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.8e-76], N[Sqrt[re], $MachinePrecision], If[LessEqual[re, 1.25e+101], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.2 \cdot 10^{-101}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{elif}\;re \leq 2.8 \cdot 10^{-76}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{elif}\;re \leq 1.25 \cdot 10^{+101}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 3.19999999999999978e-101Initial program 35.6%
+-commutative35.6%
hypot-def65.0%
Simplified65.0%
Taylor expanded in re around 0 29.1%
*-commutative29.1%
Simplified29.1%
if 3.19999999999999978e-101 < re < 2.8000000000000001e-76 or 1.24999999999999997e101 < re Initial program 35.0%
+-commutative35.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 91.3%
associate-*r*91.3%
unpow291.3%
rem-square-sqrt93.1%
metadata-eval93.1%
*-lft-identity93.1%
Simplified93.1%
if 2.8000000000000001e-76 < re < 1.24999999999999997e101Initial program 60.3%
+-commutative60.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around 0 27.7%
distribute-lft-out27.7%
+-commutative27.7%
*-commutative27.7%
+-commutative27.7%
Simplified27.7%
Final simplification42.1%
(FPCore (re im) :precision binary64 (if (or (<= re 3.9e-101) (and (not (<= re 3.9e-76)) (<= re 1.25e+101))) (* 0.5 (sqrt (* im 2.0))) (sqrt re)))
double code(double re, double im) {
double tmp;
if ((re <= 3.9e-101) || (!(re <= 3.9e-76) && (re <= 1.25e+101))) {
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 ((re <= 3.9d-101) .or. (.not. (re <= 3.9d-76)) .and. (re <= 1.25d+101)) 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 ((re <= 3.9e-101) || (!(re <= 3.9e-76) && (re <= 1.25e+101))) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if (re <= 3.9e-101) or (not (re <= 3.9e-76) and (re <= 1.25e+101)): tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if ((re <= 3.9e-101) || (!(re <= 3.9e-76) && (re <= 1.25e+101))) 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 ((re <= 3.9e-101) || (~((re <= 3.9e-76)) && (re <= 1.25e+101))) tmp = 0.5 * sqrt((im * 2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[re, 3.9e-101], And[N[Not[LessEqual[re, 3.9e-76]], $MachinePrecision], LessEqual[re, 1.25e+101]]], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.9 \cdot 10^{-101} \lor \neg \left(re \leq 3.9 \cdot 10^{-76}\right) \land re \leq 1.25 \cdot 10^{+101}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 3.90000000000000015e-101 or 3.90000000000000025e-76 < re < 1.24999999999999997e101Initial program 39.1%
+-commutative39.1%
hypot-def70.0%
Simplified70.0%
Taylor expanded in re around 0 28.0%
*-commutative28.0%
Simplified28.0%
if 3.90000000000000015e-101 < re < 3.90000000000000025e-76 or 1.24999999999999997e101 < re Initial program 35.0%
+-commutative35.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 91.3%
associate-*r*91.3%
unpow291.3%
rem-square-sqrt93.1%
metadata-eval93.1%
*-lft-identity93.1%
Simplified93.1%
Final simplification41.5%
(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 38.3%
+-commutative38.3%
hypot-def76.2%
Simplified76.2%
Taylor expanded in im around 0 26.0%
associate-*r*26.0%
unpow226.0%
rem-square-sqrt26.5%
metadata-eval26.5%
*-lft-identity26.5%
Simplified26.5%
Final simplification26.5%
(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 2023230
(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)))))