-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Choose[BigDecimal] #637
Comments
Thanks for creating this issue. I don't use a lot of arbitrary precision, and you sound like you have a specific use-case in mind, but why would you need to specify precision? What would be wrong with? Gen.choose(BigDecimal("-9.22337203685477580E+214748366"), BigDecimal("9.223372036854775807E-2147483629")) |
The numbers returned by that generator could be any size from 128 bits all the way up to 500MB in length. |
Ok, and that is what a precision parameter you mentioned would help avoid, or is this just an issue with the large numbers I chose as an example? I assumed |
Let's simplify and say we are asking for Can this generator produce |
Good point. Finding the range between the values would likely determine what the scale should be. According to the documentation for java.math.BigDecimal, subtraction adopts the largest scale of the two operands. Would that be sufficient? |
Oh, I really like that. Hang on a minute. |
In addition to implicit Choose instances for scala.math.BigDecimal and java.math.BigDecimal we also include explicit constructor methods, since users may wish to be explicit about the scale they want. We may want to put those methods directly on Gen, currently the ergonomics of using this are a bit bad: Gen.Choose.chooseBigDecimalScale(100).choose(0, 1) The BigDecimal generation is not yet tested. That will also be added in a follow up. This also optimizes the BigInt generator a bit and generalizes it to java.math.BigInteger to support that as well. Addresses typelevel#631, typelevel#637, and typelevel#664
I was initially going to just do this as part of #636, but now I'm not sure if a
Choose[BigDecimal]
implicit is even a good idea.Any practical generator of real numbers can never be truly uniform, as
Gen.choose
purports to be. This isn't a problem forchoose[Double]
,choose[Long]
, or evenchoose[BigInt]
, as all three of those values have bounded precision anyway. But when we try to implementchoose[BigDecimal]
, we have to somehow accept a new free variable, precision, that is not available via theGen.choose
interface.One way around this could be: instead of implementing
choose[BigDecimal]
, provide aChoose[BigDecimal]
factory that accepts a precision and whose result the user could declare as implicit. Seems pretty clunky, but still maybe easier than writing aBigDecimal
generator directly?The text was updated successfully, but these errors were encountered: