
(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 7 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 9.5e-43) (sqrt (* 0.5 (- (hypot re im) re))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 9.5e-43) {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
} else {
tmp = (0.5 * im) / sqrt(re);
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 9.5e-43) {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 9.5e-43: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 9.5e-43) tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 9.5e-43) tmp = sqrt((0.5 * (hypot(re, im) - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 9.5e-43], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 9.5 \cdot 10^{-43}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 9.50000000000000044e-43Initial program 53.7%
hypot-udef94.8%
add-sqr-sqrt94.2%
sqrt-unprod94.8%
*-commutative94.8%
*-commutative94.8%
swap-sqr94.8%
add-sqr-sqrt94.8%
metadata-eval94.8%
Applied egg-rr94.8%
*-commutative94.8%
associate-*r*94.8%
metadata-eval94.8%
Simplified94.8%
if 9.50000000000000044e-43 < re Initial program 12.6%
hypot-udef31.5%
add-sqr-sqrt31.4%
sqrt-unprod31.5%
*-commutative31.5%
*-commutative31.5%
swap-sqr31.5%
add-sqr-sqrt31.5%
metadata-eval31.5%
Applied egg-rr31.5%
*-commutative31.5%
associate-*r*31.5%
metadata-eval31.5%
Simplified31.5%
Taylor expanded in im around 0 84.9%
associate-*l*85.0%
unpow285.0%
rem-square-sqrt86.0%
associate-*r*86.0%
Simplified86.0%
sqrt-div85.9%
metadata-eval85.9%
un-div-inv86.0%
Applied egg-rr86.0%
Final simplification92.5%
(FPCore (re im)
:precision binary64
(if (<= re -1.45e+54)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 3.3e-44)
(* 0.5 (sqrt (* 2.0 (- im re))))
(/ (* 0.5 im) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -1.45e+54) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 3.3e-44) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} 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 <= (-1.45d+54)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 3.3d-44) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
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 <= -1.45e+54) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 3.3e-44) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.45e+54: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 3.3e-44: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.45e+54) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 3.3e-44) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.45e+54) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 3.3e-44) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.45e+54], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.3e-44], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.45 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 3.3 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.4499999999999999e54Initial program 36.1%
Taylor expanded in re around -inf 90.5%
*-commutative90.5%
Simplified90.5%
if -1.4499999999999999e54 < re < 3.30000000000000006e-44Initial program 59.6%
Taylor expanded in re around 0 81.7%
if 3.30000000000000006e-44 < re Initial program 12.6%
hypot-udef31.5%
add-sqr-sqrt31.4%
sqrt-unprod31.5%
*-commutative31.5%
*-commutative31.5%
swap-sqr31.5%
add-sqr-sqrt31.5%
metadata-eval31.5%
Applied egg-rr31.5%
*-commutative31.5%
associate-*r*31.5%
metadata-eval31.5%
Simplified31.5%
Taylor expanded in im around 0 84.9%
associate-*l*85.0%
unpow285.0%
rem-square-sqrt86.0%
associate-*r*86.0%
Simplified86.0%
sqrt-div85.9%
metadata-eval85.9%
un-div-inv86.0%
Applied egg-rr86.0%
Final simplification84.5%
(FPCore (re im) :precision binary64 (if (<= re -4e-311) (* 0.5 (sqrt (* 2.0 (* re -2.0)))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -4e-311) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} 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 <= (-4d-311)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
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 <= -4e-311) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4e-311: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -4e-311) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4e-311) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4e-311], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{-311}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.99999999999979e-311Initial program 54.6%
Taylor expanded in re around -inf 50.5%
*-commutative50.5%
Simplified50.5%
if -3.99999999999979e-311 < re Initial program 31.4%
hypot-udef56.8%
add-sqr-sqrt56.5%
sqrt-unprod56.8%
*-commutative56.8%
*-commutative56.8%
swap-sqr56.8%
add-sqr-sqrt56.8%
metadata-eval56.8%
Applied egg-rr56.8%
*-commutative56.8%
associate-*r*56.8%
metadata-eval56.8%
Simplified56.8%
Taylor expanded in im around 0 54.7%
associate-*l*54.8%
unpow254.8%
rem-square-sqrt55.4%
associate-*r*55.4%
Simplified55.4%
sqrt-div55.3%
metadata-eval55.3%
un-div-inv55.5%
Applied egg-rr55.5%
Final simplification53.0%
(FPCore (re im) :precision binary64 (* im (/ 0.5 (sqrt re))))
double code(double re, double im) {
return im * (0.5 / sqrt(re));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = im * (0.5d0 / sqrt(re))
end function
public static double code(double re, double im) {
return im * (0.5 / Math.sqrt(re));
}
def code(re, im): return im * (0.5 / math.sqrt(re))
function code(re, im) return Float64(im * Float64(0.5 / sqrt(re))) end
function tmp = code(re, im) tmp = im * (0.5 / sqrt(re)); end
code[re_, im_] := N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
im \cdot \frac{0.5}{\sqrt{re}}
\end{array}
Initial program 42.9%
hypot-udef78.2%
add-sqr-sqrt77.7%
sqrt-unprod78.2%
*-commutative78.2%
*-commutative78.2%
swap-sqr78.2%
add-sqr-sqrt78.2%
metadata-eval78.2%
Applied egg-rr78.2%
*-commutative78.2%
associate-*r*78.2%
metadata-eval78.2%
Simplified78.2%
Taylor expanded in im around 0 27.6%
associate-*l*27.6%
unpow227.6%
rem-square-sqrt27.9%
associate-*r*27.9%
Simplified27.9%
expm1-log1p-u27.7%
expm1-udef10.1%
associate-*l*10.1%
sqrt-div10.1%
metadata-eval10.1%
un-div-inv10.1%
Applied egg-rr10.1%
expm1-def27.6%
expm1-log1p27.9%
Simplified27.9%
Final simplification27.9%
(FPCore (re im) :precision binary64 (* im (sqrt (/ 0.25 re))))
double code(double re, double im) {
return im * sqrt((0.25 / re));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = im * sqrt((0.25d0 / re))
end function
public static double code(double re, double im) {
return im * Math.sqrt((0.25 / re));
}
def code(re, im): return im * math.sqrt((0.25 / re))
function code(re, im) return Float64(im * sqrt(Float64(0.25 / re))) end
function tmp = code(re, im) tmp = im * sqrt((0.25 / re)); end
code[re_, im_] := N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
im \cdot \sqrt{\frac{0.25}{re}}
\end{array}
Initial program 42.9%
hypot-udef78.2%
add-sqr-sqrt77.7%
sqrt-unprod78.2%
*-commutative78.2%
*-commutative78.2%
swap-sqr78.2%
add-sqr-sqrt78.2%
metadata-eval78.2%
Applied egg-rr78.2%
*-commutative78.2%
associate-*r*78.2%
metadata-eval78.2%
Simplified78.2%
Taylor expanded in im around 0 27.6%
associate-*l*27.6%
unpow227.6%
rem-square-sqrt27.9%
associate-*r*27.9%
Simplified27.9%
add-sqr-sqrt27.8%
pow227.8%
associate-*l*27.8%
sqrt-div27.8%
metadata-eval27.8%
un-div-inv27.8%
Applied egg-rr27.8%
unpow227.8%
add-sqr-sqrt27.9%
*-commutative27.9%
add-sqr-sqrt27.7%
sqrt-unprod27.9%
frac-times27.9%
metadata-eval27.9%
add-sqr-sqrt27.9%
Applied egg-rr27.9%
Final simplification27.9%
(FPCore (re im) :precision binary64 (/ (* 0.5 im) (sqrt re)))
double code(double re, double im) {
return (0.5 * im) / sqrt(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * im) / sqrt(re)
end function
public static double code(double re, double im) {
return (0.5 * im) / Math.sqrt(re);
}
def code(re, im): return (0.5 * im) / math.sqrt(re)
function code(re, im) return Float64(Float64(0.5 * im) / sqrt(re)) end
function tmp = code(re, im) tmp = (0.5 * im) / sqrt(re); end
code[re_, im_] := N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot im}{\sqrt{re}}
\end{array}
Initial program 42.9%
hypot-udef78.2%
add-sqr-sqrt77.7%
sqrt-unprod78.2%
*-commutative78.2%
*-commutative78.2%
swap-sqr78.2%
add-sqr-sqrt78.2%
metadata-eval78.2%
Applied egg-rr78.2%
*-commutative78.2%
associate-*r*78.2%
metadata-eval78.2%
Simplified78.2%
Taylor expanded in im around 0 27.6%
associate-*l*27.6%
unpow227.6%
rem-square-sqrt27.9%
associate-*r*27.9%
Simplified27.9%
sqrt-div27.9%
metadata-eval27.9%
un-div-inv28.0%
Applied egg-rr28.0%
Final simplification28.0%
(FPCore (re im) :precision binary64 0.0)
double code(double re, double im) {
return 0.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.0d0
end function
public static double code(double re, double im) {
return 0.0;
}
def code(re, im): return 0.0
function code(re, im) return 0.0 end
function tmp = code(re, im) tmp = 0.0; end
code[re_, im_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 42.9%
Taylor expanded in re around inf 6.6%
Taylor expanded in re around 0 6.6%
metadata-eval6.6%
Applied egg-rr6.6%
Final simplification6.6%
herbie shell --seed 2024096
(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)))))