rantly
rantly copied to clipboard
float only returns number between 0 and 1
Rantly { float } #=> a float between 0 and 1
I think this is not expected and it should return higher floats as well. :thinking:
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 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.
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).
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.
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.
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 })
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.