Rounding error
D-Man
dsh8290 at rit.edu
Wed Apr 4 18:16:31 EDT 2001
On Thu, Apr 05, 2001 at 09:58:05AM +1200, Matthew wrote:
| Hi,
|
| I'm calculating a bunch of co-ordinates as below. I'm using
| Round(<coord>,2) but as you can see rounding errors are happening...
| How do I overcome this? I'm using ActivePython Build203.
Don't use FP. Floating point is inherently inaccurate. You said they
are co-ordinates, 2-d or 3-d? I would recommend creating a Point
class, something along the lines of:
class Point :
def __init__( xn , xd , yn , yd ) :
self.xn = xn
self.xd = xd
self.yn = yn
self.yd = yd
...
where xn is the integer part and xd is the decimal part. Both should
be integers. Do the rounding yourself on the [xy]d parts if you
really want it rounded (mod 100 should work for that, but check for
overflow and handle it properly).
HTH,
-D
More information about the Python-list
mailing list