Skip to content

Instantly share code, notes, and snippets.

@llelf
Last active December 19, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llelf/1bd631f0d3f9df2ba8c0 to your computer and use it in GitHub Desktop.
Save llelf/1bd631f0d3f9df2ba8c0 to your computer and use it in GitHub Desktop.
module PI where
import Data.Number.CReal
import Data.List
fractPartToBase :: RealFrac x => Int -> x -> [Int]
fractPartToBase base x = unfoldr f x
where f d = Just (d'int, d' - fromIntegral d'int)
where d' = d * fromIntegral base
d'int = floor d'
{-
λ> take 100 $ fractPartToBase 12 (pi - 3 :: CReal)
[1,8,4,8,0,9,4,9,3,11,9,1,8,6,6,4,5,7,3,10,6,2,1,1,11,11,1,5,1,5,5,1,10,0,5,7,2,9,2,9,0,10,7,8,0,9,10,4,9,2,7,4,2,1,4,0,10,6,0,10,5,5,2,5,6,10,0,6,6,1,10,0,3,7,5,3,10,3,10,10,5,4,8,0,5,6,4,6,8,8,0,1,8,1,10,3,6,8,3,0]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment