-- | Generally useful generators not present in the upstream release
-- of Hedgehog
module Hedgehog.Gen.Double (doubleInc) where

import Data.Bits (shiftL)
import Data.Word (Word64)
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

-- | Generates a Double in the inclusive range [0, 1]
doubleInc :: Gen Double
doubleInc :: Gen Double
doubleInc = do
  b <- Range Word64 -> GenT Identity Word64
forall (m :: * -> *). MonadGen m => Range Word64 -> m Word64
Gen.word64 (Word64 -> Word64 -> Range Word64
forall a. Integral a => a -> a -> Range a
Range.linear Word64
1 Word64
hi)
  a <- Gen.word64 (Range.linear 0 b)
  return $ (fromIntegral a) / (fromIntegral b)
  where
    hi :: Word64
hi = Word64
2 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
48 :: Word64