Números Flotantes o Reales
Este puede ser un pequeño tema o un gran tema en función de nuestros intereses asi que profundizare la cuestión en la medida de mis posibilidades.
Lo primero que podemos apreciar o determinar serián los métodos y propiedades de un dato tipo flotantes, de las dos maneras:
>>>dir(2.0) ó >>>dir(float()) que nos da:
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__str__', '__sub__', '__truediv__']
>>> help(float())
Help on float object:
class float(object)
| float(x) -> floating point number
|
| Convert a string or number to a floating point number, if possible.
|
| Methods defined here:
|
| __abs__(...)
| x.__abs__() <==> abs(x)
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __coerce__(...)
| x.__coerce__(y) <==> coerce(x, y)
|
| __div__(...)
| x.__div__(y) <==> x/y
|
| __divmod__(...)
| x.__divmod__(y) <==> divmod(x, y)
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __float__(...)
| x.__float__() <==> float(x)
|
| __floordiv__(...)
| x.__floordiv__(y) <==> x//y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getnewargs__(...)
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __hash__(...)
| x.__hash__() <==> hash(x)
|
| __int__(...)
| x.__int__() <==> int(x)
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __long__(...)
| x.__long__() <==> long(x)
|
| __lt__(...)
| x.__lt__(y) <==> x<y
|
| __mod__(...)
| x.__mod__(y) <==> x%y
|
| __mul__(...)
| x.__mul__(y) <==> x*y
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __neg__(...)
| x.__neg__() <==> -x
|
| __nonzero__(...)
| x.__nonzero__() <==> x != 0
|
| __pos__(...)
| x.__pos__() <==> +x
|
| __pow__(...)
| x.__pow__(y[, z]) <==> pow(x, y[, z])
|
| __radd__(...)
| x.__radd__(y) <==> y+x
|
| __rdiv__(...)
| x.__rdiv__(y) <==> y/x
|
| __rdivmod__(...)
| x.__rdivmod__(y) <==> divmod(y, x)
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __rfloordiv__(...)
| x.__rfloordiv__(y) <==> y//x
|
| __rmod__(...)
| x.__rmod__(y) <==> y%x
|
| __rmul__(...)
| x.__rmul__(y) <==> y*x
|
| __rpow__(...)
| y.__rpow__(x[, z]) <==> pow(x, y[, z])
|
| __rsub__(...)
| x.__rsub__(y) <==> y-x
|
| __rtruediv__(...)
| x.__rtruediv__(y) <==> y/x
|
| __str__(...)
| x.__str__() <==> str(x)
|
| __sub__(...)
| x.__sub__(y) <==> x-y
|
| __truediv__(...)
| x.__truediv__(y) <==> x/y
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __getformat__ = <built-in method __getformat__ of type object at 0xb7e...
| float.__getformat__(typestr) -> string
|
| You probably don't want to use this function. It exists mainly to be
| used in Python's test suite.
|
| typestr must be 'double' or 'float'. This function returns whichever of
| 'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the
| format of floating point numbers used by the C type named by typestr.
|
| __new__ = <built-in method __new__ of type object at 0xb7ed9f20>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
| __setformat__ = <built-in method __setformat__ of type object at 0xb7e...
| float.__setformat__(typestr, fmt) -> None
|
| You probably don't want to use this function. It exists mainly to be
| used in Python's test suite.
|
| typestr must be 'double' or 'float'. fmt must be one of 'unknown',
| 'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be
| one of the latter two if it appears to match the underlying C reality.
|
| Overrides the automatic determination of C-level floating point type.
| This affects how floats are converted to and from binary strings.
>>>
Una de las cuestiones mas notorias es cuando uno divide un numero por ej.:
>>> 1/3.
0.33333333333333331
>>>
de alguna manera esta cantidad de digitos que ofrece como resultado es la mayor precisión que puede alcanzar.
La razón esta debida a que la implementación esta basado en el hardware y en el hecho de que los mismos se calculan usando matematica binaria.
De la misma manera de presentar numeros enteros se usa el operador %:
>>> '%f' % (1/3.)
'0.333333'
>>>
Si queremos ajustar la precisión podemos hacer lo siguiente:
>>> '%2.2f' % (1/3.)
'0.33'
>>> '%2.10f' % (1/3.)
'0.3333333333'
>>> '%2.40f' % (1/3.)
'0.3333333333333333148296162562473909929395'
>>>
Este último resultado es el mas llamativo dado que se visualiza el error que se comete pero existe una compensación de los mismos lo que hace insignificante el problema por ej.:
>>>1/3.+2/3.
1.0
>>> '%2.40f' % (1/3.+2/3.)
'1.0000000000000000000000000000000000000000'
>>>
Como se aprecia en la lista de metodos del tipo float esta casi todo sobre el tratamiento de este tipo pero si uno quiere realizar operaciones avanzadas como logaritmos, funciones trigonometricas y otras menudencias existe un modulo especializado para ello conocido como Numeric.
Las cosas que podemos hacer a nivel matematico la dejo para el proximo tema.