Thursday, November 1, 2012

Why continuation function also throw overflow exception

Now we know, we can use continuation function to avoid the overflow exception, but first time when we use continuation, many of us may confuse to encounter the overflow exception again if you run your continuation program in Visual Studio editor with F5 or Ctrl+F5, what's worse, the overflow exception will appear even the data is really small. But if you try some other way to run the same continuation function, like FSI or FSC, you will encounter a very interesting result. It works well in FSI and FSC, but failed only in Visual Studio editor. Here we use Fibonacci continuation as the example, please see the failed screenshot below:

Is it a bug in Visual Studio?  It is the first reaction in my mind, but after talking with Tao Liu, he told me the answer is No! Don't worry, it is because we forget that tail recursion is turned off by default when using Visual Studio and compiling in Debug mode, and how should we do now? Yeah, we can turn on it in project property:
  • right-click the project properties in Solution Explorer, select 'Properties'
  • go to the 'Build' tab
  • make sure 'Debug' configuration is selected
  • click the box 'Generate tail calls'

There is another way to workaround this exception, that is switch your program from 'Debug' mode to 'Release' mode. OK, now, just try to run your program again, you will see the real result you want.

Note: you can still find the answer here.

No comments:

Post a Comment