The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Rounding.....WTF?

Last post 03-18-2008 9:37 PM by bstorer. 21 replies.
Page 1 of 1 (22 items)
Sort Posts: Previous Next
  • 03-17-2008 5:28 PM

    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. 

    http://forums.devshed.com/net-development-87/rounding-numbers-516885.html 

    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. 
    Add 0.5
    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.

  • 03-17-2008 5:51 PM In reply to

    Re: Rounding.....WTF?

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

    There are three kinds of people: those who make things happen, those who watch things happen and those who wonder what happened.
  • 03-17-2008 6:26 PM In reply to

    Re: Rounding.....WTF?

    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.
    irc://irc.slashnet.org/#TDWTF
    <Ling> Looks like [lotus] notes was indeed clock sucking and pissing wildly on my disk
    <Duplication_Prevention_Bot> Wow, that was a disturbing image.
  • 03-18-2008 1:00 AM In reply to

    Re: Rounding.....WTF?

    eclipsed4utoo:

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

  • 03-18-2008 1:42 AM In reply to

    Re: Rounding.....WTF?

    eclipsed4utoo:

    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. 

    http://forums.devshed.com/net-development-87/rounding-numbers-516885.html 

    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. 
    Add 0.5
    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


  • 03-18-2008 4:20 AM In reply to

    Re: Rounding.....WTF?

    And then there's the Perl coders' WTF: sprintf it with format string "%.5f", then treat as a number again.
  • 03-18-2008 8:44 AM In reply to

    Re: Rounding.....WTF?

    ZiggyFish:
    he just reinvented the wheel
     

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

  • 03-18-2008 9:43 AM In reply to

    Re: Rounding.....WTF?

    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.

  • 03-18-2008 9:48 AM In reply to

    Re: Rounding.....WTF?

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

    Like Math.Round()?

  • 03-18-2008 9:59 AM In reply to

    Re: Rounding.....WTF?

     

    MasterPlanSoftware:
    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.

  • 03-18-2008 10:01 AM In reply to

    Re: Rounding.....WTF?

    pitchingchris:

     

    MasterPlanSoftware:
    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.

  • 03-18-2008 10:58 AM In reply to

    • TGV
    • Top 500 Contributor
    • Joined on 10-09-2005
    • Posts 90

    Re: Rounding.....WTF?

    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. 

  • 03-18-2008 11:21 AM In reply to

    Re: Rounding.....WTF?

    TGV:

    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.

  • 03-18-2008 6:41 PM In reply to

    Re: Rounding.....WTF?

    MasterPlanSoftware:

    ZiggyFish:
    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! 

    What if the hokey cokey really IS what it's all about?
  • 03-18-2008 6:45 PM In reply to

    Re: Rounding.....WTF?

    upsidedowncreature:
    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'?

  • 03-18-2008 6:48 PM In reply to

    Re: Rounding.....WTF?

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

    What if the hokey cokey really IS what it's all about?
  • 03-18-2008 6:53 PM In reply to

    Re: Rounding.....WTF?

    upsidedowncreature:

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

     

    I stand corrected:

    http://en.wikipedia.org/wiki/Hokey_Pokey

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

  • 03-18-2008 6:54 PM In reply to

    Re: Rounding.....WTF?

    upsidedowncreature:
    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

    < pstorer> Bans don't mean shit on the forum. It's like being on the Sex Offender List. You can still entice kids into your van with candy.

    Want more? Go the IRC channel #TDWTFMafia on irc.slashnet.org.
  • 03-18-2008 6:55 PM In reply to

    Re: Rounding.....WTF?

    morbiuswilters:

    upsidedowncreature:
    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.

  • 03-18-2008 7:05 PM In reply to

    Re: Rounding.....WTF?

    MasterPlanSoftware:
    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.

    What if the hokey cokey really IS what it's all about?
  • 03-18-2008 9:17 PM In reply to

    Re: Rounding.....WTF?

    upsidedowncreature:
    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!

    Join us at #TDWTF on irc.slashnet.org !

  • 03-18-2008 9:37 PM In reply to

    Re: Rounding.....WTF?

     

    upsidedowncreature:

    MasterPlanSoftware:
    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. 

Page 1 of 1 (22 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems