Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float only returns number between 0 and 1 #63

Open
Ana06 opened this issue Jan 2, 2019 · 7 comments
Open

float only returns number between 0 and 1 #63

Ana06 opened this issue Jan 2, 2019 · 7 comments
Labels

Comments

@Ana06
Copy link
Member

Ana06 commented Jan 2, 2019

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

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

@Ana06 Ana06 added the bug 🐛 label Jan 2, 2019
@Ana06 Ana06 mentioned this issue Jan 2, 2019
@Stratus3D
Copy link
Contributor

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.

@Ana06
Copy link
Member Author

Ana06 commented Jan 4, 2019

@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.

@Stratus3D
Copy link
Contributor

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).

@manueljacob
Copy link

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.

@Qqwy
Copy link

Qqwy commented Jun 17, 2019

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
Copy link

Qqwy commented Jun 19, 2019

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 })

@bbugh
Copy link

bbugh commented Oct 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants