Help needed to round-off a number

Jon W{tte d88-jwa at nada.kth.se
Mon Jun 5 00:59:00 AEST 1989


In article <267 at ibd.BRL.MIL> heilpern at brl.arpa (Mark A. Heilpern (IBD) <heilpern>) writes:
>There is a relatively painless way to do this:
>1) you want to round off to 'n' places:
>2) multiply your original by 10^n
>2a) add (5 / 10^n) for round-up error
>3) truncate the fraction off of your answer
>4) divide this result by 10^n
>5) you've just rounded to n decimal places.

Actually, you have NOT rounded correctly. For the given algorithm to
work, you need to replace line 2a) with:
2a) add 0.5 for round-up error (Regardless of what n is)

Also, this routine will NOT round statistically correctly regarding
odd/even numbers and negative numbers, but you can't have it all...
unless you REALLY want to slow things down!



-- 
 __       Jon W{tte (The dread Smiley Shark) email:h+ at nada.kth.se
/  \      (+46 (0) 8 258 268)
   /---   (c) 1989 Yessbox Allright Professional Products Inc. - Y.A.P.P.I.
  /       -- No More --



More information about the Comp.lang.c mailing list