
(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 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt55.4%
sqrt-unprod76.8%
sqr-neg76.8%
sqrt-prod22.5%
add-sqr-sqrt49.6%
Applied egg-rr49.6%
add-sqr-sqrt22.5%
sqrt-prod76.8%
add-sqr-sqrt21.8%
add-sqr-sqrt21.8%
sqr-neg21.8%
swap-sqr21.8%
sqrt-unprod0.0%
add-sqr-sqrt44.4%
distribute-rgt-neg-out44.4%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 2e+57) (* re re) (* im (- re im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 2e+57) {
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+57) 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+57) {
tmp = re * re;
} else {
tmp = im * (re - im);
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 2e+57: tmp = re * re else: tmp = im * (re - im) return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 2e+57) 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+57) 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+57], 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^{+57}:\\
\;\;\;\;re \cdot re\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(re - im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 2.0000000000000001e57Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt57.3%
sqrt-unprod90.6%
sqr-neg90.6%
sqrt-prod33.3%
add-sqr-sqrt78.2%
Applied egg-rr78.2%
Taylor expanded in re around inf 78.6%
Taylor expanded in re around inf 78.9%
if 2.0000000000000001e57 < (*.f64 im im) Initial program 90.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt53.2%
sqrt-unprod61.2%
sqr-neg61.2%
sqrt-prod10.4%
add-sqr-sqrt17.2%
Applied egg-rr17.2%
add-sqr-sqrt10.4%
sqrt-prod61.2%
add-sqr-sqrt8.7%
add-sqr-sqrt8.7%
sqr-neg8.7%
swap-sqr8.7%
sqrt-unprod0.0%
add-sqr-sqrt46.5%
distribute-rgt-neg-out46.5%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 86.6%
Final simplification82.5%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* re re) 4e+192) (* im (- im)) (* re re)))
double re_sqr(double re, double im) {
double tmp;
if ((re * re) <= 4e+192) {
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) <= 4d+192) 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) <= 4e+192) {
tmp = im * -im;
} else {
tmp = re * re;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (re * re) <= 4e+192: tmp = im * -im else: tmp = re * re return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(re * re) <= 4e+192) 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) <= 4e+192) tmp = im * -im; else tmp = re * re; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(re * re), $MachinePrecision], 4e+192], N[(im * (-im)), $MachinePrecision], N[(re * re), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \cdot re \leq 4 \cdot 10^{+192}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\mathbf{else}:\\
\;\;\;\;re \cdot re\\
\end{array}
\end{array}
if (*.f64 re re) < 4.00000000000000016e192Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt56.8%
sqrt-unprod69.6%
sqr-neg69.6%
sqrt-prod12.6%
add-sqr-sqrt29.0%
Applied egg-rr29.0%
add-sqr-sqrt12.6%
sqrt-prod69.6%
add-sqr-sqrt12.6%
add-sqr-sqrt12.6%
sqr-neg12.6%
swap-sqr12.6%
sqrt-unprod0.0%
add-sqr-sqrt42.9%
distribute-rgt-neg-out42.9%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in re around 0 75.0%
Taylor expanded in re around 0 75.2%
neg-mul-175.2%
Simplified75.2%
if 4.00000000000000016e192 < (*.f64 re re) Initial program 86.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt52.4%
sqrt-unprod91.7%
sqr-neg91.7%
sqrt-prod42.9%
add-sqr-sqrt91.7%
Applied egg-rr91.7%
Taylor expanded in re around inf 95.4%
Taylor expanded in re around inf 91.7%
Final simplification80.6%
(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 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt55.4%
sqrt-unprod76.8%
sqr-neg76.8%
sqrt-prod22.5%
add-sqr-sqrt49.6%
Applied egg-rr49.6%
Taylor expanded in re around inf 52.3%
Taylor expanded in re around inf 50.4%
(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 95.7%
Taylor expanded in re around 0 53.5%
neg-mul-153.5%
Simplified53.5%
rem-square-sqrt3.7%
sqrt-unprod9.1%
metadata-eval9.1%
sqrt-pow19.1%
metadata-eval9.1%
sqrt-pow19.1%
sqr-neg9.1%
add-sqr-sqrt9.1%
sqrt-pow17.7%
metadata-eval7.7%
unpow27.7%
Applied egg-rr7.7%
herbie shell --seed 2024139
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))