EmailMainTagsEditHistoryDiscussion (1)

If on floating point arithmetic we have something like:

 define round(x): return trunc (x + 0.5)
 ...
 float_average = float_sum / N
 integer_avg = round(float_average)

Using integer arithmetic we can do something similar in order to round an average to the closest integer with:

 (A_0 + A_1 + ...  +  A_(N-1) +  N/2 ) / N

Where A is the vector and N is the size of A.

It seems easy now but I got confused with this because I did read a particular case in someone else's code but I couldn't tell the general case right away.

Thanks to Javier Rojas for the discussion on the SlProg mailing list.

Loading... Vote up! Vote down! Discussion (1)

Last update: 2009-03-04 (Rev 15275)

svnwiki $Rev: 15576 $