
(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 170000.0) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
double tmp;
if (re <= 170000.0) {
tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 170000.0) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 170000.0: tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= 170000.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 170000.0) tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 170000.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 170000:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < 1.7e5Initial program 53.0%
hypot-def93.1%
Simplified93.1%
if 1.7e5 < re Initial program 10.4%
hypot-def36.7%
Simplified36.7%
Taylor expanded in re around inf 58.7%
unpow258.7%
Simplified58.7%
div-inv58.7%
sqrt-prod71.5%
sqrt-prod83.7%
add-sqr-sqrt83.9%
inv-pow83.9%
sqrt-pow184.0%
metadata-eval84.0%
Applied egg-rr84.0%
Final simplification90.8%
(FPCore (re im)
:precision binary64
(if (<= re -6.4e+47)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 1.9e-9)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -6.4e+47) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 1.9e-9) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-6.4d+47)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 1.9d-9) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -6.4e+47) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 1.9e-9) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -6.4e+47: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 1.9e-9: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -6.4e+47) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 1.9e-9) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -6.4e+47) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 1.9e-9) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -6.4e+47], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.9e-9], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.4 \cdot 10^{+47}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 1.9 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -6.4e47Initial program 23.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 81.7%
*-commutative81.7%
Simplified81.7%
if -6.4e47 < re < 1.90000000000000006e-9Initial program 65.1%
Taylor expanded in re around 0 77.1%
if 1.90000000000000006e-9 < re Initial program 10.1%
hypot-def37.6%
Simplified37.6%
Taylor expanded in re around inf 57.0%
unpow257.0%
Simplified57.0%
div-inv57.0%
sqrt-prod69.0%
sqrt-prod81.8%
add-sqr-sqrt82.1%
inv-pow82.1%
sqrt-pow182.1%
metadata-eval82.1%
Applied egg-rr82.1%
Final simplification79.3%
(FPCore (re im)
:precision binary64
(if (<= re -6.4e+43)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 50000.0)
(* 0.5 (sqrt (* 2.0 im)))
(* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -6.4e+43) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 50000.0) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-6.4d+43)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 50000.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -6.4e+43) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 50000.0) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -6.4e+43: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 50000.0: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -6.4e+43) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 50000.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -6.4e+43) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 50000.0) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -6.4e+43], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 50000.0], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.4 \cdot 10^{+43}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 50000:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -6.40000000000000029e43Initial program 23.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 81.7%
*-commutative81.7%
Simplified81.7%
if -6.40000000000000029e43 < re < 5e4Initial program 63.4%
hypot-def90.6%
Simplified90.6%
Taylor expanded in re around 0 74.1%
*-commutative74.1%
Simplified74.1%
if 5e4 < re Initial program 10.4%
hypot-def36.7%
Simplified36.7%
Taylor expanded in re around inf 58.7%
unpow258.7%
Simplified58.7%
div-inv58.7%
sqrt-prod71.5%
sqrt-prod83.7%
add-sqr-sqrt83.9%
inv-pow83.9%
sqrt-pow184.0%
metadata-eval84.0%
Applied egg-rr84.0%
Final simplification78.1%
(FPCore (re im) :precision binary64 (if (<= re -4.6e+45) (* 0.5 (sqrt (* re -4.0))) (if (<= re 0.0006) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -4.6e+45) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 0.0006) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * (im / 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.6d+45)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 0.0006d0) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * (im / sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -4.6e+45) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 0.0006) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.6e+45: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 0.0006: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.6e+45) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 0.0006) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.6e+45) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 0.0006) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.6e+45], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 0.0006], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.6 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 0.0006:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -4.60000000000000025e45Initial program 23.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 81.7%
*-commutative81.7%
Simplified81.7%
if -4.60000000000000025e45 < re < 5.99999999999999947e-4Initial program 64.3%
hypot-def91.2%
Simplified91.2%
Taylor expanded in re around 0 74.4%
*-commutative74.4%
Simplified74.4%
if 5.99999999999999947e-4 < re Initial program 10.3%
hypot-def37.2%
Simplified37.2%
Taylor expanded in re around inf 57.1%
unpow257.1%
Simplified57.1%
Taylor expanded in im around 0 83.0%
unpow-183.0%
metadata-eval83.0%
pow-sqr83.0%
rem-sqrt-square83.0%
rem-square-sqrt82.7%
fabs-sqr82.7%
rem-square-sqrt83.0%
exp-to-pow78.7%
metadata-eval78.7%
distribute-rgt-neg-in78.7%
exp-neg78.7%
exp-to-pow82.9%
unpow1/282.9%
associate-*l/83.0%
*-lft-identity83.0%
Simplified83.0%
Final simplification78.0%
(FPCore (re im) :precision binary64 (if (<= re -1.35e+45) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (re <= -1.35e+45) {
tmp = 0.5 * sqrt((re * -4.0));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.35d+45)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.35e+45) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.35e+45: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.35e+45) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.35e+45) tmp = 0.5 * sqrt((re * -4.0)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.35e+45], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.35 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if re < -1.34999999999999992e45Initial program 23.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 81.7%
*-commutative81.7%
Simplified81.7%
if -1.34999999999999992e45 < re Initial program 47.0%
hypot-def73.9%
Simplified73.9%
Taylor expanded in re around 0 57.8%
*-commutative57.8%
Simplified57.8%
Final simplification62.5%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 42.4%
hypot-def79.0%
Simplified79.0%
Taylor expanded in re around 0 51.3%
*-commutative51.3%
Simplified51.3%
Final simplification51.3%
herbie shell --seed 2023174
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))