
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
def re_sqr(re, im): return (re * re) - (im * im)
function re_sqr(re, im) return Float64(Float64(re * re) - Float64(im * im)) end
function tmp = re_sqr(re, im) tmp = (re * re) - (im * im); end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
re \cdot re - im \cdot im
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
def re_sqr(re, im): return (re * re) - (im * im)
function re_sqr(re, im) return Float64(Float64(re * re) - Float64(im * im)) end
function tmp = re_sqr(re, im) tmp = (re * re) - (im * im); end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
re \cdot re - im \cdot im
\end{array}
(FPCore re_sqr (re im) :precision binary64 (* (- re im) (+ re im)))
double re_sqr(double re, double im) {
return (re - im) * (re + im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (re - im) * (re + im)
end function
public static double re_sqr(double re, double im) {
return (re - im) * (re + im);
}
def re_sqr(re, im): return (re - im) * (re + im)
function re_sqr(re, im) return Float64(Float64(re - im) * Float64(re + im)) end
function tmp = re_sqr(re, im) tmp = (re - im) * (re + im); end
re$95$sqr[re_, im_] := N[(N[(re - im), $MachinePrecision] * N[(re + im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(re - im\right) \cdot \left(re + im\right)
\end{array}
Initial program 94.1%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt48.3%
sqrt-unprod75.5%
sqr-neg75.5%
sqrt-prod28.2%
add-sqr-sqrt55.2%
Applied egg-rr55.2%
add-sqr-sqrt28.2%
sqrt-prod75.5%
add-sqr-sqrt28.2%
add-sqr-sqrt28.2%
sqr-neg28.2%
swap-sqr28.2%
sqrt-unprod0.0%
add-sqr-sqrt51.4%
distribute-rgt-neg-out51.4%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
(FPCore re_sqr (re im)
:precision binary64
(if (<= (* im im) 2e-185)
(* re re)
(if (<= (* im im) 2e-133)
(* im (- im))
(if (<= (* im im) 2e-61) (* re re) (* im (- re im))))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 2e-185) {
tmp = re * re;
} else if ((im * im) <= 2e-133) {
tmp = im * -im;
} else if ((im * im) <= 2e-61) {
tmp = re * re;
} else {
tmp = im * (re - im);
}
return tmp;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if ((im * im) <= 2d-185) then
tmp = re * re
else if ((im * im) <= 2d-133) then
tmp = im * -im
else if ((im * im) <= 2d-61) then
tmp = re * re
else
tmp = im * (re - im)
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 2e-185) {
tmp = re * re;
} else if ((im * im) <= 2e-133) {
tmp = im * -im;
} else if ((im * im) <= 2e-61) {
tmp = re * re;
} else {
tmp = im * (re - im);
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 2e-185: tmp = re * re elif (im * im) <= 2e-133: tmp = im * -im elif (im * im) <= 2e-61: tmp = re * re else: tmp = im * (re - im) return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 2e-185) tmp = Float64(re * re); elseif (Float64(im * im) <= 2e-133) tmp = Float64(im * Float64(-im)); elseif (Float64(im * im) <= 2e-61) tmp = Float64(re * re); else tmp = Float64(im * Float64(re - im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((im * im) <= 2e-185) tmp = re * re; elseif ((im * im) <= 2e-133) tmp = im * -im; elseif ((im * im) <= 2e-61) tmp = re * re; else tmp = im * (re - im); end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 2e-185], N[(re * re), $MachinePrecision], If[LessEqual[N[(im * im), $MachinePrecision], 2e-133], N[(im * (-im)), $MachinePrecision], If[LessEqual[N[(im * im), $MachinePrecision], 2e-61], N[(re * re), $MachinePrecision], N[(im * N[(re - im), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 2 \cdot 10^{-185}:\\
\;\;\;\;re \cdot re\\
\mathbf{elif}\;im \cdot im \leq 2 \cdot 10^{-133}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\mathbf{elif}\;im \cdot im \leq 2 \cdot 10^{-61}:\\
\;\;\;\;re \cdot re\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(re - im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 2e-185 or 2.0000000000000001e-133 < (*.f64 im im) < 2.0000000000000001e-61Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.1%
sqrt-unprod93.7%
sqr-neg93.7%
sqrt-prod48.6%
add-sqr-sqrt91.2%
Applied egg-rr91.2%
Taylor expanded in re around inf 91.4%
Taylor expanded in re around inf 91.9%
if 2e-185 < (*.f64 im im) < 2.0000000000000001e-133Initial program 99.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt43.4%
sqrt-unprod57.7%
sqr-neg57.7%
sqrt-prod13.9%
add-sqr-sqrt27.4%
Applied egg-rr27.4%
add-sqr-sqrt13.9%
sqrt-prod57.7%
add-sqr-sqrt13.9%
add-sqr-sqrt13.9%
sqr-neg13.9%
swap-sqr13.9%
sqrt-unprod0.0%
add-sqr-sqrt55.8%
distribute-rgt-neg-out55.8%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 72.5%
Taylor expanded in re around 0 74.2%
neg-mul-174.2%
Simplified74.2%
if 2.0000000000000001e-61 < (*.f64 im im) Initial program 88.2%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.8%
sqrt-unprod61.5%
sqr-neg61.5%
sqrt-prod11.9%
add-sqr-sqrt26.8%
Applied egg-rr26.8%
add-sqr-sqrt11.9%
sqrt-prod61.5%
add-sqr-sqrt11.9%
add-sqr-sqrt11.9%
sqr-neg11.9%
swap-sqr11.9%
sqrt-unprod0.0%
add-sqr-sqrt47.9%
distribute-rgt-neg-out47.9%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 78.8%
Final simplification84.3%
(FPCore re_sqr (re im)
:precision binary64
(if (or (<= (* re re) 6e-72)
(and (not (<= (* re re) 1.5e+61)) (<= (* re re) 1e+167)))
(* im (- im))
(* re re)))
double re_sqr(double re, double im) {
double tmp;
if (((re * re) <= 6e-72) || (!((re * re) <= 1.5e+61) && ((re * re) <= 1e+167))) {
tmp = im * -im;
} else {
tmp = re * re;
}
return tmp;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (((re * re) <= 6d-72) .or. (.not. ((re * re) <= 1.5d+61)) .and. ((re * re) <= 1d+167)) then
tmp = im * -im
else
tmp = re * re
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if (((re * re) <= 6e-72) || (!((re * re) <= 1.5e+61) && ((re * re) <= 1e+167))) {
tmp = im * -im;
} else {
tmp = re * re;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if ((re * re) <= 6e-72) or (not ((re * re) <= 1.5e+61) and ((re * re) <= 1e+167)): tmp = im * -im else: tmp = re * re return tmp
function re_sqr(re, im) tmp = 0.0 if ((Float64(re * re) <= 6e-72) || (!(Float64(re * re) <= 1.5e+61) && (Float64(re * re) <= 1e+167))) tmp = Float64(im * Float64(-im)); else tmp = Float64(re * re); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if (((re * re) <= 6e-72) || (~(((re * re) <= 1.5e+61)) && ((re * re) <= 1e+167))) tmp = im * -im; else tmp = re * re; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[Or[LessEqual[N[(re * re), $MachinePrecision], 6e-72], And[N[Not[LessEqual[N[(re * re), $MachinePrecision], 1.5e+61]], $MachinePrecision], LessEqual[N[(re * re), $MachinePrecision], 1e+167]]], N[(im * (-im)), $MachinePrecision], N[(re * re), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \cdot re \leq 6 \cdot 10^{-72} \lor \neg \left(re \cdot re \leq 1.5 \cdot 10^{+61}\right) \land re \cdot re \leq 10^{+167}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\mathbf{else}:\\
\;\;\;\;re \cdot re\\
\end{array}
\end{array}
if (*.f64 re re) < 6e-72 or 1.5e61 < (*.f64 re re) < 1e167Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt46.1%
sqrt-unprod65.1%
sqr-neg65.1%
sqrt-prod18.8%
add-sqr-sqrt29.9%
Applied egg-rr29.9%
add-sqr-sqrt18.8%
sqrt-prod65.1%
add-sqr-sqrt18.8%
add-sqr-sqrt18.8%
sqr-neg18.8%
swap-sqr18.8%
sqrt-unprod0.0%
add-sqr-sqrt53.5%
distribute-rgt-neg-out53.5%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 83.2%
Taylor expanded in re around 0 83.7%
neg-mul-183.7%
Simplified83.7%
if 6e-72 < (*.f64 re re) < 1.5e61 or 1e167 < (*.f64 re re) Initial program 87.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt50.8%
sqrt-unprod86.9%
sqr-neg86.9%
sqrt-prod38.5%
add-sqr-sqrt83.0%
Applied egg-rr83.0%
Taylor expanded in re around inf 89.2%
Taylor expanded in re around inf 83.7%
Final simplification83.7%
(FPCore re_sqr (re im) :precision binary64 (* re re))
double re_sqr(double re, double im) {
return re * re;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = re * re
end function
public static double re_sqr(double re, double im) {
return re * re;
}
def re_sqr(re, im): return re * re
function re_sqr(re, im) return Float64(re * re) end
function tmp = re_sqr(re, im) tmp = re * re; end
re$95$sqr[re_, im_] := N[(re * re), $MachinePrecision]
\begin{array}{l}
\\
re \cdot re
\end{array}
Initial program 94.1%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt48.3%
sqrt-unprod75.5%
sqr-neg75.5%
sqrt-prod28.2%
add-sqr-sqrt55.2%
Applied egg-rr55.2%
Taylor expanded in re around inf 59.1%
Taylor expanded in re around inf 56.1%
(FPCore re_sqr (re im) :precision binary64 (* im im))
double re_sqr(double re, double im) {
return im * im;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = im * im
end function
public static double re_sqr(double re, double im) {
return im * im;
}
def re_sqr(re, im): return im * im
function re_sqr(re, im) return Float64(im * im) end
function tmp = re_sqr(re, im) tmp = im * im; end
re$95$sqr[re_, im_] := N[(im * im), $MachinePrecision]
\begin{array}{l}
\\
im \cdot im
\end{array}
Initial program 94.1%
Taylor expanded in re around 0 52.1%
neg-mul-152.1%
Simplified52.1%
unpow252.1%
add-sqr-sqrt28.5%
associate-*r*28.5%
distribute-rgt-neg-out28.5%
associate-*r*28.5%
add-sqr-sqrt4.7%
sqrt-unprod7.6%
swap-sqr7.6%
unpow27.6%
metadata-eval7.6%
pow-pow7.6%
pow1/37.6%
swap-sqr7.6%
sqr-neg7.6%
add-sqr-sqrt7.6%
add-sqr-sqrt14.8%
unpow214.8%
metadata-eval14.8%
pow-pow7.6%
pow1/314.8%
Applied egg-rr12.5%
herbie shell --seed 2024110
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))