Rounding Question

Adam DePrince adam at deprince.net
Fri Feb 23 02:20:48 EST 2001


Remco Gerlich wrote:
> Mikael Olofsson <mikael at isy.liu.se> wrote in comp.lang.python:
> > I corrected Remco:
> >  >  I'm sorry, but this also behaves badly. It rounds 10 up to 20. To get
> >  >  Jacob's desired functionality, you could modify your idea to
> >  >    rounded = number-((number-1)%10)+9
> >  >  still assuming only integer input.
> > Alex Martelli wrote:
> >  >  ...which of course is totally equivalent to the earlier
> >  >  proposed
> >  >      rounded = (number+9) - ((number+9)%10)
> >  >  since addition is commutative and associative (you can
> >  >  move the +9 from the right of the expression) AND so
> >  >  is addition in modulo-arithmetic, so that:
> >  >  (X-1)%N==(X%N)+((-1)%N)==(X%N)+((N-1)%N)==(X+N-1)%N
> > Of course! I just couldn't stay silent when Remco stumbled again.
> Yeah yeah, rub it in... :)
> Thing is I wasn't assuming integers, I wanted to write something that works
> on floats as well. He only gave integer examples, but his example code with
> math.ceil and dividing by 10.0 works on floats as well, and I thought it
> likely that he'd get non-integer depth measurements once.
                        ^^^^^^^^^

Keep in mind the dangers associated with floats.  Imprecision. 
Rounding.  Truncation.  Underflow.  

Are you generating the numbers to be rounded up via some other
computation?  If so, then what happens when a rounding error makes your
answer 40.0000001 when on paper the answer should be 40.  Your ceil
function will return 50 instead of the correct value of 40.

In your first post you mentioned that this is for diving table
computation.  Very saftey critical application.  Floating point numbers
have limitations.  Equality doesn't always work when pencil and paper
computations indicate it should.  IEEE floats can't represent numbers
like 0.4 precisely (0.4 is a repeating fraction in base 2).  The answers
will not be consistent from one type of machine to the other, or even
from one compiler to the other.  Entire careers are made out of dealing
with the weirdness of floating point numbers near boundry conditions. 
And a function that chooses an answer by checking if a number of exactly
40 or the smallest number representable that is greater than 40,
presuming you expect computed values exactly equal to 40, qualifies as a
boundry condition.  Stick with integer math.  You will live longer.

-- 
Adam DePrince
Starmedia Network, Inc.
Email:
zlib.decompress('x\332KLI\314\325KI-(\312\314KNu(.I,\312MM\311L\324K\316\317\005\000\221\331\012s')



More information about the Python-list mailing list