amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 249 forks source link

IntCollector.averaging/LongCollector.averaging could be optimized #268

Closed amaembo closed 1 year ago

amaembo commented 1 year ago

Currently, the final result could be computed using BigDecimal, which is slow and requires allocations. It looks like, we can do this in a simpler way using plain math:

OptionalDouble.of(((double) (hi + (lo < 0 ? 1 : 0)) / cnt) * 0x1.0p64 + ((double) lo) / cnt)

As * 0x1.0p64 is basically an exponent change, we don't have any machine precision problems here.