FloatingHex 0.2 → 0.3
raw patch · 4 files changed
+49/−16 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Numbers.FloatingHex: instance Data.Numbers.FloatingHex.FloatingHexReader GHC.Types.Double
+ Data.Numbers.FloatingHex: instance Data.Numbers.FloatingHex.FloatingHexReader GHC.Types.Float
- Data.Numbers.FloatingHex: readHFloat :: RealFloat a => String -> Maybe a
+ Data.Numbers.FloatingHex: readHFloat :: FloatingHexReader a => String -> Maybe a
Files
- CHANGES.md +9/−1
- Data/Numbers/FloatingHex.hs +33/−12
- FloatingHex.cabal +2/−2
- README.md +5/−1
CHANGES.md view
@@ -1,7 +1,15 @@ * Hackage: <http://hackage.haskell.org/package/FloatingHex> * GitHub: <http://github.com/LeventErkok/FloatingHex/> -* Latest Hackage released version: 0.2, 2017-01-14+* Latest Hackage released version: 0.3, 2017-01-15++### Version 0.3, 2017-01-15++ * Bump up template-haskell dependency to >= 2.10. As noted by Herbert Valerio Riedel,+ FloatingHex fails to compile with older versions.++ * Make the double->float conversions more robust, by avoiding the rational route.+ (Avoids issues in https://ghc.haskell.org/trac/ghc/ticket/3676) ### Version 0.2, 2017-01-14
Data/Numbers/FloatingHex.hs view
@@ -17,27 +17,48 @@ import Data.Char (toLower) import Data.Ratio ((%)) import Numeric (showHex, floatToDigits)+import GHC.Float import qualified Language.Haskell.TH.Syntax as TH import Language.Haskell.TH.Quote +-- | Due to intricacies of conversion between+-- float/doubles (see <https://ghc.haskell.org/trac/ghc/ticket/3676>), we explicitly introduce+-- a class to do the reading properly.+class RealFloat a => FloatingHexReader a where+ -- | Convert a hex-float to a Real-Float, if possible+ readHFloat :: String -> Maybe a++-- | The Float instance+instance FloatingHexReader Float where+ readHFloat s = double2Float `fmap` readHFloatAsDouble s++-- | The Double instance+instance FloatingHexReader Double where+ readHFloat = readHFloatAsDouble+ -- | Read a float in hexadecimal binary format. Supports negative numbers, and nan/infinity as well. -- For regular usage, the quasiquoter (`hf`) should be employed. But this function can be handy for -- programmatic interfaces.-readHFloat :: RealFloat a => String -> Maybe a-readHFloat = cvt+readHFloatAsDouble :: String -> Maybe Double+readHFloatAsDouble = cvt where cvt ('-' : cs) = ((-1) *) `fmap` go cs cvt cs = go cs go "NaN" = Just $ 0/0 go "Infinity" = Just $ 1/0- go cs = (fromRational . toRational) `fmap` parseHexFloat cs+ go cs = parseHexFloat cs -- | Turn a hexadecimal float to an internal double, if parseable. Does not support the leading--- sign bit.+-- '-' bit, although it does allow a leading +. (The former is best done out of the quasiquote,+-- since TH does not cannot represent negative 0! See <https://ghc.haskell.org/trac/ghc/ticket/13124>+-- for why we avoid this here.) parseHexFloat :: String -> Maybe Double-parseHexFloat = go0 . map toLower- where go0 ('0':'x':rest) = go1 rest+parseHexFloat = goS . map toLower+ where goS ('+':rest) = go0 rest+ goS cs = go0 cs++ go0 ('0':'x':rest) = go1 rest go0 _ = Nothing go1 cs = case break (== 'p') cs of@@ -60,12 +81,12 @@ val :: Integer -> Int -> Integer -> Double val a b e- | e > 0 = fromRational $ (top * expt) % bot- | True = fromRational $ top % (expt * bot)- where top, bot, expt :: Integer- top = a- bot = 16 ^ b- expt = 2 ^ abs e+ | e > 0 = fromRational $ (top * power) % bot+ | True = fromRational $ top % (power * bot)+ where top, bot, power :: Integer+ top = a+ bot = 16 ^ b+ power = 2 ^ abs e -- | A quasiquoter for hexadecimal floating-point literals. -- See: <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>, pages 57-58.
FloatingHex.cabal view
@@ -1,5 +1,5 @@ Name: FloatingHex-Version: 0.2+Version: 0.3 Synopsis: Read and write hexadecimal floating point numbers Description: Read and write hexadecimal floating point numbers. Provides a quasiquoter for entering hex-float literals, and a function for printing them in hexadecimal.@@ -26,5 +26,5 @@ Library ghc-options : -Wall default-language: Haskell2010- Build-Depends : base >= 4 && < 5, template-haskell+ Build-Depends : base >= 4 && < 5, template-haskell >= 2.10 Exposed-modules : Data.Numbers.FloatingHex
README.md view
@@ -39,4 +39,8 @@ test = showHFloat [hf|0x1.f44abd5aa7ca4p+25|] "" ``` -(Note that while the quasiquoter allows for floating-point patterns, it is usually not a good idea to have floating-point literals used in pattern matching.)+(Note that while the quasiquoter allows for floating-point patterns, it is usually not a good idea to use floating-point literals in pattern matching.)++### Thanks+The following people reported bugs, provided comments/feedback, or contributed to the development of+FloatingHex in various ways: Herbert Valerio Riedel.