5.16.3 FixedPoint Examples

The following examples demonstrate various ways include FixedPoint objects in your programming solutions.

>>> from fixedpoint import FixedPoint
>>> 
>>> print FixedPoint("3.14159")  # defaults to 2 decimal places
3.14
>>> print FixedPoint("3.14159",3)
3.142
>>> print FixedPoint("3.14159",6)
3.141590
>>>print 1.00 + FixedPoint("3.14159",4)
4.1416
>>>
>>>
>>> x = FixedPoint("3.14159",5)
>>> print x
3.14159
>>> x.get_precision()
5
>>> x.set_precision(7)
>>> print x
3.1415900
>>> y = x.copy()
>>> x += 1
>>> y += FixedPoint("1.2e-6",7)
>>> print x,y
4.1415900 3.1415912
>>> print y.frac()
0.1415912
>>> z = y.frac() - x.frac()
>>> print z
0.0000012
>>>
>>>
>>> r = FixedPoint("3.14159",5)
>>> print 'Result is: %s (accurate to %i decimal points.)' % (r,r.get_precision())
Result is: 3.14159 (accurate to 5 decimal points.)
>>>
>>>
>>> # Note the difference between str() and repr()
... print r     # or print str(r)
3.14159
>>> print `r`   # or print repr(r)
FixedPoint('3.14159', 5)
>>>

See About this document... for information on suggesting changes.