#5501: randomR overflow
----------------------------------+-----------------------------------------
    Reporter:  daniel.is.fischer  |       Owner:                             
        Type:  bug                |      Status:  new                        
    Priority:  normal             |   Component:  libraries/random           
     Version:  7.2.1              |    Keywords:                             
    Testcase:                     |   Blockedby:                             
          Os:  Unknown/Multiple   |    Blocking:                             
Architecture:  Unknown/Multiple   |     Failure:  Incorrect result at runtime
----------------------------------+-----------------------------------------
 When given a large range, `randomR` overflows at `Double` etc.

 `randomIvalDouble` has two problems: first, the calculation of the center,
 `(l+h)/2` overflows if the range is located near `±Infinity`; second, and
 that concerns also `randomRFloating`, the scaling factor `(h-l)` overflows
 if the range is large enough.

 Both problems can be fixed "well enough" by multiplying the bounds by 0.5
 before the calculations and scaling up at the end,
 {{{
 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
 }}}
 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).

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5501>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to