Friday, November 17, 2017

Numerical Errors in Calculating Factorials and a Workaround

It seems Tableau doesn't handle big numbers correctly in calculating factorials.

Here is an example:
When K<=20, the result K! is good.

When K>=21 the results are no more correct. Some of them are even becoming negative.

A case has been submitted to the Tableau support. Click the image to download the workbook.

[Update] Following comments by Gerardo, I figured out a solution: using Float() in the calculation will give the good result. Click the above image to view the updated workbook. Great thanks to Gerardo!

Integer operations in Tableau seem performed using fixed point arithmetic, which has limited dynamic range. Floating point arithmetic can entertain a much larger dynamic range.

2 comments:

  1. This is documented in their online help.

    http://onlinehelp.tableau.com/current/pro/desktop/en-us/help.html#functions_operators.html%3FTocPath%3DDesign%2520Views%2520and%2520Analyze%2520Data%7CAdvanced%2520Analysis%7CCalculated%2520Fields%7C_____2

    Scroll to the very bottom. It'll let you know the largest available value. It matches what you have found. I also stumbled on this a while back which led me to find to documentation for it.

    ReplyDelete
    Replies
    1. Thanks Gerardo! That's a great tip! I checked the doc you gave and derived a good solution or workaround. Just use floating point calculation for the factorials. It gives approximate numbers when the numbers are really big. It works for me because only the scale matters at that time. I am calculating Poisson Distribution anyway.

      Delete