streamly icon indicating copy to clipboard operation
streamly copied to clipboard

Handle saturation/overflow in numeric folds

Open harendra-kumar opened this issue 5 years ago • 1 comments

Data.Fold.sum

We should probably use a Bounded a constraint and make sum terminate if it overflows beyond the bound. Currently overflows give wrong sum silently.

The currentsum can be renamed to sumUnsafe, and can be used if it gives noticeable perf advantage in perf sensitive code.

For Integer type (unbounded) we can have sumInteger. For (Num a, Floating a) we can have a sumFloating. Or we can create an Unbounded type class for such types? And then have a sumUnbounded. To begin with we can just use sumUnsafe for these use cases and leave this as a TODO.

Data.Fold.product

Overflow applies to product as well in the same way as sum.

Data.Fold.minimum (maximum/minimumBy/maximumBy)

minimum/maximum and minimumBy/maximumBy would terminate if the value reached the min/max possible value for the type.

harendra-kumar avatar Nov 23 '20 07:11 harendra-kumar

Pushing this to the next milestone. This looks like a breaking change.

adithyaov avatar Feb 04 '22 01:02 adithyaov