Пример #1
0
    def do_draw(self, data):
        def draw_float_bytes(random, n):
            assert n == 8
            while True:
                i = random.randint(1, 10)
                if i <= 4:
                    f = random.choice(NASTY_FLOATS)
                elif i == 5:
                    return bytes_from_list(
                        random.randint(0, 255) for _ in range(8))
                elif i == 6:
                    f = random.random() * (random.randint(0, 1) * 2 - 1)
                elif i == 7:
                    f = random.gauss(0, 1)
                elif i == 8:
                    f = float(random.randint(-2**63, 2**63))
                else:
                    f = random.gauss(random.randint(-2**63, 2**63), 1)
                if self.permitted(f):
                    return struct_pack(b'!d', f)

        result = struct_unpack(b'!d',
                               hbytes(data.draw_bytes(8, draw_float_bytes)))[0]
        assume(self.permitted(result))
        return result
Пример #2
0
def next_up(value):
    """Return the first float larger than finite `val` - IEEE 754's `nextUp`.

    From https://stackoverflow.com/a/10426033, with thanks to Mark Dickinson.
    """
    assert isinstance(value, float)
    if math.isnan(value) or (math.isinf(value) and value > 0):
        return value
    if value == 0.0:
        value = 0.0
    # Note: n is signed; float_to_int returns unsigned
    n = struct_unpack(b'q', struct_pack(b'd', value))[0]
    if n >= 0:
        n += 1
    else:
        n -= 1
    return struct_unpack(b'd', struct_pack(b'q', n))[0]
Пример #3
0
 def reinterpret_bits(x, from_, to):
     if from_ == b"!e":
         arr = numpy.array([x], dtype=">f2")
         if numpy.isfinite(x) and not numpy.isfinite(arr[0]):
             quiet_raise(OverflowError("%r too large for float16" % (x, )))
         buf = arr.tobytes()
     else:
         buf = struct_pack(from_, x)
     if to == b"!e":
         return float(numpy.frombuffer(buf, dtype=">f2")[0])
     return struct_unpack(to, buf)[0]
Пример #4
0
 def reinterpret_bits(x, from_, to):
     if from_ == b'!e':
         arr = numpy.array([x], dtype='>f2')
         if numpy.isfinite(x) and not numpy.isfinite(arr[0]):
             quiet_raise(OverflowError('%r too large for float16' % (x,)))
         buf = arr.tobytes()
     else:
         buf = struct_pack(from_, x)
     if to == b'!e':
         return float(numpy.frombuffer(buf, dtype='>f2')[0])
     return struct_unpack(to, buf)[0]
Пример #5
0
 def do_draw(self, data):
     while True:
         data.start_example()
         i = self.sampler.sample(data)
         if i == 0:
             result = struct_unpack(b'!d', hbytes(data.draw_bytes(8)))[0]
         else:
             result = self.nasty_floats[i - 1]
             data.write(struct_pack(b'!d', result))
         data.stop_example()
         if self.permitted(result):
             return result
Пример #6
0
    def do_draw(self, data):
        def draw_float_bytes(random, n):
            assert n == 8
            i = random.randint(0, 20)
            if i <= 2:
                f = random.choice(self.critical)
            else:
                f = random.random() * (self.upper_bound -
                                       self.lower_bound) + self.lower_bound
            return struct_pack(b'!d', f)

        f = struct_unpack(b'!d', hbytes(data.draw_bytes(8,
                                                        draw_float_bytes)))[0]
        assume(self.lower_bound <= f <= self.upper_bound)
        assume(sign(self.lower_bound) <= sign(f) <= sign(self.upper_bound))
        return f
Пример #7
0
 def reinterpret_bits(x, from_, to):
     return struct_unpack(to, struct_pack(from_, x))[0]
Пример #8
0
 def reinterpret_bits(x, from_, to):
     return struct_unpack(to, struct_pack(from_, x))[0]
Пример #9
0
def int_to_float(value):
    return struct_unpack(b'!d', struct_pack(b'!Q', value))[0]
Пример #10
0
def float_to_int(value):
    return (struct_unpack(b'!Q', struct_pack(b'!d', value))[0])