
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= (+ re (sqrt (+ (* re re) (* im im)))) 0.0) (* 0.5 (/ im (sqrt (- re)))) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * (im / sqrt(-re));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if ((re + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if (re + math.sqrt(((re * re) + (im * im)))) <= 0.0: tmp = 0.5 * (im / math.sqrt(-re)) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) tmp = 0.5 * (im / sqrt(-re)); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function 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[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-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 9.3%
Simplified16.9%
Taylor expanded in re around -inf 34.2%
mul-1-neg34.2%
distribute-neg-frac34.2%
Simplified34.2%
frac-2neg34.2%
sqrt-div41.0%
remove-double-neg41.0%
unpow241.0%
sqrt-prod54.0%
add-sqr-sqrt59.4%
Applied egg-rr59.4%
if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 43.4%
Simplified91.2%
add-sqr-sqrt90.5%
sqrt-unprod91.2%
*-commutative91.2%
*-commutative91.2%
swap-sqr91.2%
add-sqr-sqrt91.2%
*-commutative91.2%
metadata-eval91.2%
Applied egg-rr91.2%
associate-*l*91.2%
metadata-eval91.2%
Simplified91.2%
Final simplification87.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -4.9e+41) (* 0.5 (/ im (sqrt (- re)))) (if (<= re 1.1e+19) (* 0.5 (sqrt (* (+ re im) 2.0))) (sqrt re))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -4.9e+41) {
tmp = 0.5 * (im / sqrt(-re));
} else if (re <= 1.1e+19) {
tmp = 0.5 * sqrt(((re + im) * 2.0));
} else {
tmp = sqrt(re);
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-4.9d+41)) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (re <= 1.1d+19) then
tmp = 0.5d0 * sqrt(((re + im) * 2.0d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -4.9e+41) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (re <= 1.1e+19) {
tmp = 0.5 * Math.sqrt(((re + im) * 2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -4.9e+41: tmp = 0.5 * (im / math.sqrt(-re)) elif re <= 1.1e+19: tmp = 0.5 * math.sqrt(((re + im) * 2.0)) else: tmp = math.sqrt(re) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -4.9e+41) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (re <= 1.1e+19) tmp = Float64(0.5 * sqrt(Float64(Float64(re + im) * 2.0))); else tmp = sqrt(re); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.9e+41) tmp = 0.5 * (im / sqrt(-re)); elseif (re <= 1.1e+19) tmp = 0.5 * sqrt(((re + im) * 2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -4.9e+41], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.1e+19], N[(0.5 * N[Sqrt[N[(N[(re + im), $MachinePrecision] * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.9 \cdot 10^{+41}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;re \leq 1.1 \cdot 10^{+19}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(re + im\right) \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -4.8999999999999999e41Initial program 7.9%
Simplified39.4%
Taylor expanded in re around -inf 50.2%
mul-1-neg50.2%
distribute-neg-frac50.2%
Simplified50.2%
frac-2neg50.2%
sqrt-div64.0%
remove-double-neg64.0%
unpow264.0%
sqrt-prod35.1%
add-sqr-sqrt42.1%
Applied egg-rr42.1%
if -4.8999999999999999e41 < re < 1.1e19Initial program 53.3%
Simplified91.3%
Taylor expanded in re around 0 45.9%
distribute-lft-out45.9%
*-commutative45.9%
Simplified45.9%
if 1.1e19 < re Initial program 38.3%
Simplified100.0%
Taylor expanded in im around 0 82.1%
*-commutative82.1%
unpow282.1%
rem-square-sqrt83.6%
associate-*r*83.6%
metadata-eval83.6%
*-lft-identity83.6%
Simplified83.6%
Final simplification55.9%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -5.2e+41) (* 0.5 (/ im (sqrt (- re)))) (if (<= re 125000000.0) (* 0.5 (sqrt (* im 2.0))) (sqrt re))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -5.2e+41) {
tmp = 0.5 * (im / sqrt(-re));
} else if (re <= 125000000.0) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = sqrt(re);
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-5.2d+41)) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (re <= 125000000.0d0) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -5.2e+41) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (re <= 125000000.0) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -5.2e+41: tmp = 0.5 * (im / math.sqrt(-re)) elif re <= 125000000.0: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = math.sqrt(re) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -5.2e+41) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (re <= 125000000.0) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = sqrt(re); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5.2e+41) tmp = 0.5 * (im / sqrt(-re)); elseif (re <= 125000000.0) tmp = 0.5 * sqrt((im * 2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -5.2e+41], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 125000000.0], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.2 \cdot 10^{+41}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;re \leq 125000000:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -5.2000000000000001e41Initial program 7.9%
Simplified39.4%
Taylor expanded in re around -inf 50.2%
mul-1-neg50.2%
distribute-neg-frac50.2%
Simplified50.2%
frac-2neg50.2%
sqrt-div64.0%
remove-double-neg64.0%
unpow264.0%
sqrt-prod35.1%
add-sqr-sqrt42.1%
Applied egg-rr42.1%
if -5.2000000000000001e41 < re < 1.25e8Initial program 53.4%
Simplified91.0%
Taylor expanded in re around 0 43.8%
*-commutative43.8%
Simplified43.8%
if 1.25e8 < re Initial program 39.0%
Simplified100.0%
Taylor expanded in im around 0 80.5%
*-commutative80.5%
unpow280.5%
rem-square-sqrt82.0%
associate-*r*82.0%
metadata-eval82.0%
*-lft-identity82.0%
Simplified82.0%
Final simplification55.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 4800000000.0) (* 0.5 (sqrt (* im 2.0))) (sqrt re)))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 4800000000.0) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = sqrt(re);
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 4800000000.0d0) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= 4800000000.0) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 4800000000.0: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = math.sqrt(re) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 4800000000.0) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = sqrt(re); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= 4800000000.0) tmp = 0.5 * sqrt((im * 2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, 4800000000.0], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4800000000:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 4.8e9Initial program 39.9%
Simplified75.8%
Taylor expanded in re around 0 35.7%
*-commutative35.7%
Simplified35.7%
if 4.8e9 < re Initial program 39.0%
Simplified100.0%
Taylor expanded in im around 0 80.5%
*-commutative80.5%
unpow280.5%
rem-square-sqrt82.0%
associate-*r*82.0%
metadata-eval82.0%
*-lft-identity82.0%
Simplified82.0%
Final simplification49.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (sqrt re))
im = abs(im);
double code(double re, double im) {
return sqrt(re);
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt(re)
end function
im = Math.abs(im);
public static double code(double re, double im) {
return Math.sqrt(re);
}
im = abs(im) def code(re, im): return math.sqrt(re)
im = abs(im) function code(re, im) return sqrt(re) end
im = abs(im) function tmp = code(re, im) tmp = sqrt(re); end
NOTE: im should be positive before calling this function code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\sqrt{re}
\end{array}
Initial program 39.6%
Simplified83.0%
Taylor expanded in im around 0 31.0%
*-commutative31.0%
unpow231.0%
rem-square-sqrt31.6%
associate-*r*31.6%
metadata-eval31.6%
*-lft-identity31.6%
Simplified31.6%
Final simplification31.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 2023312
(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)))))