You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
randomR could produce NaNs when the upper bound is infinity #54
NaN is caused by 0 * ±Infinity, happening in uniformRM when uniformFloat01M returns exactly 0 or 1.
We can probably guard this case explicitly:
instanceUniformRangeFloatwhere
uniformRM (l, h) g
|isNaN l = l
|isNaN h = h
| l == h =return l
|isInfinite l, isInfinite h =0/0|isInfinite l = l
|isInfinite h = h
|otherwise=do
x <- uniformFloat01M g
return$ x * l + (1- x) * h
This is the current behaviour: you get NaN. One alternative would be to generate Inf or -Inf each with p=0.5.
|isInfinite l = l
|isInfinite h = h
This is already the current behaviour for x \in (0,1). If x == 0 or x == 1, you can currently get NaN instead, as you pointed out. This case is currently not documented.
I see two alternatives:
We document that when l or h is infinite, you can get a NaN back (if x == 1 or x == 0)
We add guards for infinities
@Bodigrim, would you mind clarifying whether the intent of the code you proposed was to change behaviour, or primarily to "document" current behaviour?
(Edited heavily, sorry - I misunderstood part of the proposed code.)
| isInfinite l && isInfinite h = bool negate id <$> uniformM
I think asking to sample a range with infinties is indication that the user has made a mistake. If they really wanted +Inf / -Inf with equal probability then it would be much better for them to be explicit about it. I'd much prefer a NaN here.
Activity
cartazio commentedon Apr 3, 2019
Merge pull request haskell#54 from idontgetoutmuch/runs-equal
Merge pull request #54 from idontgetoutmuch/runs-equal
Bodigrim commentedon Jun 24, 2020
NaNis caused by0 * ±Infinity, happening inuniformRMwhenuniformFloat01Mreturns exactly 0 or 1.We can probably guard this case explicitly:
@curiousleo what do you think about it?
lehins commentedon Jun 24, 2020
@Bodigrim you might be interested to look at discussion in this PR: idontgetoutmuch#138
curiousleo commentedon Jun 25, 2020
Or directly here: https://hackage.haskell.org/package/random-1.2.0/docs/System-Random-Stateful.html#g:14
@Bodigrim great minds think alike :) Here's what I cooked up for my "truly random floats" experiment.
The
isNaNguards unnecessary; if one oflorhisNaN, the result will beNaN.(This guard already exists, added here: idontgetoutmuch#169)
This is the current behaviour: you get
NaN. One alternative would be to generateInfor-Infeach with p=0.5.This is already the current behaviour for
x \in (0,1). Ifx == 0orx == 1, you can currently getNaNinstead, as you pointed out. This case is currently not documented.I see two alternatives:
lorhis infinite, you can get aNaNback (ifx == 1orx == 0)@Bodigrim, would you mind clarifying whether the intent of the code you proposed was to change behaviour, or primarily to "document" current behaviour?
(Edited heavily, sorry - I misunderstood part of the proposed code.)
idontgetoutmuch commentedon Jun 25, 2020
In the email notification I just received, I see
I think asking to sample a range with infinties is indication that the user has made a mistake. If they really wanted +Inf / -Inf with equal probability then it would be much better for them to be explicit about it. I'd much prefer a NaN here.
idontgetoutmuch commentedon Jun 25, 2020
I think this is right thing to do.