Difference of squares

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

Specification

?
\[a \cdot a - b \cdot b \]

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}\;a \leq -2.2 \cdot 10^{+174}:\\ \;\;\;\;a \cdot a\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right)\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if a < -2.2000000000000002e174

    1. Initial program 71.8%

      \[a \cdot a - b \cdot b \]
    2. Taylor expanded in a around inf 94.9%

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

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

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

    if -2.2000000000000002e174 < a

    1. Initial program 95.4%

      \[a \cdot a - b \cdot b \]
    2. Step-by-step derivation
      1. fma-neg98.2%

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

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

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

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

Alternative 2: 78.5% accurate, 0.4× speedup?

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

    1. Initial program 100.0%

      \[a \cdot a - b \cdot b \]
    2. Taylor expanded in a around 0 76.1%

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

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

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

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

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

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

    1. Initial program 83.8%

      \[a \cdot a - b \cdot b \]
    2. Taylor expanded in a around inf 85.2%

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

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

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

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

Alternative 3: 96.6% accurate, 0.6× speedup?

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

    1. Initial program 100.0%

      \[a \cdot a - b \cdot b \]

    if 3.49999999999999976e287 < (*.f64 a a)

    1. Initial program 75.6%

      \[a \cdot a - b \cdot b \]
    2. Taylor expanded in a around inf 90.7%

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

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

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

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

Alternative 4: 53.7% accurate, 2.3× speedup?

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

    \[a \cdot a - b \cdot b \]
  2. Taylor expanded in a around inf 62.4%

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

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

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

    \[\leadsto a \cdot a \]

Developer target: 100.0% accurate, 1.0× speedup?

\[\left(a + b\right) \cdot \left(a - b\right) \]

Reproduce

?
herbie shell --seed 2023167 
(FPCore (a b)
  :name "Difference of squares"
  :precision binary64

  :herbie-target
  (* (+ a b) (- a b))

  (- (* a a) (* b b)))