Package topo :: Package misc :: Module fixedpoint :: Class FixedPoint
[hide private]
[frames] | no frames]

Class FixedPoint

source code


Basic FixedPoint object class, The exact value is self.n / 10**self.p; self.n is a long; self.p is an int
Instance Methods [hide private]
 
__init__(self, value=0, precision=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
get_precision(self)
Return the precision of this FixedPoint.
source code
 
set_precision(self, precision=2)
Change the precision carried by this FixedPoint to p.
source code
 
__str__(self)
x.__str__() <==> str(x)
source code
 
__repr__(self)
x.__repr__() <==> repr(x)
source code
 
copy(self) source code
 
__copy__(self) source code
 
__deepcopy__(self, memo) source code
 
__getstate__(self) source code
 
__setstate__(self, state) source code
 
unpickle(self) source code
 
__cmp__(self, other) source code
 
__hash__(self)
Caution! == values must have equal hashes, and a FixedPoint is essentially a rational in unnormalized form.
source code
 
__nonzero__(self)
Returns true if this FixedPoint is not equal to zero
source code
 
__neg__(self) source code
 
__abs__(self)
Returns new FixedPoint containing the absolute value of this FixedPoint
source code
 
__add__(self, other) source code
 
__radd__(self, other) source code
 
__sub__(self, other) source code
 
__rsub__(self, other) source code
 
__mul__(self, other) source code
 
__rmul__(self, other) source code
 
__div__(self, other) source code
 
__rdiv__(self, other) source code
 
__divmod__(self, other) source code
 
__rdivmod__(self, other) source code
 
__mod__(self, other) source code
 
__rmod__(self, other) source code
 
__float__(self)
Return the floating point representation of this FixedPoint.
source code
 
__long__(self)
EJG/DF - Should this round instead? Note e.g.
source code
 
__int__(self)
Return integer value of FixedPoint object.
source code
 
frac(self)
Return fractional portion as a FixedPoint.
source code
 
_roundquotient(self, x, y)
Divide x by y, return the result of rounding Developers may substitute their own 'round' for custom rounding y must be > 0
source code
 
__reduce(self)
Return n, p s.t.
source code
 
round(self, dividend, divisor, quotient, remainder)
rounding via nearest-even...
source code

Inherited from object: __delattr__, __format__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Properties [hide private]
  precision
Return the precision of this FixedPoint.
  n
  p

Inherited from object: __class__

Method Details [hide private]

__init__(self, value=0, precision=None)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

get_precision(self)

source code 

Return the precision of this FixedPoint.

The precision is the number of decimal digits carried after the decimal point, and is an int >= 0.

set_precision(self, precision=2)

source code 

Change the precision carried by this FixedPoint to p.

precision must be an int >= 0, and defaults to DEFAULT_PRECISION.

If precision is less than this FixedPoint's current precision, information may be lost to rounding.

__str__(self)
(Informal representation operator)

source code 
x.__str__() <==> str(x)
Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 
x.__repr__() <==> repr(x)
Overrides: object.__repr__
(inherited documentation)

__hash__(self)
(Hashing function)

source code 

Caution! == values must have equal hashes, and a FixedPoint is essentially a rational in unnormalized form. There's really no choice here but to normalize it, so hash is potentially expensive. n, p = self.__reduce()

Obscurity: if the value is an exact integer, p will be 0 now, so the hash expression reduces to hash(n). So FixedPoints that happen to be exact integers hash to the same things as their int or long equivalents. This is Good. But if a FixedPoint happens to have a value exactly representable as a float, their hashes may differ. This is a teensy bit Bad.

Overrides: object.__hash__

__float__(self)

source code 
Return the floating point representation of this FixedPoint. Caution! float can lose precision.

__long__(self)

source code 
EJG/DF - Should this round instead?
Note e.g. long(-1.9) == -1L and long(1.9) == 1L in Python
Note that __int__ inherits whatever __long__ does,
     and .frac() is affected too

frac(self)

source code 

Return fractional portion as a FixedPoint.

x.frac() + long(x) == x

__reduce(self)

source code 
Return n, p s.t. self == n/10**p and n % 10 != 0

round(self, dividend, divisor, quotient, remainder)

source code 

rounding via nearest-even
increment the quotient if
     the remainder is more than half of the divisor
  or the remainder is exactly half the divisor and the quotient is odd


Property Details [hide private]

precision

Return the precision of this FixedPoint.

The precision is the number of decimal digits carried after the decimal point, and is an int >= 0.

Get Method:
get_precision(self) - Return the precision of this FixedPoint.
Set Method:
set_precision(self, precision=2) - Change the precision carried by this FixedPoint to p.