2012-07-18 2 views
1

Je suis très nouveau à Groovy. Je résous une série de Fibonacci avec ses propriétés. J'ai un code groovy comme ceci:Que signifie cette erreur de source inconnue?

a = new BigInteger[2][2] 
a[0][0] = 1 
a[0][1] = 1 
a[1][0] = 1 
a[1][1] = 0 
temp = new BigInteger[2][2] 
temp = a 
def testFun(def a,BigInteger n) { 
    if(n == 1) 
     return a 
    b = new BigInteger[2][2] 
    def sum = 0 
    for (c = 0 ; c < 2 ; c++) { 
       for (d = 0 ; d < 2 ; d++) 
       { 
        for (k = 0 ; k < 2 ; k++) 
        { 
         sum = sum + temp[c][k] * a[k][d]; 
        } 
      b[c][d] = sum 
       sum = 0; 
      } 
     } 
     testFun(b,n-1) 
} 
c = new BigInteger[2][2] 
BigInteger n = 90 
if(n % 2 == 0) 
{ 
     c = testFun(a,n.divide(2)) 
     temp = c 
     c = testFun(c,2) 
} 
println c[0][1] 

Je reçois réponse comme:

2880067194370816120 

Mais quand je change de valeur n-à-dire 5090, je reçois une grande erreur comme ceci:

at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 
    at fibb.testFun(fibb.groovy:24) 
    at fibb$testFun.callCurrent(Unknown Source) 

Qu'est-ce qui n'a pas fonctionné? Pourquoi je reçois cette erreur?

Répondre

2

Bien que je m'attendais à voir une exception StackOverflowException dans cette pile, peut-être que vous venez de l'extraire. Je pense que vous vous trouvez dans la profondeur de récursivité maximale, ce qui explique pourquoi il fonctionne pour une petite valeur de n mais échoue avec une grande valeur. Vous voulez regarder dans la fermeture de trampoline, une partie de Groovy 1.8. Voici la section pertinente du release notes. Il y a aussi this question qui est similaire et devrait vous aider. A propos, la raison pour laquelle il est dit "Unknown Source" dans la trace de la pile est parce que Groovy n'a pas sa source disponible pour vous dire sur quelle ligne il était - c'est un faux-fuyant.

+0

L'optimisation de la récursion de queue empêcherait-elle un débordement de pile ou n'est-ce pas ce qu'elle fait? –

+1

+ 1 c'est probablement exécuté dans la console Groovy qui a des problèmes pour afficher des stacktraces massives telles que les exceptions de débordement de pile –

+0

[@arxanas] (http://stackoverflow.com/questions/11546969/what-does-this-unknown -source-error-mean # comment15270006_11547345) Groovy ne fait pas d'élimination d'appel de queue. Il a cependant [trampolines] (http://java.dzone.com/articles/cool-stuff-groovy-18). –