
(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 4 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 (fma re re (* im (- im))))
double re_sqr(double re, double im) {
return fma(re, re, (im * -im));
}
function re_sqr(re, im) return fma(re, re, Float64(im * Float64(-im))) end
re$95$sqr[re_, im_] := N[(re * re + N[(im * (-im)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)
\end{array}
Initial program 91.0%
sqr-neg91.0%
cancel-sign-sub91.0%
fma-def97.3%
Simplified97.3%
Final simplification97.3%
(FPCore re_sqr (re im)
:precision binary64
(if (or (<= (* im im) 1e-216)
(and (not (<= (* im im) 1e-183)) (<= (* im im) 1e+50)))
(* (- re im) (- re im))
(* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if (((im * im) <= 1e-216) || (!((im * im) <= 1e-183) && ((im * im) <= 1e+50))) {
tmp = (re - im) * (re - im);
} else {
tmp = im * -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) <= 1d-216) .or. (.not. ((im * im) <= 1d-183)) .and. ((im * im) <= 1d+50)) then
tmp = (re - im) * (re - im)
else
tmp = im * -im
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if (((im * im) <= 1e-216) || (!((im * im) <= 1e-183) && ((im * im) <= 1e+50))) {
tmp = (re - im) * (re - im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if ((im * im) <= 1e-216) or (not ((im * im) <= 1e-183) and ((im * im) <= 1e+50)): tmp = (re - im) * (re - im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if ((Float64(im * im) <= 1e-216) || (!(Float64(im * im) <= 1e-183) && (Float64(im * im) <= 1e+50))) tmp = Float64(Float64(re - im) * Float64(re - im)); else tmp = Float64(im * Float64(-im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if (((im * im) <= 1e-216) || (~(((im * im) <= 1e-183)) && ((im * im) <= 1e+50))) tmp = (re - im) * (re - im); else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[Or[LessEqual[N[(im * im), $MachinePrecision], 1e-216], And[N[Not[LessEqual[N[(im * im), $MachinePrecision], 1e-183]], $MachinePrecision], LessEqual[N[(im * im), $MachinePrecision], 1e+50]]], N[(N[(re - im), $MachinePrecision] * N[(re - im), $MachinePrecision]), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 10^{-216} \lor \neg \left(im \cdot im \leq 10^{-183}\right) \land im \cdot im \leq 10^{+50}:\\
\;\;\;\;\left(re - im\right) \cdot \left(re - im\right)\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 1e-216 or 1.00000000000000001e-183 < (*.f64 im im) < 1.0000000000000001e50Initial program 100.0%
difference-of-squares100.0%
add-sqr-sqrt49.6%
sqrt-prod92.3%
sqr-neg92.3%
sqrt-unprod42.7%
add-sqr-sqrt85.5%
sub-neg85.5%
pow185.5%
pow185.5%
pow-prod-up85.5%
add-sqr-sqrt42.6%
add-sqr-sqrt22.5%
difference-of-squares22.5%
metadata-eval22.5%
unpow-prod-down22.4%
Applied egg-rr22.4%
unpow222.4%
unpow222.4%
unswap-sqr22.5%
difference-of-squares22.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr22.5%
metadata-eval22.5%
unpow122.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr22.5%
metadata-eval22.5%
unpow122.5%
difference-of-squares22.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr42.8%
metadata-eval42.8%
unpow142.8%
Simplified85.5%
if 1e-216 < (*.f64 im im) < 1.00000000000000001e-183 or 1.0000000000000001e50 < (*.f64 im im) Initial program 81.9%
Taylor expanded in re around 0 81.4%
mul-1-neg81.4%
Simplified81.4%
unpow281.4%
Applied egg-rr81.4%
Final simplification83.5%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 2e+292) (- (* re re) (* im im)) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 2e+292) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -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+292) then
tmp = (re * re) - (im * im)
else
tmp = im * -im
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 2e+292) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 2e+292: tmp = (re * re) - (im * im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 2e+292) tmp = Float64(Float64(re * re) - Float64(im * im)); else tmp = Float64(im * Float64(-im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((im * im) <= 2e+292) tmp = (re * re) - (im * im); else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 2e+292], N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 2 \cdot 10^{+292}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 2e292Initial program 100.0%
if 2e292 < (*.f64 im im) Initial program 66.7%
Taylor expanded in re around 0 89.9%
mul-1-neg89.9%
Simplified89.9%
unpow289.9%
Applied egg-rr89.9%
Final simplification97.3%
(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 * Float64(-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 \left(-im\right)
\end{array}
Initial program 91.0%
Taylor expanded in re around 0 56.4%
mul-1-neg56.4%
Simplified56.4%
unpow256.4%
Applied egg-rr56.4%
Final simplification56.4%
herbie shell --seed 2024031
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))