test.check

Include a float generator

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

There seems to be a demand for a general float generator.

One of the bigger issues with a float generator is shrinking, which is not straightforward. Reid Draper intially considered including a small implementation, using fmap to coerce a ratio into a float:

(def float (gen/fmap float gen/ratio))
(gen/sample float 20)
;; => (0.0 0.0 -1.0 0.0 -1.0 0.75 0.5 0.8 -2.0 -0.5714286 2.6666667 -2.0
;;     -0.46153846 -0.125 -12.0 -1.75 -2.4 0.41666666 0.64285713 -1.125)

The Haskell implementation is performing float shrinking through fractions.

Chas Emerick implemented a bigdec generator using ideas from data.generators, but mentioned that manipulating the long and int bits would be more performant. However, such shrinking would require good knowledge on how IEEE 754 works, in order to avoid bugs whenever shrinking is performed.

A slower, but more understandable and portable double generator was also suggested, again by Chas Emerick. The implementation of said generator could be found at https://gist.github.com/cemerick/7599452

Original issue - GitHub #36

Activity

Hide
Gary Fredericks added a comment -

I've written one and added it to test.chuck here.

It scales exponentially with the size so it very quickly fills the whole double range (I think once size > 53 or so).

It will generate +/-Infinity but not NaN.

It shrinks in an okayish way.

I'm not sure what the design goals are here, so don't know if it's worth submitting yet.

Show
Gary Fredericks added a comment - I've written one and added it to test.chuck here. It scales exponentially with the size so it very quickly fills the whole double range (I think once size > 53 or so). It will generate +/-Infinity but not NaN. It shrinks in an okayish way. I'm not sure what the design goals are here, so don't know if it's worth submitting yet.

People

Vote (3)
Watch (3)

Dates

  • Created:
    Updated: