Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@felher
Copy link
Contributor

@felher felher commented May 6, 2020

Gen.weighted can't currently handle generators with zero weight because it builds a map from the the cumulative probability to the generator, but a probability of zero doesn't change the cumulative probability, overwriting the initial value for that probability.

For example, when making a map of (Gen.const(true), 1), (Gen.const(false), 0), first true gets added to the map, yielding Map(1 -> Gen.const(true). Then false gets added, resulting in Map(1 -> Gen.const(true), (1 + 0) -> Gen.const(false)). So false overwrites the earlier value, since adding zero doesn't change the probability.

The first commit creates a failing test, the second one adds a fix. The fix also works when the added double isn't zero, but is so small that the cumulative probability wouldn't change, which can (in theory) happen, since doubles are not indefinitely precise (and have their precision distributed unevenly). For example, weighted(("a", 100000000000.0), ("b", 0.000007)) now drops "b" completely instead of overwriting "a", even though "b" doesn't have zero probability.
It can (again, in theory) still fail when the values don't add up to exactly one.

I've also added a small example to the documentation of weighted.

Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felher Good catch!

@adamgfraser adamgfraser merged commit eb9f005 into zio:master May 6, 2020
@felher felher deleted the fix-zero-prob-values-in-gen-weighted branch May 6, 2020 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants