math.square on complex, real part

Percentage Accurate: 93.3% → 96.8%
Time: 2.0s
Alternatives: 4
Speedup: 2.3×

Specification

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

Local Percentage Accuracy vs ?

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.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Alternative 1: 96.8% accurate, 0.1× speedup?

\[\begin{array}{l} \mathbf{if}\;re \leq -2.2 \cdot 10^{+174}:\\ \;\;\;\;re \cdot re\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if re < -2.2000000000000002e174

    1. Initial program 71.8%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around inf 94.9%

      \[\leadsto \color{blue}{{re}^{2}} \]
    3. Step-by-step derivation
      1. unpow294.9%

        \[\leadsto \color{blue}{re \cdot re} \]
    4. Simplified94.9%

      \[\leadsto \color{blue}{re \cdot re} \]

    if -2.2000000000000002e174 < re

    1. Initial program 95.4%

      \[re \cdot re - im \cdot im \]
    2. Step-by-step derivation
      1. fma-neg98.2%

        \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, -im \cdot im\right)} \]
      2. distribute-rgt-neg-in98.2%

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.2 \cdot 10^{+174}:\\ \;\;\;\;re \cdot re\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)\\ \end{array} \]

Alternative 2: 78.5% accurate, 0.4× speedup?

\[\begin{array}{l} \mathbf{if}\;re \cdot re \leq 0.026 \lor \neg \left(re \cdot re \leq 3.5 \cdot 10^{+77}\right) \land re \cdot re \leq 3.2 \cdot 10^{+129}:\\ \;\;\;\;im \cdot \left(-im\right)\\ \mathbf{else}:\\ \;\;\;\;re \cdot re\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 re re) < 0.0259999999999999988 or 3.5000000000000001e77 < (*.f64 re re) < 3.2000000000000002e129

    1. Initial program 100.0%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around 0 76.1%

      \[\leadsto \color{blue}{-1 \cdot {im}^{2}} \]
    3. Step-by-step derivation
      1. unpow276.1%

        \[\leadsto -1 \cdot \color{blue}{\left(im \cdot im\right)} \]
      2. mul-1-neg76.1%

        \[\leadsto \color{blue}{-im \cdot im} \]
      3. distribute-rgt-neg-in76.1%

        \[\leadsto \color{blue}{im \cdot \left(-im\right)} \]
    4. Simplified76.1%

      \[\leadsto \color{blue}{im \cdot \left(-im\right)} \]

    if 0.0259999999999999988 < (*.f64 re re) < 3.5000000000000001e77 or 3.2000000000000002e129 < (*.f64 re re)

    1. Initial program 83.8%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around inf 85.2%

      \[\leadsto \color{blue}{{re}^{2}} \]
    3. Step-by-step derivation
      1. unpow285.2%

        \[\leadsto \color{blue}{re \cdot re} \]
    4. Simplified85.2%

      \[\leadsto \color{blue}{re \cdot re} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification80.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \cdot re \leq 0.026 \lor \neg \left(re \cdot re \leq 3.5 \cdot 10^{+77}\right) \land re \cdot re \leq 3.2 \cdot 10^{+129}:\\ \;\;\;\;im \cdot \left(-im\right)\\ \mathbf{else}:\\ \;\;\;\;re \cdot re\\ \end{array} \]

Alternative 3: 96.6% accurate, 0.6× speedup?

\[\begin{array}{l} \mathbf{if}\;re \cdot re \leq 3.5 \cdot 10^{+287}:\\ \;\;\;\;re \cdot re - im \cdot im\\ \mathbf{else}:\\ \;\;\;\;re \cdot re\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 re re) < 3.49999999999999976e287

    1. Initial program 100.0%

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

    if 3.49999999999999976e287 < (*.f64 re re)

    1. Initial program 75.6%

      \[re \cdot re - im \cdot im \]
    2. Taylor expanded in re around inf 90.7%

      \[\leadsto \color{blue}{{re}^{2}} \]
    3. Step-by-step derivation
      1. unpow290.7%

        \[\leadsto \color{blue}{re \cdot re} \]
    4. Simplified90.7%

      \[\leadsto \color{blue}{re \cdot re} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \cdot re \leq 3.5 \cdot 10^{+287}:\\ \;\;\;\;re \cdot re - im \cdot im\\ \mathbf{else}:\\ \;\;\;\;re \cdot re\\ \end{array} \]

Alternative 4: 53.7% accurate, 2.3× speedup?

\[re \cdot re \]
Derivation
  1. Initial program 91.8%

    \[re \cdot re - im \cdot im \]
  2. Taylor expanded in re around inf 62.4%

    \[\leadsto \color{blue}{{re}^{2}} \]
  3. Step-by-step derivation
    1. unpow262.4%

      \[\leadsto \color{blue}{re \cdot re} \]
  4. Simplified62.4%

    \[\leadsto \color{blue}{re \cdot re} \]
  5. Final simplification62.4%

    \[\leadsto re \cdot re \]

Reproduce

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