FloatingHex

Read and write hexadecimal floating point numbers

LTS Haskell 23.1:0.5
Stackage Nightly 2024-12-22:0.5
Latest on Hackage:0.5

See all snapshots FloatingHex appears in

BSD-3-Clause licensed by Levent Erkok
Maintained by [email protected]
This version can be pinned in stack with:FloatingHex-0.5@sha256:a1f524ed4f1ba087a952146f85969030e1b90ad7e55ba3849bb2162d63df3af0,1333

Module documentation for 0.5

FloatingHex: Read/Write Hexadecimal floats

Hackage version Build Status

Hexadecimal Floats

For syntax reference, see: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, pages 57-58. We slightly diverge from the standard and do not allow for the “floating-suffix,” as the type inference of Haskell makes this unnecessary. Some examples are:

  [hf|0x1p+1|]
  [hf|0x1p+8|]
  [hf|0x1.b7p-1|]
  [hf|0x1.fffffffffffffp+1023|]
  [hf|0X1.921FB4D12D84AP-1|]

This format allows for concise and precise string representation for floating point numbers. Note that you need the QuasiQuotes extension of GHC to be able to write these literals.

Example

{-# LANGUAGE QuasiQuotes #-}
import Data.Numbers.FloatingHex

-- expressions
f :: Double
f = [hf|0x1.f44abd5aa7ca4p+25|]

-- patterns
g :: Float -> String
g [hf|0x1p1|]  = "two!"
g [hf|0x1p-1|] = "half!"
g d            = "something else: " ++ show d

-- showing hexadecimal floats
test = showHFloat [hf|0x1.f44abd5aa7ca4p+25|] ""

(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.

Changes

Version 0.5, 2020-09-05

  • Changes to make it work with GHC 8.10.2

Version 0.4, 2017-01-15

  • Export the new FloatingHexReader class. Useful when used as an API.

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

  • Support for parsing nan/infinity values
  • Make the printer compliant with printf %a modifier in C

Version 0.1, 2017-01-14

  • First implementation. The quasiquoter and the pretty-printer are implemented.