Skip to content

Commit 31fb904

Browse files
committed
Apply filter to float_with_bounds
Computing floats when given bounds such as min -9.9e15 and max -1 will result in the computation `-1.0 - -9.9e15` yielding 9900000000000000.0 instead of 9899999999999999.0, which makes the generator return 0. This is a classic floating point loss of significance error. Fixes #203
1 parent 1f220e8 commit 31fb904

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

lib/stream_data.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,9 @@ defmodule StreamData do
17561756
bind(exponent_data, fn exponent ->
17571757
map(float_in_0_to_1(exponent), fn float -> float * (max - min) + min end)
17581758
end)
1759+
|> filter(fn float ->
1760+
min <= float and float <= max
1761+
end)
17591762
end)
17601763
end
17611764

test/stream_data_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ defmodule StreamDataTest do
330330
assert float >= -1.12 and float <= 4.01
331331
end
332332
end
333+
334+
property "with a large negative :min and :max option" do
335+
# The large negative min might cause loss of significance.
336+
check all float <- float(min: -9.9e15, max: -1) do
337+
assert is_float(float)
338+
assert float <= -1
339+
end
340+
end
333341
end
334342

335343
property "byte/0" do

0 commit comments

Comments
 (0)