1.1 --- a/lib/__builtins__/float.py Wed Jul 11 23:53:09 2018 +0200
1.2 +++ b/lib/__builtins__/float.py Thu Jul 12 00:15:16 2018 +0200
1.3 @@ -1,9 +1,9 @@
1.4 #!/usr/bin/env python
1.5
1.6 """
1.7 -Floating point objects.
1.8 +Floating point number objects.
1.9
1.10 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.11 +Copyright (C) 2015, 2016, 2018 Paul Boddie <paul@boddie.org.uk>
1.12
1.13 This program is free software; you can redistribute it and/or modify it under
1.14 the terms of the GNU General Public License as published by the Free Software
1.15 @@ -19,36 +19,189 @@
1.16 this program. If not, see <http://www.gnu.org/licenses/>.
1.17 """
1.18
1.19 +from __builtins__.unicode import utf8string
1.20 +from native import isinstance as _isinstance, \
1.21 + int_float, is_int, \
1.22 + float_add, float_div, float_eq, float_ge, float_gt, \
1.23 + float_le, float_lt, float_mod, float_mul, float_ne, \
1.24 + float_neg, float_pow, float_str, float_sub,
1.25 +
1.26 class float:
1.27 +
1.28 + "A floating point number abstraction."
1.29 +
1.30 def __init__(self, number_or_string=None):
1.31 - # Note member.
1.32 - self.__data__ = 0.0
1.33 +
1.34 + "Initialise the integer with the given 'number_or_string'."
1.35 +
1.36 + # NOTE: To be implemented.
1.37 +
1.38 + pass
1.39 +
1.40 + def __hash__(self):
1.41 +
1.42 + "Return a value for hashing purposes."
1.43 +
1.44 + return self
1.45 +
1.46 + def _binary_op(self, op, other):
1.47 +
1.48 + "Perform 'op' on this float and 'other' if appropriate."
1.49 +
1.50 + if _isinstance(other, float):
1.51 + return op(self, other)
1.52 + elif is_int(other):
1.53 + return op(self, int_float(other))
1.54 + else:
1.55 + return NotImplemented
1.56 +
1.57 + def _binary_op_rev(self, op, other):
1.58 +
1.59 + "Perform 'op' on 'other' and this float if appropriate."
1.60 +
1.61 + if _isinstance(other, float):
1.62 + return op(other, self)
1.63 + elif is_int(other):
1.64 + return op(int_float(other), self)
1.65 + else:
1.66 + return NotImplemented
1.67 +
1.68 + def __iadd__(self, other):
1.69 +
1.70 + "Return a new float for the addition of this float and 'other'."
1.71
1.72 - def __iadd__(self, other): pass
1.73 - def __isub__(self, other): pass
1.74 - def __add__(self, other): pass
1.75 - def __radd__(self, other): pass
1.76 - def __sub__(self, other): pass
1.77 - def __rsub__(self, other): pass
1.78 - def __mul__(self, other): pass
1.79 - def __rmul__(self, other): pass
1.80 - def __div__(self, other): pass
1.81 - def __rdiv__(self, other): pass
1.82 + return self._binary_op(float_add, other)
1.83 +
1.84 + def __isub__(self, other):
1.85 +
1.86 + "Return a new float for the subtraction from this float of 'other'."
1.87 +
1.88 + return self._binary_op(float_sub, other)
1.89 +
1.90 + def __imul__(self, other):
1.91 +
1.92 + "Return a new float for the multiplication of this float and 'other'."
1.93 +
1.94 + return self._binary_op(float_mul, other)
1.95 +
1.96 + def __idiv__(self, other):
1.97 +
1.98 + "Return a new float for the division of this float by 'other'."
1.99 +
1.100 + return self._binary_op(float_div, other)
1.101 +
1.102 + def __imod__(self, other):
1.103 +
1.104 + "Return a new float for the modulo of this float by 'other'."
1.105 +
1.106 + return self._binary_op(float_mod, other)
1.107 +
1.108 + def __ipow__(self, other):
1.109 +
1.110 + "Return a new float for the exponentiation of this float by 'other'."
1.111 +
1.112 + return self._binary_op(float_pow, other)
1.113 +
1.114 + __add__ = __radd__ = __iadd__
1.115 + __sub__ = __isub__
1.116 +
1.117 + def __rsub__(self, other):
1.118 +
1.119 + "Return a new float for the subtraction of this float from 'other'."
1.120 +
1.121 + return self._binary_op_rev(float_sub, other)
1.122 +
1.123 + __mul__ = __rmul__ = __imul__
1.124 + __div__ = __idiv__
1.125 +
1.126 + def __rdiv__(self, other):
1.127 +
1.128 + "Return a new float for the division of 'other' by this float."
1.129 +
1.130 + return self._binary_op_rev(float_div, other)
1.131 +
1.132 + # NOTE: To be implemented.
1.133 +
1.134 def __floordiv__(self, other): pass
1.135 def __rfloordiv__(self, other): pass
1.136 - def __mod__(self, other): pass
1.137 - def __rmod__(self, other): pass
1.138 - def __pow__(self, other): pass
1.139 - def __rpow__(self, other): pass
1.140 - def __lt__(self, other): pass
1.141 - def __gt__(self, other): pass
1.142 - def __le__(self, other): pass
1.143 - def __ge__(self, other): pass
1.144 - def __eq__(self, other): pass
1.145 - def __ne__(self, other): pass
1.146 - def __neg__(self): pass
1.147 - def __pos__(self): pass
1.148 - def __str__(self): pass
1.149 - def __bool__(self): pass
1.150 + def __ifloordiv__(self, other): pass
1.151 +
1.152 + __mod__ = __imod__
1.153 +
1.154 + def __rmod__(self, other):
1.155 +
1.156 + "Return a new float for the modulo of 'other' by this float."
1.157 +
1.158 + return self._binary_op_rev(float_mod, other)
1.159 +
1.160 + __pow__ = __ipow__
1.161 +
1.162 + def __rpow__(self, other):
1.163 +
1.164 + "Return a new float for the exponentiation of 'other' by this float."
1.165 +
1.166 + return self._binary_op_rev(float_pow, other)
1.167 +
1.168 + def __lt__(self, other):
1.169 +
1.170 + "Return whether this float is less than 'other'."
1.171 +
1.172 + return self._binary_op(float_lt, other)
1.173 +
1.174 + def __gt__(self, other):
1.175 +
1.176 + "Return whether this float is greater than 'other'."
1.177 +
1.178 + return self._binary_op(float_gt, other)
1.179 +
1.180 + def __le__(self, other):
1.181 +
1.182 + "Return whether this float is less than or equal to 'other'."
1.183 +
1.184 + return self._binary_op(float_le, other)
1.185 +
1.186 + def __ge__(self, other):
1.187 +
1.188 + "Return whether this float is greater than or equal to 'other'."
1.189 +
1.190 + return self._binary_op(float_ge, other)
1.191 +
1.192 + def __eq__(self, other):
1.193 +
1.194 + "Return whether this float is equal to 'other'."
1.195 +
1.196 + return self._binary_op(float_eq, other)
1.197 +
1.198 + def __ne__(self, other):
1.199 +
1.200 + "Return whether this float is not equal to 'other'."
1.201 +
1.202 + return self._binary_op(float_ne, other)
1.203 +
1.204 + def __neg__(self):
1.205 +
1.206 + "Apply the unary negation operator."
1.207 +
1.208 + return float_neg(self)
1.209 +
1.210 + def __pos__(self):
1.211 +
1.212 + "Apply the unary positive operator."
1.213 +
1.214 + return self
1.215 +
1.216 + def __str__(self):
1.217 +
1.218 + "Return a string representation."
1.219 +
1.220 + return utf8string(float_str(self))
1.221 +
1.222 + __repr__ = __str__
1.223 +
1.224 + def __bool__(self):
1.225 +
1.226 + "Return whether this float is non-zero."
1.227 +
1.228 + return float_ne(self, 0)
1.229
1.230 # vim: tabstop=4 expandtab shiftwidth=4