paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@850 | 4 | Floating point number objects. |
paul@6 | 5 | |
paul@936 | 6 | Copyright (C) 2015, 2016, 2018, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@936 | 22 | from __builtins__.unicode import unicode |
paul@850 | 23 | from native import isinstance as _isinstance, \ |
paul@850 | 24 | int_float, is_int, \ |
paul@850 | 25 | float_add, float_div, float_eq, float_ge, float_gt, \ |
paul@850 | 26 | float_le, float_lt, float_mod, float_mul, float_ne, \ |
paul@850 | 27 | float_neg, float_pow, float_str, float_sub, |
paul@850 | 28 | |
paul@355 | 29 | class float: |
paul@850 | 30 | |
paul@850 | 31 | "A floating point number abstraction." |
paul@850 | 32 | |
paul@6 | 33 | def __init__(self, number_or_string=None): |
paul@850 | 34 | |
paul@850 | 35 | "Initialise the integer with the given 'number_or_string'." |
paul@850 | 36 | |
paul@850 | 37 | # NOTE: To be implemented. |
paul@850 | 38 | |
paul@850 | 39 | pass |
paul@850 | 40 | |
paul@850 | 41 | def __hash__(self): |
paul@850 | 42 | |
paul@850 | 43 | "Return a value for hashing purposes." |
paul@850 | 44 | |
paul@850 | 45 | return self |
paul@850 | 46 | |
paul@850 | 47 | def _binary_op(self, op, other): |
paul@850 | 48 | |
paul@850 | 49 | "Perform 'op' on this float and 'other' if appropriate." |
paul@850 | 50 | |
paul@850 | 51 | if _isinstance(other, float): |
paul@850 | 52 | return op(self, other) |
paul@850 | 53 | elif is_int(other): |
paul@850 | 54 | return op(self, int_float(other)) |
paul@850 | 55 | else: |
paul@850 | 56 | return NotImplemented |
paul@850 | 57 | |
paul@850 | 58 | def _binary_op_rev(self, op, other): |
paul@850 | 59 | |
paul@850 | 60 | "Perform 'op' on 'other' and this float if appropriate." |
paul@850 | 61 | |
paul@850 | 62 | if _isinstance(other, float): |
paul@850 | 63 | return op(other, self) |
paul@850 | 64 | elif is_int(other): |
paul@850 | 65 | return op(int_float(other), self) |
paul@850 | 66 | else: |
paul@850 | 67 | return NotImplemented |
paul@850 | 68 | |
paul@850 | 69 | def __iadd__(self, other): |
paul@850 | 70 | |
paul@850 | 71 | "Return a new float for the addition of this float and 'other'." |
paul@6 | 72 | |
paul@850 | 73 | return self._binary_op(float_add, other) |
paul@850 | 74 | |
paul@850 | 75 | def __isub__(self, other): |
paul@850 | 76 | |
paul@850 | 77 | "Return a new float for the subtraction from this float of 'other'." |
paul@850 | 78 | |
paul@850 | 79 | return self._binary_op(float_sub, other) |
paul@850 | 80 | |
paul@850 | 81 | def __imul__(self, other): |
paul@850 | 82 | |
paul@850 | 83 | "Return a new float for the multiplication of this float and 'other'." |
paul@850 | 84 | |
paul@850 | 85 | return self._binary_op(float_mul, other) |
paul@850 | 86 | |
paul@850 | 87 | def __idiv__(self, other): |
paul@850 | 88 | |
paul@850 | 89 | "Return a new float for the division of this float by 'other'." |
paul@850 | 90 | |
paul@850 | 91 | return self._binary_op(float_div, other) |
paul@850 | 92 | |
paul@850 | 93 | def __imod__(self, other): |
paul@850 | 94 | |
paul@850 | 95 | "Return a new float for the modulo of this float by 'other'." |
paul@850 | 96 | |
paul@850 | 97 | return self._binary_op(float_mod, other) |
paul@850 | 98 | |
paul@850 | 99 | def __ipow__(self, other): |
paul@850 | 100 | |
paul@850 | 101 | "Return a new float for the exponentiation of this float by 'other'." |
paul@850 | 102 | |
paul@850 | 103 | return self._binary_op(float_pow, other) |
paul@850 | 104 | |
paul@850 | 105 | __add__ = __radd__ = __iadd__ |
paul@850 | 106 | __sub__ = __isub__ |
paul@850 | 107 | |
paul@850 | 108 | def __rsub__(self, other): |
paul@850 | 109 | |
paul@850 | 110 | "Return a new float for the subtraction of this float from 'other'." |
paul@850 | 111 | |
paul@850 | 112 | return self._binary_op_rev(float_sub, other) |
paul@850 | 113 | |
paul@850 | 114 | __mul__ = __rmul__ = __imul__ |
paul@850 | 115 | __div__ = __idiv__ |
paul@850 | 116 | |
paul@850 | 117 | def __rdiv__(self, other): |
paul@850 | 118 | |
paul@850 | 119 | "Return a new float for the division of 'other' by this float." |
paul@850 | 120 | |
paul@850 | 121 | return self._binary_op_rev(float_div, other) |
paul@850 | 122 | |
paul@850 | 123 | # NOTE: To be implemented. |
paul@850 | 124 | |
paul@6 | 125 | def __floordiv__(self, other): pass |
paul@6 | 126 | def __rfloordiv__(self, other): pass |
paul@850 | 127 | def __ifloordiv__(self, other): pass |
paul@850 | 128 | |
paul@850 | 129 | __mod__ = __imod__ |
paul@850 | 130 | |
paul@850 | 131 | def __rmod__(self, other): |
paul@850 | 132 | |
paul@850 | 133 | "Return a new float for the modulo of 'other' by this float." |
paul@850 | 134 | |
paul@850 | 135 | return self._binary_op_rev(float_mod, other) |
paul@850 | 136 | |
paul@850 | 137 | __pow__ = __ipow__ |
paul@850 | 138 | |
paul@850 | 139 | def __rpow__(self, other): |
paul@850 | 140 | |
paul@850 | 141 | "Return a new float for the exponentiation of 'other' by this float." |
paul@850 | 142 | |
paul@850 | 143 | return self._binary_op_rev(float_pow, other) |
paul@850 | 144 | |
paul@850 | 145 | def __lt__(self, other): |
paul@850 | 146 | |
paul@850 | 147 | "Return whether this float is less than 'other'." |
paul@850 | 148 | |
paul@850 | 149 | return self._binary_op(float_lt, other) |
paul@850 | 150 | |
paul@850 | 151 | def __gt__(self, other): |
paul@850 | 152 | |
paul@850 | 153 | "Return whether this float is greater than 'other'." |
paul@850 | 154 | |
paul@850 | 155 | return self._binary_op(float_gt, other) |
paul@850 | 156 | |
paul@850 | 157 | def __le__(self, other): |
paul@850 | 158 | |
paul@850 | 159 | "Return whether this float is less than or equal to 'other'." |
paul@850 | 160 | |
paul@850 | 161 | return self._binary_op(float_le, other) |
paul@850 | 162 | |
paul@850 | 163 | def __ge__(self, other): |
paul@850 | 164 | |
paul@850 | 165 | "Return whether this float is greater than or equal to 'other'." |
paul@850 | 166 | |
paul@850 | 167 | return self._binary_op(float_ge, other) |
paul@850 | 168 | |
paul@850 | 169 | def __eq__(self, other): |
paul@850 | 170 | |
paul@850 | 171 | "Return whether this float is equal to 'other'." |
paul@850 | 172 | |
paul@850 | 173 | return self._binary_op(float_eq, other) |
paul@850 | 174 | |
paul@850 | 175 | def __ne__(self, other): |
paul@850 | 176 | |
paul@850 | 177 | "Return whether this float is not equal to 'other'." |
paul@850 | 178 | |
paul@850 | 179 | return self._binary_op(float_ne, other) |
paul@850 | 180 | |
paul@850 | 181 | def __neg__(self): |
paul@850 | 182 | |
paul@850 | 183 | "Apply the unary negation operator." |
paul@850 | 184 | |
paul@850 | 185 | return float_neg(self) |
paul@850 | 186 | |
paul@850 | 187 | def __pos__(self): |
paul@850 | 188 | |
paul@850 | 189 | "Apply the unary positive operator." |
paul@850 | 190 | |
paul@850 | 191 | return self |
paul@850 | 192 | |
paul@850 | 193 | def __str__(self): |
paul@850 | 194 | |
paul@850 | 195 | "Return a string representation." |
paul@850 | 196 | |
paul@936 | 197 | return unicode(float_str(self)) |
paul@850 | 198 | |
paul@850 | 199 | __repr__ = __str__ |
paul@850 | 200 | |
paul@850 | 201 | def __bool__(self): |
paul@850 | 202 | |
paul@850 | 203 | "Return whether this float is non-zero." |
paul@850 | 204 | |
paul@850 | 205 | return float_ne(self, 0) |
paul@6 | 206 | |
paul@6 | 207 | # vim: tabstop=4 expandtab shiftwidth=4 |