Rounding.....WTF?

• I was just surfing around on the devshed forums.  I saw this thread on rounding a long number like 14 decimal places to 5 decimal places.  Now I thought these were some very good forums.....Until I saw this post on the forums by a member with over 1200 posts.

His steps to rounding are:

1.  Multiply by whatever power of 10 so that your desired least-significant-digit is in the 1s place
2.
3.  Truncate to an integer
4.  Convert back to the same type and divide by the same power of 10:

then his footnote:
"The only problem is you're using floating point.
You may end up with some calculation errors that come with floating
point territory."

I would love to see some of his production code.

• Joel's usually more sentient than that. Must be a bad day.

• Seeing as how his website (link in his sig) links to thedailywtf, I think he might mentioned it that way to get the luser to RTFM.

• @eclipsed4utoo said:

"The only problem is you're using floating point. You may end up with some calculation errors that come with floating point territory."

I would love to see some of his production code.

Do you mean this comment is incorrect? That 4 step algorithm actually doesn't guarantee that someone ends up with a FP number that will printf with trailing zeros after that number of decimal places. Exactly because of calculation errors. Actually because decimal fractions cannot in general be represented exactly as binary FP number.

But instead of truncating to an integer, one could use floor() function instead, which will also help to avoid overflow.

• @eclipsed4utoo said:

I was just surfing around on the devshed forums.  I saw this thread on rounding a long number like 14 decimal places to 5 decimal places.  Now I thought these were some very good forums.....Until I saw this post on the forums by a member with over 1200 posts.

His steps to rounding are:

1.  Multiply by whatever power of 10 so that your desired least-significant-digit is in the 1s place
2.
3.  Truncate to an integer
4.  Convert back to the same type and divide by the same power of 10:

then his footnote:
"The only problem is you're using floating point.
You may end up with some calculation errors that come with floating
point territory."

I would love to see some of his production code.

he just reinvented the wheel

• And then there's the Perl coders' WTF: sprintf it with format string "%.5f", then treat as a number again.

• @ZiggyFish said:

he just reinvented the wheel

And you just quoted the entire OP for no reason. Congrats.

• I can't remember how I done it, but I made a rounding routine a few years ago because I was using a proprietary device and didn't have much built in support. Sometimes it is helpful to have a nice function that might round to the nearest whatever.  So if you want your readings to round to the nearest .02  or something.  This is especially helpful in weighing applications or other industrial processes.

• @pitchingchris said:

Sometimes it is helpful to have a nice function that might round to the nearest whatever.

Like Math.Round()?

•  @MasterPlanSoftware said:

Like Math.Round()?

sure, if you have .Net or java or something, but if all you had was a prioritary C compiler and a very limited API, sometimes you have to hand-create things. It had some C runtime support, but not entirely. In these situations I just had to make do with what I had.

• @pitchingchris said:

@MasterPlanSoftware said:

Like Math.Round()?

sure, if you have .Net or java or something, but if all you had was a prioritary C compiler and a very limited API, sometimes you have to hand-create things. It had some C runtime support, but not entirely. In these situations I just had to make do with what I had.

I understand. The thread is about Math.Round though so I though this was a bit out of place.

• Math.Round does banker rounding. If you do normal computations, you don't want that. E.g., I'm using rounding to limit the dependence of my model on subtle differences between values. In such a case, banker's rounding doesn't make any sense at all.

• @TGV said:

Math.Round does banker rounding. If you do normal computations, you don't want that. E.g., I'm using rounding to limit the dependence of my model on subtle differences between values. In such a case, banker's rounding doesn't make any sense at all.

I agree with that statement. It all depends on the accurracy you want to show. Usually you have some sort of precision you want to hit, but you also need to consider the decimals after that. Just because you want .01 precision, sometimes rounding 0.015 to 0.02 is acceptable. In other times you really don't want it to round to 0.02 unless its over 0.01995. Such cases are where you want to display 2 decimials, but store a few more that you will base your calculations on later.

• @MasterPlanSoftware said:

@ZiggyFish said:

he just reinvented the wheel

And you just quoted the entire OP for no reason. Congrats.

And he's got an annoying sig .  I know who my ISP is, what OS I'm running etc.

Before you say it, yeah I'm going to change mine soon!

• @upsidedowncreature said:

Before you say it, yeah I'm going to change mine soon!

Well, my only question is did you mean to write 'hokey cokey' instead of 'hokey pokey'?

• I'm in the UK, always been "cokey" here!  I suppose there might be regional differences within countries too though.

• @upsidedowncreature said:

I'm in the UK, always been "cokey" here!  I suppose there might be regional differences within countries too though.

I stand corrected:

Wow.. UK just has to be different with everything.... how fancy.

• @upsidedowncreature said:

And he's got an annoying sig .  I know who my ISP is, what OS I'm running etc.

OMG!!!  How duz he no all that about me????!  I think he has h4x0red my compz but I run Ubuntu so it must be... magic????

Wuts this, find sexy singles in my area???  Helloooooooo ladys!!!1

• @morbiuswilters said:

@upsidedowncreature said:

And he's got an annoying sig .  I know who my ISP is, what OS I'm running etc.

OMG!!!  How duz he no all that about me????!  I think he has h4x0red my compz but I run Ubuntu so it must be... magic????

Wuts this, find sexy singles in my area???  Helloooooooo ladys!!!1

Yeah really. Another 1337 script kiddie from Slashdot... wonderful.

• @MasterPlanSoftware said:

Wow.. UK just has to be different with everything.... how fancy.

I think it all started when the Huguenots brought over a load of extra "u"'s so we had to start stuffing them into favourite, colour etc.  We'd be tripping over the damn things otherwise.

• @upsidedowncreature said:

I think it all started when the Huguenots brought over a load of extra "u"'s so we had to start stuffing them into favourite, colour etc.  We'd be tripping over the damn things otherwise.

Best reasoning EVER!

•  @upsidedowncreature said:

@MasterPlanSoftware said:

Wow.. UK just has to be different with everything.... how fancy.

I think it all started when the Huguenots brought over a load of extra "u"'s so we had to start stuffing them into favourite, colour etc.  We'd be tripping over the damn things otherwise.

Ah, the Huguenots.  It takes a special group of people to be chased out France.

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.