
(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 3 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 (* (+ im re) (- re im)))
double re_sqr(double re, double im) {
return (im + re) * (re - im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (im + re) * (re - im)
end function
public static double re_sqr(double re, double im) {
return (im + re) * (re - im);
}
def re_sqr(re, im): return (im + re) * (re - im)
function re_sqr(re, im) return Float64(Float64(im + re) * Float64(re - im)) end
function tmp = re_sqr(re, im) tmp = (im + re) * (re - im); end
re$95$sqr[re_, im_] := N[(N[(im + re), $MachinePrecision] * N[(re - im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(im + re\right) \cdot \left(re - im\right)
\end{array}
Initial program 95.7%
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
difference-of-squaresN/A
*-commutativeN/A
lower-*.f64N/A
lower--.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore re_sqr (re im) :precision binary64 (let* ((t_0 (- (* re re) (* im im))) (t_1 (* (- im) im))) (if (<= t_0 -5e-312) t_1 (if (<= t_0 INFINITY) (* re re) t_1))))
double re_sqr(double re, double im) {
double t_0 = (re * re) - (im * im);
double t_1 = -im * im;
double tmp;
if (t_0 <= -5e-312) {
tmp = t_1;
} else if (t_0 <= ((double) INFINITY)) {
tmp = re * re;
} else {
tmp = t_1;
}
return tmp;
}
public static double re_sqr(double re, double im) {
double t_0 = (re * re) - (im * im);
double t_1 = -im * im;
double tmp;
if (t_0 <= -5e-312) {
tmp = t_1;
} else if (t_0 <= Double.POSITIVE_INFINITY) {
tmp = re * re;
} else {
tmp = t_1;
}
return tmp;
}
def re_sqr(re, im): t_0 = (re * re) - (im * im) t_1 = -im * im tmp = 0 if t_0 <= -5e-312: tmp = t_1 elif t_0 <= math.inf: tmp = re * re else: tmp = t_1 return tmp
function re_sqr(re, im) t_0 = Float64(Float64(re * re) - Float64(im * im)) t_1 = Float64(Float64(-im) * im) tmp = 0.0 if (t_0 <= -5e-312) tmp = t_1; elseif (t_0 <= Inf) tmp = Float64(re * re); else tmp = t_1; end return tmp end
function tmp_2 = re_sqr(re, im) t_0 = (re * re) - (im * im); t_1 = -im * im; tmp = 0.0; if (t_0 <= -5e-312) tmp = t_1; elseif (t_0 <= Inf) tmp = re * re; else tmp = t_1; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := Block[{t$95$0 = N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[((-im) * im), $MachinePrecision]}, If[LessEqual[t$95$0, -5e-312], t$95$1, If[LessEqual[t$95$0, Infinity], N[(re * re), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := re \cdot re - im \cdot im\\
t_1 := \left(-im\right) \cdot im\\
\mathbf{if}\;t\_0 \leq -5 \cdot 10^{-312}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq \infty:\\
\;\;\;\;re \cdot re\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (-.f64 (*.f64 re re) (*.f64 im im)) < -5.0000000000022e-312 or +inf.0 < (-.f64 (*.f64 re re) (*.f64 im im)) Initial program 91.1%
Taylor expanded in re around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6497.3
Applied rewrites97.3%
if -5.0000000000022e-312 < (-.f64 (*.f64 re re) (*.f64 im im)) < +inf.0Initial program 100.0%
Taylor expanded in re around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6411.0
Applied rewrites11.0%
Taylor expanded in re around inf
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
(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%
Taylor expanded in re around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6452.8
Applied rewrites52.8%
Taylor expanded in re around inf
unpow2N/A
lower-*.f6453.8
Applied rewrites53.8%
herbie shell --seed 2024288
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))