[Tutor] Question on rounding

Sheila King sheila@thinkspot.net
Tue, 09 Jan 2001 17:55:00 -0800


If you really want exact values, you would need to work with some sort of
rational number class, that used integer types for its data members. Any time
you do floating type arithmetic, your answers will be subject to a "machine
error" that is equal to the "machine precision", which would be the smallest
possible number that can be represented on the machine (obviously, limited by
the size of the memory devoted to remembering each floating point number).

What, exactly, are you trying to do? What is the purpose or point of these
computations?

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/

On Tue, 9 Jan 2001 17:06:12 -0600, "Brad Chandler" <mbc2@netdoor.com>  wrote
about [Tutor] Question on rounding:

:I know this must have been discussed before, but I'm having a problem with
:rounding.  My hand calculator gives me the following:  25350 * .0055 =
:139.425
:
:But Python gives me 139.42499999999998. Now what I want is to round that up
:to 139.43, so I've been using round(x,2). But my numbers have been slightly
:off in python and I've just now discovered why.  I think I can just use
:round(x,3) and then round(x,2) to get the desired result, but that just
:seems messy and I'm wondering if there is a better, more correct way of
:doing it.  Below is an example of my interpreter session.
:
:>>> x=25350.00*.0055
:>>> x
:139.42499999999998
:>>> round(x,2)
:139.41999999999999
:>>> y = round(x,3)
:>>> y
:139.42500000000001
:>>> round(y,2)
:139.43000000000001
:
:
:
:_______________________________________________
:Tutor maillist  -  Tutor@python.org
:http://www.python.org/mailman/listinfo/tutor