Skip to content

Instantly share code, notes, and snippets.

@kini
Created April 14, 2013 05:27
Show Gist options
  • Save kini/5381567 to your computer and use it in GitHub Desktop.
Save kini/5381567 to your computer and use it in GitHub Desktop.
A function for computing non-integer representations ( http://en.wikipedia.org/wiki/Non-integer_representation )
-- | baserep base num digits = (mantissa, exponent)
baserep :: (RealFrac a) => a -> a -> Int -> ([Int], Int)
baserep _ _ 0 = ([], 0)
baserep base num digits
| num < 0 = undefined
| num >= base = let (x, y) = baserep base (num / base) digits in (x, y+1)
| otherwise = let d = fromIntegral (floor num)
(x, y) = baserep base ((num - fromIntegral d) * base) (digits - 1)
in (d : x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment