Re: C# generic containers from a "C++ perspective"
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Wed, 26 Mar 2008 08:38:27 -0500
Jon Harrop wrote:
Ben Voigt [C++ MVP] wrote:
Jon Harrop wrote:
My explanation of why your algorithm is fundamentally broken still
stands. If you implement the floating-point and integer algorithms
properly you will see that there is nothing worth factoring out
between them.
The implementation would be:
Maintain the last N items in a ring buffer. (This consists of an
array of size N and the index of the oldest element).
Maintain the sum of all the items in an accumulator.
On arrival of a new data point, deduct the oldest datum from the sum,
overwrite it with the new one (updating the index), add the new one
to the sum, return sum / N.
That is the fundamentally-broken algorithm that I was expecting you to
describe: you have implicitly assumed that addition and subtraction
commute, which is true for integers and not floats.
Specifically, your algorithm will suffer from cumulative round-off
errors and will not handle special values (e.g. infinities) at all.
This works very well for any numeric data type as long as the
accumulator doesn't overflow,
That is not true. You only need one element 10^17x larger than any
other currently in the buffer and you lose all precision in your
accumulator from catastrophic round-off error, hundreds of orders of
magnitude before any overflow.
but the accumulator doesn't need to be the same type as each buffer
element.
I see. And what type would you recommend for the accumulator? An
arbitrary-precision rational, perhaps?
The issue you describe is theoretically valid, but not applicable to
real-world data created via analog-to-digital sampling and linear functions
thereof, which is probably somewhere upwards of 99% of all data sets. If it
is a problem, you can always recalculate the sum directly from the stored
elements.
.
- Follow-Ups:
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- References:
- C# generic containers from a "C++ perspective"
- From: Giovanni Dicanio
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Arne Vajhøj
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- C# generic containers from a "C++ perspective"
- Prev by Date: compiling to IL code
- Next by Date: Re: Does setting object reference to null help the GC in collection
- Previous by thread: Re: C# generic containers from a "C++ perspective"
- Next by thread: Re: C# generic containers from a "C++ perspective"
- Index(es):
Relevant Pages
|
Loading