Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/hypothesis/internal/conjecture/shrinking/floats.py: 32%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

41 statements  

1# This file is part of Hypothesis, which may be found at 

2# https://github.com/HypothesisWorks/hypothesis/ 

3# 

4# Copyright the Hypothesis Authors. 

5# Individual contributors are listed in AUTHORS.rst and the git log. 

6# 

7# This Source Code Form is subject to the terms of the Mozilla Public License, 

8# v. 2.0. If a copy of the MPL was not distributed with this file, You can 

9# obtain one at https://mozilla.org/MPL/2.0/. 

10 

11import math 

12import sys 

13 

14from hypothesis.internal.conjecture.floats import float_to_lex 

15from hypothesis.internal.conjecture.shrinking.common import Shrinker 

16from hypothesis.internal.conjecture.shrinking.integer import Integer 

17 

18MAX_PRECISE_INTEGER = 2**53 

19 

20 

21class Float(Shrinker): 

22 def setup(self): 

23 self.NAN = math.nan 

24 self.debugging_enabled = True 

25 

26 def make_immutable(self, f): 

27 f = float(f) 

28 if math.isnan(f): 

29 # Always use the same NAN so it works properly in self.seen 

30 f = self.NAN 

31 return f 

32 

33 def check_invariants(self, value): 

34 # We only handle positive floats because we encode the sign separately 

35 # anyway. 

36 assert not (value < 0) 

37 

38 def left_is_better(self, left, right): 

39 lex1 = float_to_lex(left) 

40 lex2 = float_to_lex(right) 

41 return lex1 < lex2 

42 

43 def short_circuit(self): 

44 # We check for a bunch of standard "large" floats. If we're currently 

45 # worse than them and the shrink downwards doesn't help, abort early 

46 # because there's not much useful we can do here. 

47 

48 for g in [sys.float_info.max, math.inf, math.nan]: 

49 self.consider(g) 

50 

51 # If we're stuck at a nasty float don't try to shrink it further. 

52 if not math.isfinite(self.current): 

53 return True 

54 

55 def run_step(self): 

56 # above MAX_PRECISE_INTEGER, all floats are integers. Shrink like one. 

57 # TODO_BETTER_SHRINK: at 2 * MAX_PRECISE_INTEGER, n - 1 == n - 2, and 

58 # Integer.shrink will likely perform badly. We should have a specialized 

59 # big-float shrinker, which mostly follows Integer.shrink but replaces 

60 # n - 1 with next_down(n). 

61 if self.current > MAX_PRECISE_INTEGER: 

62 self.delegate(Integer, convert_to=int, convert_from=float) 

63 return 

64 

65 # Finally we get to the important bit: Each of these is a small change 

66 # to the floating point number that corresponds to a large change in 

67 # the lexical representation. Trying these ensures that our floating 

68 # point shrink can always move past these obstacles. In particular it 

69 # ensures we can always move to integer boundaries and shrink past a 

70 # change that would require shifting the exponent while not changing 

71 # the float value much. 

72 

73 # First, try dropping precision bits by rounding the scaled value. We 

74 # try values ordered from least-precise (integer) to more precise, ie. 

75 # approximate lexicographical order. Once we find an acceptable shrink, 

76 # self.consider discards the remaining attempts early and skips test 

77 # invocation. The loop count sets max fractional bits to keep, and is a 

78 # compromise between completeness and performance. 

79 

80 for p in range(10): 

81 scaled = self.current * 2**p # note: self.current may change in loop 

82 for truncate in [math.floor, math.ceil]: 

83 self.consider(truncate(scaled) / 2**p) 

84 

85 if self.consider(int(self.current)): 

86 self.debug("Just an integer now") 

87 self.delegate(Integer, convert_to=int, convert_from=float) 

88 return 

89 

90 # Now try to minimize the top part of the fraction as an integer. This 

91 # basically splits the float as k + x with 0 <= x < 1 and minimizes 

92 # k as an integer, but without the precision issues that would have. 

93 m, n = self.current.as_integer_ratio() 

94 i, r = divmod(m, n) 

95 self.call_shrinker(Integer, i, lambda k: self.consider((k * n + r) / n))