Beta
×

Welcome to the Slashdot Beta site -- learn more here. Use the link in the footer or click here to return to the Classic version of Slashdot.

Thank you!

We are sorry to see you leave - Beta is different and we value the time you took to try it out. Before you decide to go, please take a look at some value-adds for Beta and learn more about it. Thank you for reading Slashdot, and for making the site better!

Rounding Algorithms

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

Programming 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."

Sorry! There are no comments related to the filter you selected.

Round this! (5, Funny)

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

1.44th post!

Re:Round this! (0)

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

1.54th post!

Re:Round this! (2, Funny)

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

shouldn't that be '1.44st post'...the mind is torn between math and grammar.

Re:Round this! (1)

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

one point fourty-fourst? If you're going to be a grammar nazi, at least be right.

Most important... (5, Funny)

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

Round down and put the extra aside. Say, in your own account. Like the have-a-penny-need-a-penny jar at the local Gulp-n-Blow.

Re:Most important... (1)

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

Just don't put the decimal point in the wrong place...

Re:Most important... (1)

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

And don't forget the cover sheet on your TPS report.

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

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

...in federal pound-me-in-the-ass prison.

Re:Most important... (1)

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

Meant as a joke, but... I vaguely remember being taught a system in grade school, where cumulative roundoff errors were reduced somewhat by combining the total number of roundoffs, and rounding the main product up or down one or more units depending on whether you'd rounded up or down more in the sub-units. I don't recall the exact system but it does work as a method in everyday life.

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

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

There's some straight line algorithm that uses a similar method.
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)

This is what significant digits are for, though significant digit arithmetic uses probability to lower rounding error instead of forcing the user to do it himself.

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

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

Round down and put the extra aside. Say, in your own account.
It's a classic computer crime/urban legend [snopes.com] , and has been used in various films.

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

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

Is it really an "urban legend"? As much of a trope it has become in movies with technological whizzes, Snopes says it was reported in Whiteside's 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)

yeah but you might get caught and end up in pound-you-in-the-ass penitentiary

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)

Why would my wife being pregnant be a problem?

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)

Better yet... Why would she be getting rounded if she were bulimic?

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)

Just remember... your wif^H^H^Hcomputer always does _exactly_ what you tell it to do...

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

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

GIGO

   

Slide Rules and precision (5, Interesting)

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

These days kids are not taught to round. Instead you just do the compuations at absurdly large precision then on the last step round off. This way you don't accumulate systematic round-off error. It's good as long as you have the luxury of doing that. It used to be that C-programmers had a cavalier attitude of always writing the double-precision libraries first. Which is why Scientific programmers were initially slow to migrate from fortran.

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)

my favorite rounding algorithm is pi(r)^2.

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

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

missed by the mod's!

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

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

Not really, it's just not very funny.

offtopic (-1, Offtopic)

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

Round round get around
I get around
Yeah
Get around round round I get around

Re:offtopic (1, Funny)

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

I think you mean:
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)

Use integer math. After all, the computer itself does...

Re:Just avoid the problem entirely (0)

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

In fact, why not just do everything in binary? "after all the computer itself does"!

My personal rounding program (5, Funny)

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

For rounding, I use the following:
  • Mountain Dew
  • Couch
  • Lack of willpower
  • Utter disdain for annual resolutions I made less than a week ago
  • DiGiorno's pizzas.
Seems to work.

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? :P

Chuck Wagon. (0)

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

"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.""

Hey! Were's ground round on that list?

Ugh (3, Funny)

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

I don't think I know what my programs are doing 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)

Double it and add Thirty, or is that the metric conversion for Beer?

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

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

I know your comment is both off topic and just plain silly but the correct conversion between deg F and deg C is F=(C*1.8)+32

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

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

And you have no idea who Bob and Doug McKenzie are.

Please stop posting until you acquire a sense of humor.

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

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

*buys TedTschopp 42 metric beers* It's a bloody shame no one recognized the reference.

RIAA Rounding (0, Flamebait)

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

RIAA method:
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:

blame_pirates();

Re:RIAA Rounding (1, Funny)

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

RIAA method:
if (sales(type_cd,2005) < sales(type_cd,2004)) {
set_sales(type_cd,2005)=0;
blame_pirates();
}


Really? I thought it was more like:
if (sales(type_cd,2005) <= sales(type_cd,2004)) {
  set_sales(type_cd,2005,0);
}
else {
  set_projected_sales(type_cd,2005,sales(type_cd,200 5)*2);
}
blame_pirates();

Re:RIAA Rounding (1)

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

Bzz, compiler error, assignement without left-hand argument. Wow thats a horrible piece of code. If you were going to call a 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)

...has my head spinning round and round!!

One rounding algorithm (-1, Troll)

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

...involves living in Mom's basement with a limitless bag of chips and a few dozen liters of Mountain Dew / Bawls / sugary caffeinated alcohol-free beverage (SCAB).

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)

...is the round-spam-spam-random-spam-round algorithm.

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

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

...where it discusses the various rounding methods. I had actually thought of/used most of them. The one that was new to me was the round-half-even (banker's rounding). Very cool idea, and I had no idea it was commonly used.

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

What about... (2)

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

Rounding to the nearest square?

Add and truncate (1, Flamebait)

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

When I need to implement rounding, I add .5 and then truncate. I believe (perhaps naively) that this is efficient because of the lack of branching.

Re:Add and truncate (1)

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

I'm quite impressed, I never thought about using this method until you mentioned it!

Re:Add and truncate (1)

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

I thought it was pretty slick too. I think that one of my professors mentioned it to me when I was in college.

that's rounding up (1)

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

your algorithm always rounts 0.5 up.

Re:that's rounding up (1)

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

No, it's rounding to the nearest integer. If the fractional value of the number is 0.5 or greater, the number will be rounded up. If the fractional value is less than 0.5 the number is rounded down.

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)

-5.8 --> -5.8+0.5 --> -5.3 --> truncate(-5.3) = -5.0

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)

That's a good point. I use this in spreadsheets to average grades, so all the numbers I deal with are nonnegative. I've never considered how this function would work for negative numbers.

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

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

Computer games should round randomly.

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)

When you multiply and divide you often end up truncating/rounding whether you intend to or not. You just don't have enough bits for the precision you need. If you have enough operations, you can end up with some significant errors. That's why DSPs have double wide busses internally.

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)

PETER
So when the subroutine compounds the interest, right, it uses all these extra decimals places that just get rounded off. So we just simplify the whole thing and we just round it down and drop the remainder into an account that we own.

JOANNA
So you're stealing.

PETER
Ah, no. No. You don't understand. It's, uh, very complicated. It's, uh, it's, it's aggregate so I'm talking about fractions of a cent that, uh, over time, they add up to a lot.

Re:Office Space (0)

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

Wasn't that how Richard Pryor got himself in trouble in one of the superman movies as well?

Applications (1)

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

Far from being merely academic, this could be useful for all the banking accounting programmers out there collecting fractional amounts of money. A third of a cent here, a tenth of a cent there, pretty soon it adds up to real money!

Re:Applications (2, Interesting)

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

It's called ShareBuilders [sharebuilders.com] . I got a dividend for a princely sum of $2.20 USD and it was re-invested for free as 0.0385 of one share. Although I wished it would round up my stock shares somtimes. I don't like seeing 27.9995 shares when it really should be 28 shares. I hate being cheated out on 0.0005 of a share. :P

Accounting Software (0)

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

One of my perennial complaints against accounting software (at least here in Britain) is that they round half pennies differently from the government's rules on Value Added Tax. So someone buying by mail order who sends a cheque for £25 plus 17.5% VAT rounds down (correctly) and the **** accounts program generates an invoice rounding up (incorrectly) and I am for ever writing an invoice with an extra 1p line to correct the error.

Re:Accounting Software (4, Interesting)

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

GST (a VAT equiv.) in Canada and QST in Quebec round UP. Always.

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 plain rounding. But I do frequently need to do division with rounding up to nearest integer value. For that I use:

#define DIV_ROUNDUP(n, d) ((n)+((d)-1))/(d))

Re:Don't round much myself (1)

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

That code doesn't even compile, let alone work.

Re:Don't round much myself (1)

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

#define DIV_ROUNDUP(n, d) (((n)+((d)-1))/(d))

Seems dumb to me (1)

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

Why not just round to the nearest in all cases?

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)

You know what seems dumb to me? You. The whole point is what happens at X.5, which has no "nearest."

Re:Seems dumb to me (1)

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

But if you just accumulate the 0.5 instead of discarding it, then there is no "what do you do with it?" involved.

Re:Seems dumb to me (1)

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

Ah, but what if you have 0.49999999999999999999999999999999999999999999999, or any other number that just can't fit exactly into an n-bit floating or fixed point system? At that point, you are forced to do some kind of rounding, giving you a small error, and those small errors accumulate as the number of arithmetic operations increase.

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)

Forgive the reply to myself, but just to clarify: Yes, if you have 0.5 then you probably don't need to round it further. 0.5 is a convenience value to illustrate the concept of rounding. But what if, after some floating point operation, the "5" is just beyond the last digit of what can be stored in a floating point value? Now you DO have round it. After all operations are done, then you may round further, in order to display or store a number with less precision for convenience.

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)

It sounds like you are saying, instead of rounding to the nearest unit, round to the nearest half unit. If you had read the article you would know that there is no theoretical difference what place value you decide to round to.

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)

Becuase over time the error become to big.

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)

This means we can condition the casinos to better the odds on the machines by using weighted coins.

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)

RTFA. Many of the algorithms mentioned are designed simply to deal with the .5 case, where rounding up or down is equally valid depending on your application. Round half even is particularly nifty in that it is designed to make frequent roundoffs of a .5 have less of an impact on the error.

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)

>Why not just round to the nearest in all cases?

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)

> Why not just round to the nearest in all cases?

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)

Because in a computer you are dealing with an internal representation that is always finite...where as in the world of abstractions you can work with infinite precision numbers...

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)

If you're worried about cumulative rounding error buildup, then don't round until after you've accumulated.
As the article infers, time contraints on a system will often mean that you can't "afford" to do math as precisely as your architecture might theoretically allow you to. This is a common hurdle to overcome when you're working on embedded real-time systems: ie. the need to find a compromise between speed and accuracy. Just because you can do 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)

I'm currently working with floating point accumulation and I've come to realize that rounding is unbelievably important when it comes to floating point. For long accumulations or a series of operations you need round to nearest functionality, but even this can be insufficient depending on the nature of the numbers your adding. If truncation is used however, although the easiest to implement in hardware, the error can add up so fast that it'll stun you. It's good to see a fairly comprehensive summary of techniques out there that doesn't require wading through the literature.

Interval arithmetic (4, Interesting)

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

Rounding towards the nearest neighbour is the default and ubiquitously used rounding mode. The complementary rounding modes (round toward -+ infinity or 0) are useful for doing calculations with 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 performing error 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)

that's not really inviting people to visit TFA.

IEEE Standard (4, Informative)

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

And the IEEE standard for rounding is Banker's Rounding, or Even Rounding, plus whatever other names it goes by. When rounding to the nearest whole number, when the value is exactly halfway between, i.e. 2.5, the rounding algorithm chooses the nearest even number. This allows the distribution of rounding to happen in a more even distributed manner. Always rounding up, which is what US kids are taught in school, will eventually create a bias and throw the aggregates off.

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 Rounding

That'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)

As he states, "...the mind soon boggles at the variety and intricacies of the rounding algorithms"
Probably because the one that everyone would *like* to use is patented.

Social Applications (5, Funny)

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

So it turns out instead of 2, there are more like 9 different types of people.

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)

is the one used by richard pryor's character in superman iii [imdb.com] to steal all of the half pennies from his company's payroll and become fabulously rich

rip dude

Rounding Algorithms are... (1)

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

... nothing in comparison to trying to figure out what the compiler is 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)

You will never forget the day you find your first bug in gcc. I never did :)

In-Story Dupe? (1)

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

What's the difference between round-toward-zero and truncate is? Or floor round and round down? Or ceiling round and round up?

Re:In-Story Dupe? (0)

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

Rounding a negative number towards zero is "rounding up", whereas rounding a positive number towards zero is "rounding down"

so, what's the diffrence (1)

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

between round to zero and floor, and round to infinity and ceiling?

Fix() in VB (1)

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

Discovered the hard way that Fix() in VB doesn't act as I would expect.
Fix((580# * 1.3636)*10000#) gives 7908879 rather than 7908880

Re:Fix() in VB (1)

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

There are some very good books that explains this and how one should do calculations on computers and how to estimate the error propagation and growth. The field that covers this is called Numerical Analysis and should be part of any math or comp sci training. A good introduction textbook for this is Numerical Methods by Germunds Dahlquist http://www.amazon.com/gp/product/0486428079/qid=11 36506488/sr=8-5/ref=pd_bbs_5/104-6440517-4726329?n =507846&s=books&v=glance [amazon.com]

Re:Fix() in VB (1)

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

I've actually studied Numerical Analysis which is why I caught this and wasn't terribly surprised by it, but there is a tendancy when whipping up a quick bit of code to not think about nor look for such things.

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

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

I was expecting something a little better than this, like maybe some fast code to study and use [aggregate.org] .
Load More Comments
Slashdot Login

Need an Account?

Forgot your password?