rantly icon indicating copy to clipboard operation
rantly copied to clipboard

float only returns number between 0 and 1

Open Ana06 opened this issue 6 years ago • 7 comments

Rantly { float } #=> a float between 0 and 1

I think this is not expected and it should return higher floats as well. :thinking:

Ana06 avatar Jan 02 '19 10:01 Ana06

I can't speak to whether this is a bug or not. But I think the current behavior would a reasonable default for this generator.

Stratus3D avatar Jan 02 '19 20:01 Stratus3D

@Stratus3D I am not sure if I understand you correctly, do you want to keep this behavior? I think it is not expected that float return floats in a range instead of any float as integer does.

Ana06 avatar Jan 04 '19 10:01 Ana06

I guess I'm not sure how we would generate floats of infinite size. Although apparently it is possible. The Erlang library I'm familiar with generates floats with a range of infinity to infinity (https://proper-testing.github.io/apidocs/proper_types.html#float-2).

Stratus3D avatar Jan 04 '19 16:01 Stratus3D

I agree that Rantly { float } should return all kinds of Floats (finite across the whole range, infinite, different NaNs). When used as test case generator, it could be problematic if only Floats in the range between 0.0 and 1.0 are generated — it’s not obvious when scanning over the test code.

manueljacob avatar Mar 08 '19 00:03 manueljacob

It's fine to have a generator that only produces values in the range [0...1], but calling this float is highly confusing and breaks the principle of least astonishment because floats are able to contain many more numbers.

Qqwy avatar Jun 17 '19 21:06 Qqwy

By the way, the original QuickCheck implementation in Haskell defines floats in a way similar to this Ruby equivalent:

def fraction(a, b, c)
    a.to_f + b.to_f / ((c.to_f.abs) + 1.0)
end

def float
  fraction(integer, integer, integer)
end

(where integer is a function that generates an integer based on the current RNG and size, e.g. Rantly.value { integer })

Qqwy avatar Jun 19 '19 11:06 Qqwy

Not sure if this is helpful more than a year later, but in case anyone else comes across this, you can do math in in Rantly blocks on the property methods. It's easy to use this float method to produce the values you want. For example, if you wanted 4 floats in the range of 3..11:

>> Rantly(4) { range(3, 10) * float }
=> [3.3218256844886724, 4.588350561344799, 4.528890728138369, 1.6470508044388694]

As far as readability goes, I think this is quite clear.

bbugh avatar Oct 12 '20 20:10 bbugh