Author Topic: Prize Rounding Error?  (Read 820 times)

Double Eagle

  • Newbie
  • *
  • Posts: 19
    • View Profile
Prize Rounding Error?
« on: November 08, 2011, 10:56:30 AM »
I used ver. 3.0 for the first time this weekend.  When calculating the prizes for a six places, I specified to round down to the nearest $5.  The last place prize was caluculated to be $124.  The summary was correct however and did not reflect that a $1 was omitted.  I have never seen this in ver. 2.
« Last Edit: November 08, 2011, 11:04:44 AM by Double Eagle »

Corey Cooper

  • Administrator
  • Hero Member
  • *****
  • Posts: 5402
    • View Profile
Re: Prize Rounding Error?
« Reply #1 on: November 08, 2011, 10:58:08 AM »
Can you provide the tournament file so I can look at it?  You can email it to support@thetournamentdirector.net, or attach it to a post here if you're comfortable with that.

Corey Cooper

  • Administrator
  • Hero Member
  • *****
  • Posts: 5402
    • View Profile
Re: Prize Rounding Error?
« Reply #2 on: November 09, 2011, 10:08:18 PM »
Thanks for sending the tournament.  This is indeed a rounding error.  The actual value of 6th place as computed by the software is 124.99999999999998.  When the software displays the amount in a monetary format (with currency symbol and rounded to 2 decimal places), it comes out as $125.00.  In your tournament screen, you're using the <vprizes> token with the attribute roundOff="true".  Round off means drop the decimal places, which unfortunately comes out to $124.

This is a rounding error present in many languages, Javascript being one of them unfortunately.  It happens with non-integers when they are converted between binary and decimal representations.  The trick is to avoid fractional numbers in computation.  Not always possible, but for an application like the TD where the primary calculations are done with money or similar values (2 decimal places), you can multiply all values by 100 and round them off.  When you display them, you just divide by 100.  This avoids the rounding errors entirely.  Were I to write the TD all over again, I would definitely do it this way.  Unfortunately it's a tad bit late now.

I'll look at the prizes code to see if I can avoid it.  The only workaround I can think of is to turn off the roundOff attribute.

Corey Cooper

  • Administrator
  • Hero Member
  • *****
  • Posts: 5402
    • View Profile
Re: Prize Rounding Error?
« Reply #3 on: November 09, 2011, 10:42:19 PM »
I found a quick workaround for this (fixed it, but there's a better fix to be done at a later time).  I published patch 4 to fix it.  If you use the TD while online, it should automatically download the fix.

Double Eagle

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Prize Rounding Error?
« Reply #4 on: November 09, 2011, 11:01:01 PM »
Thanks for the prompt reply and fix!