Skip to content

Commit

Permalink
AverageLong: simplify (do not use BigInteger, compute the result dire…
Browse files Browse the repository at this point in the history
…ctly)

Multiplying double by 0x1.0p64 simply updates the exponent, so this should not produce any precision problems.
  • Loading branch information
amaembo committed Sep 2, 2023
1 parent 7f22178 commit 46fa336
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions src/main/java/one/util/streamex/Internals.java
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,7 @@ public AverageLong combine(AverageLong other) {
public OptionalDouble result() {
if (cnt == 0)
return OptionalDouble.empty();
if (hi == 0 && lo >= 0 || hi == -1 && lo < 0) {
return OptionalDouble.of(((double) lo) / cnt);
}
return OptionalDouble.of(new BigDecimal(new BigInteger(java.nio.ByteBuffer.allocate(16).order(
ByteOrder.BIG_ENDIAN).putLong(hi).putLong(lo).array())).divide(BigDecimal.valueOf(cnt),
MathContext.DECIMAL64).doubleValue());
return OptionalDouble.of(((double) (hi + (lo < 0 ? 1 : 0)) / cnt) * 0x1.0p64 + ((double) lo) / cnt);
}
}

Expand Down

0 comments on commit 46fa336

Please sign in to comment.