Package orm2 :: Package util :: Module fixedpoint :: Class FixedPoint
[hide private]
[frames] | no frames]

Class FixedPoint

source code

object --+
         |
        FixedPoint

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=DEFAULT_PRECISION)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
  get_precision(self)
Return the precision of this FixedPoint.
  set_precision(self, precision=DEFAULT_PRECISION)
Change the precision carried by this FixedPoint to p.
  __str__(self)
str(x)
  __repr__(self)
repr(x)
  copy(self)
  __copy__(self)
  __deepcopy__(self, memo)
  __cmp__(self, other)
  __hash__(self)
Caution! == values must have equal hashes, and a FixedPoint is essentially a rational in unnormalized form.
  __nonzero__(self)
Returns true if this FixedPoint is not equal to zero...
  __neg__(self)
  __abs__(self)
Returns new FixedPoint containing the absolute value of this FixedPoint...
  __add__(self, other)
  __radd__(self, other)
  __sub__(self, other)
  __rsub__(self, other)
  __mul__(self, other)
  __rmul__(self, other)
  __div__(self, other)
  __rdiv__(self, other)
  __divmod__(self, other)
  __rdivmod__(self, other)
  __mod__(self, other)
  __rmod__(self, other)
  __float__(self)
Return the floating point representation of this FixedPoint.
  __long__(self)
EJG/DF - Should this round instead? Note e.g.
  __int__(self)
Return integer value of FixedPoint object.
  frac(self)
Return fractional portion as a FixedPoint.
  _roundquotient(self, x, y)
...
  __reduce(self)
Return n, p s.t.
  round(self, dividend, divisor, quotient, remainder)
...

Inherited from object: __delattr__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__


Class Variables [hide private]
  __slots__ = ['n', 'p']
  n = <member 'n' of 'FixedPoint' objects>
  p = <member 'p' of 'FixedPoint' objects>

Properties [hide private]
  precision

Inherited from object: __class__


Method Details [hide private]

__init__(self, value=0, precision=DEFAULT_PRECISION)
(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=DEFAULT_PRECISION)

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 
str(x)
Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

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

copy(self)

source code 
None

__copy__(self)

source code 
None

__deepcopy__(self, memo)

source code 
None

__cmp__(self, other)
(Comparison operator)

source code 
None

__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__

__nonzero__(self)
(Boolean test operator)

source code 
Returns true if this FixedPoint is not equal to zero

__neg__(self)

source code 
None

__abs__(self)

source code 
Returns new FixedPoint containing the absolute value of this FixedPoint

__add__(self, other)
(Addition operator)

source code 
None

__radd__(self, other)
(Right-side addition operator)

source code 
None

__sub__(self, other)
(Subtraction operator)

source code 
None

__rsub__(self, other)

source code 
None

__mul__(self, other)

source code 
None

__rmul__(self, other)

source code 
None

__div__(self, other)

source code 
None

__rdiv__(self, other)

source code 
None

__divmod__(self, other)

source code 
None

__rdivmod__(self, other)

source code 
None

__mod__(self, other)

source code 
None

__rmod__(self, other)

source code 
None

__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

__int__(self)

source code 
Return integer value of FixedPoint object.

frac(self)

source code 
Return fractional portion as a FixedPoint.

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

_roundquotient(self, x, y)

source code 

Divide x by y,
return the result of rounding
Developers may substitute their own 'round' for custom rounding
y must be > 0

__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


Class Variable Details [hide private]

__slots__

None
Value:
['n', 'p']                                                             
      

n

None
Value:
<member 'n' of 'FixedPoint' objects>                                   
      

p

None
Value:
<member 'p' of 'FixedPoint' objects>                                   
      

Property Details [hide private]

precision

None
Get Method:
orm2.util.fixedpoint.FixedPoint.get_precision(self) - Return the precision of this FixedPoint.
Set Method:
orm2.util.fixedpoint.FixedPoint.set_precision(self, precision=DEFAULT_PRECISION) - Change the precision carried by this FixedPoint to p.
Delete Method:
None