# Rounding Algorithms

#### CmdrTaco posted more than 8 years ago | from the now-that's-just-wierd dept.

279
dtmos writes *"Clive Maxfield has an interesting article up on PL DesignLine cataloging most (all?) of the known rounding algorithms used in computer math. As he states, "...the mind soon boggles at the variety and intricacies of the rounding algorithms that may be used for different applications ... round-up, round-down, round-toward-nearest, arithmetic rounding, round-half-up, round-half-down, round-half-even, round-half-odd, round-toward-zero, round-away-from-zero, round-ceiling, round-floor, truncation (chopping), round-alternate, and round-random (stochastic rounding), to name but a few." It's a good read, especially if you *think* you know what your programs are doing."*

## Round this! (5, Funny)

## Anonymous Coward | more than 8 years ago | (#14405070)

## Re:Round this! (0)

## Anonymous Coward | more than 8 years ago | (#14405130)

## Re:Round this! (2, Funny)

## baadger (764884) | more than 8 years ago | (#14405399)

## Re:Round this! (1)

## Killall -9 Bash (622952) | more than 8 years ago | (#14405468)

## Most important... (5, Funny)

## T-Ranger (10520) | more than 8 years ago | (#14405072)

## Re:Most important... (1)

## darthservo (942083) | more than 8 years ago | (#14405091)

## Re:Most important... (1)

## suso (153703) | more than 8 years ago | (#14405141)

## What good will that do you... (1)

## dotgain (630123) | more than 8 years ago | (#14405466)

## Re:Most important... (1)

## Reziac (43301) | more than 8 years ago | (#14405107)

## Re:Most important... (5, Informative)

## slothman32 (629113) | more than 8 years ago | (#14405192)

It keeps adding the slope value for every x increment and when it overloads it also makes the y position go up one.

Or something like that. Bresenham's I believe.

To get on topic I would use the usual "(x).5 to (x+1).499~9 goes to (x+1)" way.

For negative, just ignore the sign when doing it, e.g. -1.6 -> -2

## Re:Most important... (2, Informative)

## poopdeville (841677) | more than 8 years ago | (#14405195)

## Re:Most important... (3, Interesting)

## PapayaSF (721268) | more than 8 years ago | (#14405235)

## Re:Most important... (4, Interesting)

## CRCulver (715279) | more than 8 years ago | (#14405318)

Computer Capers: Tales of Electronic Thievery, Embezzlement, and Fraud [amazon.com], which should have the necessary citation on the case.## Re:Most important... (1)

## Khashishi (775369) | more than 8 years ago | (#14405556)

## Think you know.... (4, Insightful)

## thewiz (24994) | more than 8 years ago | (#14405073)

especially if you *think* you know what your programs are doing.Pfft... I've been writing programs and working with computers for over 25 years. I *STILL* haven't figured out what they are doing. Come to think of it, I could say the samething about my wife.

## Re:Think you know.... (1)

## Rosco P. Coltrane (209368) | more than 8 years ago | (#14405097)

Come to think of it, I could say the samething about my wife.If your wife is getting rounded, I'd say she's either pregnant or bulimic. Either way, you have a problem.

## Re:Think you know.... (1)

## rvw14 (733613) | more than 8 years ago | (#14405150)

## Re:Think you know.... (5, Funny)

## Rosco P. Coltrane (209368) | more than 8 years ago | (#14405178)

Why would my wife being pregnant be a problem?You'll know 13 years from now

## Re:Think you know.... (0)

## Anonymous Coward | more than 8 years ago | (#14405247)

## Re:Think you know.... (1)

## Joe5678 (135227) | more than 8 years ago | (#14405570)

Better yet... Why would she be getting rounded if she were bulimic?I'm guessing that would be rounding *down*

## Re:Think you know.... (1)

## Flimzy (657419) | more than 8 years ago | (#14405413)

## Re:Think you know.... (1)

## gardyloo (512791) | more than 8 years ago | (#14405439)

## Slide Rules and precision (5, Interesting)

## goombah99 (560566) | more than 8 years ago | (#14405448)

These days it's not so true any more. First there's lots of good scientific C programmers now so the problem of parcimonius computation is well appreciated. Moreover the creation of math co-processing, vector calcualtions, and math co-processors often makes it counter-intuitive what to do.

For example it's quite likely that brute forcing a stiff calculation is double precision using a numeric co-processor will beat doing it in single precision with a few extra steps added to keep the precision in range. So being clever is not always helpful. people used to create math libraries that even cheated on using the full precision of the avialable floating point word size (sub-single precision accuracy) since it was fast (e.g. the radius libs for macintoshes) Pipelining adds more confusion, since the processor can be doing other stuff during those wait states for the higher precision. Vector code reverse this: if you are clever maybe shaving precision willlet you double the number of simultanoeus calcualtions.

In any case, what was once intuitive: minimal precision and clever rounding to avoid systematic errors means faster computation is no longer true.

Of course in the old days people learned to round early in life: no one wanted to use a 5 digit precision slide rule if you could use a 2 digit precision slide rule.

## my favorite rounding algorithm (4, Funny)

## User 956 (568564) | more than 8 years ago | (#14405074)

## Oh, man! A perfect geek joke.. (-1, Offtopic)

## IAAP (937607) | more than 8 years ago | (#14405129)

## Re:Oh, man! A perfect geek joke.. (-1, Offtopic)

## Anonymous Coward | more than 8 years ago | (#14405163)

## offtopic (-1, Offtopic)

## fredistheking (464407) | more than 8 years ago | (#14405079)

I get around

Yeah

Get around round round I get around

## Re:offtopic (1, Funny)

## jeblucas (560748) | more than 8 years ago | (#14405125)

Round-up, Round-down, get around-toward-nearest,

I get around-half-up,

Yeah,

Get around-half-down, round-half-even, round-half-odd, I get around-toward-zerowooooooHHHHooo!

## Just avoid the problem entirely (-1, Offtopic)

## localroger (258128) | more than 8 years ago | (#14405100)

## Re:Just avoid the problem entirely (0)

## Anonymous Coward | more than 8 years ago | (#14405134)

## My personal rounding program (5, Funny)

## charlesbakerharris (623282) | more than 8 years ago | (#14405105)

## Re:My personal rounding program (3, Funny)

## Spy der Mann (805235) | more than 8 years ago | (#14405225)

* DiGiorno's pizzas.

Seems to work.

So that's rounding towards positive infinity, right?

## Chuck Wagon. (0)

## Anonymous Coward | more than 8 years ago | (#14405132)

Hey! Were's ground round on that list?

## Ugh (3, Funny)

## zzen (190880) | more than 8 years ago | (#14405135)

all the time...I just hope they play nice when I'm not watching.

## Don't you just... (0, Offtopic)

## TedTschopp (244839) | more than 8 years ago | (#14405136)

## Re:Don't you just... (1, Offtopic)

## baadger (764884) | more than 8 years ago | (#14405424)

## Re:Don't you just... (0, Offtopic)

## brunson (91995) | more than 8 years ago | (#14405639)

Please stop posting until you acquire a sense of humor.

## Re:Don't you just... (1)

## immanis (557955) | more than 8 years ago | (#14405516)

## RIAA Rounding (0, Flamebait)

## ackthpt (218170) | more than 8 years ago | (#14405137)

if (sales(type_cd,2005) < sales(type_cd,2004)) {

set_sales(type_cd,2005)=0;

blame_pirates();

}

## Re:RIAA Rounding (3, Insightful)

## slavemowgli (585321) | more than 8 years ago | (#14405289)

Almost correct, but I think it can be simplified to the following:

## Re:RIAA Rounding (1, Funny)

## Anonymous Coward | more than 8 years ago | (#14405423)

RIAA method:Really? I thought it was more like:

## Re:RIAA Rounding (1)

## PetriBORG (518266) | more than 8 years ago | (#14405647)

set_sales()function, you would have to pass 0 as an arguement. Otherwise you would need a function to return a reference so that you could you could assign it like that.## Reading about all those "rounding" methods... (-1, Offtopic)

## cytoman (792326) | more than 8 years ago | (#14405138)

## One rounding algorithm (-1, Troll)

## Red Flayer (890720) | more than 8 years ago | (#14405143)

It's not a perfect algorithm, but the results have been consistently replicated by thousands.

## The best rounding algorithm... (0)

## Anonymous Coward | more than 8 years ago | (#14405146)

## I read the first half of the article... (5, Interesting)

## under_score (65824) | more than 8 years ago | (#14405161)

This is a great reference article! If you are programmer working with numerical algorithms, keep this article handy.

## Re:I read the first half of the article... (2, Informative)

## poopdeville (841677) | more than 8 years ago | (#14405236)

## What about... (2)

## Irvu (248207) | more than 8 years ago | (#14405167)

## Add and truncate (1, Flamebait)

## RackinFrackin (152232) | more than 8 years ago | (#14405188)

## Re:Add and truncate (1)

## ztransform (929641) | more than 8 years ago | (#14405432)

## Re:Add and truncate (1)

## RackinFrackin (152232) | more than 8 years ago | (#14405529)

## that's rounding up (1)

## goombah99 (560566) | more than 8 years ago | (#14405505)

## Re:that's rounding up (1)

## amjacobs (769757) | more than 8 years ago | (#14405625)

For example, take the number 2.3:

2.3 + 0.5 = 2.8 --> 2

While the number 2.8 gives the following result:

2.8 + 0.5 = 3.3 --> 3

In order to extend this method to arbitrary precision, add 5*10^-n to the original number before truncation. (where n controls the rounding precision)

## Doesn't work for negative numbers. (4, Insightful)

## Anonymous Coward | more than 8 years ago | (#14405514)

which is not what you want.

In c++, using std::floor will give the correct results with this method though

-5.8 --> -5.8+0.5 --> -5.3 --> floor(-5.3) = -6.0 (correct)

whereas :

-5.3 --> -5.3+0.5 --> -4.8 --> floor(-4.8) = -5.0 (correct)

## Re:Doesn't work for negative numbers. (1)

## RackinFrackin (152232) | more than 8 years ago | (#14405607)

## Computer games should use floor(x+rand()) (1, Troll)

## RedLaggedTeut (216304) | more than 8 years ago | (#14405194)

This means that every little bonus that the player gets might have an impact on the integer result. Example: phaos.

The "other" neat thing is that the expected value of

floor(x+rand()) == x

with 0.0=0.0

## Rounding can be really annoying. (0)

## Anonymous Coward | more than 8 years ago | (#14405215)

My own problem was creating a bunch of multiple choice student questions and neglecting to account for the truncation that resulted when I displayed the numbers. Needless to say, the students were annoyed when none of the answers were exactly what they calculated.

## Office Space (5, Funny)

## TubeSteak (669689) | more than 8 years ago | (#14405218)

## Re:Office Space (0)

## Anonymous Coward | more than 8 years ago | (#14405544)

## Applications (1)

## ch-chuck (9622) | more than 8 years ago | (#14405222)

## Re:Applications (2, Interesting)

## creimer (824291) | more than 8 years ago | (#14405324)

## Accounting Software (0)

## Anonymous Coward | more than 8 years ago | (#14405224)

## Re:Accounting Software (4, Interesting)

## renehollan (138013) | more than 8 years ago | (#14405344)

So, 7% GST on a $1 purchase, yields $1.07. On a $1.01 purchase, yields $1.09 ($1.01 + $0.0707 rounded to $0.08 = $1.09).

It used to be that Quebec added their 8% PST not on the amount excluding GST, but the amount including GST, rounded up of course, and it too was rounded. So $1.01 + 7% GST = $1.09. $1.09 + 8% PST = $1.18. Dunno if they replaced that with the 15% "harmonized" sales tax (paid to the Feds and then partially reimbursed to the province to be equivalent to the combination of 7% GST and average provincial 8% PST -- apparently Quebec was the only province to calculate their PST on top of the GST), but I doubt it.

## Don't round much myself (1)

## FirstTimeCaller (521493) | more than 8 years ago | (#14405228)

I mostly program using fixed integer arithmetic, so I don't do much

plainrounding. But I do frequently need to do division with rounding up to nearest integer value. For that I use:## Re:Don't round much myself (1)

## LukeWink (898707) | more than 8 years ago | (#14405641)

## Re:Don't round much myself (1)

## dtfarmer (548183) | more than 8 years ago | (#14405642)

## Seems dumb to me (1)

## n6kuy (172098) | more than 8 years ago | (#14405258)

If you're worried about cumulative rounding error buildup, then don't round until after you've accumulated. After all, you're not adding things up with pencil and paper anymore, are you?

## Re:Seems dumb to me (0)

## Anonymous Coward | more than 8 years ago | (#14405285)

## Re:Seems dumb to me (1)

## n6kuy (172098) | more than 8 years ago | (#14405329)

## Re:Seems dumb to me (1)

## ChadN (21033) | more than 8 years ago | (#14405431)

Yes, for certain types of numerical problems, there is a bounded precision, or set of values, for which you could create a specialized counting system (ie. many financial problems are more amenable to fixed point operations); for most everything else, there is floating point, hence non-exact numerical representations, hence rounding (at EACH operation!).

## Re:Seems dumb to me (1)

## ChadN (21033) | more than 8 years ago | (#14405645)

There ARE systems that can do math directly with rational numbers, with arbitrary precision, thus reducing the need for some of the per operation rounding (or at least further reducing the impact of that rounding), but that is not the kind of quick arithmetic that typical 32 or 64 bits-per-value CPUs provide. For speed, you accept the need to round, and try to understand the consequences to your application.

## you seem to be missing the point (2, Insightful)

## Khashishi (775369) | more than 8 years ago | (#14405528)

You think you can just eliminate the 1/2 bias like that? Ok, now you know what to do with the number 3.5. Now what do you round 3.75 and 3.25 to? You are just shifting the rounding down one binary digit.

You say to not round until the end? You miss the point of rounding, which is necessary due to efficiency, memory, or hardware concerns. Nobody makes 10000 bit ADCs, and even if they did, you'd still need to round the 10001st bit.

## Re:Seems dumb to me (1)

## jackb_guppy (204733) | more than 8 years ago | (#14405338)

In Casinos, when "counting" a change in a machine, they use rounding.

The wieght the money (not count it).

Multiple with the exchange factor (100lb = $133.34 in Pennies).

Round the amount with allernating Round-Up / Round-Down on seccuessive wieghings for the machine.

This way over time the rounding error will tend to average to 0.

## Re:Seems dumb to me (1)

## Boronx (228853) | more than 8 years ago | (#14405385)

## Re:Seems dumb to me (1)

## fishbowl (7759) | more than 8 years ago | (#14405457)

>In Casinos, when "counting" a change in a machine, they use rounding.

It is very hard to find a Las Vegas casino that still has any coin operated slot machines. This one thing has eliminated my gambling habit completely. I have relatives in Las Vegas and visit often. I typically take a few rolls of quarters, "slot machine money", for the sole purpose of throwing it away. Guess what? One trip, I showed up with my rolls of quarters -- AND THEY DON'T HAVE COIN OPERATED MACHINES ANYMORE!

I'll be damned if I'm going to go to the trouble of standing in line to buy a token card just for the privilege of throwing away my spare change.

## Re:Seems dumb to me (0)

## Anonymous Coward | more than 8 years ago | (#14405380)

Also, keep in mind, there isn't infinite precision. Many of these rounding strategies are employed in the implementation of floating point numbers, so with very precise decimals or very large numbers with smaller decimals, rounding occurs whether you want it or not. Doesn't matter much to most people, but for scientific purposes on macro- or micro- scales, the rounding matters more.

-ShadowRanger

## Re:Seems dumb to me (0)

## Anonymous Coward | more than 8 years ago | (#14405397)

I understand that there are cases where doing that skews the distribution in a poorly representative way, where different rounding methods lead to more accurate overall results.

## Re:Seems dumb to me (0)

## Anonymous Coward | more than 8 years ago | (#14405459)

You might want to round down in some cases . . .

You have 13 marbles. There and 5 kids. How many marbles can give to each kid?

## Re:Seems dumb to me (1)

## webjonesin (453397) | more than 8 years ago | (#14405463)

As it turns out...that loss of precision can actually impact the way your calculations work.

The correctness of the result of a calculation can even be impacted by the order in which the calculation is done. (this can and has resulted in things going BOOM)

There is a whole field of study devoted to this subject...check up on Numerical Analysis

## Re:Seems dumb to me (2, Informative)

## hereschenes (813329) | more than 8 years ago | (#14405474)

cando an operation on two floats in a loop 1000 times doesn't mean that you can afford the time to do it.## minus 1, rTroll) (-1, Troll)

## Anonymous Coward | more than 8 years ago | (#14405268)

## floating point (3, Interesting)

## BigMFC (592465) | more than 8 years ago | (#14405283)

## The canonical reference... (1)

## Doctor Memory (6336) | more than 8 years ago | (#14405446)

## Interval arithmetic (4, Interesting)

## Diomidis Spinellis (661697) | more than 8 years ago | (#14405287)

interval arithmetic: a calculation can be performed twice with opposing rounding modes to derive an interval value for the result. If all operations are performed in this way, the final result of a complex calculation is expressed as an interval providing the range in which the real value will be (remember, often floating point numbers only approximate the real number). Using such a package [sun.com] can save you the trouble of performingerror analysis. An article [acm.org] in the Journal of the ACM provides the details for implementing this feature.## Re:Interval arithmetic (1)

## fishbowl (7759) | more than 8 years ago | (#14405522)

"Rounding towards the nearest neighbour is the default and ubiquitously used rounding mode. "

And if a high proportion of the input data are at ".5", whether you round up or down, you've possibly got a big problem (I realize you know that obviously).

I love all the posts that claim to work strictly with integer arithmetic. That's wonderful until you have to deal with a fraction of your nice integer value, then you get to reinvent the floating point wheel

I especially love decimal numbers that cannot be precisely represented in binary, although I've never been able to explain it very well. (I do understand sign/exp/mantissa binary floats.)

## to name but a few? (0)

## Anonymous Coward | more than 8 years ago | (#14405303)

## IEEE Standard (4, Informative)

## Anonymous Coward | more than 8 years ago | (#14405307)

2.5 = 2

3.5 = 4

## Re:IEEE Standard (1)

## The_Dougster (308194) | more than 8 years ago | (#14405494)

And the IEEE standard for rounding is Banker's RoundingThat's what I have always used for programs that calculate a cost from some other factors; for instance labor costs are sometimes calculated by multiplying minutes by some factor. The result is often a decimal value that needs to be rounded to dollars and cents. If you use banker's rounding, and you should, then the bean counter upstairs will probably get the same result as your program does. This is a good thing.

## Stifling innovation in rounding (1)

## jemenake (595948) | more than 8 years ago | (#14405348)

## Social Applications (5, Funny)

## Kesch (943326) | more than 8 years ago | (#14405367)

The classics:

Those who round a glass of water up (Has been filled)

Those who round it down (Has been emptied)

The oddballs:

The round-half-up crowd(Half or greater is filled)

The round-half-down crowd(Half or less is empty)

The round toward zero types(Always empty)

The round away from zero groupies(Always Full)

The round alternate weirdos(They get interesting when you give them two glasses)

The round random subset(Carry around a coin or die to decide such problems)

And finally...

The truncate ones who cannot handle such a problem and smash the glass to make sure it is empty.

## the best rounding algorithm (0, Offtopic)

## circletimessquare (444983) | more than 8 years ago | (#14405371)

rip dude

## Rounding Algorithms are... (1)

## creimer (824291) | more than 8 years ago | (#14405417)

compileris doing. My beautiful code goes into one end and comes out as an executable file that segfaults. Of course, some twit always say the problem is with my beautiful code and not the stupid compiler.## Re:Rounding Algorithms are... (1)

## Sparr0 (451780) | more than 8 years ago | (#14405609)

## In-Story Dupe? (1)

## Boronx (228853) | more than 8 years ago | (#14405430)

## Re:In-Story Dupe? (0)

## Anonymous Coward | more than 8 years ago | (#14405475)

## so, what's the diffrence (1)

## autopr0n (534291) | more than 8 years ago | (#14405434)

## Fix() in VB (1)

## YodaToo (776221) | more than 8 years ago | (#14405445)

Fix((580# * 1.3636)*10000#) gives 7908879 rather than 7908880

## Re:Fix() in VB (1)

## rodac (580415) | more than 8 years ago | (#14405543)

## Re:Fix() in VB (1)

## YodaToo (776221) | more than 8 years ago | (#14405585)

## I was expecting something more detailed than this (2, Insightful)

## Zork the Almighty (599344) | more than 8 years ago | (#14405532)