| 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 |
|
| 11 |
import math |
| 12 |
from decimal import Decimal |
| 13 |
from fractions import Fraction |
| 14 |
from typing import Literal, cast |
| 15 |
|
| 16 |
from hypothesis.control import reject |
| 17 |
from hypothesis.errors import InvalidArgument |
| 18 |
from hypothesis.internal.conjecture.data import ConjectureData |
| 19 |
from hypothesis.internal.filtering import ( |
| 20 |
get_float_predicate_bounds, |
| 21 |
get_integer_predicate_bounds, |
| 22 |
) |
| 23 |
from hypothesis.internal.floats import ( |
| 24 |
SMALLEST_SUBNORMAL, |
| 25 |
float_of, |
| 26 |
float_to_int, |
| 27 |
int_to_float, |
| 28 |
is_negative, |
| 29 |
next_down, |
| 30 |
next_down_normal, |
| 31 |
next_up, |
| 32 |
next_up_normal, |
| 33 |
width_smallest_normals, |
| 34 |
) |
| 35 |
from hypothesis.internal.validation import ( |
| 36 |
check_type, |
| 37 |
check_valid_bound, |
| 38 |
check_valid_interval, |
| 39 |
) |
| 40 |
from hypothesis.strategies._internal.misc import nothing |
| 41 |
from hypothesis.strategies._internal.strategies import ( |
| 42 |
SampledFromStrategy, |
| 43 |
SearchStrategy, |
| 44 |
) |
| 45 |
from hypothesis.strategies._internal.utils import cacheable, defines_strategy |
| 46 |
|
| 47 |
# See https://github.com/python/mypy/issues/3186 - numbers.Real is wrong! |
| 48 |
Real = int | float | Fraction | Decimal |
| 49 |
|
| 50 |
|
| 51 |
class IntegersStrategy(SearchStrategy[int]): |
| 52 |
def __init__(self, start: int | None, end: int | None) -> None: |
| 53 |
super().__init__() |
| 54 |
assert isinstance(start, int) or start is None |
| 55 |
assert isinstance(end, int) or end is None |
| 56 |
assert start is None or end is None or start <= end |
| 57 |
self.start = start |
| 58 |
self.end = end |
| 59 |
|
| 60 |
def __repr__(self) -> str: |
| 61 |
if self.start is None and self.end is None: |
| 62 |
return "integers()" |
| 63 |
if self.end is None: |
| 64 |
return f"integers(min_value={self.start})" |
| 65 |
if self.start is None: |
| 66 |
return f"integers(max_value={self.end})" |
| 67 |
return f"integers({self.start}, {self.end})" |
| 68 |
|
| 69 |
def do_draw(self, data: ConjectureData) -> int: |
| 70 |
# For bounded integers, make the bounds and near-bounds more likely. |
| 71 |
weights = None |
| 72 |
if ( |
| 73 |
self.end is not None |
| 74 |
and self.start is not None |
| 75 |
and self.end - self.start > 127 |
| 76 |
): |
| 77 |
weights = { |
| 78 |
self.start: (2 / 128), |
| 79 |
self.start + 1: (1 / 128), |
| 80 |
self.end - 1: (1 / 128), |
| 81 |
self.end: (2 / 128), |
| 82 |
} |
| 83 |
|
| 84 |
return data.draw_integer( |
| 85 |
min_value=self.start, max_value=self.end, weights=weights |
| 86 |
) |
| 87 |
|
| 88 |
def filter(self, condition): |
| 89 |
if condition is math.isfinite: |
| 90 |
return self |
| 91 |
if condition in [math.isinf, math.isnan]: |
| 92 |
return nothing() |
| 93 |
constraints, pred = get_integer_predicate_bounds(condition) |
| 94 |
|
| 95 |
start, end = self.start, self.end |
| 96 |
if "min_value" in constraints: |
| 97 |
start = max(constraints["min_value"], -math.inf if start is None else start) |
| 98 |
if "max_value" in constraints: |
| 99 |
end = min(constraints["max_value"], math.inf if end is None else end) |
| 100 |
|
| 101 |
if start != self.start or end != self.end: |
| 102 |
if start is not None and end is not None and start > end: |
| 103 |
return nothing() |
| 104 |
self = type(self)(start, end) |
| 105 |
if pred is None: |
| 106 |
return self |
| 107 |
return super().filter(pred) |
| 108 |
|
| 109 |
|
| 110 |
@cacheable |
| 111 |
@defines_strategy(force_reusable_values=True) |
| 112 |
def integers( |
| 113 |
min_value: int | None = None, |
| 114 |
max_value: int | None = None, |
| 115 |
) -> SearchStrategy[int]: |
| 116 |
"""Returns a strategy which generates integers. |
| 117 |
|
| 118 |
If min_value is not None then all values will be >= min_value. If |
| 119 |
max_value is not None then all values will be <= max_value |
| 120 |
|
| 121 |
Examples from this strategy will shrink towards zero, and negative values |
| 122 |
will also shrink towards positive (i.e. -n may be replaced by +n). |
| 123 |
""" |
| 124 |
check_valid_bound(min_value, "min_value") |
| 125 |
check_valid_bound(max_value, "max_value") |
| 126 |
check_valid_interval(min_value, max_value, "min_value", "max_value") |
| 127 |
|
| 128 |
if min_value is not None: |
| 129 |
if min_value != int(min_value): |
| 130 |
raise InvalidArgument( |
| 131 |
f"min_value={min_value!r} of type {type(min_value)!r} " |
| 132 |
"cannot be exactly represented as an integer." |
| 133 |
) |
| 134 |
min_value = int(min_value) |
| 135 |
if max_value is not None: |
| 136 |
if max_value != int(max_value): |
| 137 |
raise InvalidArgument( |
| 138 |
f"max_value={max_value!r} of type {type(max_value)!r} " |
| 139 |
"cannot be exactly represented as an integer." |
| 140 |
) |
| 141 |
max_value = int(max_value) |
| 142 |
|
| 143 |
return IntegersStrategy(min_value, max_value) |
| 144 |
|
| 145 |
|
| 146 |
class FloatStrategy(SearchStrategy[float]): |
| 147 |
"""A strategy for floating point numbers.""" |
| 148 |
|
| 149 |
def __init__( |
| 150 |
self, |
| 151 |
*, |
| 152 |
min_value: float, |
| 153 |
max_value: float, |
| 154 |
allow_nan: bool, |
| 155 |
# The smallest nonzero number we can represent is usually a subnormal, but may |
| 156 |
# be the smallest normal if we're running in unsafe denormals-are-zero mode. |
| 157 |
# While that's usually an explicit error, we do need to handle the case where |
| 158 |
# the user passes allow_subnormal=False. |
| 159 |
smallest_nonzero_magnitude: float = SMALLEST_SUBNORMAL, |
| 160 |
): |
| 161 |
super().__init__() |
| 162 |
assert isinstance(allow_nan, bool) |
| 163 |
assert smallest_nonzero_magnitude >= 0.0, "programmer error if this is negative" |
| 164 |
if smallest_nonzero_magnitude == 0.0: # pragma: no cover |
| 165 |
raise FloatingPointError( |
| 166 |
"Got allow_subnormal=True, but we can't represent subnormal floats " |
| 167 |
"right now, in violation of the IEEE-754 floating-point " |
| 168 |
"specification. This is usually because something was compiled with " |
| 169 |
"-ffast-math or a similar option, which sets global processor state. " |
| 170 |
"See https://simonbyrne.github.io/notes/fastmath/ for a more detailed " |
| 171 |
"writeup - and good luck!" |
| 172 |
) |
| 173 |
self.min_value = min_value |
| 174 |
self.max_value = max_value |
| 175 |
self.allow_nan = allow_nan |
| 176 |
self.smallest_nonzero_magnitude = smallest_nonzero_magnitude |
| 177 |
|
| 178 |
def __repr__(self) -> str: |
| 179 |
return ( |
| 180 |
f"{self.__class__.__name__}({self.min_value=}, {self.max_value=}, " |
| 181 |
f"{self.allow_nan=}, {self.smallest_nonzero_magnitude=})" |
| 182 |
).replace("self.", "") |
| 183 |
|
| 184 |
def do_draw(self, data: ConjectureData) -> float: |
| 185 |
return data.draw_float( |
| 186 |
min_value=self.min_value, |
| 187 |
max_value=self.max_value, |
| 188 |
allow_nan=self.allow_nan, |
| 189 |
smallest_nonzero_magnitude=self.smallest_nonzero_magnitude, |
| 190 |
) |
| 191 |
|
| 192 |
def filter(self, condition): |
| 193 |
# Handle a few specific weird cases. |
| 194 |
if condition is math.isfinite: |
| 195 |
return FloatStrategy( |
| 196 |
min_value=max(self.min_value, next_up(float("-inf"))), |
| 197 |
max_value=min(self.max_value, next_down(float("inf"))), |
| 198 |
allow_nan=False, |
| 199 |
smallest_nonzero_magnitude=self.smallest_nonzero_magnitude, |
| 200 |
) |
| 201 |
if condition is math.isinf: |
| 202 |
if permitted_infs := [ |
| 203 |
x |
| 204 |
for x in (-math.inf, math.inf) |
| 205 |
if self.min_value <= x <= self.max_value |
| 206 |
]: |
| 207 |
return SampledFromStrategy(permitted_infs) |
| 208 |
return nothing() |
| 209 |
if condition is math.isnan: |
| 210 |
if not self.allow_nan: |
| 211 |
return nothing() |
| 212 |
return NanStrategy() |
| 213 |
|
| 214 |
constraints, pred = get_float_predicate_bounds(condition) |
| 215 |
if not constraints: |
| 216 |
return super().filter(pred) |
| 217 |
min_bound = max(constraints.get("min_value", -math.inf), self.min_value) |
| 218 |
max_bound = min(constraints.get("max_value", math.inf), self.max_value) |
| 219 |
|
| 220 |
# Adjustments for allow_subnormal=False, if any need to be made |
| 221 |
if -self.smallest_nonzero_magnitude < min_bound < 0: |
| 222 |
min_bound = -0.0 |
| 223 |
elif 0 < min_bound < self.smallest_nonzero_magnitude: |
| 224 |
min_bound = self.smallest_nonzero_magnitude |
| 225 |
if -self.smallest_nonzero_magnitude < max_bound < 0: |
| 226 |
max_bound = -self.smallest_nonzero_magnitude |
| 227 |
elif 0 < max_bound < self.smallest_nonzero_magnitude: |
| 228 |
max_bound = 0.0 |
| 229 |
|
| 230 |
if min_bound > max_bound: |
| 231 |
return nothing() |
| 232 |
if ( |
| 233 |
min_bound > self.min_value |
| 234 |
or self.max_value > max_bound |
| 235 |
or (self.allow_nan and (-math.inf < min_bound or max_bound < math.inf)) |
| 236 |
): |
| 237 |
self = type(self)( |
| 238 |
min_value=min_bound, |
| 239 |
max_value=max_bound, |
| 240 |
allow_nan=False, |
| 241 |
smallest_nonzero_magnitude=self.smallest_nonzero_magnitude, |
| 242 |
) |
| 243 |
if pred is None: |
| 244 |
return self |
| 245 |
return super().filter(pred) |
| 246 |
|
| 247 |
|
| 248 |
@cacheable |
| 249 |
@defines_strategy(force_reusable_values=True) |
| 250 |
def floats( |
| 251 |
min_value: Real | None = None, |
| 252 |
max_value: Real | None = None, |
| 253 |
*, |
| 254 |
allow_nan: bool | None = None, |
| 255 |
allow_infinity: bool | None = None, |
| 256 |
allow_subnormal: bool | None = None, |
| 257 |
width: Literal[16, 32, 64] = 64, |
| 258 |
exclude_min: bool = False, |
| 259 |
exclude_max: bool = False, |
| 260 |
) -> SearchStrategy[float]: |
| 261 |
"""Returns a strategy which generates floats. |
| 262 |
|
| 263 |
- If min_value is not None, all values will be ``>= min_value`` |
| 264 |
(or ``> min_value`` if ``exclude_min``). |
| 265 |
- If max_value is not None, all values will be ``<= max_value`` |
| 266 |
(or ``< max_value`` if ``exclude_max``). |
| 267 |
- If min_value or max_value is not None, it is an error to enable |
| 268 |
allow_nan. |
| 269 |
- If both min_value and max_value are not None, it is an error to enable |
| 270 |
allow_infinity. |
| 271 |
- If inferred values range does not include subnormal values, it is an error |
| 272 |
to enable allow_subnormal. |
| 273 |
|
| 274 |
Where not explicitly ruled out by the bounds, |
| 275 |
:wikipedia:`subnormals <Subnormal_number>`, infinities, and NaNs are possible |
| 276 |
values generated by this strategy. |
| 277 |
|
| 278 |
The width argument specifies the maximum number of bits of precision |
| 279 |
required to represent the generated float. Valid values are 16, 32, or 64. |
| 280 |
Passing ``width=32`` will still use the builtin 64-bit :class:`~python:float` class, |
| 281 |
but always for values which can be exactly represented as a 32-bit float. |
| 282 |
|
| 283 |
The exclude_min and exclude_max argument can be used to generate numbers |
| 284 |
from open or half-open intervals, by excluding the respective endpoints. |
| 285 |
Excluding either signed zero will also exclude the other. |
| 286 |
Attempting to exclude an endpoint which is None will raise an error; |
| 287 |
use ``allow_infinity=False`` to generate finite floats. You can however |
| 288 |
use e.g. ``min_value=-math.inf, exclude_min=True`` to exclude only |
| 289 |
one infinite endpoint. |
| 290 |
|
| 291 |
Examples from this strategy have a complicated and hard to explain |
| 292 |
shrinking behaviour, but it tries to improve "human readability". Finite |
| 293 |
numbers will be preferred to infinity and infinity will be preferred to |
| 294 |
NaN. |
| 295 |
""" |
| 296 |
check_type(bool, exclude_min, "exclude_min") |
| 297 |
check_type(bool, exclude_max, "exclude_max") |
| 298 |
|
| 299 |
if allow_nan is None: |
| 300 |
allow_nan = bool(min_value is None and max_value is None) |
| 301 |
elif allow_nan and (min_value is not None or max_value is not None): |
| 302 |
raise InvalidArgument(f"Cannot have {allow_nan=}, with min_value or max_value") |
| 303 |
|
| 304 |
if width not in (16, 32, 64): |
| 305 |
raise InvalidArgument( |
| 306 |
f"Got {width=}, but the only valid values " |
| 307 |
"are the integers 16, 32, and 64." |
| 308 |
) |
| 309 |
# Literal[16] accepts both 16 and 16.0. Normalize to the int 16 here, mainly |
| 310 |
# for mypyc. We want to support width=16.0 to make e.g. width=mywidth / 2 for |
| 311 |
# mywidth=32 easy. |
| 312 |
width = cast(Literal[16, 32, 64], int(width)) |
| 313 |
|
| 314 |
check_valid_bound(min_value, "min_value") |
| 315 |
check_valid_bound(max_value, "max_value") |
| 316 |
|
| 317 |
if math.copysign(1.0, -0.0) == 1.0: # pragma: no cover |
| 318 |
raise FloatingPointError( |
| 319 |
"Your Python install can't represent -0.0, which is required by the " |
| 320 |
"IEEE-754 floating-point specification. This is probably because it was " |
| 321 |
"compiled with an unsafe option like -ffast-math; for a more detailed " |
| 322 |
"explanation see https://simonbyrne.github.io/notes/fastmath/" |
| 323 |
) |
| 324 |
if allow_subnormal and next_up(0.0, width=width) == 0: # pragma: no cover |
| 325 |
# Not worth having separate CI envs and dependencies just to cover this branch; |
| 326 |
# discussion in https://github.com/HypothesisWorks/hypothesis/issues/3092 |
| 327 |
# |
| 328 |
# Erroring out here ensures that the database contents are interpreted |
| 329 |
# consistently - which matters for such a foundational strategy, even if it's |
| 330 |
# not always true for all user-composed strategies further up the stack. |
| 331 |
from _hypothesis_ftz_detector import identify_ftz_culprits |
| 332 |
|
| 333 |
try: |
| 334 |
ftz_pkg = identify_ftz_culprits() |
| 335 |
except Exception: |
| 336 |
ftz_pkg = None |
| 337 |
if ftz_pkg: |
| 338 |
ftz_msg = ( |
| 339 |
f"This seems to be because the `{ftz_pkg}` package was compiled with " |
| 340 |
f"-ffast-math or a similar option, which sets global processor state " |
| 341 |
f"- see https://simonbyrne.github.io/notes/fastmath/ for details. " |
| 342 |
f"If you don't know why {ftz_pkg} is installed, `pipdeptree -rp " |
| 343 |
f"{ftz_pkg}` will show which packages depend on it." |
| 344 |
) |
| 345 |
else: |
| 346 |
ftz_msg = ( |
| 347 |
"This is usually because something was compiled with -ffast-math " |
| 348 |
"or a similar option, which sets global processor state. See " |
| 349 |
"https://simonbyrne.github.io/notes/fastmath/ for a more detailed " |
| 350 |
"writeup - and good luck!" |
| 351 |
) |
| 352 |
raise FloatingPointError( |
| 353 |
f"Got {allow_subnormal=}, but we can't represent " |
| 354 |
f"subnormal floats right now, in violation of the IEEE-754 floating-point " |
| 355 |
f"specification. {ftz_msg}" |
| 356 |
) |
| 357 |
|
| 358 |
min_arg, max_arg = min_value, max_value |
| 359 |
if min_value is not None: |
| 360 |
min_value = float_of(min_value, width) |
| 361 |
assert isinstance(min_value, float) |
| 362 |
if max_value is not None: |
| 363 |
max_value = float_of(max_value, width) |
| 364 |
assert isinstance(max_value, float) |
| 365 |
|
| 366 |
if min_value != min_arg: |
| 367 |
raise InvalidArgument( |
| 368 |
f"min_value={min_arg!r} cannot be exactly represented as a float " |
| 369 |
f"of width {width} - use {min_value=} instead." |
| 370 |
) |
| 371 |
if max_value != max_arg: |
| 372 |
raise InvalidArgument( |
| 373 |
f"max_value={max_arg!r} cannot be exactly represented as a float " |
| 374 |
f"of width {width} - use {max_value=} instead." |
| 375 |
) |
| 376 |
|
| 377 |
if exclude_min and (min_value is None or min_value == math.inf): |
| 378 |
raise InvalidArgument(f"Cannot exclude {min_value=}") |
| 379 |
if exclude_max and (max_value is None or max_value == -math.inf): |
| 380 |
raise InvalidArgument(f"Cannot exclude {max_value=}") |
| 381 |
|
| 382 |
assumed_allow_subnormal = allow_subnormal is None or allow_subnormal |
| 383 |
if min_value is not None and ( |
| 384 |
exclude_min or (min_arg is not None and min_value < min_arg) |
| 385 |
): |
| 386 |
min_value = next_up_normal( |
| 387 |
min_value, width, allow_subnormal=assumed_allow_subnormal |
| 388 |
) |
| 389 |
if min_value == min_arg: |
| 390 |
assert min_value == min_arg == 0 |
| 391 |
assert is_negative(min_arg) |
| 392 |
assert not is_negative(min_value) |
| 393 |
min_value = next_up_normal( |
| 394 |
min_value, width, allow_subnormal=assumed_allow_subnormal |
| 395 |
) |
| 396 |
assert min_value > min_arg # type: ignore |
| 397 |
if max_value is not None and ( |
| 398 |
exclude_max or (max_arg is not None and max_value > max_arg) |
| 399 |
): |
| 400 |
max_value = next_down_normal( |
| 401 |
max_value, width, allow_subnormal=assumed_allow_subnormal |
| 402 |
) |
| 403 |
if max_value == max_arg: |
| 404 |
assert max_value == max_arg == 0 |
| 405 |
assert is_negative(max_value) |
| 406 |
assert not is_negative(max_arg) |
| 407 |
max_value = next_down_normal( |
| 408 |
max_value, width, allow_subnormal=assumed_allow_subnormal |
| 409 |
) |
| 410 |
assert max_value < max_arg # type: ignore |
| 411 |
|
| 412 |
if min_value == -math.inf: |
| 413 |
min_value = None |
| 414 |
if max_value == math.inf: |
| 415 |
max_value = None |
| 416 |
|
| 417 |
bad_zero_bounds = ( |
| 418 |
min_value == max_value == 0 |
| 419 |
and is_negative(max_value) |
| 420 |
and not is_negative(min_value) |
| 421 |
) |
| 422 |
if ( |
| 423 |
min_value is not None |
| 424 |
and max_value is not None |
| 425 |
and (min_value > max_value or bad_zero_bounds) |
| 426 |
): |
| 427 |
# This is a custom alternative to check_valid_interval, because we want |
| 428 |
# to include the bit-width and exclusion information in the message. |
| 429 |
msg = ( |
| 430 |
f"There are no {width}-bit floating-point values between " |
| 431 |
f"min_value={min_arg!r} and max_value={max_arg!r}" |
| 432 |
) |
| 433 |
if exclude_min or exclude_max: |
| 434 |
msg += f", {exclude_min=} and {exclude_max=}" |
| 435 |
raise InvalidArgument(msg) |
| 436 |
|
| 437 |
if allow_infinity is None: |
| 438 |
allow_infinity = bool(min_value is None or max_value is None) |
| 439 |
elif allow_infinity: |
| 440 |
if min_value is not None and max_value is not None: |
| 441 |
raise InvalidArgument( |
| 442 |
f"Cannot have {allow_infinity=}, with both min_value and max_value" |
| 443 |
) |
| 444 |
elif min_value == math.inf: |
| 445 |
if min_arg == math.inf: |
| 446 |
raise InvalidArgument("allow_infinity=False excludes min_value=inf") |
| 447 |
raise InvalidArgument( |
| 448 |
f"exclude_min=True turns min_value={min_arg!r} into inf, " |
| 449 |
"but allow_infinity=False" |
| 450 |
) |
| 451 |
elif max_value == -math.inf: |
| 452 |
if max_arg == -math.inf: |
| 453 |
raise InvalidArgument("allow_infinity=False excludes max_value=-inf") |
| 454 |
raise InvalidArgument( |
| 455 |
f"exclude_max=True turns max_value={max_arg!r} into -inf, " |
| 456 |
"but allow_infinity=False" |
| 457 |
) |
| 458 |
|
| 459 |
smallest_normal = width_smallest_normals[width] |
| 460 |
if allow_subnormal is None: |
| 461 |
if min_value is not None and max_value is not None: |
| 462 |
if min_value == max_value: |
| 463 |
allow_subnormal = -smallest_normal < min_value < smallest_normal |
| 464 |
else: |
| 465 |
allow_subnormal = ( |
| 466 |
min_value < smallest_normal and max_value > -smallest_normal |
| 467 |
) |
| 468 |
elif min_value is not None: |
| 469 |
allow_subnormal = min_value < smallest_normal |
| 470 |
elif max_value is not None: |
| 471 |
allow_subnormal = max_value > -smallest_normal |
| 472 |
else: |
| 473 |
allow_subnormal = True |
| 474 |
if allow_subnormal: |
| 475 |
if min_value is not None and min_value >= smallest_normal: |
| 476 |
raise InvalidArgument( |
| 477 |
f"allow_subnormal=True, but minimum value {min_value} " |
| 478 |
f"excludes values below float{width}'s " |
| 479 |
f"smallest positive normal {smallest_normal}" |
| 480 |
) |
| 481 |
if max_value is not None and max_value <= -smallest_normal: |
| 482 |
raise InvalidArgument( |
| 483 |
f"allow_subnormal=True, but maximum value {max_value} " |
| 484 |
f"excludes values above float{width}'s " |
| 485 |
f"smallest negative normal {-smallest_normal}" |
| 486 |
) |
| 487 |
|
| 488 |
if min_value is None: |
| 489 |
min_value = float("-inf") |
| 490 |
if max_value is None: |
| 491 |
max_value = float("inf") |
| 492 |
if not allow_infinity: |
| 493 |
min_value = max(min_value, next_up(float("-inf"))) |
| 494 |
max_value = min(max_value, next_down(float("inf"))) |
| 495 |
assert isinstance(min_value, float) |
| 496 |
assert isinstance(max_value, float) |
| 497 |
smallest_nonzero_magnitude = ( |
| 498 |
SMALLEST_SUBNORMAL if allow_subnormal else smallest_normal |
| 499 |
) |
| 500 |
result: SearchStrategy = FloatStrategy( |
| 501 |
min_value=min_value, |
| 502 |
max_value=max_value, |
| 503 |
allow_nan=allow_nan, |
| 504 |
smallest_nonzero_magnitude=smallest_nonzero_magnitude, |
| 505 |
) |
| 506 |
|
| 507 |
if width < 64: |
| 508 |
|
| 509 |
def downcast(x: float) -> float: |
| 510 |
try: |
| 511 |
return float_of(x, width) |
| 512 |
except OverflowError: # pragma: no cover |
| 513 |
reject() |
| 514 |
|
| 515 |
result = result.map(downcast) |
| 516 |
return result |
| 517 |
|
| 518 |
|
| 519 |
class NanStrategy(SearchStrategy[float]): |
| 520 |
"""Strategy for sampling the space of nan float values.""" |
| 521 |
|
| 522 |
def do_draw(self, data: ConjectureData) -> float: |
| 523 |
# Nans must have all exponent bits and the first mantissa bit set, so |
| 524 |
# we generate by taking 64 random bits and setting the required ones. |
| 525 |
sign_bit = int(data.draw_boolean()) << 63 |
| 526 |
nan_bits = float_to_int(math.nan) |
| 527 |
mantissa_bits = data.draw_integer(0, 2**52 - 1) |
| 528 |
return int_to_float(sign_bit | nan_bits | mantissa_bits) |