
(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.3%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.9%
sqrt-unprod75.2%
sqr-neg75.2%
sqrt-prod24.8%
add-sqr-sqrt53.6%
Applied egg-rr53.6%
Applied egg-rr100.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* re re) 1e+95) (* im (- re im)) (* re re)))
double re_sqr(double re, double im) {
double tmp;
if ((re * re) <= 1e+95) {
tmp = im * (re - 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) <= 1d+95) then
tmp = im * (re - 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) <= 1e+95) {
tmp = im * (re - im);
} else {
tmp = re * re;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (re * re) <= 1e+95: tmp = im * (re - im) else: tmp = re * re return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(re * re) <= 1e+95) tmp = Float64(im * Float64(re - im)); else tmp = Float64(re * re); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((re * re) <= 1e+95) tmp = im * (re - im); else tmp = re * re; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(re * re), $MachinePrecision], 1e+95], N[(im * N[(re - im), $MachinePrecision]), $MachinePrecision], N[(re * re), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \cdot re \leq 10^{+95}:\\
\;\;\;\;im \cdot \left(re - im\right)\\
\mathbf{else}:\\
\;\;\;\;re \cdot re\\
\end{array}
\end{array}
if (*.f64 re re) < 1.00000000000000002e95Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt50.2%
sqrt-unprod65.7%
sqr-neg65.7%
sqrt-prod15.4%
add-sqr-sqrt32.7%
Applied egg-rr32.7%
Applied egg-rr100.0%
Taylor expanded in re around 0 85.1%
if 1.00000000000000002e95 < (*.f64 re re) Initial program 89.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt54.1%
sqrt-unprod88.1%
sqr-neg88.1%
sqrt-prod37.6%
add-sqr-sqrt81.7%
Applied egg-rr81.7%
Taylor expanded in re around inf 90.2%
Taylor expanded in re around inf 82.0%
Final simplification83.8%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* re re) 1e+95) (* im (- im)) (* re re)))
double re_sqr(double re, double im) {
double tmp;
if ((re * re) <= 1e+95) {
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) <= 1d+95) 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) <= 1e+95) {
tmp = im * -im;
} else {
tmp = re * re;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (re * re) <= 1e+95: tmp = im * -im else: tmp = re * re return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(re * re) <= 1e+95) 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) <= 1e+95) tmp = im * -im; else tmp = re * re; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(re * re), $MachinePrecision], 1e+95], N[(im * (-im)), $MachinePrecision], N[(re * re), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \cdot re \leq 10^{+95}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\mathbf{else}:\\
\;\;\;\;re \cdot re\\
\end{array}
\end{array}
if (*.f64 re re) < 1.00000000000000002e95Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt50.2%
sqrt-unprod65.7%
sqr-neg65.7%
sqrt-prod15.4%
add-sqr-sqrt32.7%
Applied egg-rr32.7%
Applied egg-rr100.0%
Taylor expanded in re around 0 85.1%
Taylor expanded in re around 0 85.4%
neg-mul-185.4%
Simplified85.4%
if 1.00000000000000002e95 < (*.f64 re re) Initial program 89.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt54.1%
sqrt-unprod88.1%
sqr-neg88.1%
sqrt-prod37.6%
add-sqr-sqrt81.7%
Applied egg-rr81.7%
Taylor expanded in re around inf 90.2%
Taylor expanded in re around inf 82.0%
Final simplification83.9%
(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.3%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.9%
sqrt-unprod75.2%
sqr-neg75.2%
sqrt-prod24.8%
add-sqr-sqrt53.6%
Applied egg-rr53.6%
Taylor expanded in re around inf 58.1%
Taylor expanded in re around inf 54.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.3%
Taylor expanded in re around 0 56.5%
neg-mul-156.5%
Simplified56.5%
add-sqr-sqrt10.8%
sqrt-unprod17.9%
sqr-neg17.9%
sqrt-unprod14.7%
add-sqr-sqrt14.7%
unpow214.7%
Applied egg-rr14.7%
herbie shell --seed 2024146
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))