[Mono-list] StackOverflowException with decimal
Alan McGovern
alan.mcgovern at gmail.com
Sat Jun 21 21:00:38 EDT 2008
Maybe it's just me, but the Fib function is broken. It has no exit clause,
so it'll just recurse until it hits a stack overflow. Surely you should do a
check for when n <= 1.
Alan.
On Sat, Jun 21, 2008 at 11:10 PM, Brandon Perry <bperry.volatile at gmail.com>
wrote:
> I should also add that the line 50 in the stacktrace is "c = Fib(i);".
>
>
> On Sat, Jun 21, 2008 at 5:08 PM, Brandon Perry <bperry.volatile at gmail.com>
> wrote:
>
>> Hi, I am doing some of the problems at Project Euler using Mono and C#. I
>> am on the second problem, but I get a StackOverflowException error
>> everytime. The code is as follows:
>>
>> public decimal Solve()
>> {
>> decimal c, i = 0;
>> decimal sum = 0;
>> decimal cap = 4000000;
>>
>> for (;i <= cap;i++)
>> {
>> if (i <= 2)
>> {
>> sum += 1;
>> }
>> else
>> {
>> try
>> {
>> c = Fib(i);
>>
>> if (!Convert.ToBoolean(c%2))
>> {
>> sum += c;
>> }
>> }
>> catch (Exception ex)
>> {
>> Console.WriteLine(ex.ToString());
>> }
>> }
>> }
>>
>> return sum;
>> }
>>
>> public decimal Fib(decimal n)
>> {
>> return Fib(n-1) + Fib(n-2);
>> }
>>
>> The stacktrace is here:
>> http://volatileminds.net/projects/euler/stacktrace.txt
>>
>> I haven't had a chance to test this on .NET, but thought I would get a
>> second opinion as to whether it is a PEBKAC error or a bug.
>>
>> Any thoughts?
>>
>
>
> _______________________________________________
> Mono-list maillist - Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080622/ece31016/attachment.html
More information about the Mono-list
mailing list