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() will return 0.0 as a valid value if min is a very small number and max is approaching 0 from the neg. side #203

Open
conpat opened this issue Oct 30, 2024 · 2 comments
Labels

Comments

@conpat
Copy link

conpat commented Oct 30, 2024

Testing some edge cases I came across an unexpected behaviour of float

StreamData.float() will include 0.0 when:

  • negative min and max
  • max approaching 0.0 (but not touching it)
  • min bing a very big negative number.
iex()> StreamData.float(min: -9.9e25, max: -0.1) |> Enum.take(10)
[0.0, -9.9e25, 0.0, -8.6625e25, -2.4749999999999994e25, -6.0328125e25, -9.9e25,
 -1.2374999999999997e25, -1.3921875e25, 0.0]

This should not contain any 0.0

On the other side. These variations work as expected

iex()> StreamData.float(min: -9, max: -0.1) |> Enum.take(10)
[-0.09999999999999964, -9.0, -0.09999999999999964, -0.09999999999999964, -9.0,
 -9.0, -6.21875, -0.09999999999999964, -7.8875, -3.715625]

iex()> StreamData.float(max: -0.1) |> Enum.take(10)
[-0.1, -5.1, -0.1, -0.6625, -1.0375, -0.85, -1.084375, -61.4203125,
 -397.414453125, -21.90859375]

iex()> StreamData.float(min: 0.1, max: 9.9e25) |> Enum.take(10)
[4.95e25, 9.9e25, 6.1874999999999994e25, 9.9e25, 0.1, 5.878125e25, 9.9e25,
 3.673828125e25, 2.475e25, 5.64609375e25]

iex()> StreamData.float(min: 0.1) |> Enum.take(10)
[1.1, 4.1, 0.85, 2.0375, 10.81875, 5.428125, 0.7015625, 0.271875, 384.887109375,
 0.7806640625]
@conpat
Copy link
Author

conpat commented Oct 30, 2024

I just realised StreamData.float(min: -9, max: -0.1) is also not behaving as expected.

iex()> StreamData.float(min: -9, max: -0.1) |> Enum.take(10)
[-0.09999999999999964, -9.0, -0.09999999999999964, -0.09999999999999964, -9.0,
 -9.0, -6.21875, -0.09999999999999964, -7.8875, -3.715625]

max is -0.1 but -0.1 < -0.09999999999999964

@whatyouhide
Copy link
Owner

Yeah floats are nasty. I don't have bandwidth to look into this now, do you want to try and put together a fix for this? We can probably filter floats after generating them or something like that.

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

2 participants