Classes | Functions | Variables
pyclearsilver.fixedpoint Namespace Reference

Classes

class  FixedPoint
 
class  object
 

Functions

def _mkFP (n, p, FixedPoint=FixedPoint)
 
def _norm (x, y, isinstance=isinstance, FixedPoint=FixedPoint, _tento=_tento)
 
def _string2exact (s)
 
def _tento (n, cache={})
 
def _test ()
 
def addHalfAndChop (self, dividend, divisor, quotient, remainder)
 
def bankersRounding (self, dividend, divisor, quotient, remainder)
 
def property (x, y)
 

Variables

string __author__ = "Tim Peters"
 
string __copyright__ = "Copyright (C) Python Software Foundation"
 
int __version__ = 0
 
 _parser = re.compile(r, re.VERBOSE).match
 
int DEFAULT_PRECISION = 2
 

Detailed Description

FixedPoint objects support decimal arithmetic with a fixed number of
digits (called the object's precision) after the decimal point.  The
number of digits before the decimal point is variable & unbounded.

The precision is user-settable on a per-object basis when a FixedPoint
is constructed, and may vary across FixedPoint objects.  The precision
may also be changed after construction via FixedPoint.set_precision(p).
Note that if the precision of a FixedPoint is reduced via set_precision,
information may be lost to rounding.

>>> x = FixedPoint("5.55")  # precision defaults to 2
>>> print x
5.55
>>> x.set_precision(1)      # round to one fraction digit
>>> print x
5.6
>>> print FixedPoint("5.55", 1)  # same thing setting to 1 in constructor
5.6
>>> repr(x) #  returns constructor string that reproduces object exactly
"FixedPoint('5.6', 1)"
>>>

When FixedPoint objects of different precision are combined via + - * /,
the result is computed to the larger of the inputs' precisions, which also
becomes the precision of the resulting FixedPoint object.

>>> print FixedPoint("3.42") + FixedPoint("100.005", 3)
103.425
>>>

When a FixedPoint is combined with other numeric types (ints, floats,
strings representing a number) via + - * /, then similarly the computation
is carried out using-- and the result inherits --the FixedPoint's
precision.

>>> print FixedPoint(1) / 7
0.14
>>> print FixedPoint(1, 30) / 7
0.142857142857142857142857142857
>>>

The string produced by str(x) (implictly invoked by "print") always
contains at least one digit before the decimal point, followed by a
decimal point, followed by exactly x.get_precision() digits.  If x is
negative, str(x)[0] == "-".

The FixedPoint constructor can be passed an int, long, string, float,
FixedPoint, or any object convertible to a float via float() or to a
long via long().  Passing a precision is optional; if specified, the
precision must be a non-negative int.  There is no inherent limit on
the size of the precision, but if very very large you'll probably run
out of memory.

Note that conversion of floats to FixedPoint can be surprising, and
should be avoided whenever possible.  Conversion from string is exact
(up to final rounding to the requested precision), so is greatly
preferred.

>>> print FixedPoint(1.1e30)
1099999999999999993725589651456.00
>>> print FixedPoint("1.1e30")
1100000000000000000000000000000.00
>>>

The following Python operators and functions accept FixedPoints in the
expected ways:

    binary + - * / % divmod
  with auto-coercion of other types to FixedPoint.
  + - % divmod  of FixedPoints are always exact.
  * / of FixedPoints may lose information to rounding, in
      which case the result is the infinitely precise answer
      rounded to the result's precision.
  divmod(x, y) returns (q, r) where q is a long equal to
      floor(x/y) as if x/y were computed to infinite precision,
      and r is a FixedPoint equal to x - q * y; no information
      is lost.  Note that q has the sign of y, and abs(r) < abs(y).
    unary -
    == != < > <= >=  cmp
    min  max
    float  int  long    (int and long truncate)
    abs
    str  repr
    hash
    use as dict keys
    use as boolean (e.g. "if some_FixedPoint:" -- true iff not zero)

Methods unique to FixedPoints:
   .copy()              return new FixedPoint with same value
   .frac()              long(x) + x.frac() == x
   .get_precision()     return the precision(p) of this FixedPoint object
   .set_precision(p)    set the precision of this FixedPoint object
   
Provided as-is; use at your own risk; no warranty; no promises; enjoy!

Function Documentation

def pyclearsilver.fixedpoint._mkFP (   n,
  p,
  FixedPoint = FixedPoint 
)
private
Make FixedPoint objext - Return a new FixedPoint object with the selected precision.

Definition at line 495 of file fixedpoint.py.

def pyclearsilver.fixedpoint._norm (   x,
  y,
  isinstance = isinstance,
  FixedPoint = FixedPoint,
  _tento = _tento 
)
private
Return xn, yn, p s.t.
       p = max(x.p, y.p)
       x = xn / 10**p
       y = yn / 10**p

    x must be FixedPoint to begin with; if y is not FixedPoint,
    it inherits its precision from x.

    Note that this method is called a lot, so default-arg tricks are helpful.

Definition at line 469 of file fixedpoint.py.

def pyclearsilver.fixedpoint._string2exact (   s)
private
Return n, p s.t. float string value == n * 10**p exactly.

Definition at line 527 of file fixedpoint.py.

def pyclearsilver.fixedpoint._tento (   n,
  cache = {} 
)
private
Cached computation of 10**n

Definition at line 460 of file fixedpoint.py.

def pyclearsilver.fixedpoint._test ( )
private
Unit testing framework

Definition at line 560 of file fixedpoint.py.

def pyclearsilver.fixedpoint.addHalfAndChop (   self,
  dividend,
  divisor,
  quotient,
  remainder 
)
the equivalent of 'add half and chop'
increment the quotient if
     the remainder is greater than half of the divisor
  or the remainder is exactly half the divisor and the quotient is >= 0

Definition at line 138 of file fixedpoint.py.

def pyclearsilver.fixedpoint.bankersRounding (   self,
  dividend,
  divisor,
  quotient,
  remainder 
)
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

Definition at line 125 of file fixedpoint.py.

def pyclearsilver.fixedpoint.property (   x,
  y 
)

Definition at line 157 of file fixedpoint.py.

Variable Documentation

string pyclearsilver.fixedpoint.__author__ = "Tim Peters"
private

Definition at line 122 of file fixedpoint.py.

string pyclearsilver.fixedpoint.__copyright__ = "Copyright (C) Python Software Foundation"
private

Definition at line 121 of file fixedpoint.py.

int pyclearsilver.fixedpoint.__version__ = 0
private

Definition at line 123 of file fixedpoint.py.

pyclearsilver.fixedpoint._parser = re.compile(r, re.VERBOSE).match
private

Definition at line 512 of file fixedpoint.py.

int pyclearsilver.fixedpoint.DEFAULT_PRECISION = 2

Definition at line 162 of file fixedpoint.py.



pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13