| 75 | |
| 76 | @functools.total_ordering |
| 77 | class SingleFloat(object): |
| 78 | def __init__(self, value): |
| 79 | if isinstance(value, float): |
| 80 | # Restrict to the first 4 bytes (essentially) |
| 81 | self.value = ctypes.c_float(value).value |
| 82 | elif isinstance(value, ctypes.c_float): |
| 83 | self.value = value.value |
| 84 | elif isinstance(value, six.integer_types): |
| 85 | self.value = ctypes.c_float(value).value |
| 86 | else: |
| 87 | raise ValueError("Incompatible type for single-precision float: " + repr(value)) |
| 88 | |
| 89 | # Comparisons |
| 90 | def __eq__(self, other): |
| 91 | if isinstance(other, SingleFloat): |
| 92 | return _compare_floats(self.value, other.value) == 0 |
| 93 | else: |
| 94 | return False |
| 95 | |
| 96 | def __lt__(self, other): |
| 97 | return _compare_floats(self.value, other.value) < 0 |
| 98 | |
| 99 | def __str__(self): |
| 100 | return str(self.value) |
| 101 | |
| 102 | def __repr__(self): |
| 103 | return "SingleFloat(" + str(self.value) + ")" |
| 104 | |
| 105 | def __hash__(self): |
| 106 | # Left-circulate the child hash to make hash(self) != hash(self.value) |
| 107 | v_hash = hash(self.value) |
| 108 | if v_hash >= 0: |
| 109 | return (v_hash >> 16) + ((v_hash & 0xFFFF) << 16) |
| 110 | else: |
| 111 | return ((v_hash >> 16) + 1) - ((abs(v_hash) & 0xFFFF) << 16) |
| 112 | |
| 113 | def __nonzero__(self): |
| 114 | return bool(self.value) |
| 115 | |
| 116 | |
| 117 | @functools.total_ordering |
no outgoing calls