Fork me on GitHub
#clojure-spec
<
2018-03-06
>
roklenarcic11:03:13

How would I create a BigDecimal generator?

taylor12:03:05

you could use fmap to coerce some other number generator to BigDecimal but you wouldn’t get the full range of values (and you might not want that?)

gfredericks14:03:28

yeah it's hard to give a good answer without knowing what kind of distribution you want; but (gen/let [factor gen/large-integer, log gen/int] (* factor (... 10 to the logth power ...))) wouldn't be bad

gfredericks14:03:39

there's probably a method on BigDecimal that would do the scaling a bit more naturally

taylor01:03:15

Haha I just read that the largest possible big decimal value can consume about 8GB RAM

gfredericks01:03:52

I'm disappointed that there's a largest

cap10morgan19:03:28

Is there a good workaround for keeping NaN out of your spec-generated test data? Filtering at the top level doesn't keep them out of nested data structures, and they break all kinds of equality tests when they show up.

seancorfield19:03:18

@cap10morgan I thought the double-in generator had options to suppress that?

cap10morgan19:03:32

I'm working with any?

seancorfield19:03:02

Ah, then you'll get "anything" 🙂

cap10morgan19:03:11

yep. there just doesn't seem to be a way to define any-but-nan. my code handles NaNs just fine, it's the specs that barf on them (since the equality tests in my predicates choke on them).

cap10morgan19:03:57

makes spec'ing fns that can handle any value tricky

cap10morgan20:03:59

I have tested it locally and it seems to solve the problem.

gfredericks21:03:40

this is a sticky issue because merely removing NaN from everywhere isn't obviously the best thing, since NaN is an important edge case to test in a lot of situations

gfredericks21:03:58

it's obviously hard to opt-out of at the moment

cap10morgan21:03:55

Agreed but the concept of removing it from the any? generator seemed at least tacitly endorsed by the core team.

cap10morgan21:03:28

If they instead requested a way to toggle it, I’d be happy to work on that too.

noisesmith21:03:37

pedantically, if you accept any value, you better do the right thing if the value is NaN right?

cap10morgan21:03:14

Yes. And the code does. Only the spec has a problem. And it is not easy to work around.

cap10morgan21:03:35

(In my use case)

cap10morgan21:03:41

A NaN at any level of nesting blows up equality checks in predicates.

dpsutton21:03:16

and you said your code gracefully handles that, right?

dpsutton21:03:26

> do the right thing if the value is NaN right?

cap10morgan21:03:29

Yes, the code handles it just fine.

cap10morgan21:03:41

It’s not doing any equality checking.

cap10morgan21:03:52

I think my ideal solution might be a spec-specific equality function that handled NaN, but I don't know where the core team stands on that.

Alex Miller (Clojure team)21:03:40

Just driving by here - would much rather prevent the NaN from occurring in the first place. Custom equality has never gone well imho :)

cap10morgan21:03:18

@alexmiller That's fair. 🙂 Does that patch ^^^ seem OK to attach to the JIRA ticket?

Alex Miller (Clojure team)21:03:07

No time to look atm but it’s ok either way :)

cap10morgan21:03:21

OK, attaching it then.

gfredericks23:03:09

the downside to that is that the list of generators that gen/any is composed of is now in two places

cap10morgan23:03:09

@gfredericks Not sure I follow. Where are the 2 places?

gfredericks23:03:08

Didn't you paste & tweak that code from test.check?

cap10morgan23:03:07

yes, but it is the one and only code for the any? generator now. and test.check itself largely repeats that one-of form between the any and any-printable generators.

gfredericks23:03:22

Yes, I was pointing out that if test.check added anything to its gen/any, that wouldn't propogate to spec anymore

cap10morgan23:03:36

If test.check defined the vector separately I definitely would have just re-used it though. Not sure there's a good way to re-use it as is.

gfredericks23:03:47

Even if test.check changed you couldn't take advantage of that without introducing a stricter version requirement between the two. I'm not sure if that's acceptable or not