Skip to content

Commit

Permalink
Revert, fix, and add test for fractional-part shrink
Browse files Browse the repository at this point in the history
  • Loading branch information
jobh committed Aug 5, 2023
1 parent a2dabb4 commit b47c8a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ def run_step(self):
scaled = self.current * 2**p # note: self.current may change in loop
for truncate in [math.floor, math.ceil]:
self.consider(truncate(scaled) / 2**p)

# Now try to minimize the top part of the fraction as an integer. This
# basically splits the float as k + x with 0 <= x < 1 and minimizes
# k as an integer, but without the precision issues that would have.
m, n = self.current.as_integer_ratio()
i, r = divmod(m, n)
self.call_shrinker(Integer, i, lambda k: self.consider((k * n + r) / n))
4 changes: 4 additions & 0 deletions hypothesis-python/tests/conjecture/test_float_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def test_shrink_down_to_half():
assert minimal_from(0.75, lambda x: 0 < x < 1) == 0.5


def test_shrink_fractional_part():
assert minimal_from(2.5, lambda x: divmod(x, 1)[1] == 0.5) == 1.5


def test_does_not_shrink_across_one():
# This is something of an odd special case. Because of our encoding we
# prefer all numbers >= 1 to all numbers in 0 < x < 1. For the most part
Expand Down

0 comments on commit b47c8a3

Please sign in to comment.