From a1248d498af178340b852738f6c41fede337e1d4 Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Sun, 22 Feb 2026 22:39:53 +1100 Subject: uniform: ensure open bounds --- src/PPL/Distr.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/PPL/Distr.hs b/src/PPL/Distr.hs index cd8f029..d265f05 100644 --- a/src/PPL/Distr.hs +++ b/src/PPL/Distr.hs @@ -3,6 +3,7 @@ {-# LANGUAGE ViewPatterns #-} module PPL.Distr where +import Debug.Trace import Data.Bits (countLeadingZeros, countTrailingZeros, shiftL, shiftR, (.&.)) import Data.Functor ((<&>)) @@ -105,9 +106,9 @@ geom p = first 0 <$> iid (bern p) uniform :: Prob Double uniform = do z <- unbounded - let r = countTrailingZeros z - 11 + let r = countTrailingZeros z - 12 e <- if r >= 0 then countTrailingZeros <$> unbounded else pure r - pure . unsafeCoerce $ (1 + (z `shiftR` 11)) `shiftR` 1 - ((unsafeCoerce e - 1011) `shiftL` 52) + pure . unsafeCoerce $ z `shiftR` 12 - ((unsafeCoerce e - 1010) `shiftL` 52) -- Uses algorithm from 10.1145/3503512 bounded :: Double -> Double -> Prob Double -- cgit v1.2.3