Skip to content

Generators for floating point numbers #6

@Shimuuar

Description

@Shimuuar

This question surfaced in discussion on PR #1 but it wasn't discussed in any depth. Here is quote and links from @cartazio:

There’s several gotchas with those algorithms for float and double.

The usual unit interval used in extent Haskell Libs is wrong.

See the commented out bit linked herein
https://github.com/haskell/random/blob/master/src/Data/Distribution/FloatingInterval.hs

@idontgetoutmuch

Here's another implementation for getting random floating point numbers

https://github.com/mokus0/random-fu/blob/69a563a7b0cf444748e4b38a8bda7ada0b9acf14/random-source/src/Data/Random/Internal/Words.hs#L103

wordToFloat :: Word64 -> Float
wordToFloat x = (encodeFloat $! toInteger (x .&. 0x007fffff {- 2^23-1 -} )) $ (-23)

I think it would be convenient to put such a function in random rather than lots of packages implementing it themselves but I don't feel strongly about it.

It seems there's general consensus that we should include generators for floating point numbers but very little discussion on implementation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions