GHC: Ticket #5501: randomR overflow
http://ghc.haskell.org/trac/ghc/ticket/5501
<p>
When given a large range, <tt>randomR</tt> overflows at <tt>Double</tt> etc.
</p>
<p>
<tt>randomIvalDouble</tt> has two problems: first, the calculation of the center, <tt>(l+h)/2</tt> overflows if the range is located near <tt>±Infinity</tt>; second, and that concerns also <tt>randomRFloating</tt>, the scaling factor <tt>(h-l)</tt> overflows if the range is large enough.
</p>
<p>
Both problems can be fixed "well enough" by multiplying the bounds by 0.5 before the calculations and scaling up at the end,
</p>
<pre class="wiki">0.5*l + 0.5*h instead of (l+h)/2
(0.5*h - 0.5*l)/(0.5*realToFrac int32Count) in randomIvalDouble
2.0*(0.5*l + coef*(0.5*h - 0.5*l)) in randomRFloating
</pre><p>
These transformations can introduce a small error when a subnormal number is involved, but I think we can ignore that (no sane person would have a [nonzero] subnormal number as a bound, and a correct-for-all-cases transformation would be somewhat convoluted).
</p>
en-usGHChttp://ghc.haskell.org/trac/ghc/chrome/site/ghc_logo.png
http://ghc.haskell.org/trac/ghc/ticket/5501
Trac 1.0.1iglooThu, 29 Sep 2011 21:37:35 GMTowner set
http://ghc.haskell.org/trac/ghc/ticket/5501#comment:1
http://ghc.haskell.org/trac/ghc/ticket/5501#comment:1
<ul>
<li><strong>owner</strong>
set to <em>rrnewton</em>
</li>
</ul>
TicketrrnewtonFri, 30 Sep 2011 17:34:15 GMTstatus changed; resolution set
http://ghc.haskell.org/trac/ghc/ticket/5501#comment:2
http://ghc.haskell.org/trac/ghc/ticket/5501#comment:2
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
</ul>
<p>
Thanks! Fixes applied. Changes in random version 1.0.1.1.
</p>
<p>
It looks like there may have been some performance regression; more testing is needed. I don't think it's due to these changes, however. (Though they do add a few extra multiplies.)
</p>
Ticket