math.square on complex, real part

?

Percentage Accurate: 93.8% → 97.1%
Time: 1.9s
Precision: binary64
Cost: 6784

?

\[re \cdot re - im \cdot im \]
\[\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right) \]
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
(FPCore re_sqr (re im) :precision binary64 (fma re re (* im (- im))))
double re_sqr(double re, double im) {
	return (re * re) - (im * im);
}
double re_sqr(double re, double im) {
	return fma(re, re, (im * -im));
}
function re_sqr(re, im)
	return Float64(Float64(re * re) - Float64(im * im))
end
function re_sqr(re, im)
	return fma(re, re, Float64(im * Float64(-im)))
end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
re$95$sqr[re_, im_] := N[(re * re + N[(im * (-im)), $MachinePrecision]), $MachinePrecision]
re \cdot re - im \cdot im
\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Derivation?

  1. Initial program 96.5%

    \[re \cdot re - im \cdot im \]
  2. Simplified99.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)} \]
    Step-by-step derivation

    [Start]96.5

    \[ re \cdot re - im \cdot im \]

    fma-neg [=>]99.2

    \[ \color{blue}{\mathsf{fma}\left(re, re, -im \cdot im\right)} \]

    distribute-rgt-neg-in [=>]99.2

    \[ \mathsf{fma}\left(re, re, \color{blue}{im \cdot \left(-im\right)}\right) \]
  3. Final simplification99.2%

    \[\leadsto \mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right) \]

Alternatives

Alternative 1
Accuracy75.4%
Cost785
\[\begin{array}{l} \mathbf{if}\;re \leq -3.4 \cdot 10^{-5}:\\ \;\;\;\;re \cdot re\\ \mathbf{elif}\;re \leq 5.1 \cdot 10^{-145} \lor \neg \left(re \leq 2.4 \cdot 10^{-77}\right) \land re \leq 2.3 \cdot 10^{+51}:\\ \;\;\;\;im \cdot \left(-im\right)\\ \mathbf{else}:\\ \;\;\;\;re \cdot re\\ \end{array} \]
Alternative 2
Accuracy97.1%
Cost708
\[\begin{array}{l} \mathbf{if}\;im \cdot im \leq 2 \cdot 10^{+303}:\\ \;\;\;\;re \cdot re - im \cdot im\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(-im\right)\\ \end{array} \]
Alternative 3
Accuracy54.4%
Cost192
\[re \cdot re \]

Error

Reproduce?

herbie shell --seed 2023160 
(FPCore re_sqr (re im)
  :name "math.square on complex, real part"
  :precision binary64
  (- (* re re) (* im im)))