FastMath test3

?

Percentage Accurate: 97.9% → 100.0%
Time: 3.2s
Precision: binary64
Cost: 6848

?

\[\left(d1 \cdot 3 + d1 \cdot d2\right) + d1 \cdot d3 \]
\[\mathsf{fma}\left(d1, 3, d1 \cdot \left(d2 + d3\right)\right) \]
(FPCore (d1 d2 d3) :precision binary64 (+ (+ (* d1 3.0) (* d1 d2)) (* d1 d3)))
(FPCore (d1 d2 d3) :precision binary64 (fma d1 3.0 (* d1 (+ d2 d3))))
double code(double d1, double d2, double d3) {
	return ((d1 * 3.0) + (d1 * d2)) + (d1 * d3);
}
double code(double d1, double d2, double d3) {
	return fma(d1, 3.0, (d1 * (d2 + d3)));
}
function code(d1, d2, d3)
	return Float64(Float64(Float64(d1 * 3.0) + Float64(d1 * d2)) + Float64(d1 * d3))
end
function code(d1, d2, d3)
	return fma(d1, 3.0, Float64(d1 * Float64(d2 + d3)))
end
code[d1_, d2_, d3_] := N[(N[(N[(d1 * 3.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * d3), $MachinePrecision]), $MachinePrecision]
code[d1_, d2_, d3_] := N[(d1 * 3.0 + N[(d1 * N[(d2 + d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(d1 \cdot 3 + d1 \cdot d2\right) + d1 \cdot d3
\mathsf{fma}\left(d1, 3, d1 \cdot \left(d2 + d3\right)\right)

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.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Target

Original97.9%
Target99.9%
Herbie100.0%
\[d1 \cdot \left(\left(3 + d2\right) + d3\right) \]

Derivation?

  1. Initial program 97.2%

    \[\left(d1 \cdot 3 + d1 \cdot d2\right) + d1 \cdot d3 \]
  2. Simplified100.0%

    \[\leadsto \color{blue}{d1 \cdot \left(\left(3 + d2\right) + d3\right)} \]
    Step-by-step derivation

    [Start]97.2%

    \[ \left(d1 \cdot 3 + d1 \cdot d2\right) + d1 \cdot d3 \]

    distribute-lft-out [=>]97.6%

    \[ \color{blue}{d1 \cdot \left(3 + d2\right)} + d1 \cdot d3 \]

    distribute-lft-out [=>]100.0%

    \[ \color{blue}{d1 \cdot \left(\left(3 + d2\right) + d3\right)} \]
  3. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(d1, 3, d1 \cdot \left(d2 + d3\right)\right)} \]
    Step-by-step derivation

    [Start]100.0%

    \[ d1 \cdot \left(\left(3 + d2\right) + d3\right) \]

    associate-+l+ [=>]100.0%

    \[ d1 \cdot \color{blue}{\left(3 + \left(d2 + d3\right)\right)} \]

    distribute-lft-in [=>]99.5%

    \[ \color{blue}{d1 \cdot 3 + d1 \cdot \left(d2 + d3\right)} \]

    fma-def [=>]100.0%

    \[ \color{blue}{\mathsf{fma}\left(d1, 3, d1 \cdot \left(d2 + d3\right)\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(d1, 3, d1 \cdot \left(d2 + d3\right)\right) \]

Alternatives

Alternative 1
Accuracy100.0%
Cost6848
\[\mathsf{fma}\left(d1, 3, d1 \cdot \left(d2 + d3\right)\right) \]
Alternative 2
Accuracy52.4%
Cost853
\[\begin{array}{l} \mathbf{if}\;d2 \leq -1250000:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq -6.5 \cdot 10^{-32}:\\ \;\;\;\;d1 \cdot d3\\ \mathbf{elif}\;d2 \leq -1.34 \cdot 10^{-166} \lor \neg \left(d2 \leq -1.55 \cdot 10^{-190}\right) \land d2 \leq -1 \cdot 10^{-276}:\\ \;\;\;\;d1 \cdot 3\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d3\\ \end{array} \]
Alternative 3
Accuracy76.1%
Cost452
\[\begin{array}{l} \mathbf{if}\;d2 \leq -1300000:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(3 + d3\right)\\ \end{array} \]
Alternative 4
Accuracy76.2%
Cost452
\[\begin{array}{l} \mathbf{if}\;d2 \leq -0.019:\\ \;\;\;\;d1 \cdot \left(3 + d2\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(3 + d3\right)\\ \end{array} \]
Alternative 5
Accuracy99.9%
Cost448
\[d1 \cdot \left(d3 + \left(3 + d2\right)\right) \]
Alternative 6
Accuracy44.4%
Cost324
\[\begin{array}{l} \mathbf{if}\;d3 \leq 0.00016:\\ \;\;\;\;d1 \cdot 3\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d3\\ \end{array} \]
Alternative 7
Accuracy26.6%
Cost192
\[d1 \cdot 3 \]

Reproduce?

herbie shell --seed 2023178 
(FPCore (d1 d2 d3)
  :name "FastMath test3"
  :precision binary64

  :herbie-target
  (* d1 (+ (+ 3.0 d2) d3))

  (+ (+ (* d1 3.0) (* d1 d2)) (* d1 d3)))