MyNixOS website logo
Description

Read and write hexadecimal floating point numbers.

Read and write hexadecimal floating point numbers. Provides a quasiquoter for entering hex-float literals, and a function for printing them in hexadecimal.

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.

For details, please see: http://github.com/LeventErkok/FloatingHex/

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.

Metadata

Version

0.5

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows